Project

General

Profile

1
# XPath "function" elements that transform their subpaths
2

    
3
import util
4
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
    if name.startswith('_') and name in funcs:
16
        return funcs[name](path.pop(0).attrs, path)
17
    else: return path
18

    
19
#### XPath functions
20

    
21
# Function names must start with _ to avoid collisions with real tags
22
# Functions take arguments (args, path)
23

    
24
def _forEach(args, path):
25
    '''Replaces '_val' in `do` with each item in the `in` list'''
26
    in_, do = [a[0] for a in args]
27
    assert in_.name == 'in'
28
    assert do.name == 'do'
29
    in_ = in_.attrs
30
    do = do.value
31
    
32
    for_path = []
33
    for with_, in in_: for_path += xpath.parse(do.replace('_val', with_.name))
34
    return for_path + path
35
funcs['_forEach'] = _forEach
(19-19/19)