1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 19:44:45 +00:00

migrate from PyYAML to ruamel.yaml

This commit is contained in:
deadc0de6
2019-06-06 17:11:13 +02:00
parent cd722e6eb1
commit d008e6a895
11 changed files with 71 additions and 76 deletions

View File

@@ -11,7 +11,7 @@ import string
import tempfile
from unittest import TestCase
import yaml
from ruamel.yaml import YAML as yaml
from dotdrop.options import Options
from dotdrop.linktypes import LinkTypes
@@ -221,9 +221,8 @@ def create_yaml_keyval(pairs, parent_dir=None, top_key=None):
if not parent_dir:
parent_dir = get_tempdir()
fd, file_name = tempfile.mkstemp(dir=parent_dir, suffix='.yaml', text=True)
with os.fdopen(fd, 'w') as f:
yaml.safe_dump(pairs, f)
_, file_name = tempfile.mkstemp(dir=parent_dir, suffix='.yaml', text=True)
yaml_dump(pairs, file_name)
return file_name
@@ -234,8 +233,7 @@ def populate_fake_config(config, dotfiles={}, profiles={}, actions={},
is_path = isinstance(config, str)
if is_path:
config_path = config
with open(config_path) as config_file:
config = yaml.safe_load(config_file)
config = yaml_load(config_path)
config['dotfiles'] = dotfiles
config['profiles'] = profiles
@@ -246,9 +244,7 @@ def populate_fake_config(config, dotfiles={}, profiles={}, actions={},
config['dynvariables'] = dynvariables
if is_path:
with open(config_path, 'w') as config_file:
yaml.safe_dump(config, config_file, default_flow_style=False,
indent=2)
yaml_dump(config, config_path)
def file_in_yaml(yaml_file, path, link=False):
@@ -256,8 +252,7 @@ def file_in_yaml(yaml_file, path, link=False):
strip = get_path_strip_version(path)
if isinstance(yaml_file, str):
with open(yaml_file) as f:
yaml_conf = yaml.safe_load(f)
yaml_conf = yaml_load(yaml_file)
else:
yaml_conf = yaml_file
@@ -275,3 +270,18 @@ def file_in_yaml(yaml_file, path, link=False):
return False
return in_src and in_dst and has_link
return in_src and in_dst
def yaml_load(path):
with open(path, 'r') as f:
content = yaml(typ='safe').load(f)
return content
def yaml_dump(content, path):
with open(path, 'w') as f:
y = yaml()
y.default_flow_style = False
y.indent = 2
y.typ = 'safe'
y.dump(content, f)

View File

@@ -7,7 +7,6 @@ basic unittest for the import function
import unittest
import os
import yaml
from dotdrop.dotdrop import cmd_importer
from dotdrop.dotdrop import cmd_list_profiles
@@ -18,7 +17,8 @@ from dotdrop.linktypes import LinkTypes
from tests.helpers import (clean, create_dir, create_fake_config,
create_random_file, edit_content, file_in_yaml,
get_path_strip_version, get_string, get_tempdir,
load_options, populate_fake_config)
load_options, populate_fake_config,
yaml_load)
class TestImport(unittest.TestCase):
@@ -31,10 +31,7 @@ class TestImport(unittest.TestCase):
def load_yaml(self, path):
"""Load yaml to dict"""
self.assertTrue(os.path.exists(path))
content = ''
with open(path, 'r') as f:
content = yaml.safe_load(f)
return content
return yaml_load(path)
def assert_file(self, path, o, profile):
"""Make sure path has been inserted in conf for profile"""

View File

@@ -4,7 +4,6 @@ Copyright (c) 2019, deadc0de6
basic unittest for the remove function
"""
import yaml
import unittest
import os
@@ -12,7 +11,7 @@ import os
from dotdrop.dotdrop import cmd_remove
from tests.helpers import (clean, create_dir,
create_random_file, load_options,
get_tempdir)
get_tempdir, yaml_load, yaml_dump)
class TestRemove(unittest.TestCase):
@@ -20,10 +19,7 @@ class TestRemove(unittest.TestCase):
def load_yaml(self, path):
"""Load yaml to dict"""
self.assertTrue(os.path.exists(path))
content = ''
with open(path, 'r') as f:
content = yaml.safe_load(f)
return content
return yaml_load(path)
def test_remove(self):
"""test the remove command"""
@@ -71,8 +67,7 @@ class TestRemove(unittest.TestCase):
},
}
with open(confpath, 'w') as f:
yaml.safe_dump(configdic, f)
yaml_dump(configdic, confpath)
o = load_options(confpath, 'host1')
o.remove_path = ['f_test1']
o.remove_iskey = True
@@ -87,7 +82,7 @@ class TestRemove(unittest.TestCase):
self.assertTrue(os.path.exists(df3))
# load dict
y = self.load_yaml(confpath)
y = yaml_load(confpath)
# ensure not present
self.assertTrue('f_test1' not in y['dotfiles'])
@@ -114,7 +109,7 @@ class TestRemove(unittest.TestCase):
self.assertFalse(os.path.exists(df3))
# load dict
y = self.load_yaml(confpath)
y = yaml_load(confpath)
# ensure not present
self.assertTrue('f_test3' not in y['dotfiles'])

View File

@@ -8,14 +8,13 @@ basic unittest for the config parser
import unittest
from unittest.mock import patch
import os
import yaml
from dotdrop.cfg_yaml import CfgYaml as Cfg
from dotdrop.options import Options
from dotdrop.linktypes import LinkTypes
from tests.helpers import (SubsetTestCase, _fake_args, clean,
create_fake_config, create_yaml_keyval, get_tempdir,
populate_fake_config)
populate_fake_config, yaml_load, yaml_dump)
class TestConfig(SubsetTestCase):
@@ -142,8 +141,7 @@ profiles:
create=self.CONFIG_CREATE)
# edit the config
with open(confpath, 'r') as f:
content = yaml.safe_load(f)
content = yaml_load(confpath)
# adding dotfiles
df1key = 'f_vimrc'
@@ -162,9 +160,7 @@ profiles:
}
# save the new config
with open(confpath, 'w') as f:
yaml.safe_dump(content, f, default_flow_style=False,
indent=2)
yaml_dump(content, confpath)
# do the tests
conf = Cfg(confpath, debug=True)
@@ -185,17 +181,14 @@ profiles:
# test not existing included profile
# edit the config
with open(confpath, 'r') as f:
content = yaml.safe_load(f)
content = yaml_load(confpath)
content['profiles'] = {
pf1key: {'dotfiles': [df2key], 'include': ['host2']},
pf2key: {'dotfiles': [df1key], 'include': ['host3']}
}
# save the new config
with open(confpath, 'w') as f:
yaml.safe_dump(content, f, default_flow_style=False,
indent=2)
yaml_dump(content, confpath)
# do the tests
conf = Cfg(confpath, debug=True)