def create_POM(data, dtd): """Given a python object, produce reasonable markup from that. Return a valid POM node. Dictionaries produce sections (divs) where key names are the class attribute name; lists produce ordered lists; Sets produce unordered lists; Tuples produce a fragment collection (invisible), an ElementNode is taken as-is, and strings return as Text nodes. Callables are called with the DTD, and the return value checked. """ if data is None: return NBSP # Good representation of nothing? it = type(data) if it is dict: creator = FlowCreator(dtd) outer = POM.Fragments() for name, content in data.iteritems(): div = creator.get_section(name) div.append(create_POM(content, dtd)) outer.append(div) return outer elif it is list: creator = FlowCreator(dtd) ol = creator.get_ordered_list() for item in data: ol.add_item(item) return ol elif it is set: creator = FlowCreator(dtd) ul = creator.get_unordered_list() for item in data: ul.add_item(item) return ul elif it is tuple: frags = POM.Fragments() for item in data: frags.append(create_POM(item, dtd)) return frags elif it is unicode: return POM.Text(data) elif isinstance(data, POM.ElementNode): return data elif it is FunctionType: return check_object(data(dtd)) else: return POM.Text(data)
def check_object(obj): if obj is None: return NBSP if type(obj) in (str, unicode, int, float, long): return POM.Text(str(obj)) if isinstance(obj, POM.ElementNode): return obj raise ValidationError("bad initializer object: should be string or ElementNode instance.")
def _add_options(self, sl, enums): et = type(enums) if et is Enums and enums: for i, name in enums.choices: opt = self.dtd.Option(value=i) opt.append(POM.Text(name)) sl.append(opt) return if et is dict and enums: for key, val in enums.items(): opt = self.dtd.Optgroup(label=key) self._add_options(opt, val) sl.append(opt) return if et is tuple and enums: name, value = enums opt = self.dtd.Option(value=value) opt.append(POM.Text(name)) return if et is list and enums: for item in enums: it = type(item) if it is tuple: # add "selected" item by adding (value, flag) opt = self.dtd.Option(value=item[0]) opt.append(POM.Text(item[1])) if len(item) > 2 and item[2]: opt.selected = "selected" elif it is dict: # make option groups by passing dictionaries for key, val in item.items(): optgrp = self.dtd.Optgroup(label=key) sl.append(optgrp) self._add_options(optgrp, val) elif it is Enum: # a named number opt = self.dtd.Option(value=int(item)) opt.append(POM.Text(item)) elif it is list: # nested lists will be flattened self._add_options(sl, item) else: opt = self.dtd.Option(value=item) opt.append(POM.Text(item)) sl.append(opt) return else: opt = self.dtd.Option(value=enums) opt.append(POM.Text(enums)) sl.append(opt) return
def characters(self, text): if self.stack and text: self.stack[-1].append(POM.Text(text))
def check_object(obj): if type(obj) in (str, unicode): return POM.Text(obj) if isinstance(obj, POM.ElementNode): return obj raise POM.ValidationError, "bad initializer object"
def get_header(self, level, text, **kwargs): hobj = Heading(level=level) hobj.append(POM.Text(text)) return hobj
def add_title(self, title): ti = self.head.add(self.dtd.Title) ti.append(POM.Text(title))
def new_preformat(self, text, **kwargs): p = self.get_preformat(**kwargs) t = POM.Text(text) p.append(t) self.append(p) return p
def get_header(self, level, text, **kwargs): hobj = get_inlinecontainer(self.dtd, "H%d" % (level, ), kwargs) hobj.append(POM.Text(text)) return hobj