def _write_json_file(self, state): """write to the json file :param state [dict]: key/value pair """ with atomic_write_file(self.file_location) as outfile: json.dump(state, outfile, sort_keys=True, indent=4, separators=(',', ': '))
def _write_json_file(self, filename, state): """ Write a json file given a filename under the default config path. Creates the file and directory if required. """ json_file = os.path.join(self._options.config_path, filename) # Its ok to try and create this everytime as we aren't updating config # often. if not os.path.exists(json_file): common.file_util.mkdir_p(self._options.config_path) with atomic_write_file(json_file) as outfile: json.dump(state, outfile, sort_keys=True, indent=4, separators=(',', ': '))
def test_atomic_write_file(self): """Test atomic write file""" tempdir = mkdtemp(delete=True) self.file_name = os.path.join(tempdir, "atomic_tmp_file") with atomic_write_file(self.file_name) as fd: fd.write("testing..") file_state = os.stat(self.file_name) self.assertTrue(file_state.st_size == 9) def tmp_func(): with atomic_write_file(self.file_name): raise Exception("Ouch~") self.assertRaises(Exception, tmp_func)
def tmp_func(): with atomic_write_file(self.file_name): raise Exception("Ouch~")