def backup_csv(cli_args): log = MainLog(cli_args.verbose) timestamp = now_timestamp() backup_path = config.ConfigurationManager().get( config.SettingKeys.PATH_BACKUP, convert=Path) csv_file_path = config.ConfigurationManager().get( config.SettingKeys.PATH_ECONOMY_CSV, convert=Path) dest = backup_path / "databases" / f"{csv_file_path.name}_{timestamp}" try: shutil.copy(csv_file_path, dest) except PermissionError: shutil.copyfile(csv_file_path, dest) log.log(f"backed up: {csv_file_path} --> {dest}")
def download(item: dict, extr: bool = False, server=Server.WB1, limit=None): file_name = item["name"] repl_list = [ (" ", r"\ "), # TODO: use escaped for all? ("'", "*"), ("[", "*"), ("]", "*"), ("(", "*"), (")", "*") ] for char, repl in repl_list: file_name = file_name.replace(char, repl) print(f'downloading: {cstr(file_name, "orange")}') conf = config.ConfigurationManager() dl_dir = conf.get("path_download") limit_str = f"-l {limit} " if limit is not None else "" command = f'scp {limit_str}-r {server.value}:"~/files/{file_name}" "{dl_dir}"' local_command(command, hide_output=False) # only run extract if dest was default dl dir if extr: path_for_extract_cmd = Path(dl_dir) / file_name if not path_for_extract_cmd.exists(): return pfcs(f"running extract command on: g[{path_for_extract_cmd}]") extract.extract_item(path_for_extract_cmd)
def test_config_default(tmp_path): _file = tmp_path / "_settings.txt" with open(_file, "w") as settings_file: settings_file.write("\n".join( ["[default]", r"hue_ip=127.0.0.1", "", "[wb]", "password=abc123"])) cfg = config.ConfigurationManager(file_path=_file, verbose=False) cfg.set_config_file(_file) assert cfg.get(config.SettingKeys.WB_USERNAME) is None assert cfg.get(config.SettingKeys.WB_USERNAME, default="user") == "user"
def test_config_get(tmp_path): _file = tmp_path / "_settings.txt" with open(_file, "w") as settings_file: settings_file.write("\n".join( ["[default]", r"hue_ip=127.0.0.1", "", "[wb]", "password=abc123"])) cfg = config.ConfigurationManager(file_path=_file, verbose=False) cfg.set_config_file(_file) assert cfg.get(config.SettingKeys.IP_HUE) == "127.0.0.1" assert cfg.get(config.SettingKeys.WB_PASSWORD, section=config.SettingSection.WB) == "abc123"
def _determine_language(self): cfg = config.ConfigurationManager() path_txt = Path(cfg.get('path_scripts')) / 'txt' langs = {'en': {'points': 0}, 'sv': {'points': 0}} for lang in langs: with open(path_txt / f'sub_words_{lang}.txt', encoding='utf-8') as word_file: for word in word_file.read().split('\n'): langs[lang]['points'] += self.contents.count(f' {word} ') self.language = Language.English if langs['en']['points'] > langs[ 'sv']['points'] else Language.Swedish
def read_csv(cli_args) -> [list, None]: log = MainLog(True) csv_path = config.ConfigurationManager().get( config.SettingKeys.PATH_ECONOMY_CSV, convert=Path) if csv_path is None: log.log_warn(f"csv path not set in settings file!") return None if not csv_path.is_file(): log.log_warn(f"not such file: {csv_path}") return None with open(csv_path, mode="r", newline="") as csvfile: reader = csv.DictReader(csvfile) return [r for r in reader]
def update_csv(cli_args): log = MainLog(cli_args.verbose) date = datetime.now().strftime(DATE_FMT) csv_path = config.ConfigurationManager().get( config.SettingKeys.PATH_ECONOMY_CSV, convert=Path) fieldnames = [c.value for c in Columns] if not csv_path.is_file(): log.log(f"creating new file: {csv_path}") with open(csv_path, "w", newline="") as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() with open(csv_path, "a+", newline="") as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fieldnames) data_dict = {Columns.Date.value: date, Columns.Bank.value: cli_args.bank, Columns.Account.value: cli_args.account, Columns.Balance.value: cli_args.balance} writer.writerow(data_dict) log.log(f"wrote to {csv_path}:", info_str_line2=f"{data_dict}")
def wb_scp_torrents(server=Server.WB1): "send torrent files to wb watch dir" torrent_file_list = [] conf = config.ConfigurationManager() dl_paths = [ Path.home() / "mnt" / "downloads", Path(conf.get("path_download")) ] for dl_path in dl_paths: if not dl_path.exists(): continue torrent_file_list += dl_path.glob("**/*.torrent") for torrent_file in torrent_file_list: command = f'scp "{str(torrent_file)}" {server.value}:~/watch' pfcs(f"sending torrent: g[{torrent_file.name}]") if local_command(command, hide_output=True): try: torrent_file.unlink() # remove file pfcs(f"removed local torrent: o[{torrent_file.name}]") except: pfcs(f"failed to remove local torrent: e[{torrent_file.name}]")
#!/usr/bin/env python3 import argparse import os import sys import platform import config from printout import cstr, pcstr from run import local_command CONFIG = config.ConfigurationManager() NAS_IP = CONFIG.get('ds_ip') def is_ds_special_dir(dir_str): return dir_str.startswith('@') def get_ds_shares() -> list: return ['APPS', 'TV', 'FILM', 'MISC', 'BACKUP', 'DATA', 'DOCEDU'] def get_mount_dest() -> str: return CONFIG.get("ds_mount_path") def _print_error_invalid_share(string): print(f"invalid share: {cstr(string, 'red')}") def mount(ds_share):
import argparse import glob import os import shutil from pathlib import Path import config import run import util import util_movie import util_tv from printout import cstr, pfcs OPJ = os.path.join CFG = config.ConfigurationManager() def validate_path(path): if isinstance(path, str): path = Path(path) if not isinstance(path, Path): print(f"invalid path: {path}") return None path = path.resolve() if not path.exists(): print(f"path does not exist: {str(path)}") return None return path