From 681e1f74da069c3869afecb9990e4d4a064a03a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferit=20Yi=C4=9Fit=20BALABAN?= Date: Sun, 3 Apr 2022 01:53:39 +0300 Subject: [PATCH] Read dotman settings at start up to SETTINGS dictionary --- dotman.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/dotman.py b/dotman.py index 1515719..a1475df 100755 --- a/dotman.py +++ b/dotman.py @@ -14,12 +14,14 @@ from termcolor import colored, cprint SETTINGS = { 'DOTFILES_REPOSITORY': '$HOME/repos/dotfiles', 'REMOTE_REPOSITORY': 'https://github.com/fybx/dotfiles', - 'LOCAL_CONFIG': '$HOME/.config' + 'LOCAL_CONFIG': '$HOME/.config', + 'SETUP_FILE': 'same-directory' } DOTFILES_REPOSITORY = SETTINGS['DOTFILES_REPOSITORY'] REMOTE_REPOSITORY = SETTINGS['REMOTE_REPOSITORY'] LOCAL_CONFIG = SETTINGS['LOCAL_CONFIG'] -VER = 'v1.6' +SETUP_FILE = SETTINGS['SETUP_FILE'] +VER = 'v1.7' help_message = f''' dotman {VER} dotfiles manager by fyb @@ -28,6 +30,7 @@ this have the same effect as calling dotman without any key. Keys: -i, --interactive Interactively backup or deploy dotfiles. Not supplying any key will result in interactive mode. +-s, --setup-dotman Interactively set variables (DOTFILES_REPOSITORY, LOCAL_CONFIG, etc.) for your dotman setup. -b, --backup Backup your dotfiles. Doesn't require user assistance but errors may occur. -d, --deploy Deploy your dotfiles. Doesn't require user assistance but errors may occur. -v, --version Shows the version and quits @@ -35,6 +38,22 @@ Keys: ''' +def read_file(path: str): + with open(path, 'r') as f: + content = f.readlines() + f.close() + return content + + +def read_setup(): + lines = read_file('setup.dtm') if SETUP_FILE == 'same-directory' else read_file(os.path.expandvars(SETUP_FILE)) + lines = [line.strip() for line in lines if not line.startswith('#') and line.replace(' ', '') != ''] + for line in lines: + key = line.split(':', 1)[0].rstrip() + value = line.split(':', 1)[1].lstrip() + SETTINGS[key] = value + + def remove_from_left_until_slash(text): iterator = len(text) - 1 buffer = '' @@ -114,8 +133,9 @@ def commit_then_push(): def main(): global DOTFILES_REPOSITORY global LOCAL_CONFIG - DOTFILES_REPOSITORY = os.path.expandvars(DOTFILES_REPOSITORY) - LOCAL_CONFIG = os.path.expandvars(LOCAL_CONFIG) + read_setup() + SETTINGS['DOTFILES_REPOSITORY'] = os.path.expandvars(DOTFILES_REPOSITORY) + SETTINGS['LOCAL_CONFIG'] = os.path.expandvars(LOCAL_CONFIG) remote_shortname = REMOTE_REPOSITORY.removeprefix("https://github.com/") local_repo_exists = os.path.exists(DOTFILES_REPOSITORY)