Project

General

Profile

« Previous | Next » 

Revision 72

Fixed ch_map_root to support subpaths which follow the root by -> rather than /. Changed spreadsheet syntax to have : between label and root.

View differences:

ch_map_root
3 3

  
4 4
import csv
5 5
import os.path
6
import re
6 7
import sys
7 8

  
8 9
sys.path.append(os.path.dirname(__file__)+"/../lib")
......
27 28
    cols = reader.next()
28 29
    for i in xrange(len(configs)):
29 30
        config = configs[i]
30
        config['root'] = cols[i]
31
        label = cols[i].partition('/')[0]
32
        config['prefix'] = label+config['in_root']
33
        config['prefix_len'] = len(config['prefix'])
34
        cols[i] = label+config['out_root']
31
        label, sep, config['root'] = cols[i].partition(':')
32
        cols[i] = label+sep+config['out_root']
33
        config['in_root_re'] = r'^'+re.escape(config['in_root'])+r'\b'
35 34
    writer.writerow(cols)
36 35
    for row in reader:
37 36
        for i in xrange(len(configs)): row[i] = configs[i]['root']+row[i]
38
        if row[0].startswith(configs[0]['prefix']):
39
            if not row[1].startswith(configs[1]['prefix']):
40
                raise SystemExit('Map error: Root "'+configs[1]['prefix']
41
                    +'" is not contained in output mapping: '+row[1])
42
            for i in xrange(len(configs)):
43
                row[i] = row[i][configs[i]['prefix_len']:]
37
        def sub(i):
38
            row[i], n = re.subn(configs[i]['in_root_re'], r'', row[i])
39
            return n > 0
40
        if sub(0):
41
            if not sub(1): raise SystemExit('Map error: Root "'
42
                +configs[1]['in_root']+'" is not contained in output mapping: '
43
                +row[1])
44 44
            writer.writerow(row)
45 45

  
46 46
main()

Also available in: Unified diff