Back to index

python3.2  3.2.2
Classes | Functions | Variables
xml.etree.ElementPath Namespace Reference

Classes

class  _SelectorContext

Functions

def xpath_tokenizer
def get_parent_map
def prepare_child
def prepare_star
def prepare_self
def prepare_descendant
def prepare_parent
def prepare_predicate
def iterfind
 Generate all matching objects.
def find
 Find first matching object.
def findall
 Find all matching objects.
def findtext
 Find text for first matching object.

Variables

tuple xpath_tokenizer_re
dictionary ops
dictionary _cache = {}

Function Documentation

def xml.etree.ElementPath.find (   elem,
  path,
  namespaces = None 
)

Find first matching object.

Definition at line 283 of file ElementPath.py.

00283 
00284 def find(elem, path, namespaces=None):
00285     try:
00286         return next(iterfind(elem, path, namespaces))
00287     except StopIteration:
00288         return None

Here is the call graph for this function:

def xml.etree.ElementPath.findall (   elem,
  path,
  namespaces = None 
)

Find all matching objects.

Definition at line 292 of file ElementPath.py.

00292 
00293 def findall(elem, path, namespaces=None):
00294     return list(iterfind(elem, path, namespaces))

Here is the call graph for this function:

def xml.etree.ElementPath.findtext (   elem,
  path,
  default = None,
  namespaces = None 
)

Find text for first matching object.

Definition at line 298 of file ElementPath.py.

00298 
00299 def findtext(elem, path, default=None, namespaces=None):
00300     try:
00301         elem = next(iterfind(elem, path, namespaces))
00302         return elem.text or ""
00303     except StopIteration:
00304         return default

Here is the call graph for this function:

Definition at line 87 of file ElementPath.py.

00087 
00088 def get_parent_map(context):
00089     parent_map = context.parent_map
00090     if parent_map is None:
00091         context.parent_map = parent_map = {}
00092         for p in context.root.iter():
00093             for e in p:
00094                 parent_map[e] = p
00095     return parent_map

Here is the caller graph for this function:

def xml.etree.ElementPath.iterfind (   elem,
  path,
  namespaces = None 
)

Generate all matching objects.

Definition at line 247 of file ElementPath.py.

00247 
00248 def iterfind(elem, path, namespaces=None):
00249     # compile selector pattern
00250     if path[-1:] == "/":
00251         path = path + "*" # implicit all (FIXME: keep this?)
00252     try:
00253         selector = _cache[path]
00254     except KeyError:
00255         if len(_cache) > 100:
00256             _cache.clear()
00257         if path[:1] == "/":
00258             raise SyntaxError("cannot use absolute path on element")
00259         next = iter(xpath_tokenizer(path, namespaces)).__next__
00260         token = next()
00261         selector = []
00262         while 1:
00263             try:
00264                 selector.append(ops[token[0]](next, token))
00265             except StopIteration:
00266                 raise SyntaxError("invalid path")
00267             try:
00268                 token = next()
00269                 if token[0] == "/":
00270                     token = next()
00271             except StopIteration:
00272                 break
00273         _cache[path] = selector
00274     # execute selector pattern
00275     result = [elem]
00276     context = _SelectorContext(elem)
00277     for select in selector:
00278         result = select(context, result)
00279     return result

Here is the call graph for this function:

Here is the caller graph for this function:

def xml.etree.ElementPath.prepare_child (   next,
  token 
)

Definition at line 96 of file ElementPath.py.

00096 
00097 def prepare_child(next, token):
00098     tag = token[1]
00099     def select(context, result):
00100         for elem in result:
00101             for e in elem:
00102                 if e.tag == tag:
00103                     yield e
00104     return select

Here is the call graph for this function:

def xml.etree.ElementPath.prepare_descendant (   next,
  token 
)

Definition at line 118 of file ElementPath.py.

00118 
00119 def prepare_descendant(next, token):
00120     token = next()
00121     if token[0] == "*":
00122         tag = "*"
00123     elif not token[0]:
00124         tag = token[1]
00125     else:
00126         raise SyntaxError("invalid descendant")
00127     def select(context, result):
00128         for elem in result:
00129             for e in elem.iter(tag):
00130                 if e is not elem:
00131                     yield e
00132     return select

Here is the call graph for this function:

def xml.etree.ElementPath.prepare_parent (   next,
  token 
)

Definition at line 133 of file ElementPath.py.

00133 
00134 def prepare_parent(next, token):
00135     def select(context, result):
00136         # FIXME: raise error if .. is applied at toplevel?
00137         parent_map = get_parent_map(context)
00138         result_map = {}
00139         for elem in result:
00140             if elem in parent_map:
00141                 parent = parent_map[elem]
00142                 if parent not in result_map:
00143                     result_map[parent] = None
00144                     yield parent
00145     return select

Here is the call graph for this function:

def xml.etree.ElementPath.prepare_predicate (   next,
  token 
)

