def test_modules(self): module = communication.module("flask") self.assertEqual(module, httprequests) module = communication.module("none") self.assertEqual(module, localserver)
def run(command=None, config_filepath=None, verbose=False, **cl_kwargs): """High-level API entry point, useful for scripts. Run 'command' passing 'cl_kwargs' according to what the command line interface accepts (consult help via `financeager [command] --help`), e.g. {"command": "add", "name": "champagne", "value": "99"}. All kwargs are passed to 'communication.run()'. 'config' specifies the path to a custom config file (optional). If 'verbose' is set, debug level log messages are printed to the terminal. :return: UNIX return code (zero for success, non-zero otherwise) """ if verbose: make_log_stream_handler_verbose() exit_code = FAILURE if config_filepath is None and os.path.exists(financeager.CONFIG_FILEPATH): config_filepath = financeager.CONFIG_FILEPATH try: configuration = Configuration(filepath=config_filepath) except InvalidConfigError as e: logger.error("Invalid configuration: {}".format(e)) return FAILURE backend_name = configuration.get_option("SERVICE", "name") communication_module = communication.module(backend_name) proxy_kwargs = {} if backend_name == "flask": init_logger("urllib3") proxy_kwargs["http_config"] = configuration.get_option("SERVICE:FLASK") else: # 'none' is the only other option proxy_kwargs["data_dir"] = financeager.DATA_DIR # Indicate whether to store request offline, if failed store_offline = False proxy = communication_module.proxy(**proxy_kwargs) try: logger.info( communication.run(proxy, command, default_category=configuration.get_option( "FRONTEND", "default_category"), date_format=configuration.get_option( "FRONTEND", "date_format"), **cl_kwargs)) if offline.recover(proxy): logger.info("Recovered offline backup.") exit_code = SUCCESS except OfflineRecoveryError: logger.error("Offline backup recovery failed!") except (PreprocessingError, InvalidRequest) as e: # Command is erroneous and hence not stored offline logger.error(e) except CommunicationError as e: logger.error(e) store_offline = True except Exception: logger.exception("Unexpected error") store_offline = True if store_offline and offline.add(command, **cl_kwargs): logger.info("Stored '{}' request in offline backup.".format(command)) if backend_name == "none": communication.run(proxy, "stop") return exit_code