Revision 1001
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/xpath.py | ||
---|---|---|
6 | 6 |
import util |
7 | 7 |
import xml_dom |
8 | 8 |
|
9 |
##### Path elements |
|
10 |
|
|
9 | 11 |
class XpathElem: |
10 | 12 |
def __init__(self, name, value=None, is_attr=False): |
11 | 13 |
if name == '': name = '.' |
... | ... | |
32 | 34 |
|
33 | 35 |
def __eq__(self, other): return self.__dict__ == other.__dict__ |
34 | 36 |
|
37 |
##### Paths |
|
38 |
|
|
35 | 39 |
def is_positive(path): return path[0].is_positive |
36 | 40 |
|
37 | 41 |
def value(path): return path[-1].value |
... | ... | |
42 | 46 |
if len(elem.keys) >= 1 and value(elem.keys[0]) == None: return elem.keys[0] |
43 | 47 |
else: return None |
44 | 48 |
|
49 |
instance_level = 1 # root's grandchildren |
|
50 |
|
|
51 |
def obj(path): |
|
52 |
obj_path = copy.deepcopy(path[:instance_level+1]) |
|
53 |
obj_path[-1].is_ptr = False # prevent pointer w/o target |
|
54 |
return obj_path |
|
55 |
|
|
56 |
def set_id(path, id_, has_types=True): |
|
57 |
if has_types: id_level = instance_level |
|
58 |
else: id_level = 0 # root's children |
|
59 |
if path[0].name == '.': id_level += 1 # explicit root element |
|
60 |
path[id_level].keys.append([XpathElem('id', id_, True)]) |
|
61 |
|
|
62 |
def is_id(path): return path[0].is_attr and path[0].name == 'id' |
|
63 |
|
|
64 |
def is_instance(elem): return elem.keys != [] and is_id(elem.keys[0]) |
|
65 |
|
|
66 |
##### Parsing |
|
67 |
|
|
45 | 68 |
def expand_abbr(name, repl): |
46 | 69 |
before, abbr, after = name.partition('*') |
47 | 70 |
if abbr != '': name = before+repl+after |
... | ... | |
131 | 154 |
_cache[str_] = path |
132 | 155 |
return path |
133 | 156 |
|
134 |
instance_level = 1 # root's grandchildren
|
|
157 |
##### Querying/creating XML trees
|
|
135 | 158 |
|
136 |
def obj(path): |
|
137 |
obj_path = copy.deepcopy(path[:instance_level+1]) |
|
138 |
obj_path[-1].is_ptr = False # prevent pointer w/o target |
|
139 |
return obj_path |
|
140 |
|
|
141 |
def set_id(path, id_, has_types=True): |
|
142 |
if has_types: id_level = instance_level |
|
143 |
else: id_level = 0 # root's children |
|
144 |
if path[0].name == '.': id_level += 1 # explicit root element |
|
145 |
path[id_level].keys.append([XpathElem('id', id_, True)]) |
|
146 |
|
|
147 |
def is_id(path): return path[0].is_attr and path[0].name == 'id' |
|
148 |
|
|
149 |
def is_instance(elem): return elem.keys != [] and is_id(elem.keys[0]) |
|
150 |
|
|
151 | 159 |
def get(root, xpath, create=False, last_only=None, limit=1): |
152 | 160 |
'''Warning: The last_only optimization may put data that should be together |
153 | 161 |
into separate nodes''' |
Also available in: Unified diff
xpath.py: Added documentation labels to each section