Definition at line 146 of file ElementPath.py.

00146 
00147 def prepare_predicate(next, token):
00148     # FIXME: replace with real parser!!! refs:
00149     # http://effbot.org/zone/simple-iterator-parser.htm
00150     # http://javascript.crockford.com/tdop/tdop.html
00151     signature = []
00152     predicate = []
00153     while 1:
00154         token = next()
00155         if token[0] == "]":
00156             break
00157         if token[0] and token[0][:1] in "'\"":
00158             token = "'", token[0][1:-1]
00159         signature.append(token[0] or "-")
00160         predicate.append(token[1])
00161     signature = "".join(signature)
00162     # use signature to determine predicate type
00163     if signature == "@-":
00164         # [@attribute] predicate
00165         key = predicate[1]
00166         def select(context, result):
00167             for elem in result:
00168                 if elem.get(key) is not None:
00169                     yield elem
00170         return select
00171     if signature == "@-='":
00172         # [@attribute='value']
00173         key = predicate[1]
00174         value = predicate[-1]
00175         def select(context, result):
00176             for elem in result:
00177                 if elem.get(key) == value:
00178                     yield elem
00179         return select
00180     if signature == "-" and not re.match("\d+$", predicate[0]):
00181         # [tag]
00182         tag = predicate[0]
00183         def select(context, result):
00184             for elem in result:
00185                 if elem.find(tag) is not None:
00186                     yield elem
00187         return select
00188     if signature == "-='" and not re.match("\d+$", predicate[0]):
00189         # [tag='value']
00190         tag = predicate[0]
00191         value = predicate[-1]
00192         def select(context, result):
00193             for elem in result:
00194                 for e in elem.findall(tag):
00195                     if "".join(e.itertext()) == value:
00196                         yield elem
00197                         break
00198         return select
00199     if signature == "-" or signature == "-()" or signature == "-()-":
00200         # [index] or [last()] or [last()-index]
00201         if signature == "-":
00202             index = int(predicate[0]) - 1
00203         else:
00204             if predicate[0] != "last":
00205                 raise SyntaxError("unsupported function")
00206             if signature == "-()-":
00207                 try:
00208                     index = int(predicate[2]) - 1
00209                 except ValueError:
00210                     raise SyntaxError("unsupported expression")
00211             else:
00212                 index = -1
00213         def select(context, result):
00214             parent_map = get_parent_map(context)
00215             for elem in result:
00216                 try:
00217                     parent = parent_map[elem]
00218                     # FIXME: what if the selector is "*" ?
00219                     elems = list(parent.findall(elem.tag))
00220                     if elems[index] is elem:
00221                         yield elem
00222                 except (IndexError, KeyError):
00223                     pass
00224         return select
00225     raise SyntaxError("invalid predicate")

Here is the call graph for this function:

def xml.etree.ElementPath.prepare_self (   next,
  token 
)

Definition at line 112 of file ElementPath.py.

00112 
00113 def prepare_self(next, token):
00114     def select(context, result):
00115         for elem in result:
00116             yield elem
00117     return select

Here is the call graph for this function:

def xml.etree.ElementPath.prepare_star (   next,
  token 
)

Definition at line 105 of file ElementPath.py.

00105 
00106 def prepare_star(next, token):
00107     def select(context, result):
00108         for elem in result:
00109             for e in elem:
00110                 yield e
00111     return select

Here is the call graph for this function:

def xml.etree.ElementPath.xpath_tokenizer (   pattern,
  namespaces = None 
)

Definition at line 73 of file ElementPath.py.

00073 
00074 def xpath_tokenizer(pattern, namespaces=None):
00075     for token in xpath_tokenizer_re.findall(pattern):
00076         tag = token[1]
00077         if tag and tag[0] != "{" and ":" in tag:
00078             try:
00079                 prefix, uri = tag.split(":", 1)
00080                 if not namespaces:
00081                     raise KeyError
00082                 yield token[0], "{%s}%s" % (namespaces[prefix], uri)
00083             except KeyError:
00084                 raise SyntaxError("prefix %r not found in prefix map" % prefix)
00085         else:
00086             yield token

Here is the caller graph for this function:


Variable Documentation

Definition at line 235 of file ElementPath.py.

Initial value:
00001 {
00002     "": prepare_child,
00003     "*": prepare_star,
00004     ".": prepare_self,
00005     "..": prepare_parent,
00006     "//": prepare_descendant,
00007     "[": prepare_predicate,
00008     }

Definition at line 226 of file ElementPath.py.

Initial value:
00001 re.compile(
00002     "("
00003     "'[^']*'|\"[^\"]*\"|"
00004     "::|"
00005     "//?|"
00006     "\.\.|"
00007     "\(\)|"
00008     "[/.*:\[\]\(\)@=])|"
00009     "((?:\{[^}]+\})?[^/\[\]\(\)@=\s]+)|"
00010     "\s+"
00011     )

Definition at line 61 of file ElementPath.py.