some attr reorganization
[PyX/mjg.git] / pyx / attr.py
blob178672ef4122331f6cc70754123c6e4b8f1c3933
1 #!/usr/bin/env python
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"""
33 newattrs = []
34 for attr in attrs:
35 newattrs = attr.merge(newattrs)
36 return newattrs
39 def getattrs(attrs, getclasses):
40 """return all attributes in the attribute list attrs, which are
41 instances of one of the classes in getclasses"""
42 result = []
43 try:
44 for attr in attrs:
45 if isinstance(attr, getclasses):
46 result.append(attr)
47 break
48 except TypeError: # workaround for Python 2.1 and older
49 for attr in attrs:
50 for getclass in getclasses:
51 if isinstance(attr, getclass):
52 result.append(attr)
53 break
54 return result
57 def checkattrs(attrs, allowedclasses):
58 """check whether only attributes which are instances of classes in
59 allowedclasses are present in the attribute list attrs"""
60 if len(attrs) != len(getattrs(attrs, allowedclasses)):
61 for attr1, attr2 in zip(attrs, getattrs(attrs, allowedclasses)):
62 if attr1 is not attr2:
63 raise TypeError("instance %r not allowed" % attr1)
64 else:
65 raise TypeError("instance %r not allowed" % attrs[len(getattrs(attrs, allowedclasses))])
68 # attr class and simple descendants
71 class attr:
73 """ attr is the base class of all attributes, i.e., colors, decorators,
74 styles, text attributes and trafos"""
76 def merge(self, attrs):
77 """merge self into list of attrs
79 self may either be appended to attrs or inserted at a proper position
80 immediately before a dependent attribute. Attributes of the same type
81 should be removed, if redundant. Note that it is safe to modify
82 attrs."""
84 attrs.append(self)
85 return attrs
88 class exclusiveattr(attr):
90 """an attribute which swallows all but the last of the same type in an
91 attribute list"""
93 def __init__(self, exclusiveclass):
94 self.exclusiveclass = exclusiveclass
96 def merge(self, attrs):
97 attrs = [attr for attr in attrs if not isinstance(attr, self.exclusiveclass)]
98 attrs.append(self)
99 return attrs
102 class clearclass(attr):
104 """a special attribute which allows to remove all predecessing attributes of
105 the same type in an attribute list"""
107 def __init__(self, clearclass):
108 self.clearclass = clearclass
110 def merge(self, attrs):
111 return [attr for attr in attrs if not isinstance(attr, self.clearclass)]
114 # XXX is _clear a good choice?
116 class _clear(attr):
118 """a special attribute which removes all predecessing attributes
119 in an attribute list"""
121 def merge(self, attrs):
122 return []
124 # we define the attribute "clear", an instance of "_clear",
125 # which can be used to remove all predecessing attributes
126 # in an attribute list
128 clear = _clear()