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)
19 self.text_filters = []
21 def register_text_filter(self, fun):
22 self.text_filters.append(fun)
24 def filter_text(self, text):
25 # TODO: just work on the tree and let lxml handle escaping.
26 e = etree.Element("x")
28 # This whole mixing text with ML is so wrong.
29 output = etree.tostring(e, encoding=unicode)[3:-4]
30 for flt in self.text_filters:
35 def generate(self, document):
36 """Generate text from node using handlers defined in class."""
37 output = self._handle_element(document)
38 return u''.join([x for x in flatten(output) if x is not None])
42 """Returnes merged scoped options for current node.
44 # Here we can see how a decision not to return the modified map
45 # leads to a need for a hack.
46 return reduce(lambda a, b: a.update(b) or a, self._options, defaultdict(lambda: None))
49 def options(self, opts):
50 """Sets options overrides for current and child nodes
52 self._options.append(opts)
55 def _handle_for_element(self, element):
58 # from nose.tools import set_trace
60 if element.tag[0] == '{':
61 for nshort, nhref in element.nsmap.items():
63 if element.tag.index('{%s}' % nhref) == 0:
65 tagname = element.tag[len('{%s}' % nhref):]
70 raise ValueError("Strange ns for tag: %s, nsmap: %s" %
71 (element.tag, element.nsmap))
76 meth_name = "handle_%s__%s" % (ns, tagname)
78 meth_name = "handle_%s" % (tagname,)
80 handler = getattr(self, meth_name, None)
83 def next(self, element):
88 sibling = element.getnext()
89 if sibling is not None: return sibling # found a new branch to dig into
90 element = element.getparent()
91 if element is None: return None # end of tree
93 def _handle_element(self, element):
94 if isinstance(element, etree._Comment): return None
96 handler = self._handle_for_element(element)
99 options_scopes = len(self._options)
102 pre = [self.filter_text(element.text)]
103 post = [self.filter_text(element.tail)]
105 vals = handler(element)
106 # depending on number of returned values, vals can be None, a value, or a tuple.
107 # how poorly designed is that? 9 lines below are needed just to unpack this.
109 return [self.filter_text(element.tail)]
111 if not isinstance(vals, tuple):
112 return [vals, self.filter_text(element.tail)]
114 pre = [vals[0], self.filter_text(element.text)]
115 post = [vals[1], self.filter_text(element.tail)]
117 out = pre + [self._handle_element(child) for child in element] + post
119 # clean up option scopes if necessary
120 self._options = self._options[0:options_scopes]
124 def tag_open_close(name_, classes_=None, **attrs):
125 u"""Creates tag beginning and end.
127 >>> tag_open_close("a", "klass", x=u"ą<")
128 (u'<a x="\\u0105<" class="klass">', u'</a>')
132 if isinstance(classes_, (tuple, list)): classes_ = ' '.join(classes_)
133 attrs['class'] = classes_
135 e = etree.Element(name_)
137 for k, v in attrs.items():
139 pre, post = etree.tostring(e, encoding=unicode).split(u"> <")
140 return pre + u">", u"<" + post
142 def tag(name_, classes_=None, **attrs):
143 """Returns a handler which wraps node contents in tag `name', with class attribute
144 set to `classes' and other attributes according to keyword paramters
146 def _hnd(self, element):
147 return tag_open_close(name_, classes_, **attrs)
151 def tagged(name, classes=None, **attrs):
152 """Handler decorator which wraps handler output in tag `name', with class attribute
153 set to `classes' and other attributes according to keyword paramters
156 if isinstance(classes, (tuple,list)): classes = ' '.join(classes)
157 attrs['class'] = classes
158 a = ''.join([' %s="%s"' % (k,v) for (k,v) in attrs.items()])
160 def _wrap(self, element):
164 prepend = "<%s%s>" % (name, a)
165 append = "</%s>" % name
167 if isinstance(r, tuple):
168 return prepend + r[0], r[1] + append
169 return prepend + r + append
174 def ifoption(**options):
175 """Decorator which enables node only when options are set
178 def _handler(self, *args, **kw):
180 for k, v in options.items():
183 return f(self, *args, **kw)
187 def flatten(l, ltypes=(list, tuple)):
188 """flatten function from BasicPropery/BasicTypes package
194 while isinstance(l[i], ltypes):