This repository was archived by the owner on Mar 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (38 loc) · 1.22 KB
/
main.py
File metadata and controls
47 lines (38 loc) · 1.22 KB
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
"""Properties template processor.
Usage:
main.py load <template> <mapping> [<output>]
main.py --version
Options:
-h --help Show this screen.
--version Show version.
"""
import json
import sys
from string import Template
import javaproperties
from docopt import docopt
def substitute(props, mapping, template = lambda st, m: Template(st).substitute(m)):
"""
Returns a substituted dictionary, where
all the values in props have been substituted
with values in mapping.
Template is an optional function that
takes a template string and mapping and
translates it into another string.
"""
return { k: template(v, mapping) for k, v in props.items() }
if __name__ == "__main__":
arguments = docopt(__doc__, version="0.1")
try:
props = javaproperties.load(open(arguments['<template>'], 'r'))
mapping = json.load(open(arguments['<mapping>'], 'r'))
except Exception as e:
print(e)
exit()
substitution = substitute(props, mapping)
output = arguments['<output>']
if output is not None:
with open(output, 'w') as f:
f.write(javaproperties.dumps(substitution))
else:
print(javaproperties.dumps(substitution))