-
Notifications
You must be signed in to change notification settings - Fork 3
/
local_opt.py
70 lines (60 loc) · 1.74 KB
/
local_opt.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
import sys
import re
if sys.argv[1] == "-":
file = sys.stdin
else:
file = open( sys.argv[1], 'r' )
int_re = re.compile("^[0-9]")
var_map = {}
party_mode = 0
could_be_local = {}
local_lines = { 1 : "", 2: "" }
neutral = {}
linenum = 0
for line in file.xreadlines():
args = line.split()
linenum += 1
if len( args ) == 0:
continue
if args[0] == ".input":
var_map[ args[1] ] = int( args[2] )
elif args[0] == ".remove":
del var_map[ args[1] ]
elif args[0][0] == ".":
continue
else:
party_arg = []
for arg in args[2:]:
arg_party = 0
if re.match(int_re, arg) != None:
arg_party = 0
elif not arg in var_map:
raise Exception("cannot identify "+arg+" on line "+str(linenum) )
elif var_map[arg] == 0:
neutral[arg] = line
party_arg.append( arg_party )
if party_arg > 0 and party_arg != party_mode:
local_lines[party_arg] += line
could_be_local[ args[0] ] = party_arg
var_map[ args[0] ] = party_arg
in_party = False
printed_locals = False
file.seek(0)
for line in file.xreadlines():
args = line.split()
if args[0] == ".startparty":
in_party = True
elif args[0] == ".endparty":
in_party = False
elif args[0] == ".remove" and args[1] in could_be_local:
continue
elif not printed_locals and not in_party and args[0][0] != ".":
print ".startparty 1"
print local_lines[1],
print ".endparty 1"
print ".startparty 2"
print local_lines[2],
print ".endparty 2"
printed_locals = True
if not args[0] in could_be_local:
print line.strip()