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
    # Process subpaths
14
    path[1:] = process(path[1:])
15
    branches = path[0].other_branches
16
    for i, branch in enumerate(branches): branches[i] = process(branch)
17
    
18
    name = path[0].name
19
    if name.startswith('_') and name in funcs:
20
        return funcs[name](path.pop(0).attrs, path)
21
    else: return path
22

    
23
#### XPath functions
24

    
25
# Function names must start with _ to avoid collisions with real tags
26
# Functions take arguments (args, path)
27

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