Browse code
Added the ability for symbcalc to work as a filter.
fiddlerwoaroof authored on 25/04/2015 02:40:16
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -3,6 +3,7 @@ from __future__ import division |
3 | 3 |
import argparse |
4 | 4 |
import collections |
5 | 5 |
import os |
6 |
+import sys |
|
6 | 7 |
|
7 | 8 |
def parse_line(line): |
8 | 9 |
line = line.split(':') |
... | ... |
@@ -14,38 +15,37 @@ def parse_line(line): |
14 | 15 |
expr = '' |
15 | 16 |
var = line.pop().strip() |
16 | 17 |
desc = ':'.join(line).strip() |
18 |
+ expr = expr.partition(' = ')[0] |
|
17 | 19 |
return expr,var,desc |
18 | 20 |
|
19 | 21 |
def eval_line(expr,variables): |
20 | 22 |
return eval(expr, {}, variables) |
21 | 23 |
|
22 |
-def run_template(t, values): |
|
23 |
- with open(t) as f: |
|
24 |
- variables = {} |
|
25 |
- for line in f: |
|
26 |
- line = line.strip() |
|
27 |
- if line == '': |
|
28 |
- print;continue |
|
29 |
- expr, var, desc = parse_line(line) |
|
30 |
- if expr == '': |
|
31 |
- if values != []: |
|
32 |
- expr = values.pop(0) |
|
33 |
- else: |
|
34 |
- expr = raw_input('%s? ' % desc) |
|
35 |
- res = variables[var] = eval_line(expr, variables) |
|
24 |
+def run_template(f, values): |
|
25 |
+ variables = {} |
|
26 |
+ for line in f: |
|
27 |
+ line = line.strip() |
|
28 |
+ if line == '': |
|
29 |
+ print;continue |
|
30 |
+ expr, var, desc = parse_line(line) |
|
31 |
+ if expr == '': |
|
32 |
+ if values != []: |
|
33 |
+ expr = values.pop(0) |
|
36 | 34 |
else: |
37 |
- res = variables[var] = eval_line(expr, variables) |
|
38 |
- print '%s: %s = %s' % (desc, var, res) |
|
35 |
+ expr = raw_input('%s? ' % desc) |
|
36 |
+ res = variables[var] = eval_line(expr, variables) |
|
37 |
+ else: |
|
38 |
+ res = variables[var] = eval_line(expr, variables) |
|
39 |
+ print '%s: %s: %s = %s' % (desc, var, expr, res) |
|
39 | 40 |
|
40 |
-def get_template_info(t, verbose=False): |
|
41 |
- with open(t) as f: |
|
42 |
- tmpl = f.read() |
|
43 |
- variables = ' '.join(var for expr,var,desc in map(parse_line, tmpl.split('\n')) if expr == '' and var != '') |
|
44 |
- print 'Template Usage: %s %s' % (os.path.basename(t).rpartition('.')[0], variables) |
|
45 |
- if verbose: |
|
46 |
|
|
47 |
- print 'Template Source:' |
|
48 |
- print tmpl |
|
41 |
+def get_template_info(f, verbose=False): |
|
42 |
+ tmpl = f.read() |
|
43 |
+ variables = ' '.join(var for expr,var,desc in map(parse_line, tmpl.split('\n')) if expr == '' and var != '') |
|
44 |
+ print 'Template Usage: %s %s' % (os.path.basename(t).rpartition('.')[0], variables) |
|
45 |
+ if verbose: |
|
46 |
|
|
47 |
+ print 'Template Source:' |
|
48 |
+ print tmpl |
|
49 | 49 |
|
50 | 50 |
|
51 | 51 |
|
... | ... |
@@ -59,24 +59,28 @@ def main(): |
59 | 59 |
argparser.add_argument('-v', action='store_true') |
60 | 60 |
args = argparser.parse_args() |
61 | 61 |
template = None |
62 |
- if args.template: |
|
63 |
- template = '%s.tmpl' % os.path.join(args.td, args.template) |
|
62 |
+ if len(sys.argv) > 1: |
|
63 |
+ if args.template: |
|
64 |
+ template = '%s.tmpl' % os.path.join(args.td, args.template) |
|
64 | 65 |
|
65 |
- if args.i: |
|
66 |
- if template: |
|
67 |
- get_template_info(template, args.v) |
|
66 |
+ if args.i: |
|
67 |
+ if template: |
|
68 |
+ with open(template) as file_: |
|
69 |
+ get_template_info(file_, args.v) |
|
70 |
+ else: |
|
71 |
+ print 'You must supply a template to get info about' |
|
72 |
+ elif args.template: |
|
73 |
+ try: |
|
74 |
+ with open(template) as file_: |
|
75 |
+ run_template(file_, args.values) |
|
76 |
+ except (KeyboardInterrupt, EOFError): pass |
|
68 | 77 |
else: |
69 |
- print 'You must supply a template to get info about' |
|
70 |
- elif args.template: |
|
71 |
- try: |
|
72 |
- run_template(template, args.values) |
|
73 |
- except (KeyboardInterrupt, EOFError): pass |
|
74 |
- else: |
|
75 |
- print 'Available Templates:' |
|
76 |
- print '---' |
|
77 |
- for x in os.listdir(args.td): |
|
78 |
- if not x.endswith('.tmpl'): continue |
|
79 |
- print x.rpartition('.')[0] |
|
78 |
+ print 'Available Templates:' |
|
79 |
+ print '---' |
|
80 |
+ for x in os.listdir(args.td): |
|
81 |
+ if not x.endswith('.tmpl'): continue |
|
82 |
+ print x.rpartition('.')[0] |
|
83 |
+ else: run_template(sys.stdin, []) |
|
80 | 84 |
|
81 | 85 |
|
82 | 86 |
if __name__ == '__main__': |