1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 17:24:46 +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

@@ -6,7 +6,7 @@ handle lower level of the config file
""" """
import os import os
import yaml from ruamel.yaml import YAML as yaml
import glob import glob
from copy import deepcopy from copy import deepcopy
@@ -572,12 +572,11 @@ class CfgYaml:
content = {} content = {}
if not os.path.exists(path): if not os.path.exists(path):
raise YamlException('config path not found: {}'.format(path)) raise YamlException('config path not found: {}'.format(path))
with open(path, 'r') as f: try:
try: content = self._yaml_load(path)
content = yaml.safe_load(f) except Exception as e:
except Exception as e: self.log.err(e)
self.log.err(e) raise YamlException('invalid config: {}'.format(path))
raise YamlException('invalid config: {}'.format(path))
return content return content
def _new_profile(self, key): def _new_profile(self, key):
@@ -733,18 +732,6 @@ class CfgYaml:
new[k] = newv new[k] = newv
return new return new
def _yaml_version(self):
"""returns True if version >= 5.1"""
minv = [5, 1]
try:
cur = list(map(int, yaml.__version__.split('.')))
if cur.pop(0) >= minv.pop(0) and \
cur.pop(1) >= minv.pop(1):
return True
except Exception:
return False
return False
def save(self): def save(self):
"""save this instance and return True if saved""" """save this instance and return True if saved"""
if not self.dirty: if not self.dirty:
@@ -760,19 +747,14 @@ class CfgYaml:
if self.key_profiles not in content: if self.key_profiles not in content:
content[self.key_profiles] = None content[self.key_profiles] = None
opts = {'default_flow_style': False, 'indent': 2}
if self._yaml_version():
opts['sort_keys'] = False
data = yaml.safe_dump(content, **opts)
# ensure no null are displayed
data = data.replace('null', '')
# save to file # save to file
if self.debug: if self.debug:
self.log.dbg('saving: {}'.format(content)) self.log.dbg('saving to {}'.format(self.path))
with open(self.path, 'w') as f: try:
f.write(data) self._yaml_dump(content, self.path)
except Exception as e:
self.log.err(e)
raise YamlException('error saving config: {}'.format(self.path))
self.dirty = False self.dirty = False
return True return True
@@ -780,3 +762,18 @@ class CfgYaml:
def dump(self): def dump(self):
"""dump the config dictionary""" """dump the config dictionary"""
return self.yaml_dict return self.yaml_dict
def _yaml_load(self, path):
"""load from yaml"""
with open(path, 'r') as f:
content = yaml(typ='safe').load(f)
return content
def _yaml_dump(self, content, path):
"""dump to yaml"""
with open(self.path, 'w') as f:
y = yaml()
y.default_flow_style = False
y.indent = 2
y.typ = 'safe'
y.dump(content, f)

View File

@@ -472,7 +472,10 @@ def cmd_remove(o):
LOG.raw(o.conf.dump()) LOG.raw(o.conf.dump())
else: else:
o.conf.save() o.conf.save()
LOG.log('\ndotfile(s) removed: {}'.format(','.join(removed))) if removed:
LOG.log('\ndotfile(s) removed: {}'.format(','.join(removed)))
else:
LOG.log('\nno dotfile removed')
return True return True

View File

@@ -10,7 +10,7 @@ pkgbase = dotdrop
depends = python-setuptools depends = python-setuptools
depends = python-jinja depends = python-jinja
depends = python-docopt depends = python-docopt
depends = python-pyaml depends = python-ruamel-yaml
source = git+https://github.com/deadc0de6/dotdrop.git#tag=v0.28.0 source = git+https://github.com/deadc0de6/dotdrop.git#tag=v0.28.0
md5sums = SKIP md5sums = SKIP

View File

@@ -8,7 +8,7 @@ arch=('any')
url="https://github.com/deadc0de6/dotdrop" url="https://github.com/deadc0de6/dotdrop"
license=('GPL') license=('GPL')
groups=() groups=()
depends=('python' 'python-setuptools' 'python-jinja' 'python-docopt' 'python-pyaml') depends=('python' 'python-setuptools' 'python-jinja' 'python-docopt' 'python-ruamel-yaml')
makedepends=('git') makedepends=('git')
source=("git+https://github.com/deadc0de6/dotdrop.git#tag=v${pkgver}") source=("git+https://github.com/deadc0de6/dotdrop.git#tag=v${pkgver}")
md5sums=('SKIP') md5sums=('SKIP')

View File

@@ -1,3 +1,3 @@
Jinja2; python_version >= '3.0' Jinja2; python_version >= '3.0'
docopt; python_version >= '3.0' docopt; python_version >= '3.0'
PyYAML; python_version >= '3.0' ruamel.yaml; python_version >= '3.0'

View File

@@ -13,7 +13,7 @@ usage example:
from docopt import docopt from docopt import docopt
import sys import sys
import os import os
import yaml from ruamel.yaml import YAML as yaml
USAGE = """ USAGE = """
change-link.py change-link.py
@@ -42,7 +42,7 @@ def main():
ignores = args['--ignore'] ignores = args['--ignore']
with open(path, 'r') as f: with open(path, 'r') as f:
content = yaml.safe_load(f) content = yaml(typ='safe').load(f)
for k, v in content[key].items(): for k, v in content[key].items():
if k in ignores: if k in ignores:
continue continue

View File

