012fb419ac72961c21d9b98f96b733366b8ee049
[django-cas-provider.git] / cas_provider / xml.py
1 # Import etree from anywhere
2 try:
3     # lxml http://codespeak.net/lxml/
4     from lxml import etree
5     
6     # Define register_namespace function and ElementRoot for proper serialization
7     NSMAP = {}
8     def register_namespace(prefix, uri):
9         NSMAP[prefix] = uri
10     
11     def ElementRoot(*args, **kwargs):
12         return etree.Element(*args, nsmap=NSMAP, **kwargs)
13
14 except ImportError:
15     try:
16         # Python 2.5
17         import xml.etree.cElementTree as etree
18     except ImportError:
19         try:
20             # Python 2.5
21             import xml.etree.ElementTree as etree
22         except ImportError:
23             try:
24                 # normal cElementTree install
25                 import cElementTree as etree
26             except ImportError:
27                 # normal ElementTree install
28                 import elementtree.ElementTree as etree
29
30     try:
31         register_namespace = ET.register_namespace
32     except AttributeError:
33         def register_namespace(prefix, uri):
34             ET._namespace_map[uri] = prefix
35
36     def ElementRoot(*args, **kwargs):
37         return etree.Element(*args, **kwargs)
38
39 __all__ = ('etree', 'register_namespace', 'ElementRoot')