def getTreeBuilder(treeType, implementation=None, **kwargs): """Get a TreeBuilder class for various types of tree with built-in support treeType - the name of the tree type required (case-insensitive). Supported values are "simpletree", "dom", "etree" and "beautifulsoup" "simpletree" - a built-in DOM-ish tree type with support for some more pythonic idioms. "dom" - A generic builder for DOM implementations, defaulting to a xml.dom.minidom based implementation for the sake of backwards compatibility (as releases up until 0.10 had a builder called "dom" that was a minidom implemenation). "etree" - A generic builder for tree implementations exposing an elementtree-like interface (known to work with ElementTree, cElementTree and lxml.etree). "beautifulsoup" - Beautiful soup (if installed) implementation - (Currently applies to the "etree" and "dom" tree types). A module implementing the tree type e.g. xml.etree.ElementTree or lxml.etree.""" treeType = treeType.lower() if treeType not in treeBuilderCache: if treeType == "dom": from . import dom # XXX: Keep backwards compatibility by using minidom if no implementation is given if implementation == None: from xml.dom import minidom implementation = minidom # XXX: NEVER cache here, caching is done in the dom submodule return dom.getDomModule(implementation, **kwargs).TreeBuilder elif treeType == "simpletree": from . import simpletree treeBuilderCache[treeType] = simpletree.TreeBuilder elif treeType == "beautifulsoup": from . import soup treeBuilderCache[treeType] = soup.TreeBuilder elif treeType == "lxml": from . import etree_lxml treeBuilderCache[treeType] = etree_lxml.TreeBuilder elif treeType == "etree": if implementation is None: try: import xml.etree.cElementTree as etree except: import xml.etree.ElementTree as etree implementation = etree from . import etree # XXX: NEVER cache here, caching is done in the etree submodule return etree.getETreeModule(implementation, **kwargs).TreeBuilder return treeBuilderCache.get(treeType)
def getTreeWalker(treeType, implementation=None, **kwargs): """Get a TreeWalker class for various types of tree with built-in support treeType - the name of the tree type required (case-insensitive). Supported values are "simpletree", "dom", "etree" and "beautifulsoup" "simpletree" - a built-in DOM-ish tree type with support for some more pythonic idioms. "dom" - The xml.dom.minidom DOM implementation "pulldom" - The xml.dom.pulldom event stream "etree" - A generic walker for tree implementations exposing an elementtree-like interface (known to work with ElementTree, cElementTree and lxml.etree). "lxml" - Optimized walker for lxml.etree "beautifulsoup" - Beautiful soup (if installed) "genshi" - a Genshi stream implementation - (Currently applies to the "etree" tree type only). A module implementing the tree type e.g. xml.etree.ElementTree or cElementTree.""" treeType = treeType.lower() if treeType not in treeWalkerCache: if treeType in ("dom", "pulldom", "simpletree"): mod = __import__(treeType, globals()) treeWalkerCache[treeType] = mod.TreeWalker elif treeType == "genshi": from . import genshistream treeWalkerCache[treeType] = genshistream.TreeWalker elif treeType == "beautifulsoup": from . import soup treeWalkerCache[treeType] = soup.TreeWalker elif treeType == "lxml": from . import lxmletree treeWalkerCache[treeType] = lxmletree.TreeWalker elif treeType == "etree": from . import etree if implementation is None: try: from xml.etree import cElementTree as etree except: from xml.etree import ElementTree as etree implementation = etree # XXX: NEVER cache here, caching is done in the etree submodule return etree.getETreeModule(implementation, **kwargs).TreeWalker return treeWalkerCache.get(treeType)