-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremap.py
81 lines (73 loc) · 2.48 KB
/
remap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
'''
Author: Cristian Andrei Calin <cristi.calin@orange.com>
usage: remap.py [-h] -i INPUT -m MAP [-o {json,yaml}]
Remap values inside a structured file.
optional arguments:
-h, --help show this help message and exit
-i INPUT, --input INPUT
Input file
-m MAP, --map MAP Remapping file
-o {json,yaml}, --output {json,yaml}
Output format
'''
import re
import json
import yaml
import argparse
import json_tools
'''
Parse a file and deduce the format based on the file extension
Input:
f - file descriptor of the file to be parsed
Output:
the map resulted from loading the file
'''
def parse(f):
p = f.name.split(".")
exec "result = %s.load(f)" % p[1]
return result
'''
Remap specific values before doing a diff, this is to prevent useless output
Input:
obj - the map to parse
mapping - the mapping to apply
Output:
the remapped object
'''
def do_remapping(obj, mapping):
for mapping_node in mapping["paths"]:
path = json_tools.path.resolve(obj, mapping_node["path"])
for path_node in path:
for field in mapping_node["fields"]:
for remap in mapping_node["map"]:
new = re.sub(r"\b%s\b" % (remap), mapping_node["map"][remap], path_node[field])
if new != path_node[field]:
path_node[field] = new
if mapping_node["sort"]:
path.sort(key=lambda node: node[mapping_node["sort_key"]])
return obj
'''
This program does a remapping of values in structured files (json, yaml).
- loads the file
- loads a remapping file
- applies the remapping
- prints the remapped object to standard output in the requested format
'''
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Remap values inside a structured file.')
parser.add_argument('-i', '--input', required=True, type=argparse.FileType('r'), help='Input file')
parser.add_argument('-m', '--map', required=True, type=argparse.FileType('r'), help='Remapping file')
parser.add_argument('-o', '--output', required=False, choices=['json', 'yaml'], help='Output format')
args = parser.parse_args()
infile = parse(args.input)
if args.map is not None:
mapping = parse(args.map)
outfile = do_remapping(infile, mapping)
output_format = args.input.name.split(".")[-1]
if args.output is not None:
output_format = args.output
if output_format == 'yaml':
print yaml.dump(outfile, indent=2, default_flow_style=False)
else:
print json.dumps(outfile, indent=2, ensure_ascii=True)