normpath.split should now work always as expected
[PyX/mjg.git] / pyx / attr.py
blob8a8c048ebf89cb4378f94797631f5f7215555a67
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2003-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
7 # Copyright (C) 2003-2004 André Wobst <wobsta@users.sourceforge.net>
9 # This file is part of PyX (http://pyx.sourceforge.net/).
11 # PyX is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # PyX is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with PyX; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 # some helper functions for the attribute handling
30 def mergeattrs(attrs):
31 """perform merging of the attribute list attrs as defined by the
32 merge methods of the attributes"""
33 newattrs = []
34 for a in attrs:
35 # XXX Do we really need this test?
36 if isinstance(a, attr):
37 newattrs = a.merge(newattrs)
38 else:
39 raise TypeError("only instances of class attr.attr are allowed")
40 return newattrs
43 def getattrs(attrs, getclasses):
44 """return all attributes in the attribute list attrs, which are
45 instances of one of the classes in getclasses"""
46 result = []
47 try:
48 for attr in attrs:
49 if isinstance(attr, getclasses):
50 result.append(attr)
51 except TypeError: # workaround for Python 2.1 and older
52 for attr in attrs:
53 for getclass in getclasses:
54 if isinstance(attr, getclass):
55 result.append(attr)
56 break
57 return result
60 def checkattrs(attrs, allowedclasses):
61 """check whether only attributes which are instances of classes in
62 allowedclasses are present in the attribute list attrs; if not it
63 raises a TypeError"""
64 if len(attrs) != len(getattrs(attrs, allowedclasses)):
65 for attr1, attr2 in zip(attrs, getattrs(attrs, allowedclasses)):
66 if attr1 is not attr2:
67 raise TypeError("instance %r not allowed" % attr1)
68 else:
69 raise TypeError("instance %r not allowed" % attrs[len(getattrs(attrs, allowedclasses))])
72 # attr class and simple descendants
75 class attr:
77 """ attr is the base class of all attributes, i.e., colors, decorators,
78 styles, text attributes and trafos"""
80 def merge(self, attrs):
81 """merge self into list of attrs
83 self may either be appended to attrs or inserted at a proper position
84 immediately before a dependent attribute. Attributes of the same type
85 should be removed, if redundant. Note that it is safe to modify
86 attrs."""
88 attrs.append(self)
89 return attrs
92 class exclusiveattr(attr):
94 """an attribute which swallows all but the last of the same type (specified
95 by the exlusiveclass argument to the constructor) in an attribute list"""
97 def __init__(self, exclusiveclass):
98 self.exclusiveclass = exclusiveclass
100 def merge(self, attrs):
101 attrs = [attr for attr in attrs if not isinstance(attr, self.exclusiveclass)]
102 attrs.append(self)
103 return attrs
106 class sortbeforeattr(attr):
108 """an attribute which places itself previous to all attributes given
109 in the beforetheclasses argument to the constructor"""
111 def __init__(self, beforetheclasses):
112 self.beforetheclasses = beforetheclasses
114 def merge(self, attrs):
115 first = 1
116 result = []
117 try:
118 for attr in attrs:
119 if first and isinstance(attr, self.beforetheclasses):
120 result.append(self)
121 first = 0
122 result.append(attr)
123 except TypeError: # workaround for Python 2.1 and older
124 for attr in attrs:
125 if first:
126 for dependedclass in self.beforetheclasses:
127 if isinstance(attr, dependedclass):
128 result.append(self)
129 first = 0
130 break
131 result.append(attr)
132 if first:
133 result.append(self)
134 return result
137 class sortbeforeexclusiveattr(attr):
139 """an attribute which swallows all but the last of the same type (specified
140 by the exlusiveclass argument to the constructor) in an attribute list and
141 places itself previous to all attributes given in the beforetheclasses
142 argument to the constructor"""
144 def __init__(self, exclusiveclass, beforetheclasses):
145 self.exclusiveclass = exclusiveclass
146 self.beforetheclasses = beforetheclasses
148 def merge(self, attrs):
149 first = 1
150 result = []
151 try:
152 for attr in attrs:
153 if first and isinstance(attr, self.beforetheclasses):
154 result.append(self)
155 first = 0
156 if not isinstance(attr, self.exclusiveclass):
157 result.append(attr)
158 except TypeError: # workaround for Python 2.1 and older
159 for attr in attrs:
160 if first:
161 for dependedclass in self.beforetheclasses:
162 if isinstance(attr, dependedclass):
163 result.append(self)
164 first = 0
165 break
166 if not isinstance(attr, self.exclusiveclass):
167 result.append(attr)
168 if first:
169 result.append(self)
170 return result
173 class clearclass(attr):
175 """a special attribute which allows to remove all predecessing attributes of
176 the same type in an attribute list"""
178 def __init__(self, clearclass):
179 self.clearclass = clearclass
181 def merge(self, attrs):
182 return [attr for attr in attrs if not isinstance(attr, self.clearclass)]
185 # XXX is _clear a good choice?
187 class _clear(attr):
189 """a special attribute which removes all predecessing attributes
190 in an attribute list"""
192 def merge(self, attrs):
193 return []
195 # we define the attribute "clear", an instance of "_clear",
196 # which can be used to remove all predecessing attributes
197 # in an attribute list
199 clear = _clear()
202 # changeable attrs
205 def selectattrs(attrs, index, total):
206 """performs select calls for all changeable attributes and
207 returns the resulting attribute list
208 - attrs should be a list containing attributes and changeable
209 attributes
210 - index should be an unsigned integer
211 - total should be a positive number
212 - valid sections fullfill 0<=index<total
213 - returns None, when attrs is None
214 - returns None, when a changeable attribute returns None"""
215 if attrs is None:
216 return None
217 result = []
218 for a in attrs:
219 if isinstance(a, changeattr):
220 select = a.select(index, total)
221 if select is None:
222 return None
223 result.append(select)
224 else:
225 result.append(a)
226 return result
229 def selectattr(attr, index, total):
230 """as select, but for a single attribute"""
231 if isinstance(attr, changeattr):
232 select = attr.select(index, total)
233 if select is None:
234 return None
235 return select
236 else:
237 return attr
240 class changeattr:
242 """changeattr is the base class of all changeable attributes"""
244 def select(self, index, total):
245 """returns an attribute for a given index out of a total number
246 if attributes to be provided
247 - index should be an unsigned integer
248 - total should be a positive number
249 - valid selections fullfill 0 <= index < total
250 - the select method may raise a ValueError, when the
251 changeable attribute does not allow for a requested
252 selection"""
254 raise RuntimeError("not implemented")
257 class changelist(changeattr):
259 """a changeable attribute over a list of attribute choises"""
261 def __init__(self, attrs, restartable=1):
262 """initializes the instance
263 - attrs is a list of attributes to cycle
264 - restartable is a boolean, allowing to start from
265 the beginning after the end was reached; otherwise
266 selecting beyond the list returns a None"""
267 self.attrs = attrs
268 self.restartable = restartable
270 def select(self, index, total):
271 if self.restartable:
272 return self.attrs[index % len(self.attrs)]
273 elif index < len(self.attrs):
274 return self.attrs[index]