forked from fyngyrz/aa_macro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aagen
executable file
·212 lines (192 loc) · 4.51 KB
/
aagen
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/python
import sys
import os
from aa_macro import *
report = False
suppress = False
nocolor = False
genoutput = True
try:
from aa_ansiicolor import *
except:
ac = None
else:
ac = htmlAnsii()
def lx(s,color):
global ac
global nocolor
if ac != None:
if nocolor == False:
s = ac.c(s,color)
return s
argv = sys.argv[1:]
def optr(oa,ob,p,c):
flgcolor = 'purple' # flag color
optcolor = 'white' # option color
parcolor = 'green' # parenthesis color
prmcolor = 'yellow' # parameter color
barcolor = 'white' # 'or' bar color
comcolor = 'green' # comment color
s = ''
s += 4 * ' '
pm = p
if pm != '': pm = ' ' + pm
if p != '': p = ' ' + lx(p,prmcolor)
l = len('%s %s%s%s%s%s' % ('(','-'+oa,'|','--'+ob,pm,')'))
pad = (28 - l) * ' '
x = '%s %s%s%s%s%s%s' % (lx('(',parcolor),lx('-'+oa,flgcolor),lx('|',barcolor),lx('--'+ob,flgcolor),p,lx(')',parcolor),pad)
s += '%s %s' % (x,lx(c,comcolor))
print s
def usage():
print "Usage"
print "-------------------------------------------------------------------------------"
print "aagen inputFileName"
optr('f','file','outputFileName','Write output to file')
optr('r','report','','Report input and output text lengths')
optr('s','suppress','','Suppress stdout usage printout when error')
optr('nc','ncolor','','Don\'t use console color (color requires aa_ansiicolor)')
optr('ng','nogen','','Don\'t generate output')
optr('o','open','','Send output to OS X HTML browser')
optr('i','include','includeFileName','Process this file first')
print "-------------------------------------------------------------------------------"
def error(msg):
global c
global suppress
if suppress != True:
usage()
sys.stderr.write(lx('ERROR: ','red') + lx(msg,'yellow')+'\n')
raise SystemExit
def idarg(arg,match):
alen = len(match)
if arg[:alen] == match:
return True
return False
ofn = ''
ifn = ''
mfn = ''
off = False
mff = False
mmff = False
gfn = False
osx = False
noparms = True
for arg in argv:
if off == True:
ofn = arg
off = False
if arg != '':
gfn = True
else:
error('No filename specified after "--file or -f"')
elif mff == True:
mfn = arg
mff = False
if arg != '':
mmff = True
else:
error('No filename specified after "--include or -i"')
elif idarg(arg,'--file') == True or idarg(arg,'-f') == True:
off = True
noparms = False
elif idarg(arg,'--open') == True or idarg(arg,'-o') == True:
osx = True
noparms = False
elif idarg(arg,'--include') == True or idarg(arg,'-i') == True:
mff = True
noparms = False
elif idarg(arg,'--report') == True or idarg(arg,'-r') == True:
report = True
noparms = False
elif idarg(arg,'--suppress') == True or idarg(arg,'-s') == True:
suppress = True
noparms = False
elif idarg(arg,'--nogen') == True or idarg(arg,'-ng') == True:
genoutput = False
noparms = False
elif idarg(arg,'--nocolor') == True or idarg(arg,'-nc') == True:
nocolor = True
else:
noparms = False
ifn = arg
if ifn == '':
error('No inputFileName supplied')
if noparms == True:
usage()
raise SystemExit
if ifn == '':
error('inputFileName not supplied')
try:
ifh = open(ifn)
except:
error('can\'t open inputFileName "%s" for reading' % (ifn,))
try:
inputText = ifh.read()
except:
try:
ifh.close()
except:
pass
error('Unable to read inputFileName "%s"' % (ifn,))
if gfn == True:
try:
fh = open(ofn,'w')
except:
try:
ifh.close()
except:
pass
error('Cannot open outputFileName "%s" for writing' % (ofn,))
icontent = ''
if mmff == True:
try:
mfh = open(mfn)
except:
error('Cannot open includeFileName "%s" for reading' % (mfn,))
else:
try:
icontent = mfh.read()
except:
try:
mfh.close()
except:
pass
error('Cannot read includeFileName "%s"' % (mfn,))
else:
try:
mfh.close()
except:
error('Could not close includeFileName "%s"' % (mfn,))
il = len(inputText)
if icontent != '':
mod = macro()
mod.do(icontent)
o = mod.do(inputText)
else:
o = str(macro(inputText))
ol = len(o)
if gfn == True:
try:
fh.write(o)
except:
error('Could not write outputFileName "%s"' % (ofn,))
try:
fh.close()
except:
pass
try:
fh.close()
except:
error('Could not close outputFileName "%s"' % (ofn,))
else:
try:
if genoutput == True:
sys.stdout.write(o)
except:
error('Could not write to stdout')
if osx == True:
if gfn == True:
cmd = 'open "%s"' % (ofn)
os.system(cmd)
if report == True:
print lx(' Read ','green') + lx('%d' % (il,),'white')+lx(' characters','green')
print lx('Generated ','green') + lx('%d' % (ol,),'white')+lx(' characters','green')