def main(): """Main executable. CoBib's main function used to parse optional keyword arguments and subcommands. """ if len(sys.argv) > 1 and any([a[0] == '_' for a in sys.argv]): # zsh helper function called zsh_main() sys.exit() # initialize logging log_to_stream() subcommands = [cmd.split(':')[0] for cmd in zsh_helper.list_commands()] parser = argparse.ArgumentParser(prog='CoBib', description=""" Cobib input arguments. If no arguments are given, the TUI will start as a default. """) parser.add_argument("--version", action="version", version="%(prog)s v{}".format(__version__)) parser.add_argument('--verbose', '-v', action='count', default=0) parser.add_argument("-l", "--logfile", type=argparse.FileType('w'), help="Alternative log file") parser.add_argument("-c", "--config", type=argparse.FileType('r'), help="Alternative config file") parser.add_argument('command', help="subcommand to be called", choices=subcommands, nargs='?') parser.add_argument('args', nargs=argparse.REMAINDER) args = parser.parse_args() if args.logfile: LOGGER.info('Switching to FileHandler logger in %s', args.logfile.name) log_to_file('DEBUG' if args.verbose > 1 else 'INFO', logfile=args.logfile.name) # set logging verbosity level if args.verbose == 1: logging.getLogger().setLevel(logging.INFO) LOGGER.info('Logging level set to INFO.') elif args.verbose > 1: logging.getLogger().setLevel(logging.DEBUG) LOGGER.info('Logging level set to DEBUG.') CONFIG.set_config(args.config) try: CONFIG.validate() except RuntimeError as exc: LOGGER.error(exc) sys.exit(1) if args.command == 'init': # the database file may not exist yet, thus we ensure to execute the command before trying # to read the database file subcmd = getattr(commands, 'InitCommand')() subcmd.execute(args.args) return read_database() if not args.command: if args.logfile is None: LOGGER.info('Switching to FileHandler logger in %s', '/tmp/cobib.log') log_to_file('DEBUG' if args.verbose > 1 else 'INFO') else: LOGGER.info( 'Already logging to %s. NOT switching to "/tmp/cobib.log"', args.logfile) tui() else: subcmd = getattr(commands, args.command.title() + 'Command')() subcmd.execute(args.args)
def test_valid_tui_colors(setup, color): """Test curses color specification validation.""" with pytest.raises(RuntimeError) as exc_info: CONFIG.config.get('COLORS', {})[color] = 'test' CONFIG.validate() assert str(exc_info.value) == 'Unknown color specification: test'
def test_missing_section(setup, section): """Test raised RuntimeError for missing configuration section.""" with pytest.raises(RuntimeError) as exc_info: del CONFIG.config[section] CONFIG.validate() assert section in str(exc_info.value)
def test_database_section(setup, section, field): """Test raised RuntimeError for missing config fields.""" with pytest.raises(RuntimeError) as exc_info: del CONFIG.config.get(section, {})[field] CONFIG.validate() assert f'{section}/{field}' in str(exc_info.value)
def test_base_config(setup): """Test the initial configuration passes all validation checks.""" CONFIG.validate()