-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint2.py
199 lines (186 loc) · 5.82 KB
/
print2.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
#!/usr/bin/python3
import sys
linevar = 'line'
printline = "utl_file.put_line(my_file, {});\n"
def isnumform(s):
for i in s:
if i not in "'.0123456789,":
return False
return True
def isstr(s):
if not s.startswith("'") or not s.startswith("'"):
return False
return "'" in s.replace("''", "")
def printparse(s):
# replace tabs as spaces
s = s.replace('\t',' ')
t = ''
level = ['']
k = 0
out = []
edit = ''
coords = []
s += ' '
while k<len(s)-1:
t = ''
if s[k] == "'":
t += s[k]
k += 1
while s[k]!="'" or s[k:k+2] == "''":
if s[k] == "'":
t += s[k]
k += 1
t += s[k]
k += 1
out += t + "'"
t = ''
k += 1
while not (s.startswith(' edit ', k) or s[k] in "('"):
t += s[k]
k += 1
out += t
t = ''
if s.startswith(' edit ', k):
k += 6
while s[k]==' ':
k += 1
edit = "'"
while k<len(s)-1 and s[k] != ' ':
edit += s[k]
k += 1
if s[k] != ' ':
edit += s[k]
k += 1
edit += "'"
elif s[k] == '(':
m = k
while s[k] != ')':
k += 1
t = s[m:k].split(',')
k += 1
t[0] = t[0][1:]
if len(t) == 3:
coords = t
elif len(t) == 2:
coords = t + ['']
else:
coords = t + ['+0','']
#return edit, coords, and out
s = ''
for i in out:
s += i
coords[0] = coords[0].strip()
coords[1] = coords[1].strip()
coords[2] = coords[2].strip()
if edit.endswith("9.99'"):
edit = edit[0:-5]+"0.00'"
elif edit.endswith("9.9'"):
edit = edit[0:-4]+"0.0"
return (edit, coords, s.strip())
def printle(rows):
# l is a list of rows to write
s = ''
for row in rows:
row = sorted(row)
if len(row) == 0:
s += "{} := ''\n".format(linevar)
else:
first = True
for i in row:
n = 2
str = ''
while n<len(i):
if i[n+1] == '':
str += i[n]
elif 'x-' in i[n+1]:
str += "replace(to_char({}, 'FM{}), ',', '-')".format(i[n], i[n+1][1:].replace('x', '0').replace('-', ','))
elif isnumform(i[n+1]):
form = i[n+1][1:-1]
if form[0] == '0':
#if the format has a leading 0, then it will pad zeros, so no need to lpad
str += "to_char({}, 'FM{}')".format(i[n], form)
else:
# the format has no leading zero, so we manually pad it
str += "lpad(to_char({}, 'FM{}'), {})".format(i[n], form, len(form))
else:
str += "to_char({}, {})".format(i[n], i[n+1])
n += 2
if n<len(i):
str += ' || '
if first:
first = False
if i[0] > 1:
pre = "'" + ' '*(i[0] - 1) + "' || "
else:
pre = ''
if i[1] != '':
if isstr(str):
str = str[1:-1]
str += ' '*(int(i[1]) - len(str))
str = str[0:int(i[1])]
s += "{} := {}'{}';\n".format(linevar, pre, str)
else:
s += "{} := {}rpad({}, {});\n".format(linevar, pre, str, i[1])
else:
s += "{} := {}{};\n".format(linevar, pre, str)
else:
if i[1] != '':
s += "{} := f_overstr({}, {}, {}, width=>{});\n".format(linevar, linevar, str, i[0], i[1])
else:
s += "{} := f_overstr({}, {}, {});\n".format(linevar, linevar, str, i[0])
s += printline.format(linevar)
return s.replace("' || '", '')
def printify(s):
src = []
for i in s.split('\n'):
src += [i.strip()]
# describe the prints
rows = []
k = 0
s = ''
row = 0
while k<len(src):
if src[k].startswith('print '):
(edit, coords, str) = printparse(src[k][6:])
if coords[0][0] == '+':
for i in range(int(coords[0][1:])-1):
rows += [[]]
s += printle(rows)
rows = [[[int(coords[1]), coords[2], str, edit]]]
else:
if int(coords[0]) != 0:
row = 1 + int(coords[0])
while len(rows)<=row:
rows += [[]]
if coords[1][0].startswith('+'):
if int(coords[1][1:]) > 0:
rows[row][-1][0] += " || {}".format(' '*int(coords[1][1:]))
rows[row][-1] += [str, edit]
else:
rows[row] += [[int(coords[1]), coords[2], str, edit]]
else:
s += src[k] + '\n'
k += 1
s += printle(rows)
return s
if len(sys.argv) == 1:
try:
import pyperclip
pyperclip.copy(printify(pyperclip.paste()))
except:
pass
k = 1
while k<len(sys.argv):
fin = sys.argv[k]
f = open(fin, 'r')
s = f.read()
f.close()
s = printify(s)
if k+1 < len(sys.argv):
fout = sys.argv[k+1]
f = open(fout,'w')
f.write(s)
f.close()
else:
print(s)
k += 2