feat: v0.1.0 #1

Merged
dmitry merged 12 commits from dev into main 2025-12-27 12:05:30 +03:00
2 changed files with 31 additions and 37 deletions
Showing only changes of commit d7636a6e09 - Show all commits

View File

@@ -122,8 +122,8 @@ scripts = [
reload = ['echo', 'this is my reload command'] reload = ['echo', 'this is my reload command']
######################### #########################
settings for some # settings for some
commonly used apps # commonly used apps
######################### #########################
[task.niri] [task.niri]

View File

@@ -1,9 +1,10 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os, sys, subprocess, tomllib import os, sys, subprocess, tomllib, argparse
COMMAND_LIST = 'list' COMMAND_LIST = 'list'
COMMAND_LOAD = 'load' COMMAND_LOAD = 'load'
COMMAND_SAVE = 'save' COMMAND_SAVE = 'save'
COMMAND_CTX_NAME = 'context_name'
COMMANDS_ALL = [COMMAND_LIST, COMMAND_LOAD, COMMAND_SAVE] COMMANDS_ALL = [COMMAND_LIST, COMMAND_LOAD, COMMAND_SAVE]
SECTION_GENERAL = 'general' SECTION_GENERAL = 'general'
@@ -27,15 +28,8 @@ ENV_CONTEXT_NAME = 'CONTEXT_NAME'
ENV_CONTEXT_SRC = 'CONTEXT_SRC' ENV_CONTEXT_SRC = 'CONTEXT_SRC'
ENV_CONTEXT_DST = 'CONTEXT_DST' ENV_CONTEXT_DST = 'CONTEXT_DST'
HELP_MESSAGE = f'''Usage: {os.path.basename(sys.argv[0])} <command> [context_name] APP_NAME = os.path.basename(sys.argv[0])
APP_DESC = 'A simple configuration switcher for various usage scenarios.'
Commands are:
{COMMAND_LIST} - to list installed contexts (from $HOME/.config/userctx)
{COMMAND_LOAD} - to load context "context_name"
{COMMAND_SAVE} - to save current configs of managed apps as context
"context_name" - must be a directory name present in $HOME/.config/userctx'''
HOME = os.path.expanduser('~') HOME = os.path.expanduser('~')
XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.join(HOME, '.config')) XDG_CONFIG_HOME = os.getenv('XDG_CONFIG_HOME', os.path.join(HOME, '.config'))
@@ -223,8 +217,8 @@ class ContextManager(object):
def __init__(self, config: ContextManagerConfig): def __init__(self, config: ContextManagerConfig):
self.config = config self.config = config
def get_contexts_list(self) -> str: def print_contexts_list(self) -> str:
return '\n'.join(self.config.get_contexts_list()) print('\n'.join(self.config.get_contexts_list()))
def apply_context(self, context_name: str) -> None: def apply_context(self, context_name: str) -> None:
print(f'loading and applying context: "{context_name}"') print(f'loading and applying context: "{context_name}"')
@@ -252,37 +246,37 @@ class ContextManager(object):
class Runner(object): class Runner(object):
def __init__(self):
self.cmd = sys.argv[1] if len(sys.argv) > 1 else ''
self.context_name = ' '.join(sys.argv[2:]) if len(sys.argv) > 2 else ''
self.config_file_path = os.path.join(XDG_CONFIG_HOME, 'userctx', 'config.toml')
def run(self) -> int: def run(self) -> int:
if self.cmd == '': p = argparse.ArgumentParser(prog=APP_NAME, description=APP_DESC)
print(HELP_MESSAGE) p.add_argument('-c', '--config', help='path to config file, default: %(default)s',
return 0 default=os.path.join(XDG_CONFIG_HOME, 'userctx', 'config.toml'))
if self.cmd not in COMMANDS_ALL: subp = p.add_subparsers(help='command to execute', dest='command')
print(f'invalid command. valid commands are: {", ".join(COMMANDS_ALL)}') loadp = subp.add_parser(COMMAND_LOAD, help='load context (provide name of context)')
loadp.add_argument(COMMAND_CTX_NAME, help='name of context to load')
listp = subp.add_parser(COMMAND_LIST, help='list available contexts')
savep = subp.add_parser(COMMAND_SAVE, help='save current context (provide name of context)')
savep.add_argument(COMMAND_CTX_NAME, help='name of context to save')
try: # just in case
args = p.parse_args()
except Exception as e:
print("error parsing command-line arguments:", e)
return 1 return 1
if self.cmd in [COMMAND_LOAD, COMMAND_SAVE] and self.context_name == '':
print('invalid command. please provide name of context to apply')
return 2
try: try:
config = ContextManagerConfig(self.config_file_path) config = ContextManagerConfig(args.config)
except Exception as e: except Exception as e:
print("error loading config:", e) print("error loading config:", e)
return 3 return 2
try: try:
ctxmgr = ContextManager(config) ctxmgr = ContextManager(config)
if self.cmd == COMMAND_LIST: if args.command == COMMAND_LIST:
print(ctxmgr.get_contexts_list()) ctxmgr.print_contexts_list()
elif self.cmd == COMMAND_LOAD: elif args.command == COMMAND_LOAD:
ctxmgr.apply_context(self.context_name) ctxmgr.apply_context(args.__dict__[COMMAND_CTX_NAME]) # guaranteed to be present by argparse
elif self.cmd == COMMAND_SAVE: elif args.command == COMMAND_SAVE:
print('save not implemented') raise Exception('"save" not implemented')
except Exception as e: except Exception as e:
print(f'error executing command "{self.cmd}":', e) print(f'error executing command:', e)
return 4 return 3
return 0 return 0