forked from fablab-wue/piTelex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txDevArchive.py
executable file
·317 lines (274 loc) · 10.6 KB
/
txDevArchive.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
313
314
315
316
317
#!/usr/bin/env python3
"""
Telex Device - Archive all printed messages
"""
__author__ = "Björn Schließmann"
__license__ = "GPL3"
import datetime
import os
import re
import logging
l = logging.getLogger("piTelex." + __name__)
import txBase
# ASCII shifts (called "direction shifts" from here on out) for tagging
# inbound/outbound text
INBOUND = "\x0e" # SI
OUTBOUND = "\x0f" # SO
# ANSI colour escape codes
ANSI_RED_FOREGROUND = "\x1b[31m"
ANSI_RESET = "\x1b[0m"
class TelexArchive(txBase.TelexBase):
def __init__(self, **params):
super().__init__()
self.id = 'Arc'
self.params = params
self._current_msg = []
# Internal states:
# - 0: idle / startup
# - 1: dialling
# - 2: recording message
self._state = 0
# Direction of characters (for text colour):
# - False: inbound
# - True: outbound
# - None: undefined
self._direction_out = None
# Number dialled (for discerning inbound/outbound connection)
self._dial_number = None
# Time when connection was made
self._timestamp = None
# The subdirectory to place archive files in is read from
# configuration. Relative paths are taken relative to where piTelex
# scripts are stored; absolute paths are just that.
self.arclog_path = params.get("path", "archive")
if not self.arclog_path:
self.arclog_path = "archive"
if not os.path.isabs(self.arclog_path):
self.arclog_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.arclog_path)
try:
os.mkdir(self.arclog_path)
except FileExistsError:
pass
l.info("Archiving in {!r}".format(self.arclog_path))
def __del__(self):
self.exit()
super().__del__()
def exit(self):
pass
def read(self) -> str:
return ''
def write(self, data:str, source:str):
"""
telex.py main loop writes all data passing through piTelex to us by
this method.
"""
if len(data) > 1:
# this is a command
if data == "\x1bWB":
# Dialling triggered
self._state = 1
elif data.startswith("\x1b#") and self._state == 1:
# Dial command: record. The number dialled last will remain
# (and should be the one that was successful)
self._dial_number = data[2:]
elif data == "\x1bA":
if self._state >= 2:
l.warning("Redundant printer start command detected (ignored)")
return
self._state = 2
l.info("recording new message") # TODO debug
self._timestamp = datetime.datetime.now()
elif data == "\x1bZ":
if self._state <= 0:
l.warning("Redundant printer stop command detected (ignored)")
return
elif self._state >= 2:
self.save_msg()
self._direction_out = None
self._state = 0
else:
# this is data
if self._state < 1:
# printer not running -- don't save data
return
# Data direction: Everything from </> (i-Telex server/client
# module) is inbound, rest outbound
direction_out = source not in ['iTc', 'iTs']
if not direction_out == self._direction_out:
# Direction changed, need to insert direction shift
self._current_msg.append(OUTBOUND if direction_out else INBOUND)
self._direction_out = direction_out
# Filter data
# TODO others?
data = data.lower()
self._current_msg.append(data)
def filename(self, wru="[unknown]", direction="with", timestamp=None) -> str:
"""
Return filename for an archive file.
Optionally:
- incorporate WRU string (use generic name if not given)
- also a direction for naming (from/to/with)
- use a struct_time for the timestamp (else "now")
"""
fn = {}
if not timestamp:
timestamp = datetime.datetime.now()
fn["timestamp"] = timestamp.strftime("%Y-%m-%d %H.%M.%S.%f")[:-3]
fn["title"] = "msg {} {}".format(direction, wru)
return os.path.join(self.arclog_path, "{timestamp} {title}.txt".format(**fn))
@classmethod
def find_WRU_answer(cls, data, inbound=False) -> str:
"""
Extract the first WRU answerback from data and return it (stripped from
line breaks and special characters).
WRU answerbacks are specified in ITU-T S.6 as follows:
- 20 characters long
- following sequence:
1. letter or figure shift
2. CR
3. LF
4.-19. 16 signals
20. letter shift (optional)
We identify the answerback code like so (proudly supported by re
module):
- Filter out all colour ESC sequences, shifts and CRs
- Trigger on first WRU character (@ or #, see below).
- After this, allow for up to 4 characters until one or more newlines.
- After this, the match group begins -- record 5..30 characters until
the next newline. (In theory, only 17; allow for longer answerbacks)
The previous sermon is only valid for outbound connections,
unfortunately. Incoming connections are typically done as follows (pure
convention however):
- The remote sends WRU, we send our WRU answer
- The remote triggers his own WRU answer
To overcome this in most cases, if inbound=True is given, don't record
the line directly after WRU, but the next one after that. Also, trigger
on inbound WRU character (#) instead of outbound (@).
"""
# Filter out direction shifts
data = re.sub(pattern=INBOUND+"|"+OUTBOUND, string=data, repl="")
# Filter out letter/figure shift, CR and CR helper character
data = re.sub(pattern="<|>|\r|❮", string=data, repl="")
# Find WRU answerback match and return it
try:
if inbound:
match = re.findall("#.{0,4}\n+.+\n+(.{5,30})\n", data)[0]
else:
match = re.findall("@.{0,4}\n+(.{5,30})\n", data)[0]
except IndexError:
l.info("No WRU answerback found")
return None
else:
return match
@classmethod
def prettify(self, msg) -> str:
"""
Post-process the message msg to make it visually pleasing when
"catting" in console.
Currently, the following is done:
- Remove letter/figure shift.
- Replace ASCII Shift-in/Shift-Out used to discern inbound/outbound
characters by ANSI escape sequences for text colour change.
- Replace piTelex internal WRU characters by Unicode character U+2720
(✠).
- Insert a "❮" character wherever isolated CRs may lead to
overprinting, to make this obvious.
"""
# Remove letter/figure shifts
msg = re.sub(pattern="<|>", string=msg, repl="")
# Replace @/# by ✠
msg = re.sub(pattern="@", string=msg, repl="✠")
msg = re.sub(pattern="#", string=msg, repl="✠")
# If a CR could lead to overprinting, replace it with "❮". The
# following conditions apply:
# - Every CR must be followed by another CR or a newline,
# - except there are no printable characters between it and the newline
# before it.
# Examples see in prettify_cr_test.
# 1. Replace multiple consecutive CRs by one
msg = re.sub(pattern="\r+", string=msg, repl="\r")
# 2. Delete rogue CRs at beginnings of lines, allowing for a single
# unprintable direction shift if present
msg = re.sub(pattern="^([{}]?)\r".format(INBOUND+OUTBOUND), string=msg, repl="\g<1>", flags=re.MULTILINE)
# 3. Replace by "<" all CRs not followed by newline
msg = re.sub(pattern="\r([^\n])", string=msg, repl="❮\g<1>")
# Replace direction shifts by ANSI ESC sequences for colour change
msg = re.sub(pattern=OUTBOUND, string=msg, repl=ANSI_RED_FOREGROUND)
msg = re.sub(pattern=INBOUND, string=msg, repl=ANSI_RESET)
# Add a ANSI_RESET to avoid messing up the console
return msg+ANSI_RESET
def save_msg(self) -> str:
"""
Save the current message to file and get ready for the next. Return the
file name.
"""
msg = "".join(self._current_msg)
self._current_msg = []
if self._dial_number:
# outbound connection
direction = "to"
wru = self.find_WRU_answer(msg, inbound=False)
else:
# inbound connection
direction = "from"
wru = self.find_WRU_answer(msg, inbound=True)
# Fallback if no WRU answer found
if not wru:
if self._dial_number:
wru = self._dial_number
else:
wru = "[unknown]"
self._dial_number = None
filename = self.filename(wru=wru, direction=direction, timestamp=self._timestamp)
self._timestamp = None
l.info("saving {}, length {}".format(filename, len(msg)))
with open(filename, mode="w", encoding="utf-8", newline="") as f:
f.write(msg)
return filename
prettify_cr_test = """
\rOK: "Rogue" CR at beginning of line
\r\r\rOK: Multiple CRs at beginning of line
Test data\r
Test data\r\r\r
Last two lines also ok: CR(s) at line ending
Test data 1\rTest data 2, not OK: First part will be overprinted
\r\r\rtest1\rtest2\r\r\rtest3\r\r
The previous line should render as 'test1❮test2❮test3'
"""
prettify_lf_test = """There should come a single newline after this
\r\x0fHow far down am I?"""+OUTBOUND
wru_outbound_test = """12345678+<<<<<<<\r
>88.88.8888 88:88\r
@<\r
>12345678< example d<>\r
87654321 <ich d\r
\r
---message---\r
>\r
87654321 <ich d>@<\r
>12345678< example d<"""
wru_inbound_test = """<<<\r
88.88.8888 88:88\r
>>#<\r
>87654321< ich d<>\r
12345678< example d<<<\r\r
\r
---message---\r
\r
>#<\r
>87654321< ich d<>\r
12345678< example d<<<"""
def main():
import sys
files = sys.argv[1:]
if not files:
print("""txDevArchive prettifier
Usage: {} <filename>""".format(sys.argv[0]))
return
import io
for f in files:
with io.open(f, mode="r", encoding="utf-8", newline="") as msg:
msg = msg.read()
print(TelexArchive.prettify(msg))
if __name__ == "__main__":
main()