def save(self): """Save our recorded responses to a file. Returns: A boolean indicating if responses were saved or not. Responses will only be saved if record mode is enabled. """ if self._record: output_filename = os.path.join(get_lightning_path(), self._output_file) dirname, filename = os.path.split(output_filename) if not os.path.exists(dirname): os.makedirs(dirname) with open(output_filename, 'w') as output_file: output_file.write(json.dumps(self._serialize(), indent=2, sort_keys=True)) return True return False
def load(self): """Load RecordedResponses from an input file. Returns: A dict containing the recorded responses keyed by method and uri. If the input file was not found, default to record mode and return an empty matches hash. """ result = {'GET': {}, 'POST': {}} if not self._record: try: input_filename = os.path.join(get_lightning_path(), self._input_file) with open(input_filename, 'r') as input_file: result = json.load(input_file) result = self._unserialize(result) # print('Loaded playback file: %s' % self._input_file) except (IOError, ValueError) as exc: print('Unable to load file: %s' % (exc.message or exc[1])) self._matches = result return result