1 |
1341
|
aaronmk
|
# XPath "function" elements that transform their subpaths
|
2 |
|
|
|
3 |
1346
|
aaronmk
|
import util
|
4 |
1341
|
aaronmk
|
import xpath
|
5 |
|
|
|
6 |
|
|
##### Functions
|
7 |
|
|
|
8 |
|
|
funcs = {}
|
9 |
|
|
|
10 |
|
|
def process(path):
|
11 |
|
|
if path == []: return path
|
12 |
|
|
|
13 |
|
|
path[1:] = process(path[1:])
|
14 |
|
|
name = path[0].name
|
15 |
1343
|
aaronmk
|
if name.startswith('_') and name in funcs:
|
16 |
|
|
return funcs[name](path.pop(0).attrs, path)
|
17 |
1341
|
aaronmk
|
else: return path
|
18 |
|
|
|
19 |
|
|
#### XPath functions
|
20 |
|
|
|
21 |
|
|
# Function names must start with _ to avoid collisions with real tags
|
22 |
1343
|
aaronmk
|
# Functions take arguments (args, path)
|
23 |
1341
|
aaronmk
|
|
24 |
1346
|
aaronmk
|
def _forEach(args, path):
|
25 |
1347
|
aaronmk
|
'''Replaces '_val' in `do` with each item in the `in_` list'''
|
26 |
1343
|
aaronmk
|
in_, do = args
|
27 |
|
|
list_ = [in_[1:]] + in_[0].other_branches
|
28 |
1347
|
aaronmk
|
do = do[0].name
|
29 |
1343
|
aaronmk
|
|
30 |
|
|
for_path = []
|
31 |
1347
|
aaronmk
|
for with_, in list_: for_path += xpath.parse(do.replace('_val', with_.name))
|
32 |
1343
|
aaronmk
|
return for_path + path
|
33 |
1346
|
aaronmk
|
funcs['_forEach'] = _forEach
|