-
Notifications
You must be signed in to change notification settings - Fork 2
/
lightmeter_table.py
executable file
·79 lines (71 loc) · 2.49 KB
/
lightmeter_table.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
#!/usr/bin/env python3
import sys
import atexit
import argparse
# PANDAS compatible JSON table-schema
# https://specs.frictionlessdata.io/table-schema/
jsonSchemaPrefix = """\
{"schema": {
"primaryKey": ["TS"],
"fields": [
{"name": "TS",
"type": "datetime",
"title": "Timestamp",
"description": "ISO8601 string, UTC"},
{"name": "T",
"type": "number",
"title": "Temperature",
"description": "Temperature in degrees Celsius"},
{"name": "L",
"type": "integer",
"title": "Light level",
"description": "Light level counts, no calibration"},
{"name": "D",
"type": "integer",
"title": "Daylight",
"description": "Daylight sensor reading in Lux"},
{"name": "S",
"type": "boolean",
"title": "Status",
"description": "True if everything is OK, false otherwise"}
]
},
"data": ["""
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Convert json_line or '
'json_line_long format into json_table. '
'Useful for sending the output of '
'lightmeter.py over the network and '
'storing it on the other side.')
parser.add_argument('-o', '--outfile', type=str, default='-',
help='output file, stdout if unspecified')
parser.add_argument('-i', '--infile', type=str, default='-',
help='input file, stdin if unspecified')
args = parser.parse_args()
if args.outfile == '-':
outfile = sys.stdout
else:
outfile = open(args.outfile, 'w')
if args.infile == '-':
infile = sys.stdin
else:
infile = open(args.infile, 'r')
longNames = ('utc', 'temperature', 'lightlevel', 'daylight', 'status')
shortNames = ('TS', 'T', 'L', 'D', 'S')
print(jsonSchemaPrefix, end='', file=outfile)
printComma = ''
@atexit.register
def finish():
print('\n]}', file=outfile)
outfile.close()
while True:
line = infile.readline()
if line == '':
break
if 'lightlevel' in line:
# long format json
for long, short in zip(longNames, shortNames):
line = line.replace(long, short)
line = line.rstrip()
print(printComma, line, end='', sep='\n', flush=True, file=outfile)
printComma = ','