-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowrawtape.py
312 lines (277 loc) · 7.44 KB
/
showrawtape.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
#~ punchconverter
#~ to convert plain ascii to the punchcodes available on several
#~ non-ascii machines using punchtape. Tape can be 5,6,7 or 8 bit,
#~ but that translation is not done here (could be build in though)
#- call script like this:
#- $ python converter1_0.py -f fb -i textfile.txt
#- The filename is first printed as ascii-art followed by the code format
#- so the command above will translate into:
#- textfile.txt code: fb >| blabla
#- so it is clear for what machine the punchtape is
#---------- preparation --------------------
#~ export the .odf file to csv with the following settings:
#~ -carrakter set: western europe (ascii)
#~ -field delimiter= ,
#~ -text delimiter="
#~ -save cell contents as shown
#~ -quote all text cells
#~ ----------- TODO ---------------
#~ -handle crlf in ita2
#~ -cleanup & documentation
#~ ----------- DONE ---------------
#~ -flexo spd
#~ -ita2
#~ -header and footer
#~ -plaintext code
#~ -no conversion for punch
__author__ = 'macsimski'
import argparse
import csv
import serial
import array
import sys
from time import sleep
#- use stio as display to save punch paper. ascii-art representation of the tape
def displaytape(ch):
global i
print '-',
for n in range(8):
if n==3:
print '*',
if ((ch>>n)&1):
print 'O',
else:
print '.',
print '|',
print i
#~ print (ch>>2)&1
def punch(ch):
sleep(0.05)
port.write (chr(ch))
def punchtape(h): #output function accepts dec ascii number
# print h.encode("hex")
if args.verbose:
displaytape(h) # to screen
if not args.punch:
punch(h) # to paper # not done yet
#- ----------------- lookup code and set shiftstate -------------------
def translate(pri): # can be string of length 1
global oldcase
global uppercase
global i
for i in pri:
try:
p = lowerdict[i]
uppercase = False
except KeyError:
try:
p= upperdict[i]
uppercase = True
except:
t=i
if ord(i) == 10: # dicts don't like cr
i='cr'
punchtape(lowerdict['@@ret'])
break
elif ord(i) == 13:
i='lf'
punchtape(lowerdict['@@lfd'])
break
else:
i='???'
punchtape(lowerdict[' '])
break
i=t
if (uppercase != oldcase):
t=i # temp buffer for actual char
if uppercase:
i='Uc'
punchtape(lowerdict['@@ucs']) # print 'uppercase '
else:
i='Lc'
punchtape(lowerdict['@@lcs']) # print 'lowercase'
oldcase = uppercase
i=t # restore actual char
punchtape(p) # p is ascii number
#- -------------- Punch human readable punch charracters -------------
def plainpunch(pri):
global i
for i in pri:
try:
p = plaindict[i]
for d in range(0,len(p),2):
# print p[d:d+2]
# punchtape()
punchtape(int(p[d:d+2],16)>>8-bytedict[args.format])
except:
print 'plain???'
def punchheader(): # punch asciiart filename at beginning
global i
room = ' '
plainpunch(room)
for b in args.input: plainpunch(b)
plainpunch (' ')
plainpunch(' code ')
plainpunch(args.format)
plainpunch(' >>|')
i='nop'
for n in range(1,6):
punchtape(lowerdict['@@nop'])
def punchfooter():
global i
# room = ' '
# plainpunch(room)
i='nop'
for n in range(1,10):
punchtape(lowerdict['@@nop'])
# ------------------ main code ----------------
# cli arguments handling
class Writer():
def __init__(name, table):
self._name = name
self._table = table
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Universal punchcode translator.',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-i','--input', help='text input filename',required=True)
parser.add_argument('-f','--format', help="""fb=flexowriter Bull
fp=flexo president
tsa=typesetter 6bit version a
tsb=typesetter version b
tx=ita2 telex
ba=textbanner
pt=no conversion
""",default='pt')
parser.add_argument('-p','--port', help='serial port',default='/dev/ttyUSB0') # change for macs and or windows
parser.add_argument('-v','--verbose', help='y or n display on screen default n', action='store_true')
parser.add_argument('-n','--punch', help='do not open a serial port to punch', action='store_true')
args = parser.parse_args()
codedict = { 'fb':'friden_spd.csv',
'fp':'friden_pres.csv',
'tsa':'teletypeset_a.csv',
'tsb':'teletypeset_b.csv',
'tx':'ita2.csv',
'pt':'ascii', # for ascii devices
'ba':'ascii' # to produce a banner without header
}
bytedict = { 'fb':8,
'fp':8,
'tsa':6,
'tsb':6,
'tx':5,
'ba':8,
'pt':8
}
#- ---------- declaration of several vars
global uppercase
global oldcase
uppercase = False
oldcase = False
plaintextfile="plaintext.csv"
#~ --------------------- open serial port -------------------
if not args.punch:
try:
port = serial.Serial(
port=args.port,
baudrate=600,
# timeout=1,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
)
print 'using: ', port.name
except:
print args.port, " not available, sorry"
sys.exit(1)
#~ ---------------- codefile ----------------------
try:
codefile = codedict[args.format]
print' '
print 'codebase: ',codefile
except 'KeyError':
print 'no known translation for that format found'
sys.exit(1)
#~ ----------------- plaintext dict ------------------
plaindict={}
try:
infile = open(plaintextfile, mode='r')
reader = csv.reader(infile)
for rows in reader:
if rows[3]:
plaindict[rows[2]]=rows[3]
print("Input file: %s" % args.input )
except "FileNotFoundError":
print plaintextfile, " is not found"
#~ ------------------ conversion dict --------------------
upperdict ={}
lowerdict={}
if (not args.format=='pt') & (not args.format=='ba'):
try:
infile = open(codefile,mode='r')
reader = csv.reader(infile)
for r in reader:
if r[3]:
upperdict[r[4]] = int(r[0])
lowerdict[r[3]] = int(r[0])
except "FileNotFoundError":
print codefile, " cannot be found. sorry"
sys.exit(1)
else:
print 'no conversion'
# ----------------- start punching ----------------
global i
if (not args.format=='pt') & (not args.format=='ba'):
punchheader() # punch plaintext filename and codeformat on begin of tape
with open(args.input) as f:
global i
while True:
c= f.read(1)
if not c:
# print "EOF"
break
if c == '@': # escape char?
try: c=f.read(1)
except:
print "[x] done"
break
if c == '@': # ! second @ can be the beginning of a escape sequence
try: c=f.read(3)
except:
print "kloar" # ----- needs real code ---------
break
try:
t= lowerdict['@@' + c ] # lookup @@xyz as one thing
i=c
punchtape(t)
except 'KeyError':
translate("@@") # no escape seq. print all
translate(c)
else: translate("@"+ c) # print first '@' with the following 1
else: # normal char
translate(c)
#~ ----------------------- no conversion ----------------
elif args.format=='pt':
punchheader() # punch plaintext filename and codeformat on begin of tape
with open(args.input) as f:
for line in f:
for i in line:
c=i
if i==chr(13):
i='cr'
elif i==chr(10):
i='lf'
punchtape(ord(c))
#~ ------------------------------ textbanner --------------------------
elif args.format=='ba':
punchfooter()
with open(args.input) as f:
for line in f:
for c in line:
if c==chr(13):
c=' '
elif c==chr(10):
c=' '
plainpunch((c))
punchfooter()
print 'Done.'