Пример #1
0
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
Пример #2
0
    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
Пример #3
0
    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))
Пример #4
0
    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))
Пример #5
0
    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)
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
    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)
Пример #9
0
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)
Пример #10
0
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
Пример #11
0
 def setUp(self):
     file = generate_fake_config()
     file_save(file)
Пример #12
0
    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)
Пример #13
0
    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)
Пример #14
0
    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)