Project

General

Profile

« Previous | Next » 

Revision 154

xpath.py: Added caching of parsed XPaths. Added automatic conversion of strings to paths where needed.

View differences:

scripts/lib/xpath.py
41 41
    len(elem.keys[0]) == 1: return elem.keys[0]
42 42
    else: return None
43 43

  
44
_cache = {}
45

  
44 46
def parse(str_):
47
    try: return _cache[str_]
48
    except KeyError: pass
49
    
45 50
    parser = Parser(str_)
46 51
    
47 52
    def _path():
......
117 122
    parser.str_('/') # optional leading /
118 123
    path = _path()
119 124
    parser.end()
125
    _cache[str_] = path
120 126
    return path
121 127

  
122 128
instance_level = 1
......
139 145
    '''Warning: The last_only optimization may put data that should be together
140 146
    into separate nodes'''
141 147
    if last_only == None: last_only = create
148
    if type(xpath) == str: xpath = parse(xpath)
142 149
    
143 150
    if create and not is_positive(xpath): return None
144 151
    doc = parent.ownerDocument

Also available in: Unified diff