-
Notifications
You must be signed in to change notification settings - Fork 0
/
psprint.py
220 lines (194 loc) · 6.35 KB
/
psprint.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
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
213
214
215
216
217
218
219
220
#!/usr/bin/python3
import sys
def parse(s, fontsize=10, fontname='Courier', fontform=''):
def parsearg(match=' ,\t\r\n'):
nonlocal s, k
stck = [match]
while s[k] in ' \t\r\n':
k += 1
x = ''
while len(stck) > 0:
if stck[-1] == "'":
if s[k] == "'":
if s[k+1] == "'":
x += "'"
k += 1
else:
stck.pop()
elif s[k] in ")]}":
if len(stck) == 0:
print("Error at {}: Found unmatched {}.".format(k, s[k]))
print(stck)
print(x)
raise SystemExit
elif s[k] in stck[-1]:
stck.pop()
else:
print("Error at {}: Found {}, but expected {}.".format(k, s[k], stck[-1]))
print(stck)
print(x)
raise SystemExit
elif s[k] == '(':
stck += [')']
elif s[k] == '[':
stck += [']']
elif s[k] == '{':
stck += ['}']
elif s[k] == "'":
stck += ["'"]
elif s[k] in stck[-1]:
stck.pop()
x += s[k]
k += 1
return x[0:-1].strip()
s = s.strip() + '\n:'
k = 0
out = ''
form = fontform
while k < len(s) - 2:
if s[k:k+6].upper() == 'PRINT ':
w = '-1'
k += 6
str = parsearg()
while s[k] != '(':
k += 1
k += 1
y = parsearg(',)')
if y == '':
y == '+0'
if s[k-1] == ')':
x = '+0'
else:
x = parsearg(',)')
if s[k-1] == ')':
w = '-1'
else:
w = parsearg(')')
while s[k] in ' \t\r\n':
k += 1
if s.startswith('edit', k):
k += 4
str = "to_char({}, '{}')".format(str, parsearg(' '))
if w != '-1':
if str.startswith("'") and str != "''":
str = "rpad({}, {})".format(str, w)
else:
str = "rpad(nvl({}, ' '), {})".format(str, w)
if s.startswith('bold', k):
if form != 'Bold':
form = 'Bold'
out += "\npz_ps.setfont(file, {}, '{}', 'Bold');\n".format(fontsize, fontname)
k += 4
elif s.startswith('italic', k):
if form != 'Italic':
form = 'Italic'
out += "\npz_ps.setfont(file, {}, '{}', 'Italic');\n".format(fontsize, fontname)
k += 6
elif s.startswith('BoldItalic', k):
if form != 'BoldItalic':
form = 'BoldItalic'
out += "\npz_ps.setfont(file, {}, '{}', 'BoldItalic');\n".format(fontsize, fontname)
k += 10
elif form != fontform:
form = fontform
out += "\npz_ps.setfont(file, {}, '{}', '{}');\n".format(fontsize, fontname, form)
while s[k] in ' \t\r\n':
k += 1
if s.startswith('center', k):
k += 6
out += "\npz_ps.text_c(file, {}, {});\n".format(y, str)
else:
out += "\npz_ps.text(file, {}, {}, {});\n".format(y, x, str)
elif s[k:k+8].upper() == 'GRAPHIC ':
# graphic (x, y, z) cmd arg arg
# graphic (5,1,106) box 2 20 ==> pz_ps.box(5, 1, 2, 106, 20);
# graphic (4,92,2) vert-line 20 ==> pz_ps.vertline(4, 92, 2, 20);
# graphic (39,1,106) horz-line 5 ==> pz_ps.horizline(39, 1, 106, 5);
k += 8
while s[k] != '(':
k += 1
k += 1
y = parsearg()
x = parsearg()
z = parsearg(')')
while s[k] in ' \t\r\n':
k += 1
if s.startswith('box', k):
k += 4
arg1 = parsearg()
arg2 = parsearg()
out += '\npz_ps.box(file, {}, {}, {}, {}, {});\n'.format(y, x, arg1, z, arg2)
elif s.startswith('vert-line', k):
k += 10
arg1 = parsearg()
out += '\npz_ps.vertline(file, {}, {}, {}, {});\n'.format(y, x, z, arg1)
elif s.startswith('horz-line', k):
k += 10
arg1 = parsearg()
out += '\npz_ps.horizline(file, {}, {}, {}, {});\n'.format(y, x, z, arg1)
else:
out += s[k]
k += 1
if form != fontform:
form = fontform
out += "\npz_ps.setfont(file, {}, '{}', '{}');\n".format(fontsize, fontname, form)
while '\n\n' in out:
out = out.replace('\n\n', '\n')
return out.strip()
help = """
./psprint.py [flags] infile [outfile]
flags
-o followed by the output file (same as using outfile).
-size followed by font size, defaults to 10
-font followed by font name, defaults to Courier
-form followed by font form (e.g. 'Bold', 'Roman', etc.), defaults to ''
if outfile and -o are omitted, this will print to the terminal.
"""
infile = ''
outfile = ''
fontsize = '10'
fontname = 'Courier'
fontform = ''
k = 1
while k < len(sys.argv):
opt = sys.argv[k]
if opt == '-o':
outfile = sys.argv[k + 1]
k += 1
elif opt == '-font':
fontname = sys.argv[k + 1]
k += 1
elif opt == '-form':
fontform = sys.argv[k + 1]
k += 1
elif opt == '-size':
fontsize = sys.argv[k + 1]
k += 1
elif infile == '':
infile = opt
elif outfile == '':
outfile = opt
else:
print(help)
raise SystemExit
k += 1
if infile == '':
try:
import pyperclip
s = parse(pyperclip.paste(), fontsize, fontname, fontform)
pyperclip.copy(s)
print(s)
except:
print(help)
raise SystemExit
else:
f = open(infile, 'r')
s = f.read()
f.close()
s = parse(s, fontsize, fontname, fontform)
if outfile != '':
f = open(outfile, 'w')
f.write(s)
f.close()
else:
print(s)