-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMCDataTypes.py
98 lines (83 loc) · 2.62 KB
/
TMCDataTypes.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
# rigolrdout - Read data and screenshots from Rigol oscilloscopes
# Copyright (C) 2012-2018 Johannes Bauer
#
# This file is part of rigolrdout.
#
# rigolrdout 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.
#
# rigolrdout 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 rigolrdout; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Johannes Bauer <[email protected]>
import json
import base64
import hashlib
import gzip
class TMCBool(object):
_FALSE_VALUES = set([ "0", "off" ])
_TRUE_VALUES = set([ "1", "on" ])
_VALID_VALUES = _FALSE_VALUES | _TRUE_VALUES
def __init__(self, value):
assert(isinstance(value, str))
self._orig_value = value
value = value.lower()
if value not in self._VALID_VALUES:
raise TypeError("'%s' is not a valid TMC boolean." % (value))
self._bool_value = value in self._TRUE_VALUES
def to_repr(self):
return bool(self)
def __bool__(self):
return self._bool_value
class TMCFloat(object):
def __init__(self, value):
assert(isinstance(value, str))
self._orig_value = value
self._flt_value = float(value)
def to_repr(self):
return {
"orig": self._orig_value,
"flt": float(self),
}
def __float__(self):
return self._flt_value
class TMCRawData(object):
def __init__(self, data, file_format, metadata = None):
self._data = bytes(data)
self._file_format = file_format
self._metadata = metadata
@property
def data(self):
return self._data
@property
def file_format(self):
return self._file_format
@property
def metadata(self):
return self._metadata
def to_repr(self, external_filename = None):
result = {
"length": len(self._data),
"format": self._file_format,
"sha256": hashlib.sha256(self._data).hexdigest(),
}
if external_filename is None:
result["gzip_compressed_data"] = base64.b64encode(gzip.compress(self._data)).decode("ascii")
result["storage"] = "inline"
else:
result["filename"] = external_filename
result["storage"] = "external"
if self._metadata is not None:
result["meta"] = self._metadata
return result
class TMCJSONEncoder(json.JSONEncoder):
def default(self, obj):
return obj.to_repr()