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
27 isinstance(1, (int, float))
29 # workaround for Python 2.1
30 _isinstance
= isinstance
31 def isinstance(instance
, clsarg
):
33 if _isinstance(clsarg
, types
.ClassType
):
34 return _isinstance(instance
, clsarg
)
36 if _isinstance(instance
, cls
):
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"""
49 # XXX Do we really need this test?
50 if isinstance(a
, attr
):
51 newattrs
= a
.merge(newattrs
)
53 raise TypeError("only instances of class attr.attr are allowed")
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
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
)
72 raise TypeError("instance %r not allowed" % attrs
[len(getattrs(attrs
, allowedclasses
))])
75 # attr class and simple descendants
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
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
)]
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
):
121 if first
and isinstance(attr
, self
.beforetheclasses
):
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
):
145 if first
and isinstance(attr
, self
.beforetheclasses
):
148 if not isinstance(attr
, self
.exclusiveclass
):
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
)]
169 """a special attribute which removes all predecessing attributes
170 in an attribute list"""
172 def merge(self
, attrs
):
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
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
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"""
199 if isinstance(a
, changeattr
):
200 select
= a
.select(index
, total
)
203 result
.append(select
)
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
)
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
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"""
250 def select(self
, index
, total
):
252 return self
.attrs
[index
% len(self
.attrs
)]
253 elif index
< len(self
.attrs
):
254 return self
.attrs
[index
]