Read dotman settings at start up to SETTINGS dictionary

This commit is contained in:
Ferit Yiğit BALABAN 2022-04-03 01:53:39 +03:00
parent f17bb80689
commit 681e1f74da
No known key found for this signature in database
GPG Key ID: BB8494BA65A90842

View File

@ -14,12 +14,14 @@ from termcolor import colored, cprint
SETTINGS = { SETTINGS = {
'DOTFILES_REPOSITORY': '$HOME/repos/dotfiles', 'DOTFILES_REPOSITORY': '$HOME/repos/dotfiles',
'REMOTE_REPOSITORY': 'https://github.com/fybx/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'] DOTFILES_REPOSITORY = SETTINGS['DOTFILES_REPOSITORY']
REMOTE_REPOSITORY = SETTINGS['REMOTE_REPOSITORY'] REMOTE_REPOSITORY = SETTINGS['REMOTE_REPOSITORY']
LOCAL_CONFIG = SETTINGS['LOCAL_CONFIG'] LOCAL_CONFIG = SETTINGS['LOCAL_CONFIG']
VER = 'v1.6' SETUP_FILE = SETTINGS['SETUP_FILE']
VER = 'v1.7'
help_message = f''' help_message = f'''
dotman {VER} dotfiles manager by fyb dotman {VER} dotfiles manager by fyb
@ -28,6 +30,7 @@ this have the same effect as calling dotman without any key.
Keys: Keys:
-i, --interactive Interactively backup or deploy dotfiles. Not supplying any key will result in interactive mode. -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. -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. -d, --deploy Deploy your dotfiles. Doesn't require user assistance but errors may occur.
-v, --version Shows the version and quits -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): def remove_from_left_until_slash(text):
iterator = len(text) - 1 iterator = len(text) - 1
buffer = '' buffer = ''
@ -114,8 +133,9 @@ def commit_then_push():
def main(): def main():
global DOTFILES_REPOSITORY global DOTFILES_REPOSITORY
global LOCAL_CONFIG global LOCAL_CONFIG
DOTFILES_REPOSITORY = os.path.expandvars(DOTFILES_REPOSITORY) read_setup()
LOCAL_CONFIG = os.path.expandvars(LOCAL_CONFIG) SETTINGS['DOTFILES_REPOSITORY'] = os.path.expandvars(DOTFILES_REPOSITORY)
SETTINGS['LOCAL_CONFIG'] = os.path.expandvars(LOCAL_CONFIG)
remote_shortname = REMOTE_REPOSITORY.removeprefix("https://github.com/") remote_shortname = REMOTE_REPOSITORY.removeprefix("https://github.com/")
local_repo_exists = os.path.exists(DOTFILES_REPOSITORY) local_repo_exists = os.path.exists(DOTFILES_REPOSITORY)