-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_brewpi_json.py
More file actions
executable file
·72 lines (59 loc) · 1.79 KB
/
parse_brewpi_json.py
File metadata and controls
executable file
·72 lines (59 loc) · 1.79 KB
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
#!/usr/bin/python
import os
import os.path
import json
import re
import tabulate
def get_json_file():
dir='/var/www/html/data'
json_list = []
for root, dirs, files in os.walk( dir ):
for f in files:
if f.endswith( '.json' ):
json_list.append( os.path.join( root, f ) )
sorted_list = sorted( json_list, key=os.path.getmtime )
return sorted_list[-1]
###
# Remove columns of data in which there is no value present in any row
# PARAMS:
# rows - list of lists, assume first row is headers
# Note: headers are excluded from checking for empty values,
# but will be filtered based on data from the remaining rows
###
def filter_empty_cols( rows ):
cols_with_data = []
for ary in rows[1:]:
for i, elem in enumerate( ary ):
if elem is not None:
cols_with_data.append( i )
valid_cols = set( sorted( cols_with_data ) )
cleanrows = []
for ary in rows:
newrow = []
for i in valid_cols:
newrow.append( ary[i] )
cleanrows.append( newrow )
return cleanrows
jfile = get_json_file()
print( "\n{0}\n".format( os.path.basename( jfile ) ) )
with open( jfile ) as fh:
data = json.load( fh )
rex = re.compile( 'Time|Temp|SG' )
col_nums = []
for k,v in enumerate( data[ 'cols' ] ):
name = v['id']
if rex.search( name ):
col_nums.append( k )
headers = [ data['cols'][i]['id'] for i in col_nums ]
rows = []
for row in data['rows']:
values = []
for i in col_nums:
elem = row['c'][i]
val = None
if elem is not None:
val = elem['v']
values.append( val )
rows.append( values )
filtered_rows = filter_empty_cols( [ headers ] + rows )
print( tabulate.tabulate( filtered_rows, headers="firstrow" ) )