4 # Copyright (C) 2002 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2002 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 "arg is string-like (cf. python cookbook 3.2)"
47 if type(arg
+ 0.0) is type(arg
):
54 """arg is sequence-like (e.g. has a len)
55 a string is *not* considered to be a sequence"""
56 if isstring(arg
): return 0
62 def ensuresequence(arg
):
63 """return arg or (arg,) depending on the result of issequence,
64 None is converted to ()"""
65 if isstring(arg
): return (arg
,)
66 if arg
is None: return ()
67 if issequence(arg
): return arg
71 """return list(arg) or [arg] depending on the result of isequence,
72 None is converted to []"""
73 if isstring(arg
): return [arg
]
74 if arg
is None: return []
75 if issequence(arg
): return list(arg
)
78 def getitemno(arg
, n
):
86 def issequenceofsequences(arg
):
87 """check if arg has a sequence or None as it's first entry"""
88 return issequence(arg
) and len(arg
) and (issequence(arg
[0]) or arg
[0] is None)
91 def getsequenceno(arg
, n
):
92 """get sequence number n if arg is a sequence of sequences,
93 otherwise it gets just arg"""
94 if issequenceofsequences(arg
):
102 class AttrError(base
.PyXExcept
): pass
105 def checkattr(attrs
, allowonce
=(), allowmulti
=()):
106 """checks the sequence attrs for occurencies of instances
107 the classes provided as a tuple to allowonce are allowed only once
108 the classes provided as a tuple to allowonce are allowed multiple times"""
111 for once
in allowonce
:
112 if isinstance(attr
, once
):
114 raise AttrError("only a single instance of %r allowed" % once
)
119 for multi
in allowmulti
:
120 if isinstance(attr
, multi
):
123 raise AttrError("%r not allowed" % attr
)
125 def getattrs(attrs
, get
, default
=nodefault
):
126 """creates a list of instances of class get out of the sequence attrs
127 when no instances are found it returns default when set (whatever it is)
128 when no instances are found it raises AttrError when default is not set"""
131 if isinstance(attr
, get
):
138 if default
is nodefault
:
144 def countattrs(attrs
, check
):
145 "count the occurancies of instances of class get out of the sequence attrs"
146 return len(getattrs(attrs
, check
, ()))
148 def getattr(attrs
, get
, default
=nodefault
):
149 """get the instance of class get out of the sequence attrs
150 when no instance is found it returns default when set (whatever it is)
151 when no instance is found it raises AttrError when default is not set
152 when no multiple instances are found it always raises AttrError"""
154 result
= getattrs(attrs
, get
)
156 if default
is nodefault
:
164 def getfirstattr(attrs
, get
, default
=nodefault
):
165 """get the first instance of class get out of the sequence attrs
166 when no instances are found it returns default when set (whatever it is)
167 when no instances are found it raises AttrError when default is not set"""
169 result
= getattrs(attrs
, get
)
171 if default
is nodefault
:
177 def getlastattr(attrs
, get
, default
=nodefault
):
178 """get the last instance of class get out of the sequence attrs
179 when no instances are found it returns default when set (whatever it is)
180 when no instances are found it raises AttrError when default is not set"""
182 result
= getattrs(attrs
, get
)
184 if default
is nodefault
:
190 def delattr(attrs
, remove
):
191 """create a new list of instances out of the sequence attrs
192 where all instances of class remove are removed"""
195 if not isinstance(attr
, remove
):
200 if __name__
=="__main__":
207 checkattr((a(), A(), A()), (a
, b
), (A
, B
))
208 checkattr((c(), A(), A()), (a
, b
), (A
, B
))
210 checkattr((a(), A(), A(), a()), (a
, b
), (A
, B
))
212 except AttrError
: pass
214 checkattr((c(), A(), A(), c()), (a
, b
), (A
, B
))
216 except AttrError
: pass
218 if getattrs((x1
, A(), A()), a
) != [x1
]:
220 if getattrs((x1
, A(), A(), x2
), a
) != [x1
, x2
]:
222 if getattr((x1
, A(), A()), a
) != x1
:
225 getattr((x1
, A(), A(), x2
), a
)
227 except AttrError
: pass
228 if getfirstattr((x1
, A(), A(), x2
), a
) != x1
:
230 if getlastattr((x1
, A(), A(), x2
), a
) != x2
:
232 if getattr((x1
, A(), A()), a
, x2
) != x1
:
234 if getattr((A(), A()), a
, x2
) != x2
: