remove shebang -- see comment 3 on https://bugzilla.redhat.com/bugzilla/show_bug...
[PyX/mjg.git] / pyx / attr.py
blob6efa6c6125e69c4ecb85eecb030239df3f37f78b
1 # -*- coding: ISO-8859-1 -*-
4 # Copyright (C) 2003-2004 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
6 # Copyright (C) 2003-2004 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 # check for an isinstance which accepts both a class and a sequence of classes
25 # as second argument and emulate this behaviour if necessary
26 try:
27 isinstance(1, (int, float))
28 except TypeError:
29 # workaround for Python 2.1
30 _isinstance = isinstance
31 def isinstance(instance, clsarg):
32 import types
33 if _isinstance(clsarg, types.ClassType):
34 return _isinstance(instance, clsarg)
35 for cls in clsarg:
36 if _isinstance(instance, cls):
37 return 1
38 return 0
41 # some helper functions for the attribute handling
44 def mergeattrs(attrs):
45 """perform merging of the attribute list attrs as defined by the
46 merge methods of the attributes"""
47 newattrs = []
48 for a in attrs:
49 # XXX Do we really need this test?
50 if isinstance(a, attr):
51 newattrs = a.merge(newattrs)
52 else:
53 raise TypeError("only instances of class attr.attr are allowed")
54 return newattrs
57 def getattrs(attrs, getclasses):
58 """return all attributes in the attribute list attrs, which are
59 instances of one of the classes in getclasses"""
60 return [attr for attr in attrs if isinstance(attr, tuple(getclasses))]
63 def checkattrs(attrs, allowedclasses):
64 """check whether only attributes which are instances of classes in
65 allowedclasses are present in the attribute list attrs; if not it
66 raises a TypeError"""
67 if len(attrs) != len(getattrs(attrs, allowedclasses)):
68 for attr1, attr2 in zip(attrs, getattrs(attrs, allowedclasses)):
69 if attr1 is not attr2:
70 raise TypeError("instance %r not allowed" % attr1)
71 else:
72 raise TypeError("instance %r not allowed" % attrs[len(getattrs(attrs, allowedclasses))])
75 # attr class and simple descendants
78 class attr:
80 """ attr is the base class of all attributes, i.e., colors, decorators,
81 styles, text attributes and trafos"""
83 def merge(self, attrs):
84 """merge self into list of attrs
86 self may either be appended to attrs or inserted at a proper position
87 immediately before a dependent attribute. Attributes of the same type
88 should be removed, if redundant. Note that it is safe to modify
89 attrs."""
91 attrs.append(self)
92 return attrs
95 class exclusiveattr(attr):
97 """an attribute which swallows all but the last of the same type (specified
98 by the exlusiveclass argument to the constructor) in an attribute list"""
100 def __init__(self, exclusiveclass):
101 self.exclusiveclass = exclusiveclass
103 def merge(self, attrs):
104 attrs = [attr for attr in attrs if not isinstance(attr, self.exclusiveclass)]
105 attrs.append(self)
106 return attrs
109 class sortbeforeattr(attr):
111 """an attribute which places itself previous to all attributes given
112 in the beforetheclasses argument to the constructor"""
114 def __init__(self, beforetheclasses):
115 self.beforetheclasses = tuple(beforetheclasses)
117 def merge(self, attrs):
118 first = 1
119 result = []
120 for attr in attrs:
121 if first and isinstance(attr, self.beforetheclasses):
122 result.append(self)
123 first = 0
124 result.append(attr)
125 if first:
126 result.append(self)
127 return result
130 class sortbeforeexclusiveattr(attr):
132 """an attribute which swallows all but the last of the same type (specified
133 by the exlusiveclass argument to the constructor) in an attribute list and
134 places itself previous to all attributes given in the beforetheclasses
135 argument to the constructor"""
137 def __init__(self, exclusiveclass, beforetheclasses):
138 self.exclusiveclass = exclusiveclass
139 self.beforetheclasses = tuple(beforetheclasses)
141 def merge(self, attrs):
142 first = 1
143 result = []
144 for attr in attrs:
145 if first and isinstance(attr, self.beforetheclasses):
146 result.append(self)
147 first = 0
148 if not isinstance(attr, self.exclusiveclass):
149 result.append(attr)
150 if first:
151 result.append(self)
152 return result
155 class clearclass(attr):
157 """a special attribute which allows to remove all predecessing attributes of
158 the same type in an attribute list"""
160 def __init__(self, clearclass):
161 self.clearclass = clearclass
163 def merge(self, attrs):
164 return [attr for attr in attrs if not isinstance(attr, self.clearclass)]
167 class _clear(attr):
169 """a special attribute which removes all predecessing attributes
170 in an attribute list"""
172 def merge(self, attrs):
173 return []
175 # we define the attribute "clear", an instance of "_clear",
176 # which can be used to remove all predecessing attributes
177 # in an attribute list
179 clear = _clear()
182 # changeable attrs
185 def selectattrs(attrs, index, total):
186 """performs select calls for all changeable attributes and
187 returns the resulting attribute list
188 - attrs should be a list containing attributes and changeable
189 attributes
190 - index should be an unsigned integer
191 - total should be a positive number
192 - valid sections fullfill 0<=index<total
193 - returns None, when attrs is None
194 - returns None, when a changeable attribute returns None"""
195 if attrs is None:
196 return None
197 result = []
198 for a in attrs:
199 if isinstance(a, changeattr):
200 select = a.select(index, total)
201 if select is None:
202 return None
203 result.append(select)
204 else:
205 result.append(a)
206 return result
209 def selectattr(attr, index, total):
210 """as select, but for a single attribute"""
211 if isinstance(attr, changeattr):
212 select = attr.select(index, total)
213 if select is None:
214 return None
215 return select
216 else:
217 return attr
220 class changeattr:
222 """changeattr is the base class of all changeable attributes"""
224 def select(self, index, total):
225 """returns an attribute for a given index out of a total number
226 if attributes to be provided
227 - index should be an unsigned integer
228 - total should be a positive number
229 - valid selections fullfill 0 <= index < total
230 - the select method may raise a ValueError, when the
231 changeable attribute does not allow for a requested
232 selection"""
234 raise RuntimeError("not implemented")
237 class changelist(changeattr):
239 """a changeable attribute over a list of attribute choises"""
241 def __init__(self, attrs, cyclic=1):
242 """initializes the instance
243 - attrs is a list of attributes to cycle
244 - If cyclic is set, we restart from the beginning after
245 the end of the list has been reached; otherwise
246 selecting beyond the end of the list returns None"""
247 self.attrs = attrs
248 self.cyclic = cyclic
250 def select(self, index, total):
251 if self.cyclic:
252 return self.attrs[index % len(self.attrs)]
253 elif index < len(self.attrs):
254 return self.attrs[index]
255 else:
256 return None