moin-2.0 / build / lib / MoinMoin / util /

Full commit
# Copyright: 2008 MoinMoin:BastianBlank
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

MoinMoin - Tree name and element generator

from emeraldtree import ElementTree as ET

class Name(ET.QName):
    Represents a QName and factory for elements with this QName
    def __call__(self, attrib=None, children=(), **extra):
        return ET.Element(self, attrib=attrib, children=children, **extra)

class Namespace(unicode):
    Represents a namespace and factory for Names within this namespace
    def __call__(self, name):
        Create a Name within this namespace

        :param name: The name within this namespace.
        :returns: A Name
        return Name(name, self)

    def __getattr__(self, key):
        Create a Name within this namespace

        The given key is used to generate a QName within the represented
        namespace.  Two modifications are applied to the key:
         - a trailing "_" (underscore) is removed and
         - all included "_" (underscore) are replaced by "-" (hyphen).

        :returns: A Name
        if '_' in key:
            if key.startswith('_'):
                raise AttributeError(key)
            if key.endswith('_'):
                key = key[:-1]
            key = key.replace('_', '-')
        return Name(key, self)

    def __repr__(self):
        return '<{0}({1!r})>'.format(self.__class__.__name__, self)

    def namespace(self):
        return self

# MoinMoin namespaces
moin_page = Namespace('')

# Well-known namespaces
dc = Namespace('')
html = Namespace('')
mathml = Namespace('')
svg = Namespace('')
xinclude = Namespace('')
xlink = Namespace('')
docbook = Namespace('')
xml = Namespace('')