1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# eval_line.py |
4
|
|
|
# |
5
|
|
|
# Copyright (C) 2014-2020, Takazumi Shirayanagi |
6
|
|
|
# This software is released under the new BSD License, |
7
|
|
|
# see LICENSE |
8
|
|
|
# |
9
|
|
|
|
10
|
|
|
import re |
11
|
|
|
import codecs |
12
|
|
|
|
13
|
|
|
from argparse import ArgumentParser |
14
|
|
|
from argparse import Action |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
# EvalIntAction |
18
|
|
|
class EvalIntAction(Action): |
19
|
|
|
def __call__(self, parser, namespace, values, options_string=None): |
20
|
|
|
setattr(namespace, self.dest, int(eval(values[0]))) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
# command line option |
24
|
|
View Code Duplication |
def parse_command_line(): |
|
|
|
|
25
|
|
|
parser = ArgumentParser() |
26
|
|
|
parser.add_argument( |
27
|
|
|
'-v', |
28
|
|
|
'--version', |
29
|
|
|
action='version', |
30
|
|
|
version=u'%(prog)s version 0.2' |
31
|
|
|
) |
32
|
|
|
parser.add_argument( |
33
|
|
|
'-r', |
34
|
|
|
'--repeat', |
35
|
|
|
action=EvalIntAction, |
36
|
|
|
nargs=1, |
37
|
|
|
default=1, |
38
|
|
|
help='Set eval repeat count.' |
39
|
|
|
) |
40
|
|
|
parser.add_argument( |
41
|
|
|
'-s', |
42
|
|
|
'--start', |
43
|
|
|
action=EvalIntAction, |
44
|
|
|
nargs=1, |
45
|
|
|
default=0, |
46
|
|
|
help='Set eval repeat start no.' |
47
|
|
|
) |
48
|
|
|
parser.add_argument( |
49
|
|
|
'-o', |
50
|
|
|
'--output', |
51
|
|
|
default=None, |
52
|
|
|
help='output file path.' |
53
|
|
|
) |
54
|
|
|
parser.add_argument( |
55
|
|
|
'--encoding', |
56
|
|
|
default=None, |
57
|
|
|
help='output file encoding.' |
58
|
|
|
) |
59
|
|
|
parser.add_argument( |
60
|
|
|
'expression', |
61
|
|
|
metavar='EXP', |
62
|
|
|
nargs='+', |
63
|
|
|
help='eval expressions' |
64
|
|
|
) |
65
|
|
|
options = parser.parse_args() |
66
|
|
|
return options |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def eval_line(r, define, line): |
70
|
|
|
m = r.finditer(define) |
71
|
|
|
if m is None: |
72
|
|
|
return define |
73
|
|
|
else: |
74
|
|
|
out = define |
75
|
|
|
for e in m: |
76
|
|
|
out = out.replace(e.group(0), str(eval(e.group(1)))) |
77
|
|
|
return out |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
def main(): |
81
|
|
|
re_eval = re.compile(r'\${([^}]+)}') |
82
|
|
|
options = parse_command_line() |
83
|
|
|
define = options.expression[0] |
84
|
|
|
for a in options.expression[1:]: |
85
|
|
|
define += ' ' + a |
86
|
|
|
|
87
|
|
|
output = options.output |
88
|
|
|
if output is None: |
89
|
|
|
for line in range(options.repeat): |
90
|
|
|
print(eval_line(re_eval, define, line + options.start)) |
91
|
|
|
else: |
92
|
|
|
if options.encoding: |
93
|
|
|
file = codecs.open(output, 'w', options.encoding) |
94
|
|
|
else: |
95
|
|
|
file = open(output, 'w') |
96
|
|
|
for line in range(options.repeat): |
97
|
|
|
file.write(eval_line(re_eval, define, line + options.start)) |
98
|
|
|
file.write('\n') |
99
|
|
|
file.close() |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
if __name__ == '__main__': |
103
|
|
|
main() |
104
|
|
|
|