-
Notifications
You must be signed in to change notification settings - Fork 112
/
reassembler.py
executable file
·141 lines (115 loc) · 3.74 KB
/
reassembler.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
#!/usr/bin/env python3
# vim: set ts=4 sw=4 tw=0 et pm=:
import sys
import fileinput
import argparse
import os
from os.path import splitext, basename
import importlib
import pkgutil
import iridiumtk.config
import iridiumtk.reassembler
parser = argparse.ArgumentParser()
def parse_comma(arg):
return arg.split(',')
parser.add_argument("-v", "--verbose", action="store_true",
help="increase output verbosity")
parser.add_argument("-i", "--input", default=None,
help="input filename")
parser.add_argument("-o", "--output", default=None,
help="output filename")
parser.add_argument("-m", "--mode", default=None, required=True,
help="processing mode")
parser.add_argument("-a", "--args", default=[], type=parse_comma,
help="comma separated additional arguments")
parser.add_argument("-s", "--stats", action="store_true",
help="enable statistics")
parser.add_argument("-d", "--debug", action="store_true",
help=argparse.SUPPRESS)
parser.add_argument("--station", default=None,
help="optional station ID for acars")
parser.add_argument("remainder", nargs='*',
help=argparse.SUPPRESS)
config=parser.parse_args()
if config.stats:
import curses
curses.setupterm(fd=sys.stderr.fileno())
eol=(curses.tigetstr('el')+curses.tigetstr('cr')).decode("ascii")
if config.input is None:
if not config.remainder:
config.input = "/dev/stdin"
else:
config.input = config.remainder[0]
config.outbase, _= splitext(config.input)
if config.outbase.startswith('/dev'):
config.outbase=basename(config.outbase)
if config.output is None:
outfile=sys.stdout
elif config.output == "" or config.output == "=":
config.output="%s.%s" % (config.outbase, config.mode)
outfile=open(config.output,"w")
else:
outfile=open(config.output,"w")
config.outfile=outfile
state=None
if 'state' in config.args:
import pickle
statefile="%s.state" % (config.mode)
try:
with open(statefile) as f:
state=pickle.load(f)
except (IOError, EOFError):
pass
config.state=state
validargs=()
zx=None
iridiumtk.config.outfile=config.outfile
iridiumtk.config.config=config
plugins = iridiumtk.reassembler.get_plugins(iridiumtk.reassembler)
modes={}
for s, v in plugins.items():
for mode in v.modes:
modes[mode[0]]=[v]+mode[1:]
if config.debug or config.mode == 'help':
cwd = os.getcwd()+'/'
for mode, info in sorted(modes.items()):
path=info[0].__spec__.origin
if path.startswith(cwd):
path=path[len(cwd):]
print("Mode %-10s class %-22s source %-37s"%(mode, info[1].__name__, path), end='')
if len(info)>2:
print(" - Options: ",info[2])
else:
print()
print()
if config.mode not in modes:
raise SystemExit("No plugin found for mode: "+config.mode)
zx=modes[config.mode][1]()
if len(modes[config.mode])>2:
validargs=modes[config.mode][2]
for x in config.args:
if x not in validargs:
raise Exception("unknown -a option: "+x)
if config.input.startswith("zmq:"):
try:
topics=zx.topic
if not isinstance(topics,list):
topics=[topics]
except AttributeError:
print("mode '%s' does not support streaming"%mode, file=sys.stderr)
sys.exit(1)
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect ("tcp://localhost:4223")
for topic in topics:
socket.setsockopt(zmq.SUBSCRIBE, bytes(topic,"ascii"))
config.iobj=iter(socket.recv_string,"")
else:
config.iobj=fileinput.input(config.input)
try:
zx.run(config.iobj)
except BrokenPipeError as e:
raise SystemExit(e)
except KeyboardInterrupt:
print("")