Add config updating based on version

Signed-off-by: nathom <nathanthomas707@gmail.com>
This commit is contained in:
nathom 2021-06-19 19:36:49 -07:00
parent f9556153df
commit 4c7db94a3b

View file

@ -34,7 +34,7 @@ class Config:
default_config_path = os.path.join(os.path.dirname(__file__), "config.toml") default_config_path = os.path.join(os.path.dirname(__file__), "config.toml")
with open(default_config_path) as cfg: with open(default_config_path) as cfg:
defaults: Dict[str, Any] = tomlkit.parse(cfg.read()) defaults: Dict[str, Any] = tomlkit.parse(cfg.read().strip())
def __init__(self, path: str = None): def __init__(self, path: str = None):
"""Create a Config object with state. """Create a Config object with state.
@ -53,15 +53,15 @@ class Config:
else: else:
self._path = path self._path = path
if not os.path.isfile(self._path): if os.path.isfile(self._path):
self.load()
if self.file["misc"]["version"] != self.defaults["misc"]["version"]:
click.secho("Updating config file to new version. Some settings may be lost.", fg="yellow")
self.update()
self.load()
else:
logger.debug("Creating toml config file at '%s'", self._path) logger.debug("Creating toml config file at '%s'", self._path)
shutil.copy(self.default_config_path, CONFIG_PATH) shutil.copy(self.default_config_path, CONFIG_PATH)
else:
self.load()
if self.file["misc"]["version"] != __version__:
click.secho("Updating config file to new version...", fg="green")
self.reset()
self.load()
def update(self): def update(self):
"""Reset the config file except for credentials.""" """Reset the config file except for credentials."""
@ -86,7 +86,7 @@ class Config:
def load(self): def load(self):
"""Load infomation from the config files, making a deepcopy.""" """Load infomation from the config files, making a deepcopy."""
with open(self._path) as cfg: with open(self._path) as cfg:
for k, v in tomlkit.loads(cfg.read()).items(): for k, v in tomlkit.loads(cfg.read().strip()).items():
self.file[k] = v self.file[k] = v
if hasattr(v, "copy"): if hasattr(v, "copy"):
self.session[k] = v.copy() self.session[k] = v.copy()