2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2003 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2003 Michael Schindler <m-schindler@users.sourceforge.net>
7 # Copyright (C) 2003 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"""
35 if isinstance(aattr
, attr
):
36 newattrs
= aattr
.merge(newattrs
)
38 raise TypeError("only instances of class attr.attr are allowed")
42 def getattrs(attrs
, getclasses
):
43 """return all attributes in the attribute list attrs, which are
44 instances of one of the classes in getclasses"""
48 if isinstance(attr
, getclasses
):
50 except TypeError: # workaround for Python 2.1 and older
52 for getclass
in getclasses
:
53 if isinstance(attr
, getclass
):
59 def checkattrs(attrs
, allowedclasses
):
60 """check whether only attributes which are instances of classes in
61 allowedclasses are present in the attribute list attrs"""
62 if len(attrs
) != len(getattrs(attrs
, allowedclasses
)):
63 for attr1
, attr2
in zip(attrs
, getattrs(attrs
, allowedclasses
)):
64 if attr1
is not attr2
:
65 raise TypeError("instance %r not allowed" % attr1
)
67 raise TypeError("instance %r not allowed" % attrs
[len(getattrs(attrs
, allowedclasses
))])
70 # attr class and simple descendants
75 """ attr is the base class of all attributes, i.e., colors, decorators,
76 styles, text attributes and trafos"""
78 def merge(self
, attrs
):
79 """merge self into list of attrs
81 self may either be appended to attrs or inserted at a proper position
82 immediately before a dependent attribute. Attributes of the same type
83 should be removed, if redundant. Note that it is safe to modify
90 class exclusiveattr(attr
):
92 """an attribute which swallows all but the last of the same type in an
95 def __init__(self
, exclusiveclass
):
96 self
.exclusiveclass
= exclusiveclass
98 def merge(self
, attrs
):
99 attrs
= [attr
for attr
in attrs
if not isinstance(attr
, self
.exclusiveclass
)]
104 class sortbeforeattr(attr
):
106 """an attribute which places itself previous to all attributes given
107 in the beforetheclasses argument to the constructor"""
109 def __init__(self
, beforetheclasses
):
110 self
.beforetheclasses
= beforetheclasses
112 def merge(self
, attrs
):
117 if first
and isinstance(attr
, self
.beforetheclasses
):
121 except TypeError: # workaround for Python 2.1 and older
124 for dependedclass
in self
.beforetheclasses
:
125 if isinstance(attr
, dependedclass
):
135 class clearclass(attr
):
137 """a special attribute which allows to remove all predecessing attributes of
138 the same type in an attribute list"""
140 def __init__(self
, clearclass
):
141 self
.clearclass
= clearclass
143 def merge(self
, attrs
):
144 return [attr
for attr
in attrs
if not isinstance(attr
, self
.clearclass
)]
147 # XXX is _clear a good choice?
151 """a special attribute which removes all predecessing attributes
152 in an attribute list"""
154 def merge(self
, attrs
):
157 # we define the attribute "clear", an instance of "_clear",
158 # which can be used to remove all predecessing attributes
159 # in an attribute list