Ejemplo n.º 1
0
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)