def config(show, show_source, init_source, db_dir, set, get): '''CLI for managing configurations. There are many configurations kept in "template-config.yaml" in source code directory or "~/.virsorter" (when source code directory is not writable for user). This file can located with `virsorter config --show-source`. You can set the configurations with `virsorter config --set KEY=VAL`. Alternative, you can edit in the configuration file ("template-config.yaml") directly. ''' from virsorter.config import (TEMPLATE, SRC_CONFIG_DIR, USER_CONFIG_DIR, init_config_template) if init_source: if db_dir == None: mes = '--db-dir is required for --init-source' logging.critical(mes) sys.exit(1) else: if not os.path.isdir(db_dir): mes = (f'--db-dir {db_dir} does NOT exist yet; Make sure it ' 'is created later\n') logging.warning(mes) db_dir = os.path.abspath(db_dir) init_config_template(SRC_CONFIG_DIR, USER_CONFIG_DIR, db_dir) sys.exit(0) if not os.path.isfile(TEMPLATE): mes = ('config file "template-config.yaml" has not been ' 'initialized yet; Please use ' '`virsorter config --init-source --db-dir PATH` to initialize') logging.critical(mes) sys.exit(1) config = get_default_config() if show: YAML().dump(config, sys.stdout) sys.exit(0) if show_source: mes = f'config file path: {TEMPLATE}\n' sys.stdout.write(mes) sys.exit(0) if get != None: s = get lis = [var.strip() for var in s.split(',')] for var in lis: temp = config for i in var.split('.'): i = i.strip() try: temp = temp[i] except KeyError as e: mes = f'{i} is not a key in config file ({TEMPLATE})' logging.critical(mes) sys.exit(1) mes = f'{var}: {temp}\n' sys.stdout.write(mes) sys.exit(0) if set != None: s = set lis = [item.strip() for item in s.split(',')] for item in lis: temp = config var, val = item.split('=') var = var.strip() val = val.strip() keys = [key.strip() for key in var.split('.')] for i in range(len(keys)): if i == (len(keys) - 1): # stop at 2nd last key break key = keys[i] try: temp = temp[key] except KeyError as e: mes = f'{key} is not a key in config file ({TEMPLATE})' logging.critical(mes) sys.exit(1) last_key = keys[-1] try: old_val = temp[last_key] if isinstance(old_val, int): try: val = int(val) except ValueError as e: mes = f'{var} is supposed to be an integer' logging.critical(mes) sys.exit(1) elif isinstance(old_val, float): val = float(val) try: val = float(val) except ValueError as e: mes = f'{var} is supposed to be a float' logging.critical(mes) sys.exit(1) # only convert to abspath when the old one exists # since sometimes just want to set relative path elif os.path.exists(old_val): val = os.path.abspath(val) temp[last_key] = val except KeyError as e: mes = f'{last_key} is not a key in config file ({TEMPLATE})' logging.critical(mes) sys.exit(1) mes = f'{var}: {old_val} ==> {val}\n' sys.stdout.write(mes) with open(TEMPLATE, 'w') as fw: YAML().dump(config, fw) sys.exit(0)
def config(show, show_source, init_source, db_dir, set, get): '''CLI for managing configurations. There are many configurations kept in "template-config.yaml" in source code directory or "~/.virsorter" (when source code directory is not writable for user). This file can located with `virsorter config --show-source`. You can set the configurations with `virsorter config --set KEY=VAL`. Alternative, you can edit in the configuration file ("template-config.yaml") directly. ''' from virsorter.config import (TEMPLATE, SRC_CONFIG_DIR, USER_CONFIG_DIR, init_config_template) if init_source: if db_dir == None: mes = '--db-dir is required for --init-source' logging.critical(mes) sys.exit(1) else: init_config_template(SRC_CONFIG_DIR, USER_CONFIG_DIR, db_dir) sys.exit(0) if not os.path.isfile(TEMPLATE): mes = ('config file "template-config.yaml" has not been ' 'initialized yet; Please use ' '`virsorter config --init-source --db-dir PATH` to initialize') logging.critical(mes) sys.exit(1) config = get_default_config() if show: YAML().dump(config, sys.stdout) sys.exit(0) if show_source: mes = f'config file path: {TEMPLATE}\n' sys.stdout.write(mes) sys.exit(0) if get != None: s = get lis = [var.strip() for var in s.split(',')] for var in lis: temp = config for i in var.split('.'): i = i.strip() try: temp = temp[i] except KeyError as e: mes = f'{i} is not a key in config file ({TEMPLATE})' logging.critical(mes) sys.exit(1) mes = f'{var}: {temp}\n' sys.stdout.write(mes) sys.exit(0) if set != None: s = set lis = [item.strip() for item in s.split(',')] for item in lis: temp = config var, val = item.split('=') var = var.strip() val = val.strip() keys = [key.strip() for key in var.split('.')] for i in range(len(keys)): if i == (len(keys) - 1): # stop at 2nd last key break key = keys[i] try: temp = temp[key] except KeyError as e: mes = f'{key} is not a key in config file ({TEMPLATE})' logging.critical(mes) sys.exit(1) last_key = keys[-1] try: old_val = temp[last_key] temp[last_key] = val except KeyError as e: mes = f'{last_key} is not a key in config file ({TEMPLATE})' logging.critical(mes) sys.exit(1) mes = f'{var}: {old_val} ==> {val}\n' sys.stdout.write(mes) with open(TEMPLATE, 'w') as fw: YAML().dump(config, fw) sys.exit(0)