Browse Source

Use Path instead of strings, makes life easier

master
Raphael Roberts 5 years ago
parent
commit
8a06226c8c
  1. 19
      openwrt_backup/config.py

19
openwrt_backup/config.py

@ -1,24 +1,25 @@
import os
from pathlib import Path
import shutil import shutil
import appdirs as _appdirs import appdirs as _appdirs
import toml import toml
def ensure_directory_exists(path):
os.makedirs(path, exist_ok=True)
return path
def ensure_directory_exists(path: str):
p = Path(path)
p.mkdir(parents=True, exist_ok=True)
return p
appdirs = _appdirs.AppDirs("openwrt_backup") appdirs = _appdirs.AppDirs("openwrt_backup")
CONFIG_DIR = ensure_directory_exists(appdirs.user_config_dir) CONFIG_DIR = ensure_directory_exists(appdirs.user_config_dir)
CACHE_DIR = ensure_directory_exists(appdirs.user_cache_dir) CACHE_DIR = ensure_directory_exists(appdirs.user_cache_dir)
CONFIG_FILE_PATH = os.path.join(CONFIG_DIR, "config.toml")
PACKAGE_ROOT = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE_PATH = CONFIG_DIR / "config.toml"
PACKAGE_ROOT = Path(__file__).absolute().parent
if not os.path.exists(CONFIG_FILE_PATH):
shutil.copy(os.path.join(PACKAGE_ROOT, "config.toml"), CONFIG_FILE_PATH)
if not CONFIG_FILE_PATH.exists():
shutil.copy(PACKAGE_ROOT / "config.toml", CONFIG_FILE_PATH)
with open(CONFIG_FILE_PATH, "r") as config_file:
with CONFIG_FILE_PATH.open("r") as config_file:
CONFIG_DATA = toml.load(config_file) CONFIG_DATA = toml.load(config_file)
Loading…
Cancel
Save