From 5e1a49d959dcdce4552e99e79a39d3456161cda4 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Sat, 12 Mar 2022 15:05:38 +0100 Subject: [PATCH] handle toml for #343 --- dotdrop/cfg_yaml.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 44624c5..2345362 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -26,6 +26,7 @@ import io from copy import deepcopy from itertools import chain from ruamel.yaml import YAML as yaml +import toml # local imports from dotdrop.version import __version__ as VERSION @@ -1210,6 +1211,17 @@ class CfgYaml: @classmethod def _yaml_load(cls, path): + """load config file""" + is_yaml = path.lower().endswith(".yaml") + if is_yaml: + return cls.__yaml_load(path) + is_toml = path.lower().endswith(".toml") + if is_toml: + return cls.__toml_load(path) + raise YamlException("unsupported format") + + @classmethod + def __yaml_load(cls, path): """load from yaml""" with open(path, 'r', encoding='utf8') as file: data = yaml() @@ -1217,8 +1229,27 @@ class CfgYaml: content = data.load(file) return content + @classmethod + def __toml_load(cls, path): + """load from toml""" + with open(path, 'r', encoding='utf8') as file: + data = file.read() + content = toml.loads(data) + return content + @classmethod def _yaml_dump(cls, content, where): + """dump config file""" + is_yaml = where.lower().endswith(".yaml") + if is_yaml: + return cls.__yaml_dump(content, where) + is_toml = where.lower().endswith(".toml") + if is_toml: + return cls.__toml_dump(content, where) + raise YamlException("unsupported format") + + @classmethod + def __yaml_dump(cls, content, where): """dump to yaml""" data = yaml() data.default_flow_style = False @@ -1226,6 +1257,12 @@ class CfgYaml: data.typ = 'rt' data.dump(content, where) + @classmethod + def __toml_dump(cls, content, where): + """dump to toml""" + with open(where, 'w', encoding='utf8') as file: + toml.dump(content, file) + ######################################################## # templating ########################################################