ioos / pyoos

A Python library for collecting Met/Ocean observations
GNU Lesser General Public License v3.0
34 stars 33 forks source link

Add Namespaces #60

Closed ocefpaf closed 5 years ago

ocefpaf commented 7 years ago

OWSLib has this nice idea of monkey patching etree to add some namespaces. Maybe we could do something similar here to simply the code base. Does that make sense?

def patch_well_known_namespaces(etree_module):
    import warnings
    from pyoos.namespaces import Namespaces
    ns = Namespaces()

    """Monkey patches the etree module to add some well-known namespaces."""

    try:
        register_namespace = etree_module.register_namespace
    except AttributeError:
        try:
            etree_module._namespace_map

            def register_namespace(prefix, uri):
                etree_module._namespace_map[uri] = prefix
        except AttributeError:
            def register_namespace(prefix, uri):
                pass
            warnings.warn("Only 'lxml.etree' >= 2.3 and 'xml.etree.ElementTree' >= 1.3 are fully supported!")

    for k, v in six.iteritems(ns.get_namespaces()):
        register_namespace(k, v)

patch_well_known_namespaces(etree)