make all parts of the manual compile again; parts of the manual are still out of...
[PyX/mjg.git] / pyx / attr.py
blob745bb3b017da5af667dc2ac8aeef96759a289c60
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 aattr in attrs:
35 if isinstance(aattr, attr):
36 newattrs = aattr.merge(newattrs)
37 else:
38 raise TypeError("only instances of class attr.attr are allowed")
39 return newattrs
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"""
45 result = []
46 try:
47 for attr in attrs:
48 if isinstance(attr, getclasses):
49 result.append(attr)
50 except TypeError: # workaround for Python 2.1 and older
51 for attr in attrs:
52 for getclass in getclasses:
53 if isinstance(attr, getclass):
54 result.append(attr)
55 break
56 return result
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)
66 else:
67 raise TypeError("instance %r not allowed" % attrs[len(getattrs(attrs, allowedclasses))])
70 # attr class and simple descendants
73 class attr:
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
84 attrs."""
86 attrs.append(self)
87 return attrs
90 class exclusiveattr(attr):
92 """an attribute which swallows all but the last of the same type in an
93 attribute list"""
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)]
100 attrs.append(self)
101 return attrs
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):
113 first = 1
114 result = []
115 try:
116 for attr in attrs:
117 if first and isinstance(attr, self.beforetheclasses):
118 result.append(self)
119 first = 0
120 result.append(attr)
121 except TypeError: # workaround for Python 2.1 and older
122 for attr in attrs:
123 if first:
124 for dependedclass in self.beforetheclasses:
125 if isinstance(attr, dependedclass):
126 result.append(self)
127 first = 0
128 break
129 result.append(attr)
130 if first:
131 result.append(self)
132 return result
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?
149 class _clear(attr):
151 """a special attribute which removes all predecessing attributes
152 in an attribute list"""
154 def merge(self, attrs):
155 return []
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
161 clear = _clear()