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"""
36 # XXX Should we skip merging of the default attributes?
38 newattrs
= a
.merge(newattrs
)
42 # XXX Do we really need this test?
43 if isinstance(a
, attr
):
44 newattrs
= a
.merge(newattrs
)
46 raise TypeError("only instances of class attr.attr are allowed")
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"""
56 if isinstance(attr
, getclasses
):
58 except TypeError: # workaround for Python 2.1 and older
60 for getclass
in getclasses
:
61 if isinstance(attr
, getclass
):
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
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
)
76 raise TypeError("instance %r not allowed" % attrs
[len(getattrs(attrs
, allowedclasses
))])
79 # attr class and simple descendants
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
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
)]
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
):
126 if first
and isinstance(attr
, self
.beforetheclasses
):
130 except TypeError: # workaround for Python 2.1 and older
133 for dependedclass
in self
.beforetheclasses
:
134 if isinstance(attr
, dependedclass
):
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
):
160 if first
and isinstance(attr
, self
.beforetheclasses
):
163 if not isinstance(attr
, self
.exclusiveclass
):
165 except TypeError: # workaround for Python 2.1 and older
168 for dependedclass
in self
.beforetheclasses
:
169 if isinstance(attr
, dependedclass
):
173 if not isinstance(attr
, self
.exclusiveclass
):
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?
196 """a special attribute which removes all predecessing attributes
197 in an attribute list"""
199 def merge(self
, attrs
):
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
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
217 - index should be an unsigned integer
218 - total should be a positive number
219 - valid sections fullfill 0<=index<total"""
222 if isinstance(a
, changeattr
):
223 result
.append(a
.select(index
, total
))
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
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"""
256 self
.restartable
= restartable
258 def select(self
, index
, total
):
260 return self
.attrs
[index
% length(self
.attrs
)]
261 elif index
< length(self
.attrs
):
262 return self
.attrs
[index
]