ignore build dir
[PyX/mjg.git] / pyx / attr.py
blob6ab120031e51604c3c92392b712b5b24c4e03ebf
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, defaults=[]):
31 """perform merging of the attribute list attrs as defined by the
32 merge methods of the attributes"""
33 newattrs = []
34 if attrs is None:
35 return None
36 # XXX Should we skip merging of the default attributes?
37 for a in defaults:
38 newattrs = a.merge(newattrs)
39 for a in attrs:
40 if a is None:
41 return None
42 # XXX Do we really need this test?
43 if isinstance(a, attr):
44 newattrs = a.merge(newattrs)
45 else:
46 raise TypeError("only instances of class attr.attr are allowed")
47 return newattrs
50 def getattrs(attrs, getclasses):
51 """return all attributes in the attribute list attrs, which are
52 instances of one of the classes in getclasses"""
53 result = []
54 try:
55 for attr in attrs:
56 if isinstance(attr, getclasses):
57 result.append(attr)
58 except TypeError: # workaround for Python 2.1 and older
59 for attr in attrs:
60 for getclass in getclasses:
61 if isinstance(attr, getclass):
62 result.append(attr)
63 break
64 return result
67 def checkattrs(attrs, allowedclasses):
68 """check whether only attributes which are instances of classes in
69 allowedclasses are present in the attribute list attrs; if not it
70 raises a TypeError"""
71 if len(attrs) != len(getattrs(attrs, allowedclasses)):
72 for attr1, attr2 in zip(attrs, getattrs(attrs, allowedclasses)):
73 if attr1 is not attr2:
74 raise TypeError("instance %r not allowed" % attr1)
75 else:
76 raise TypeError("instance %r not allowed" % attrs[len(getattrs(attrs, allowedclasses))])
79 # attr class and simple descendants
82 class attr:
84 """ attr is the base class of all attributes, i.e., colors, decorators,
85 styles, text attributes and trafos"""
87 def merge(self, attrs):
88 """merge self into list of attrs
90 self may either be appended to attrs or inserted at a proper position
91 immediately before a dependent attribute. Attributes of the same type
92 should be removed, if redundant. Note that it is safe to modify
93 attrs."""
95 attrs.append(self)
96 return attrs
99 class exclusiveattr(attr):
101 """an attribute which swallows all but the last of the same type (specified
102 by the exlusiveclass argument to the constructor) in an attribute list"""
104 def __init__(self, exclusiveclass):
105 self.exclusiveclass = exclusiveclass
107 def merge(self, attrs):
108 attrs = [attr for attr in attrs if not isinstance(attr, self.exclusiveclass)]
109 attrs.append(self)
110 return attrs
113 class sortbeforeattr(attr):
115 """an attribute which places itself previous to all attributes given
116 in the beforetheclasses argument to the constructor"""
118 def __init__(self, beforetheclasses):
119 self.beforetheclasses = beforetheclasses
121 def merge(self, attrs):
122 first = 1
123 result = []
124 try:
125 for attr in attrs:
126 if first and isinstance(attr, self.beforetheclasses):
127 result.append(self)
128 first = 0
129 result.append(attr)
130 except TypeError: # workaround for Python 2.1 and older
131 for attr in attrs:
132 if first:
133 for dependedclass in self.beforetheclasses:
134 if isinstance(attr, dependedclass):
135 result.append(self)
136 first = 0
137 break
138 result.append(attr)
139 if first:
140 result.append(self)
141 return result
144 class sortbeforeexclusiveattr(attr):
146 """an attribute which swallows all but the last of the same type (specified
147 by the exlusiveclass argument to the constructor) in an attribute list and
148 places itself previous to all attributes given in the beforetheclasses
149 argument to the constructor"""
151 def __init__(self, exclusiveclass, beforetheclasses):
152 self.exclusiveclass = exclusiveclass
153 self.beforetheclasses = beforetheclasses
155 def merge(self, attrs):
156 first = 1
157 result = []
158 try:
159 for attr in attrs:
160 if first and isinstance(attr, self.beforetheclasses):
161 result.append(self)
162 first = 0
163 if not isinstance(attr, self.exclusiveclass):
164 result.append(attr)
165 except TypeError: # workaround for Python 2.1 and older
166 for attr in attrs:
167 if first:
168 for dependedclass in self.beforetheclasses:
169 if isinstance(attr, dependedclass):
170 result.append(self)
171 first = 0
172 break
173 if not isinstance(attr, self.exclusiveclass):
174 result.append(attr)
175 if first:
176 result.append(self)
177 return result
180 class clearclass(attr):
182 """a special attribute which allows to remove all predecessing attributes of
183 the same type in an attribute list"""
185 def __init__(self, clearclass):
186 self.clearclass = clearclass
188 def merge(self, attrs):
189 return [attr for attr in attrs if not isinstance(attr, self.clearclass)]
192 # XXX is _clear a good choice?
194 class _clear(attr):
196 """a special attribute which removes all predecessing attributes
197 in an attribute list"""
199 def merge(self, attrs):
200 return []
202 # we define the attribute "clear", an instance of "_clear",
203 # which can be used to remove all predecessing attributes
204 # in an attribute list
206 clear = _clear()
209 # changeable attrs
212 def select(attrs, index, total):
213 """performs select calls for all changeable attributes and
214 returns the resulting attribute list
215 - attrs should be a list containing attributes and changeable
216 attributes
217 - index should be an unsigned integer
218 - total should be a positive number
219 - valid sections fullfill 0<=index<total"""
220 result = []
221 for a in attrs:
222 if isinstance(a, changeattr):
223 result.append(a.select(index, total))
224 else:
225 result.append(a)
228 class changeattr:
230 """changeattr is the base class of all changeable attributes"""
232 def select(self, index, total):
233 """returns an attribute for a given index out of a total number
234 if attributes to be provided
235 - index should be an unsigned integer
236 - total should be a positive number
237 - valid selections fullfill 0 <= index < total
238 - the select method may raise a ValueError, when the
239 changeable attribute does not allow for a requested
240 selection"""
242 raise RuntimeError("not implemented")
245 class changelist(changeattr):
247 """a changeable attribute over a list of attribute choises"""
249 def __init__(self, attrs, restartable=1):
250 """initializes the instance
251 - attrs is a list of attributes to cycle
252 - restartable is a boolean, allowing to start from
253 the beginning after the end was reached; otherwise
254 selecting beyond the list returns a None"""
255 self.attrs = attrs
256 self.restartable = restartable
258 def select(self, index, total):
259 if self.restartable:
260 return self.attrs[index % length(self.attrs)]
261 elif index < length(self.attrs):
262 return self.attrs[index]