Classes | |
class | SVGDepthIterator |
nested class More... | |
Public Member Functions | |
def | __contains__ |
def | __delitem__ |
def | __eq__ |
def | __getitem__ |
def | __init__ |
def | __iter__ |
def | __ne__ |
def | __repr__ |
def | __setitem__ |
def | __str__ |
def | append |
def | breadth_first |
def | clone |
def | depth_first |
end nested class | |
def | extend |
def | firefox |
def | inkscape |
def | inkview |
def | interpret_fileName |
def | items |
def | keys |
def | prepend |
def | save |
def | standalone_xml |
def | tree |
def | values |
def | xml |
Public Attributes | |
attr | |
sub | |
t | |
Private Member Functions | |
def | __standalone_xml |
A tree representation of an SVG image or image fragment. SVG(t, sub, sub, sub..., attribute=value) t required SVG type name sub optional list nested SVG elements or text/Unicode attribute=value pairs optional keywords SVG attributes In attribute names, "__" becomes ":" and "_" becomes "-". SVG in XML <g id="mygroup" fill="blue"> <rect x="1" y="1" width="2" height="2" /> <rect x="3" y="3" width="2" height="2" /> </g> SVG in Python >>> svg = SVG("g", SVG("rect", x=1, y=1, width=2, height=2), \ ... SVG("rect", x=3, y=3, width=2, height=2), \ ... id="mygroup", fill="blue") Sub-elements and attributes may be accessed through tree-indexing: >>> svg = SVG("text", SVG("tspan", "hello there"), stroke="none", fill="black") >>> svg[0] <tspan (1 sub) /> >>> svg[0, 0] 'hello there' >>> svg["fill"] 'black' Iteration is depth-first: >>> svg = SVG("g", SVG("g", SVG("line", x1=0, y1=0, x2=1, y2=1)), \ ... SVG("text", SVG("tspan", "hello again"))) ... >>> for ti, s in svg: ... print ti, repr(s) ... (0,) <g (1 sub) /> (0, 0) <line x2=1 y1=0 x1=0 y2=1 /> (0, 0, 'x2') 1 (0, 0, 'y1') 0 (0, 0, 'x1') 0 (0, 0, 'y2') 1 (1,) <text (1 sub) /> (1, 0) <tspan (1 sub) /> (1, 0, 0) 'hello again' Use "print" to navigate: >>> print svg None <g (2 sub) /> [0] <g (1 sub) /> [0, 0] <line x2=1 y1=0 x1=0 y2=1 /> [1] <text (1 sub) /> [1, 0] <tspan (1 sub) />
def svgfig::SVG::__init__ | ( | self, | |
t_sub, | |||
attr | |||
) |
Definition at line 123 of file svgfig.py.
00124 : 00125 if len(t_sub) == 0: raise TypeError, "SVG element must have a t (SVG type)" 00126 00127 # first argument is t (SVG type) 00128 self.t = t_sub[0] 00129 # the rest are sub-elements 00130 self.sub = list(t_sub[1:]) 00131 00132 # keyword arguments are attributes 00133 # need to preprocess to handle differences between SVG and Python syntax 00134 self.attr = attr_preprocess(attr)
def svgfig::SVG::__contains__ | ( | self, | |
value | |||
) |
def svgfig::SVG::__delitem__ | ( | self, | |
ti | |||
) |
Index is a list that descends tree, returning a sub-element if it ends with a number and an attribute if it ends with a string.
Definition at line 157 of file svgfig.py.
00158 : 00159 """Index is a list that descends tree, returning a sub-element if 00160 it ends with a number and an attribute if it ends with a string.""" 00161 obj = self 00162 if isinstance(ti, (list, tuple)): 00163 for i in ti[:-1]: obj = obj[i] 00164 ti = ti[-1] 00165 00166 if isinstance(ti, (int, long, slice)): del obj.sub[ti] 00167 else: del obj.attr[ti]
def svgfig::SVG::__eq__ | ( | self, | |
other | |||
) |
x == y iff x represents the same SVG as y.
def svgfig::SVG::__getitem__ | ( | self, | |
ti | |||
) |
Index is a list that descends tree, returning a sub-element if it ends with a number and an attribute if it ends with a string.
Definition at line 135 of file svgfig.py.
00136 : 00137 """Index is a list that descends tree, returning a sub-element if 00138 it ends with a number and an attribute if it ends with a string.""" 00139 obj = self 00140 if isinstance(ti, (list, tuple)): 00141 for i in ti[:-1]: obj = obj[i] 00142 ti = ti[-1] 00143 00144 if isinstance(ti, (int, long, slice)): return obj.sub[ti] 00145 else: return obj.attr[ti]
def svgfig::SVG::__iter__ | ( | self | ) |
Definition at line 246 of file svgfig.py.
00246 : return self.depth_first() 00247
def svgfig::SVG::__ne__ | ( | self, | |
other | |||
) |
def svgfig::SVG::__repr__ | ( | self | ) |
def svgfig::SVG::__setitem__ | ( | self, | |
ti, | |||
value | |||
) |
Index is a list that descends tree, returning a sub-element if it ends with a number and an attribute if it ends with a string.
Definition at line 146 of file svgfig.py.
00147 : 00148 """Index is a list that descends tree, returning a sub-element if 00149 it ends with a number and an attribute if it ends with a string.""" 00150 obj = self 00151 if isinstance(ti, (list, tuple)): 00152 for i in ti[:-1]: obj = obj[i] 00153 ti = ti[-1] 00154 00155 if isinstance(ti, (int, long, slice)): obj.sub[ti] = value 00156 else: obj.attr[ti] = value
def svgfig::SVG::__standalone_xml | ( | self, | |
indent, | |||
newl | |||
) | [private] |
Definition at line 372 of file svgfig.py.
00373 : 00374 output = [u"<%s" % self.t] 00375 00376 for n, v in self.attr.items(): 00377 if isinstance(v, dict): 00378 v = "; ".join(["%s:%s" % (ni, vi) for ni, vi in v.items()]) 00379 elif isinstance(v, (list, tuple)): 00380 v = ", ".join(v) 00381 output.append(u" %s=\"%s\"" % (n, v)) 00382 00383 if len(self.sub) == 0: 00384 output.append(u" />%s%s" % (newl, newl)) 00385 return output 00386 00387 elif self.t == "text" or self.t == "tspan" or self.t == "style": 00388 output.append(u">") 00389 00390 else: 00391 output.append(u">%s%s" % (newl, newl)) 00392 00393 for s in self.sub: 00394 if isinstance(s, SVG): output.extend(s.__standalone_xml(indent, newl)) 00395 else: output.append(unicode(s)) 00396 00397 if self.t == "tspan": output.append(u"</%s>" % self.t) 00398 else: output.append(u"</%s>%s%s" % (self.t, newl, newl)) 00399 00400 return output
def svgfig::SVG::__str__ | ( | self | ) |
def svgfig::SVG::append | ( | self, | |
x | |||
) |
def svgfig::SVG::breadth_first | ( | self, | |
depth_limit = None |
|||
) |
Not implemented yet. Any ideas on how to do it? Returns a breadth-first generator over the SVG. If depth_limit is a number, stop recursion at that depth.
Definition at line 239 of file svgfig.py.
00240 : 00241 """Not implemented yet. Any ideas on how to do it? 00242 00243 Returns a breadth-first generator over the SVG. If depth_limit 00244 is a number, stop recursion at that depth.""" 00245 raise NotImplementedError, "Got an algorithm for breadth-first searching a tree without effectively copying the tree?"
def svgfig::SVG::clone | ( | self, | |
shallow = False |
|||
) |
def svgfig::SVG::depth_first | ( | self, | |
depth_limit = None |
|||
) |
end nested class
Returns a depth-first generator over the SVG. If depth_limit is a number, stop recursion at that depth.
def svgfig::SVG::extend | ( | self, | |
x | |||
) |
def svgfig::SVG::firefox | ( | self, | |
fileName = None , |
|||
encoding = "utf-8" |
|||
) |
View in "firefox", assuming that program is available on your system. fileName default=None note that any file named _default_fileName will be overwritten if no fileName is specified. If the extension is ".svgz" or ".gz", the output will be gzipped encoding default="utf-8" file encoding (default is Unicode)
Definition at line 461 of file svgfig.py.
00462 : 00463 """View in "firefox", assuming that program is available on your system. 00464 00465 fileName default=None note that any file named _default_fileName will be 00466 overwritten if no fileName is specified. If the extension 00467 is ".svgz" or ".gz", the output will be gzipped 00468 encoding default="utf-8" file encoding (default is Unicode) 00469 """ 00470 fileName = self.interpret_fileName(fileName) 00471 self.save(fileName, encoding) 00472 os.spawnvp(os.P_NOWAIT, "firefox", ("firefox", fileName))
def svgfig::SVG::inkscape | ( | self, | |
fileName = None , |
|||
encoding = "utf-8" |
|||
) |
View in "inkscape", assuming that program is available on your system. fileName default=None note that any file named _default_fileName will be overwritten if no fileName is specified. If the extension is ".svgz" or ".gz", the output will be gzipped encoding default="utf-8" file encoding (default is Unicode)
Definition at line 449 of file svgfig.py.
00450 : 00451 """View in "inkscape", assuming that program is available on your system. 00452 00453 fileName default=None note that any file named _default_fileName will be 00454 overwritten if no fileName is specified. If the extension 00455 is ".svgz" or ".gz", the output will be gzipped 00456 encoding default="utf-8" file encoding (default is Unicode) 00457 """ 00458 fileName = self.interpret_fileName(fileName) 00459 self.save(fileName, encoding) 00460 os.spawnvp(os.P_NOWAIT, "inkscape", ("inkscape", fileName))
def svgfig::SVG::inkview | ( | self, | |
fileName = None , |
|||
encoding = "utf-8" |
|||
) |
View in "inkview", assuming that program is available on your system. fileName default=None note that any file named _default_fileName will be overwritten if no fileName is specified. If the extension is ".svgz" or ".gz", the output will be gzipped encoding default="utf-8" file encoding (default is Unicode)
Definition at line 437 of file svgfig.py.
00438 : 00439 """View in "inkview", assuming that program is available on your system. 00440 00441 fileName default=None note that any file named _default_fileName will be 00442 overwritten if no fileName is specified. If the extension 00443 is ".svgz" or ".gz", the output will be gzipped 00444 encoding default="utf-8" file encoding (default is Unicode) 00445 """ 00446 fileName = self.interpret_fileName(fileName) 00447 self.save(fileName, encoding) 00448 os.spawnvp(os.P_NOWAIT, "inkview", ("inkview", fileName))
def svgfig::SVG::interpret_fileName | ( | self, | |
fileName = None |
|||
) |
def svgfig::SVG::items | ( | self, | |
sub = True , |
|||
attr = True , |
|||
text = True |
|||
) |
Get a recursively-generated list of tree-index, sub-element/attribute pairs. If sub == False, do not show sub-elements. If attr == False, do not show attributes. If text == False, do not show text/Unicode sub-elements.
Definition at line 248 of file svgfig.py.
00249 : 00250 """Get a recursively-generated list of tree-index, sub-element/attribute pairs. 00251 00252 If sub == False, do not show sub-elements. 00253 If attr == False, do not show attributes. 00254 If text == False, do not show text/Unicode sub-elements. 00255 """ 00256 output = [] 00257 for ti, s in self: 00258 show = False 00259 if isinstance(ti[-1], (int, long)): 00260 if isinstance(s, basestring): show = text 00261 else: show = sub 00262 else: show = attr 00263 00264 if show: output.append((ti, s)) 00265 return output
def svgfig::SVG::keys | ( | self, | |
sub = True , |
|||
attr = True , |
|||
text = True |
|||
) |
Get a recursively-generated list of tree-indexes. If sub == False, do not show sub-elements. If attr == False, do not show attributes. If text == False, do not show text/Unicode sub-elements.
Definition at line 266 of file svgfig.py.
00267 : 00268 """Get a recursively-generated list of tree-indexes. 00269 00270 If sub == False, do not show sub-elements. 00271 If attr == False, do not show attributes. 00272 If text == False, do not show text/Unicode sub-elements. 00273 """ 00274 return [ti for ti, s in self.items(sub, attr, text)]
def svgfig::SVG::prepend | ( | self, | |
x | |||
) |
def svgfig::SVG::save | ( | self, | |
fileName = None , |
|||
encoding = "utf-8" , |
|||
compresslevel = None |
|||
) |
Save to a file for viewing. Note that svg.save() overwrites the file named _default_fileName. fileName default=None note that _default_fileName will be overwritten if no fileName is specified. If the extension is ".svgz" or ".gz", the output will be gzipped encoding default="utf-8" file encoding (default is Unicode) compresslevel default=None if a number, the output will be gzipped with that compression level (1-9, 1 being fastest and 9 most thorough)
Definition at line 408 of file svgfig.py.
00409 : 00410 """Save to a file for viewing. Note that svg.save() overwrites the file named _default_fileName. 00411 00412 fileName default=None note that _default_fileName will be overwritten if 00413 no fileName is specified. If the extension 00414 is ".svgz" or ".gz", the output will be gzipped 00415 encoding default="utf-8" file encoding (default is Unicode) 00416 compresslevel default=None if a number, the output will be gzipped with that 00417 compression level (1-9, 1 being fastest and 9 most 00418 thorough) 00419 """ 00420 fileName = self.interpret_fileName(fileName) 00421 00422 if compresslevel != None or re.search("\.svgz$", fileName, re.I) or re.search("\.gz$", fileName, re.I): 00423 import gzip 00424 if compresslevel == None: 00425 f = gzip.GzipFile(fileName, "w") 00426 else: 00427 f = gzip.GzipFile(fileName, "w", compresslevel) 00428 00429 f = codecs.EncodedFile(f, "utf-8", encoding) 00430 f.write(self.standalone_xml()) 00431 f.close() 00432 00433 else: 00434 f = codecs.open(fileName, "w", encoding=encoding) 00435 f.write(self.standalone_xml()) 00436 f.close()
def svgfig::SVG::standalone_xml | ( | self, | |
indent = " " , |
|||
newl = "\n" |
|||
) |
Get an XML representation of the SVG that can be saved/rendered. indent string used for indenting newl string used for newlines
Definition at line 357 of file svgfig.py.
00358 : 00359 """Get an XML representation of the SVG that can be saved/rendered. 00360 00361 indent string used for indenting 00362 newl string used for newlines 00363 """ 00364 00365 if self.t == "svg": top = self 00366 else: top = canvas(self) 00367 return """\ 00368 <?xml version="1.0" standalone="no"?> 00369 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 00370 00371 """ + ("".join(top.__standalone_xml(indent, newl))) # end of return statement
def svgfig::SVG::tree | ( | self, | |
depth_limit = None , |
|||
sub = True , |
|||
attr = True , |
|||
text = True , |
|||
tree_width = 20 , |
|||
obj_width = 80 |
|||
) |
Print (actually, return a string of) the tree in a form useful for browsing. If depth_limit == a number, stop recursion at that depth. If sub == False, do not show sub-elements. If attr == False, do not show attributes. If text == False, do not show text/Unicode sub-elements. tree_width is the number of characters reserved for printing tree indexes. obj_width is the number of characters reserved for printing sub-elements/attributes.
Definition at line 290 of file svgfig.py.
00291 : 00292 """Print (actually, return a string of) the tree in a form useful for browsing. 00293 00294 If depth_limit == a number, stop recursion at that depth. 00295 If sub == False, do not show sub-elements. 00296 If attr == False, do not show attributes. 00297 If text == False, do not show text/Unicode sub-elements. 00298 tree_width is the number of characters reserved for printing tree indexes. 00299 obj_width is the number of characters reserved for printing sub-elements/attributes. 00300 """ 00301 00302 output = [] 00303 00304 line = "%s %s" % (("%%-%ds" % tree_width) % repr(None), ("%%-%ds" % obj_width) % (repr(self))[0:obj_width]) 00305 output.append(line) 00306 00307 for ti, s in self.depth_first(depth_limit): 00308 show = False 00309 if isinstance(ti[-1], (int, long)): 00310 if isinstance(s, basestring): show = text 00311 else: show = sub 00312 else: show = attr 00313 00314 if show: 00315 line = "%s %s" % (("%%-%ds" % tree_width) % repr(list(ti)), ("%%-%ds" % obj_width) % (" "*len(ti) + repr(s))[0:obj_width]) 00316 output.append(line) 00317 00318 return "\n".join(output)
def svgfig::SVG::values | ( | self, | |
sub = True , |
|||
attr = True , |
|||
text = True |
|||
) |
Get a recursively-generated list of sub-elements and attributes. If sub == False, do not show sub-elements. If attr == False, do not show attributes. If text == False, do not show text/Unicode sub-elements.
Definition at line 275 of file svgfig.py.
00276 : 00277 """Get a recursively-generated list of sub-elements and attributes. 00278 00279 If sub == False, do not show sub-elements. 00280 If attr == False, do not show attributes. 00281 If text == False, do not show text/Unicode sub-elements. 00282 """ 00283 return [s for ti, s in self.items(sub, attr, text)]
def svgfig::SVG::xml | ( | self, | |
indent = " " , |
|||
newl = "\n" , |
|||
depth_limit = None , |
|||
depth = 0 |
|||
) |
Get an XML representation of the SVG. indent string used for indenting newl string used for newlines If depth_limit == a number, stop recursion at that depth. depth starting depth (not useful for users) print svg.xml()
Definition at line 319 of file svgfig.py.
00320 : 00321 """Get an XML representation of the SVG. 00322 00323 indent string used for indenting 00324 newl string used for newlines 00325 If depth_limit == a number, stop recursion at that depth. 00326 depth starting depth (not useful for users) 00327 00328 print svg.xml() 00329 """ 00330 00331 attrstr = [] 00332 for n, v in self.attr.items(): 00333 if isinstance(v, dict): 00334 v = "; ".join(["%s:%s" % (ni, vi) for ni, vi in v.items()]) 00335 elif isinstance(v, (list, tuple)): 00336 v = ", ".join(v) 00337 attrstr.append(" %s=%s" % (n, repr(v))) 00338 attrstr = "".join(attrstr) 00339 00340 if len(self.sub) == 0: return "%s<%s%s />" % (indent * depth, self.t, attrstr) 00341 00342 if depth_limit == None or depth_limit > depth: 00343 substr = [] 00344 for s in self.sub: 00345 if isinstance(s, SVG): 00346 substr.append(s.xml(indent, newl, depth_limit, depth + 1) + newl) 00347 elif isinstance(s, str): 00348 substr.append("%s%s%s" % (indent * (depth + 1), s, newl)) 00349 else: 00350 substr.append("%s%s%s" % (indent * (depth + 1), repr(s), newl)) 00351 substr = "".join(substr) 00352 00353 return "%s<%s%s>%s%s%s</%s>" % (indent * depth, self.t, attrstr, newl, substr, indent * depth, self.t) 00354 00355 else: 00356 return "%s<%s (%d sub)%s />" % (indent * depth, self.t, len(self.sub), attrstr)