1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from collections import defaultdict
11 """Transforms XML to some text.
12 Used instead of XSLT which is difficult and cumbersome.
15 def __init__(self, options=None):
18 self._options.append(options)
20 def generate(self, document):
21 """Generate text from node using handlers defined in class."""
22 output = self._handle_element(document)
23 return u''.join([x for x in flatten(output) if x is not None])
27 """Returnes merged scoped options for current node.
29 # Here we can see how a decision not to return the modified map
30 # leads to a need for a hack.
31 return reduce(lambda a, b: a.update(b) or a, self._options, defaultdict(lambda: False))
34 def options(self, opts):
35 """Sets options overrides for current and child nodes
37 self._options.append(opts)
40 def _handle_for_element(self, element):
43 # from nose.tools import set_trace
45 if isinstance(element, etree._Comment): return None
47 if element.tag[0] == '{':
48 for nshort, nhref in element.nsmap.items():
50 if element.tag.index('{%s}' % nhref) == 0:
52 tagname = element.tag[len('{%s}' % nhref):]
57 raise ValueError("Strange ns for tag: %s, nsmap: %s" %
58 (element.tag, element.nsmap))
63 meth_name = "handle_%s__%s" % (ns, tagname)
65 meth_name = "handle_%s" % (tagname,)
67 handler = getattr(self, meth_name, None)
70 def next(self, element):
75 sibling = element.getnext()
76 if sibling is not None: return sibling # found a new branch to dig into
77 element = element.getparent()
78 if element is None: return None # end of tree
80 def _handle_element(self, element):
81 handler = self._handle_for_element(element)
84 options_scopes = len(self._options)
90 vals = handler(element)
91 # depending on number of returned values, vals can be None, a value, or a tuple.
92 # how poorly designed is that? 9 lines below are needed just to unpack this.
96 if not isinstance(vals, tuple):
100 pre = [vals[0], element.text]
104 post.append(element.tail)
106 out = pre + [self._handle_element(child) for child in element] + post
108 # clean up option scopes if necessary
109 self._options = self._options[0:options_scopes]
113 def tag(name, classes=None, **attrs):
114 """Returns a handler which wraps node contents in tag `name', with class attribute
115 set to `classes' and other attributes according to keyword paramters
118 if isinstance(classes, (tuple, list)): classes = ' '.join(classes)
119 attrs['class'] = classes
120 a = ''.join([' %s="%s"' % (k,v) for (k,v) in attrs.items()])
121 def _hnd(self, element):
122 return "<%s%s>" % (name, a), "</%s>" % name
126 def tagged(name, classes=None, **attrs):
127 """Handler decorator which wraps handler output in tag `name', with class attribute
128 set to `classes' and other attributes according to keyword paramters
131 if isinstance(classes, (tuple,list)): classes = ' '.join(classes)
132 attrs['class'] = classes
133 a = ''.join([' %s="%s"' % (k,v) for (k,v) in attrs.items()])
135 def _wrap(self, element):
139 prepend = "<%s%s>" % (name, a)
140 append = "</%s>" % name
142 if isinstance(r, tuple):
143 return prepend + r[0], r[1] + append
144 return prepend + r + append
149 def ifoption(**options):
150 """Decorator which enables node only when options are set
153 def _handler(self, *args, **kw):
155 for k, v in options.items():
158 return f(self, *args, **kw)
162 def flatten(l, ltypes=(list, tuple)):
163 """flatten function from BasicPropery/BasicTypes package
169 while isinstance(l[i], ltypes):