@@ -40,7 +40,7 @@ setup(
keywords='dotfiles jinja2', keywords='dotfiles jinja2',
packages=find_packages(exclude=['tests*']), packages=find_packages(exclude=['tests*']),
install_requires=['docopt', 'Jinja2', 'PyYAML'], install_requires=['docopt', 'Jinja2', 'ruamel.yaml'],
extras_require={ extras_require={
'dev': ['check-manifest'], 'dev': ['check-manifest'],

View File

@@ -11,7 +11,7 @@ import string
import tempfile import tempfile
from unittest import TestCase from unittest import TestCase
import yaml from ruamel.yaml import YAML as yaml
from dotdrop.options import Options from dotdrop.options import Options
from dotdrop.linktypes import LinkTypes from dotdrop.linktypes import LinkTypes
@@ -221,9 +221,8 @@ def create_yaml_keyval(pairs, parent_dir=None, top_key=None):
if not parent_dir: if not parent_dir:
parent_dir = get_tempdir() parent_dir = get_tempdir()
fd, file_name = tempfile.mkstemp(dir=parent_dir, suffix='.yaml', text=True) _, file_name = tempfile.mkstemp(dir=parent_dir, suffix='.yaml', text=True)
with os.fdopen(fd, 'w') as f: yaml_dump(pairs, file_name)
yaml.safe_dump(pairs, f)
return file_name return file_name
@@ -234,8 +233,7 @@ def populate_fake_config(config, dotfiles={}, profiles={}, actions={},
is_path = isinstance(config, str) is_path = isinstance(config, str)
if is_path: if is_path:
config_path = config config_path = config
with open(config_path) as config_file: config = yaml_load(config_path)
config = yaml.safe_load(config_file)
config['dotfiles'] = dotfiles config['dotfiles'] = dotfiles
config['profiles'] = profiles config['profiles'] = profiles
@@ -246,9 +244,7 @@ def populate_fake_config(config, dotfiles={}, profiles={}, actions={},
config['dynvariables'] = dynvariables config['dynvariables'] = dynvariables
if is_path: if is_path:
with open(config_path, 'w') as config_file: yaml_dump(config, config_path)
yaml.safe_dump(config, config_file, default_flow_style=False,
indent=2)
def file_in_yaml(yaml_file, path, link=False): 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) strip = get_path_strip_version(path)
if isinstance(yaml_file, str): if isinstance(yaml_file, str):
with open(yaml_file) as f: yaml_conf = yaml_load(yaml_file)
yaml_conf = yaml.safe_load(f)
else: else:
yaml_conf = yaml_file yaml_conf = yaml_file
@@ -275,3 +270,18 @@ def file_in_yaml(yaml_file, path, link=False):
return False return False
return in_src and in_dst and has_link return in_src and in_dst and has_link
return in_src and in_dst 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 unittest
import os import os
import yaml
from dotdrop.dotdrop import cmd_importer from dotdrop.dotdrop import cmd_importer
from dotdrop.dotdrop import cmd_list_profiles 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, from tests.helpers import (clean, create_dir, create_fake_config,
create_random_file, edit_content, file_in_yaml, create_random_file, edit_content, file_in_yaml,
get_path_strip_version, get_string, get_tempdir, get_path_strip_version, get_string, get_tempdir,
load_options, populate_fake_config) load_options, populate_fake_config,
yaml_load)
class TestImport(unittest.TestCase): class TestImport(unittest.TestCase):
@@ -31,10 +31,7 @@ class TestImport(unittest.TestCase):
def load_yaml(self, path): def load_yaml(self, path):
"""Load yaml to dict""" """Load yaml to dict"""
self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.exists(path))
content = '' return yaml_load(path)
with open(path, 'r') as f:
content = yaml.safe_load(f)
return content
def assert_file(self, path, o, profile): def assert_file(self, path, o, profile):
"""Make sure path has been inserted in conf for 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 basic unittest for the remove function
""" """
import yaml
import unittest import unittest
import os import os
@@ -12,7 +11,7 @@ import os
from dotdrop.dotdrop import cmd_remove from dotdrop.dotdrop import cmd_remove
from tests.helpers import (clean, create_dir, from tests.helpers import (clean, create_dir,
create_random_file, load_options, create_random_file, load_options,
get_tempdir) get_tempdir, yaml_load, yaml_dump)
class TestRemove(unittest.TestCase): class TestRemove(unittest.TestCase):
@@ -20,10 +19,7 @@ class TestRemove(unittest.TestCase):
def load_yaml(self, path): def load_yaml(self, path):
"""Load yaml to dict""" """Load yaml to dict"""
self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.exists(path))
content = '' return yaml_load(path)
with open(path, 'r') as f:
content = yaml.safe_load(f)
return content
def test_remove(self): def test_remove(self):
"""test the remove command""" """test the remove command"""
@@ -71,8 +67,7 @@ class TestRemove(unittest.TestCase):
}, },
} }
with open(confpath, 'w') as f: yaml_dump(configdic, confpath)
yaml.safe_dump(configdic, f)
o = load_options(confpath, 'host1') o = load_options(confpath, 'host1')
o.remove_path = ['f_test1'] o.remove_path = ['f_test1']
o.remove_iskey = True o.remove_iskey = True
@@ -87,7 +82,7 @@ class TestRemove(unittest.TestCase):
self.assertTrue(os.path.exists(df3)) self.assertTrue(os.path.exists(df3))
# load dict # load dict
y = self.load_yaml(confpath) y = yaml_load(confpath)
# ensure not present # ensure not present
self.assertTrue('f_test1' not in y['dotfiles']) self.assertTrue('f_test1' not in y['dotfiles'])
@@ -114,7 +109,7 @@ class TestRemove(unittest.TestCase):
self.assertFalse(os.path.exists(df3)) self.assertFalse(os.path.exists(df3))
# load dict # load dict
y = self.load_yaml(confpath) y = yaml_load(confpath)
# ensure not present # ensure not present
self.assertTrue('f_test3' not in y['dotfiles']) self.assertTrue('f_test3' not in y['dotfiles'])

View File

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