/usr/share/pyshared/advancedcaching/pyfo.py is in agtl 0.8.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | """pyfo - Generate XML using native python data structures.
Created and maintained by Luke Arno <luke.arno@gmail.com>
See documentation of pyfo method in this module for details.
Copyright (C) 2006-2007 Central Piedmont Community College
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to
the Free Software Foundation, Inc., 51 Franklin Street,
Fifth Floor, Boston, MA 02110-1301 USA
Central Piedmont Community College
1325 East 7th St.
Charlotte, NC 28204, USA
Luke Arno can be found at http://lukearno.com/
"""
from xml.sax.saxutils import escape
def isiterable(it):
"""return True if 'it' is iterable else return False."""
try:
iter(it)
except:
return False
else:
return True
def make_attributes(dct):
"""Turn a dict into string of XML attributes."""
return u"".join((' %s="%s"' % (x, escape(unicode(y)))
for x, y in dct.iteritems()))
def pyfo(node,
prolog=False,
pretty=False,
indent_size=2,
encoding='utf-8',
collapse=True):
"""Generate XML using native python data structures.
node structure like (name, contents) or (name, contents, attribs)
accepts stings, callables, or another node structure.
pyfo should be called with a tuple of two or three items like so:
(element, contents, attributes) or a string.
for a tuple:
the first item:
is the element name.
the second item:
if it is callable, it is called
and its return value .
if it is a list, pyfo is called on all its members
and the results are concatenated to become the contents.
otherwise it is run through 'unicode' and 'escape'.
optional third item:
should be a dictionary used as xml attributes
for a string:
just return it as unicode.
"""
if callable(node):
node = node()
if not node:
return u""
if pretty and pretty >= 0:
if pretty is True: pretty = 1
indent = '\n' + (" " * indent_size * pretty)
unindent = '\n' + (" " * indent_size * (pretty-1))
pretty += 1
else:
unindent = indent = ""
if isinstance(node, basestring):
return unicode(node)
elif len(node) == 2:
name, contents = node
dct = {}
else:
name, contents, dct = node
leaf = False
if callable(contents):
contents = contents()
if isinstance(contents, dict):
contents = contents.items()
if isinstance(contents, tuple):
contents = pyfo(contents,
pretty=pretty,
indent_size=indent_size,
collapse=collapse)
elif not isinstance(contents, basestring) and isiterable(contents):
cgen = (pyfo(c,
pretty=pretty,
indent_size=indent_size,
collapse=collapse)
for c in contents)
contents = indent.join((c for c in cgen if c))
elif contents not in [None, ""]:
contents = escape(unicode(contents))
leaf = True
if leaf:
indent = unindent = ""
if prolog:
prolog = u'<?xml version="1.0" encoding="%s"?>\n' % encoding
else:
prolog = u''
if contents or not collapse:
return u'%s<%s%s>%s%s%s</%s>' % (prolog,
name,
make_attributes(dct),
indent,
contents or '',
unindent,
name)
else:
return u'%s<%s%s/>' % (prolog,
name,
make_attributes(dct))
|