55
66from opentrons .containers .calibrator import Calibrator
77from opentrons .util .vector import (Vector , VectorEncoder )
8+ from opentrons .util import environment
89from opentrons .robot .command import Command
910from opentrons import Robot
1011
12+ from opentrons .util .log import get_logger
13+
1114
1215JSON_ERROR = None
1316if sys .version_info > (3 , 4 ):
1619 JSON_ERROR = json .decoder .JSONDecodeError
1720
1821
19- CALIBRATIONS_FOLDER = 'calibrations'
20- CALIBRATIONS_FILE = 'calibrations.json'
22+ log = get_logger (__name__ )
2123
2224
2325class Instrument (object ):
@@ -121,13 +123,8 @@ def init_calibrations(self, key, attributes=None):
121123 for key in attributes :
122124 self .persisted_defaults [key ] = copy .copy (getattr (self , key ))
123125
124- if not os .path .isdir (self ._get_calibration_dir ()):
125- os .mkdir (self ._get_calibration_dir ())
126-
127126 if not os .path .isfile (self ._get_calibration_file_path ()):
128127 self ._write_blank_calibrations_file ()
129- else :
130- self ._check_calibrations_version ()
131128
132129 def update_calibrations (self ):
133130 """
@@ -180,18 +177,11 @@ def _write_blank_calibrations_file(self):
180177 'data' : {}
181178 }))
182179
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-
190180 def _get_calibration_file_path (self ):
191181 """
192182 :return: the absolute file path of the calibration file
193183 """
194- return os . path . join ( self . _get_calibration_dir (), CALIBRATIONS_FILE )
184+ return environment . get_path ( ' CALIBRATIONS_FILE' )
195185
196186 def _get_calibration (self ):
197187 """
@@ -212,30 +202,37 @@ def _build_calibration_data(self):
212202 def _read_calibrations (self ):
213203 """
214204 Reads calibration data from file system.
205+ Expects a valid valibration format
215206 :return: json of calibration data
216207 """
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 )
224213
225- def _check_calibrations_version (self ):
214+ return self ._restore_vector (loaded_json )
215+
216+ def _validate_calibration_file (self , file_path ):
226217 """
227218 Read calibration file, and checks for version number
228219 If no version number, file is replaced with version number
229220 """
230- with open (self ._get_calibration_file_path ()) as f :
221+ valid = False
222+ with open (file_path ) as f :
231223 try :
232224 file = json .load (f )
233225 version = file .get ('version' )
234226 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 ()
239236
240237 def _strip_vector (self , obj , root = True ):
241238 """
0 commit comments