-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOutputBackends.py
400 lines (336 loc) · 13 KB
/
OutputBackends.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#
# GammaScoutUtil - Tool to communicate with Gamma Scout Geiger counters.
# Copyright (C) 2011-2011 Johannes Bauer
#
# This file is part of GammaScoutUtil.
#
# GammaScoutUtil is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; this program is ONLY licensed under
# version 3 of the License, later versions are explicitly excluded.
#
# GammaScoutUtil is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GammaScoutUtil; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Johannes Bauer <[email protected]>
#
import sys
import csv
import struct
import hashlib
import logging
import datetime
import os
try:
import pymysql
except ImportError:
pymysql = None
import Globals
from Exceptions import InvalidArgumentException
from DosisConversion import DosisConversion
from SQLite import SQLite
def _strftimeexpand(filename, args):
if args["localstrftime"]:
now = datetime.datetime.now()
else:
now = datetime.datetime.utcnow()
filename = now.strftime(filename)
directory = os.path.dirname(filename)
try:
os.makedirs(directory)
except OSError:
pass
return filename
def _parseconnstr(connstr, acceptkeywords):
"""Acceptkeywords is a dictionary of default items that are returned if the
argument wasn't specified. Only keys in that dictionary are allowed to be
specified in the connstr."""
assert(isinstance(connstr, str))
assert(isinstance(acceptkeywords, dict))
resultdict = dict(acceptkeywords)
for item in connstr.split(","):
keyvalue = item.split("=")
if len(keyvalue) != 2:
raise InvalidArgumentException("Connection string must be of the form key1=value1,key2=value2,... but item '%s' contains an illegal number of values (%d instead of 2)." % (item, len(keyvalue)))
(key, value) = keyvalue
if key not in acceptkeywords:
raise InvalidArgumentException("Connection string recognizes keywords %s, but keyword '%s' was given on command line." % (", ".join(sorted(list(acceptkeywords.keys()))), key))
resultdict[key] = value
return resultdict
class OutputBackend():
def __init__(self, filename, args):
self._filename = filename
self._args = args
def initdata(self, logsize, datablob):
pass
def newinterval(self, fromtime, totime, counts):
pass
def close(self):
pass
class FileWrapper():
def __init__(self, filename, args):
self._filename = filename
self._args = args
if self._filename == "-":
self._f = sys.stdout
else:
self._f = open(filename, "w", encoding = "utf-8")
def write(self, data, **kwargs):
try:
result = self._f.write(data, **kwargs)
except UnicodeEncodeError:
data = data.replace("µ", "u")
data = data.encode("ascii", errors = "replace").decode("ascii")
result = self._f.write(data, **kwargs)
if self._args["line_buffered"]:
self._f.flush()
return result
def close(self):
if self._filename != "-":
self._f.close()
class OutputBackendBIN(OutputBackend):
def __init__(self, filename, args):
OutputBackend.__init__(self, filename, args)
self._f = open(_strftimeexpand(filename, args), "wb")
def initdata(self, logsize, datablob):
data = ("Gamma scout binary data blob " + Globals.VERSION).encode("utf-8")
data += bytes([ 0 ])
data += struct.pack("<L", logsize)
data += datablob
self._f.write(data)
# Append SHA-256 hash of data
self._f.write(hashlib.sha256(data).digest())
@staticmethod
def readdata(filename, force):
log = logging.getLogger("gsu.fileops." + __class__.__name__)
data = open(filename, "rb").read()
hashval = data[-32:]
data = data[:-32]
calchash = hashlib.sha256(data).digest()
if calchash != hashval:
if not force:
raise InvalidArgumentException("File has incorrect hash value, will not read it without specification of --force")
loc = data.index(bytes([0]))
version = data[:loc].decode("utf-8")
log.debug("Reading file data from file generated by %s" % (version))
data = data[loc + 1:]
(length,) = struct.unpack("<L", data[:4])
data = data[4:]
return (length, data)
def close(self):
self._f.close()
class OutputBackendCSV(OutputBackend):
def __init__(self, filename, args):
OutputBackend.__init__(self, filename, args)
self._f = FileWrapper(_strftimeexpand(filename, args), args)
self._csv = csv.writer(self._f)
if not self._args["noheader"]:
self._csv.writerow(["From", "To", "Counts", "Seconds", "CPM", "CPS", "µSv/h"])
def newinterval(self, fromtime, totime, counts):
delta = (totime - fromtime)
totalseconds = delta.days * 86400 + delta.seconds
cps = counts / totalseconds
self._csv.writerow([ fromtime.strftime(self._args["date_format"]), totime.strftime(self._args["date_format"]), counts, totalseconds, 60 * cps, cps, DosisConversion.cts_per_sec_to_usv_per_hr(cps) ])
def close(self):
self._f.close()
class OutputBackendTXT(OutputBackend):
def get_known_args():
return [ "fromtime", "midtime", "totime", "counts", "intervallen", "cps", "cpm", "usvperhr" ]
def __init__(self, filename, args):
OutputBackend.__init__(self, filename, args)
self._sampleno = 0
self._f = FileWrapper(_strftimeexpand(filename, args), args)
if (not self._args["noheader"]) and (not self._args["txt_format"]):
heading = "%-20s %-20s %6s %6s %4s %5s %6s" % ("From", "To", "Counts", "Seconds", "CPM", "CPS", "µSv/hr")
print(heading, file = self._f)
print("-" * len(heading), file = self._f)
def newinterval(self, fromtime, totime, counts):
self._sampleno += 1
delta = (totime - fromtime)
totalseconds = round(delta.days * 86400 + delta.seconds)
cps = counts / totalseconds
midtime = fromtime + datetime.timedelta(0, totalseconds / 2)
intervallenstr = {
300: "5 min",
600: "10 min",
}.get(totalseconds, "?")
variables = {
"fromtime": fromtime.strftime(self._args["date_format"]),
"totime": totime.strftime(self._args["date_format"]),
"midtime": midtime.strftime(self._args["date_format"]),
"counts": counts,
"intervallen": totalseconds,
"cps": cps,
"cpm": 60 * cps,
"usvperhr": DosisConversion.cts_per_sec_to_usv_per_hr(cps),
"sampleno": self._sampleno,
"intervallenstr": intervallenstr,
}
if self._args["txt_format"] is None:
if not self._args["gstool_txt_format"]:
# Regular format
print("%(fromtime)-20s %(totime)-20s %(counts)6d %(intervallen)6d %(cpm)6.1f %(cps)7.3f %(usvperhr)6.3f" % (variables), file = self._f)
else:
# GSTool format
print("%(sampleno)04d %(intervallenstr)-7s %(fromtime)-19s %(totime)-19s %(counts)010d %(cps)-6.3f %(usvperhr)5.3f" % (variables), file = self._f)
else:
# Custom format
print(self._args["txt_format"] % (variables), file = self._f)
def close(self):
self._f.close()
class OutputBackendXML(OutputBackend):
def __init__(self, filename, args):
OutputBackend.__init__(self, filename, args)
self._f = FileWrapper(_strftimeexpand(filename, args), args)
if not self._args["noheader"]:
print("<?xml version=\"1.0\" encoding=\"utf-8\" ?>", file = self._f)
print("<gammascout>", file = self._f)
def newinterval(self, fromtime, totime, counts):
delta = (totime - fromtime)
totalseconds = delta.days * 86400 + delta.seconds
cps = counts / totalseconds
doserate = DosisConversion.cts_per_sec_to_usv_per_hr(cps)
print(" <interval from=\"%s\" to=\"%s\" secs=\"%s\" counts=\"%d\" doserate=\"%.5f\" />" % (fromtime.strftime(self._args["date_format"]), totime.strftime(self._args["date_format"]), totalseconds, counts, doserate), file = self._f)
def close(self):
print("</gammascout>", file = self._f)
self._f.close()
class OutputBackendSqlite(OutputBackend):
def __init__(self, filename, args):
OutputBackend.__init__(self, filename, args)
self._db = SQLite(_strftimeexpand(filename, args))
self._db.exec_mayfail_commit("""CREATE TABLE metadata (
key character varying PRIMARY KEY,
value character varying NOT NULL
);""")
self._db.exec_mayfail_commit("INSERT INTO metadata (key, value) VALUES ('dbversion', '1');")
self._db.exec_mayfail_commit("""CREATE TABLE data (
id integer PRIMARY KEY,
tfrom timestamp NOT NULL,
tto timestamp NOT NULL,
counts integer NOT NULL,
CHECK(tto > tfrom),
CHECK(counts >= 0)
);""")
def newinterval(self, fromtime, totime, counts):
self._db.execute("INSERT INTO data (tfrom, tto, counts) VALUES (?, ?, ?);", fromtime, totime, counts)
def close(self):
self._db.commit()
class OutputBackendSQL(OutputBackend):
def __init__(self, connstring, args):
OutputBackend.__init__(self, connstring, args)
self._dbdef = _parseconnstr(connstring, {
"file": "-",
"dialect": "sqlite",
"dbname": "gammascout",
"tablename": "data",
})
knowndialects = [ "sqlite", "mysql" ]
if self._dbdef["dialect"] not in knowndialects:
raise InvalidArgumentException("dialect must be one of %s, but %s was given." % (", ".join(sorted(list(knowndialects))), self._dbdef["dialect"]))
self._f = FileWrapper(_strftimeexpand(self._dbdef["file"], args), args)
if self._dbdef["dialect"] == "sqlite":
self._createdb_sqlite()
self.newinterval = self._newinterval_sqlite
self.close = self._close_sqlite
elif self._dbdef["dialect"] == "mysql":
self._createdb_mysql()
self.newinterval = self._newinterval_mysql
self.close = self._close_mysql
def _createdb_sqlite(self):
print("BEGIN TRANSACTION;", file = self._f)
print("""CREATE TABLE IF NOT EXISTS %(dbname)s (
id integer PRIMARY KEY,
tfrom timestamp NOT NULL,
tto timestamp NOT NULL,
counts integer NOT NULL,
CHECK(tto > tfrom),
CHECK(counts >= 0)
);""" % self._dbdef, file = self._f)
def _newinterval_sqlite(self, fromtime, totime, counts):
print("INSERT INTO data (tfrom, tto, counts) VALUES ('%s', '%s', %d);" % (fromtime, totime, counts), file = self._f)
def _close_sqlite(self):
print("COMMIT;", file = self._f)
def _createdb_mysql(self):
print("CREATE DATABASE /*!32312 IF NOT EXISTS*/ `%(dbname)s` /*!40100 DEFAULT CHARACTER SET utf8 */;" % self._dbdef, file = self._f)
print("CONNECT `%(dbname)s`;" % self._dbdef, file = self._f)
print("START TRANSACTION;", file = self._f)
print("""CREATE TABLE IF NOT EXISTS `%(tablename)s` (
`id` int(12) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`tfrom` timestamp NOT NULL,
`tto` timestamp NOT NULL,
`counts` int(8) NOT NULL,
CHECK(tto > tfrom),
CHECK(counts >= 0)
);""" % self._dbdef, file = self._f)
def _newinterval_mysql(self, fromtime, totime, counts):
print("INSERT INTO data (`tfrom`, `tto`, `counts`) VALUES ('%s', '%s', %d);" % (fromtime, totime, counts), file = self._f)
def _close_mysql(self):
print("COMMIT;", file = self._f)
class OutputBackendMySQL(OutputBackend):
def __init__(self, connstring, args):
OutputBackend.__init__(self, connstring, args)
if pymysql is None:
raise InvalidArgumentException("The MySQL backend is not available from Python. Please install the PyMySQL pacakge available from http://www.pymysql.org")
self._dbdef = _parseconnstr(connstring, {
"host": None,
"port": None,
"unixsocket": None,
"user": None,
"password": None,
"dbname": "gammascout",
"tablename": "data",
})
if self._dbdef["port"] is not None:
self._dbdef["port"] = int(self._dbdef["port"])
connparams = {
"host": self._dbdef["host"],
"user": self._dbdef["user"],
"passwd": self._dbdef["password"],
"port": self._dbdef["port"],
"unixsocket": self._dbdef["unixsocket"],
}
for key in list(connparams.keys()):
if connparams[key] is None:
del connparams[key]
self._db = pymysql.Connect(**connparams)
self._cursor = self._db.cursor()
self._cursor.execute("CREATE DATABASE /*!32312 IF NOT EXISTS*/ %s;" % (self._dbdef["dbname"]))
self._cursor.execute("USE %s;" % (self._dbdef["dbname"]))
self._cursor.execute("""CREATE TABLE IF NOT EXISTS metadata (
`key` varchar(128) PRIMARY KEY,
`value` varchar(128) NOT NULL
);""")
self._exec_mayfail("INSERT INTO metadata (`key`, `value`) VALUES ('dbversion', '1');")
self._cursor.execute("""CREATE TABLE IF NOT EXISTS `%(tablename)s` (
`id` int(12) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`tfrom` timestamp NOT NULL,
`tto` timestamp NOT NULL,
`counts` int(8) NOT NULL,
CHECK(tto > tfrom),
CHECK(counts >= 0)
);""" % self._dbdef)
def _exec_mayfail(self, sql):
try:
self._cursor.execute(sql)
except pymysql.err.IntegrityError:
pass
def newinterval(self, fromtime, totime, counts):
self._cursor.execute("INSERT INTO data (`tfrom`, `tto`, `counts`) VALUES ('%s', '%s', %d);" % (fromtime, totime, counts))
def close(self):
self._db.commit()
def getbackendbyname(name):
return {
"bin": OutputBackendBIN,
"txt": OutputBackendTXT,
"csv": OutputBackendCSV,
"sqlite": OutputBackendSqlite,
"xml": OutputBackendXML,
"sql": OutputBackendSQL,
"mysql": OutputBackendMySQL,
}[name]