-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonHelper.py
105 lines (93 loc) · 4.08 KB
/
jsonHelper.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 24 11:24:35 2020
@author: jackr
"""
from PySide2 import QtCore
import json
import jsonschema
import os
# from jsonschema import validate
class JSONHelper(QtCore.QObject):
# Can be redone with getters and setters for the stepModel property. This will be faster with less conversions on the data object.
# https://qmlbook.github.io/ch18-python/python.html
nextModel = QtCore.Signal(str, str, str)
@QtCore.Slot(str, str)
def saveProtocol(self, fileName, jString):
# with open(fileName, "w") as outfile:
# json.dump(jString, outfile)
with open(os.path.normpath(fileName), "w") as outfile:
outfile.write(jString)
@QtCore.Slot(str, str, str)
def openProtocol(self, fileName, protocolName, pathSaved):
# take accepted filename and check if it's .json
jsondata=[]
try:
with open(os.path.normpath(fileName), encoding='utf-8') as data_file:
jsondata = json.load(data_file)
except Exception as e:
print(e)
print("An error has occurred with the file selected.")
return
# validate .json object?
# check if json object has the propper keys for all elements?
isValid = self.validateJson(jsondata)
print('Is the jsondata valid: {}'.format(isValid))
if not isValid:
print('JSON has key value error. opName, opTime, volume, extraVolOut, inSpeed, chamberSpeed, lineSpeed, numCycles, washSyr, washReagent, and loadType are the only keys accepted.')
return
else:
# Send JSON string with file info
self.nextModel.emit(json.dumps(jsondata), protocolName, pathSaved)
def openToRun(self, fileName):
# take accepted filename and check if it's .json
jsondata=[]
try:
with open(os.path.normpath(fileName), encoding='utf-8') as data_file:
jsondata = json.load(data_file)
except Exception as e:
print(e)
print("An error has occurred with the file selected.")
return
# validate .json object?
# check if json object has the propper keys for all elements?
isValid = self.validateJson(jsondata)
print('Is the jsondata valid: {}'.format(isValid))
if not isValid:
print('JSON has key value error. opName, opTime, volume, extraVolOut, inSpeed, chamberSpeed, lineSpeed, numCycles, washSyr, washReagent, and loadType are the only keys accepted.')
return
else:
# Send JSON string with file info
return(jsondata)
def validateJson(self, jsonData):
# Describe what kind of json you expect.
# Need to make this more functional with filter masks or regex
protocolSchema = {
"type": "array",
"items": {
"type": "object",
"properties": {
"opName": {"type": "string"},
"opTime": {"type": "string"},
"mixAfterSecs": {"type": ["integer", "null"]},
"volume": {"type": "string"},
"extraVolOut": {"type": "string"},
"inSpeed": {"type": ["integer", "null"]},
"chamberSpeed": {"type": ["integer", "null"]},
"lineSpeed": {"type": ["integer", "null"]},
"numCycles": {"type": ["integer", "null"]},
"washSyr": {"type": "string"},
"washReagent": {"type": "string"},
"loadType": {"type": "string"},
},
"required": ["opName", "opTime", "mixAfterSecs", "volume", "extraVolOut",
"inSpeed", "chamberSpeed", "lineSpeed", "numCycles",
"washSyr", "washReagent", "loadType"]
}
}
try:
jsonschema.validate(instance=jsonData, schema=protocolSchema)
return True
except jsonschema.exceptions.ValidationError as err:
return False