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):
99 pre = [vals[0], element.text]
103 post.append(element.tail)
105 out = pre + [self._handle_element(child) for child in element] + post
107 # clean up option scopes if necessary
108 self._options = self._options[0:options_scopes]
112 def tag(name, classes=None, **attrs):
113 """Returns a handler which wraps node contents in tag `name', with class attribute
114 set to `classes' and other attributes according to keyword paramters
117 if isinstance(classes, (tuple, list)): classes = ' '.join(classes)
118 attrs['class'] = classes
119 a = ''.join([' %s="%s"' % (k,v) for (k,v) in attrs.items()])
120 def _hnd(self, element):
121 return "<%s%s>" % (name, a), "</%s>" % name
125 def tagged(name, classes=None, **attrs):
126 """Handler decorator which wraps handler output in tag `name', with class attribute
127 set to `classes' and other attributes according to keyword paramters
130 if isinstance(classes, (tuple,list)): classes = ' '.join(classes)
131 attrs['class'] = classes
132 a = ''.join([' %s="%s"' % (k,v) for (k,v) in attrs.items()])
134 def _wrap(self, element):
138 prepend = "<%s%s>" % (name, a)
139 append = "</%s>" % name
141 if isinstance(r, tuple):
142 return prepend + r[0], r[1] + append
143 return prepend + r + append
148 def ifoption(**options):
149 """Decorator which enables node only when options are set
152 def _handler(self, *args, **kw):
154 for k, v in options.items():
157 return f(self, *args, **kw)
161 def flatten(l, ltypes=(list, tuple)):
162 """flatten function from BasicPropery/BasicTypes package
168 while isinstance(l[i], ltypes):