PageRenderTime 42ms CodeModel.GetById 32ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/lib/python/indra/util/fastest_elementtree.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 64 lines | 55 code | 0 blank | 9 comment | 0 complexity | 25b597a34782dfb1684e4ce12e2250e1 MD5 | raw file
 1"""\
 2@file fastest_elementtree.py
 3@brief Concealing some gnarly import logic in here.  This should export the interface of elementtree.
 4
 5$LicenseInfo:firstyear=2008&license=mit$
 6
 7Copyright (c) 2008-2009, Linden Research, Inc.
 8
 9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26$/LicenseInfo$
27"""
28
29# The parsing exception raised by the underlying library depends
30# on the ElementTree implementation we're using, so we provide an
31# alias here.
32#
33# Use ElementTreeError as the exception type for catching parsing
34# errors.
35
36
37# Using cElementTree might cause some unforeseen problems, so here's a
38# convenient off switch.
39use_celementree = True
40
41try:
42    if not use_celementree:
43        raise ImportError()
44    # Python 2.3 and 2.4.
45    from cElementTree import *
46    ElementTreeError = SyntaxError
47except ImportError:
48    try:
49        if not use_celementree:
50            raise ImportError()
51        # Python 2.5 and above.
52        from xml.etree.cElementTree import *
53        ElementTreeError = SyntaxError
54    except ImportError:
55        # Pure Python code.
56        try:
57            # Python 2.3 and 2.4.
58            from elementtree.ElementTree import *
59        except ImportError:
60            # Python 2.5 and above.
61            from xml.etree.ElementTree import *
62
63        # The pure Python ElementTree module uses Expat for parsing.
64        from xml.parsers.expat import ExpatError as ElementTreeError