make all parts of the manual compile again; parts of the manual are still out of...
[PyX/mjg.git] / pyx / helper.py
blobc5883a8e8c1d6ffc1d0167af5f8f3ae644abaa53
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2002 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 import base
28 class nodefault: pass
31 def isstring(arg):
32 "arg is string-like (cf. python cookbook 3.2)"
33 try: arg + ''
34 except: return 0
35 return 1
38 def isnumber(arg):
39 "arg is number-like"
40 try: arg + 0
41 except: return 0
42 return 1
45 def isinteger(arg):
46 "arg is integer-like"
47 try:
48 if type(arg + 0.0) is type(arg):
49 return 0
50 return 1
51 except: return 0
54 def issequence(arg):
55 """arg is sequence-like (e.g. has a len)
56 a string is *not* considered to be a sequence"""
57 if isstring(arg): return 0
58 try: len(arg)
59 except: return 0
60 return 1
63 def ensuresequence(arg):
64 """return arg or (arg,) depending on the result of issequence,
65 None is converted to ()"""
66 if isstring(arg): return (arg,)
67 if arg is None: return ()
68 if issequence(arg): return arg
69 return (arg,)
72 def ensurelist(arg):
73 """return list(arg) or [arg] depending on the result of isequence,
74 None is converted to []"""
75 if isstring(arg): return [arg]
76 if arg is None: return []
77 if issequence(arg): return list(arg)
78 return [arg]
80 def getitemno(arg, n):
81 """get item number n if arg is a sequence (when the sequence
82 is not long enough, None is returned), otherweise arg is
83 returned"""
84 if issequence(arg):
85 try: return arg[n]
86 except: return None
87 else:
88 return arg
91 def issequenceofsequences(arg):
92 """check if arg has a sequence or None as it's first entry"""
93 return issequence(arg) and len(arg) and (issequence(arg[0]) or arg[0] is None)
96 def getsequenceno(arg, n):
97 """get sequence number n if arg is a sequence of sequences (when
98 the sequence is not long enough, None is returned), otherwise
99 arg is returned"""
100 if issequenceofsequences(arg):
101 try: return arg[n]
102 except: return None
103 else:
104 return arg
107 class AttrError(base.PyXExcept): pass
110 def checkattr(attrs, allowonce=(), allowmulti=()):
111 """checks the sequence attrs for occurencies of instances
112 the classes provided as a tuple to allowonce are allowed only once
113 the classes provided as a tuple to allowonce are allowed multiple times"""
114 hadonce = []
115 for attr in attrs:
116 for once in allowonce:
117 if isinstance(attr, once):
118 if once in hadonce:
119 raise AttrError("only a single instance of %r allowed" % once)
120 else:
121 hadonce += [once]
122 break
123 else:
124 for multi in allowmulti:
125 if isinstance(attr, multi):
126 break
127 else:
128 raise AttrError("%r not allowed" % attr)
131 def getattrs(attrs, get, default=nodefault):
132 """creates a list of instances of class get out of the sequence attrs
133 when no instances are found it returns default when set (whatever it is)
134 when no instances are found it raises AttrError when default is not set"""
135 first = 1
136 for attr in attrs:
137 if isinstance(attr, get):
138 if first:
139 result = [attr]
140 first = 0
141 else:
142 result.append(attr)
143 if first:
144 if default is nodefault:
145 raise AttrError
146 else:
147 return default
148 return result
151 def countattrs(attrs, check):
152 "count the occurancies of instances of class get out of the sequence attrs"
153 return len(getattrs(attrs, check, ()))
156 def getattr(attrs, get, default=nodefault):
157 """get the instance of class get out of the sequence attrs
158 when no instance is found it returns default when set (whatever it is)
159 when no instance is found it raises AttrError when default is not set
160 when no multiple instances are found it always raises AttrError"""
161 try:
162 result = getattrs(attrs, get)
163 except AttrError:
164 if default is nodefault:
165 raise AttrError
166 else:
167 return default
168 if len(result) > 1:
169 raise AttrError
170 return result[0]
173 def getfirstattr(attrs, get, default=nodefault):
174 """get the first instance of class get out of the sequence attrs
175 when no instances are found it returns default when set (whatever it is)
176 when no instances are found it raises AttrError when default is not set"""
177 try:
178 result = getattrs(attrs, get)
179 except AttrError:
180 if default is nodefault:
181 raise AttrError
182 else:
183 return default
184 return result[0]
187 def getlastattr(attrs, get, default=nodefault):
188 """get the last instance of class get out of the sequence attrs
189 when no instances are found it returns default when set (whatever it is)
190 when no instances are found it raises AttrError when default is not set"""
191 try:
192 result = getattrs(attrs, get)
193 except AttrError:
194 if default is nodefault:
195 raise AttrError
196 else:
197 return default
198 return result[-1]
201 def delattr(attrs, remove):
202 """create a new list of instances out of the sequence attrs
203 where all instances of class remove are removed"""
204 result = []
205 for attr in attrs:
206 if not isinstance(attr, remove):
207 result.append(attr)
208 return result