5
5
6
6
from opentrons .containers .calibrator import Calibrator
7
7
from opentrons .util .vector import (Vector , VectorEncoder )
8
+ from opentrons .util import environment
8
9
from opentrons .robot .command import Command
9
10
from opentrons import Robot
10
11
12
+ from opentrons .util .log import get_logger
13
+
11
14
12
15
JSON_ERROR = None
13
16
if sys .version_info > (3 , 4 ):
16
19
JSON_ERROR = json .decoder .JSONDecodeError
17
20
18
21
19
- CALIBRATIONS_FOLDER = 'calibrations'
20
- CALIBRATIONS_FILE = 'calibrations.json'
22
+ log = get_logger (__name__ )
21
23
22
24
23
25
class Instrument (object ):
@@ -121,13 +123,8 @@ def init_calibrations(self, key, attributes=None):
121
123
for key in attributes :
122
124
self .persisted_defaults [key ] = copy .copy (getattr (self , key ))
123
125
124
- if not os .path .isdir (self ._get_calibration_dir ()):
125
- os .mkdir (self ._get_calibration_dir ())
126
-
127
126
if not os .path .isfile (self ._get_calibration_file_path ()):
128
127
self ._write_blank_calibrations_file ()
129
- else :
130
- self ._check_calibrations_version ()
131
128
132
129
def update_calibrations (self ):
133
130
"""
@@ -180,18 +177,11 @@ def _write_blank_calibrations_file(self):
180
177
'data' : {}
181
178
}))
182
179
183
- def _get_calibration_dir (self ):
184
- """
185
- :return: the directory to save calibration data
186
- """
187
- DATA_DIR = os .environ .get ('APP_DATA_DIR' ) or os .getcwd ()
188
- return os .path .join (DATA_DIR , CALIBRATIONS_FOLDER )
189
-
190
180
def _get_calibration_file_path (self ):
191
181
"""
192
182
:return: the absolute file path of the calibration file
193
183
"""
194
- return os . path . join ( self . _get_calibration_dir (), CALIBRATIONS_FILE )
184
+ return environment . get_path ( ' CALIBRATIONS_FILE' )
195
185
196
186
def _get_calibration (self ):
197
187
"""
@@ -212,30 +202,37 @@ def _build_calibration_data(self):
212
202
def _read_calibrations (self ):
213
203
"""
214
204
Reads calibration data from file system.
205
+ Expects a valid valibration format
215
206
:return: json of calibration data
216
207
"""
217
- with open (self ._get_calibration_file_path ()) as f :
218
- try :
219
- loaded_json = json .load (f )
220
- except json .decoder .JSONDecodeError :
221
- self ._write_blank_calibrations_file ()
222
- return self ._read_calibrations ()
223
- return self ._restore_vector (loaded_json )
208
+ file_path = self ._get_calibration_file_path ()
209
+ self ._validate_calibration_file (file_path )
210
+ loaded_json = ""
211
+ with open (file_path ) as f :
212
+ loaded_json = json .load (f )
224
213
225
- def _check_calibrations_version (self ):
214
+ return self ._restore_vector (loaded_json )
215
+
216
+ def _validate_calibration_file (self , file_path ):
226
217
"""
227
218
Read calibration file, and checks for version number
228
219
If no version number, file is replaced with version number
229
220
"""
230
- with open (self ._get_calibration_file_path ()) as f :
221
+ valid = False
222
+ with open (file_path ) as f :
231
223
try :
232
224
file = json .load (f )
233
225
version = file .get ('version' )
234
226
data = file .get ('data' )
235
- if not version or not data or len (file .keys ()) > 2 :
236
- self ._write_blank_calibrations_file ()
237
- except json .decoder .JSONDecodeError :
238
- self ._write_blank_calibrations_file ()
227
+ if version and data and len (file .keys ()) == 2 :
228
+ valid = True
229
+ except json .decoder .JSONDecodeError as e :
230
+ log .error (
231
+ 'Error parsing calibration data (file: {}): {}' .format (
232
+ file_path , e ))
233
+
234
+ if not valid :
235
+ self ._write_blank_calibrations_file ()
239
236
240
237
def _strip_vector (self , obj , root = True ):
241
238
"""
0 commit comments