1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-11 03:14:15 +00:00

properly handle toml

This commit is contained in:
deadc0de6
2022-03-12 15:40:40 +01:00
committed by deadc0de
parent 23a9a1f971
commit da094e3181

View File

@@ -118,6 +118,8 @@ class CfgYaml:
self._reloading = reloading self._reloading = reloading
self._debug = debug self._debug = debug
self._log = Logger(debug=self._debug) self._log = Logger(debug=self._debug)
# config format
self._config_format = 'yaml'
# config needs to be written # config needs to be written
self._dirty = False self._dirty = False
# indicates the config has been updated # indicates the config has been updated
@@ -468,7 +470,7 @@ class CfgYaml:
self._dbg('saving to {}'.format(self._path)) self._dbg('saving to {}'.format(self._path))
try: try:
with open(self._path, 'w', encoding='utf8') as file: with open(self._path, 'w', encoding='utf8') as file:
self._yaml_dump(content, file) self._yaml_dump(content, file, fmt=self._config_format)
except Exception as exc: except Exception as exc:
self._log.err(exc) self._log.err(exc)
err = 'error saving config: {}'.format(self._path) err = 'error saving config: {}'.format(self._path)
@@ -486,7 +488,7 @@ class CfgYaml:
"""dump the config dictionary""" """dump the config dictionary"""
output = io.StringIO() output = io.StringIO()
content = self._prepare_to_save(self._yaml_dict.copy()) content = self._prepare_to_save(self._yaml_dict.copy())
self._yaml_dump(content, output) self._yaml_dump(content, output, fmt=self._config_format)
return output.getvalue() return output.getvalue()
######################################################## ########################################################
@@ -1173,12 +1175,14 @@ class CfgYaml:
self._dbg(cfg.rstrip()) self._dbg(cfg.rstrip())
self._dbg('----------end:{}----------'.format(path)) self._dbg('----------end:{}----------'.format(path))
try: try:
content = self._yaml_load(path) content, fmt = self._yaml_load(path)
self._config_format = fmt
except Exception as exc: except Exception as exc:
self._log.err(exc) self._log.err(exc)
err = 'config yaml error: {}'.format(path) err = 'config yaml error: {}'.format(path)
raise YamlException(err) from exc raise YamlException(err) from exc
if self._debug:
self._dbg('format: {}'.format(self._config_format))
return content return content
def _validate(self, yamldict): def _validate(self, yamldict):
@@ -1214,10 +1218,10 @@ class CfgYaml:
"""load config file""" """load config file"""
is_yaml = path.lower().endswith(".yaml") is_yaml = path.lower().endswith(".yaml")
if is_yaml: if is_yaml:
return cls.__yaml_load(path) return cls.__yaml_load(path), 'yaml'
is_toml = path.lower().endswith(".toml") is_toml = path.lower().endswith(".toml")
if is_toml: if is_toml:
return cls.__toml_load(path) return cls.__toml_load(path), 'toml'
raise YamlException("unsupported format") raise YamlException("unsupported format")
@classmethod @classmethod
@@ -1235,33 +1239,36 @@ class CfgYaml:
with open(path, 'r', encoding='utf8') as file: with open(path, 'r', encoding='utf8') as file:
data = file.read() data = file.read()
content = toml.loads(data) content = toml.loads(data)
# handle inexistent dotfiles/profiles
# since toml doesn't have a nul/nil/null/none
if cls.key_dotfiles not in content:
content[cls.key_dotfiles] = None
if cls.key_profiles not in content:
content[cls.key_profiles] = None
return content return content
@classmethod @classmethod
def _yaml_dump(cls, content, where): def _yaml_dump(cls, content, file, fmt='yaml'):
"""dump config file""" """dump config file"""
is_yaml = where.lower().endswith(".yaml") if 'toml':
if is_yaml: return cls.__toml_dump(content, file)
return cls.__yaml_dump(content, where) if 'yaml':
is_toml = where.lower().endswith(".toml") return cls.__yaml_dump(content, file)
if is_toml: raise YamlException("unsupported format")
return cls.__toml_dump(content, where)
raise YamlException("unsupported format")
@classmethod @classmethod
def __yaml_dump(cls, content, where): def __yaml_dump(cls, content, file):
"""dump to yaml""" """dump to yaml"""
data = yaml() data = yaml()
data.default_flow_style = False data.default_flow_style = False
data.indent = 2 data.indent = 2
data.typ = 'rt' data.typ = 'rt'
data.dump(content, where) data.dump(content, file)
@classmethod @classmethod
def __toml_dump(cls, content, where): def __toml_dump(cls, content, file):
"""dump to toml""" """dump to toml"""
with open(where, 'w', encoding='utf8') as file: toml.dump(content, file)
toml.dump(content, file)
######################################################## ########################################################
# templating # templating
@@ -1697,7 +1704,7 @@ class CfgYaml:
if self._debug: if self._debug:
self._dbg('saving uservariables values to {}'.format(path)) self._dbg('saving uservariables values to {}'.format(path))
with open(path, 'w', encoding='utf8') as file: with open(path, 'w', encoding='utf8') as file:
self._yaml_dump(content, file) self._yaml_dump(content, file, fmt=self._config_format)
except Exception as exc: except Exception as exc:
# self._log.err(exc) # self._log.err(exc)
err = 'error saving uservariables to {}'.format(path) err = 'error saving uservariables to {}'.format(path)