def yml_create(yml_path, val): if (yml_path is None) or (val is None) or (type(val) is not dict): return content = dump(val) file = File(yml_path, content) file_save(file) return file
def parse_and_save(): def insert(): def replace(old, new): return str(yaml_as_dict).replace(old, new) def build(): dict_built = {new_key: val[1]} for index, item in enumerate(list(yaml_last_val.keys())): dict_built[list( yaml_last_val.keys())[index]] = yaml_last_val[list( yaml_last_val.keys())[index]] return dict_built new_key = refs_passed[len(refs_passed) - 1] if type(yaml_last_val) is dict: _old = str(yaml_last_val) _new = str(build()) return replace(_old, _new) if type(yaml_last_val) is int or type(yaml_last_val) is float: return replace(str(yaml_last_val), str({new_key: val[1]})) return replace("'" + str(yaml_last_val) + "'", str({new_key: val[1]})) jsonified = insert().replace("'", "\"") casted = json.loads(jsonified) file_save(File(path, dump(casted)), False) return casted
def test_should_not_delete_directory_because_directory_exist_but_has_content(self): dir_create(test_path) file_path = test_path + '/test_file.txt' file_save(File(file_path, 'Test content'), append=False) dir_delete(test_path, force=False) self.assertTrue(is_dir_exist(test_path)) self.assertTrue(file_exist(file_path))
def test_should_delete_directory_with_success_with_content(self): dir_create(test_path) file_path = test_path + '/test_file.txt' file_save(File(file_path, 'Test content'), append=False) dir_delete(test_path, force=True) self.assertFalse(is_dir_exist(file_path)) self.assertFalse(file_exist(file_path))
def test_should_file_save_create_new_file_if_not_exist(self): path = '/tmp/another_test_file' content = 'File created once not exist' delete_tmp_file(path) file_save(File(path, content)) self.assertTrue(file_exist(path)) self.assertEqual(file_load(path).content, content) delete_tmp_file(path)
def test_should_file_save_append_mode_with_success(self): to_append = 'New appended line!' create_tmp_file() # Write some content to test file in write mode file_save(File(file_path, fake_content), False) # Append one row to test file file_save(File(file_path, to_append), True) self.assertEqual(file_load(file_path).content, fake_content + to_append)
def test_should_not_replace_when_content_not_occur_in_file(self): path = create_tmp_file() file_save(File(path, fake_content), False) old = 'this phrase not occur' new = 'This is a file content' result = file_change(file_path, old, new) contains = str(file_load(path).content).__contains__(new) self.assertEqual(1, result) self.assertFalse(contains)
def test_should_replace_content_with_success_if_content_exist(self): path = create_tmp_file() file_save(File(path, fake_content), False) old = 'This is fake file content' new = 'This is a file content' result = file_change(file_path, old, new) contains = str(file_load(path).content).__contains__(new) self.assertEqual(0, result) self.assertTrue(contains)
def yml_delete(path, refs): yaml_as_dict = yml_load(path) refs = refs.split('.') command_del = 'del yaml_as_dict' for ref in refs: command_del = command_del + "['" + ref + "']" exec(command_del) file = File(path, dump(yaml_as_dict)) file_save(file, False)
def yml_change(path, val): yaml_as_dict = yml_load(path) refs = val[0].split('.') command = 'yaml_as_dict' for ref in refs: command = command + "['" + ref + "']" command = command + " = '" + val[1] + "'" try: exec(command) except KeyError: return None file = File(path, dump(yaml_as_dict)) file_save(file, False) return file
def setUp(self): file = generate_fake_config() file_save(file)
def test_should_delete_file_if_exist(self): path = create_tmp_file() file_save(File(path, fake_content), False) result = file_delete(path) self.assertEqual(0, result)
def test_should_file_save_override_mode_with_success__file_should_be_always_the_same(self): create_tmp_file() for x in range(2): file_save(File(file_path, fake_content), False) self.assertEqual(file_load(file_path).content, fake_content)
def test_should_file_save_append_mode_with_failure__file_should_not_be_same_as_before(self): create_tmp_file() for x in range(2): file_save(File(file_path, fake_content), True) self.assertNotEqual(file_load(file_path).content, fake_content)