def generate_config(args: argparse.Namespace) -> configuration.Config: """ Combine parsed command line arguments with configuration from a config file. """ argscfg = { "gPort": args.g_port, "pPort": args.p_port, "xPort": args.x_port, "app_mode": args.app_mode } return {**configuration.from_file(args.config_file), **argscfg}
def test_load_from_file(): cfg = config.from_file(Path("test") / Path("data") / Path("config.json")) # Check unchanged defaults are the same assert not cfg["app_mode"] assert not cfg["debug"] assert cfg["pPort"] == 55851 assert cfg["xPort"] == 55852 assert cfg["gPort"] == 55853 assert len( cfg["dataHashSecret"]) == 64 and not cfg["dataHashSecret"].isspace() assert PureWindowsPath( cfg["data"]) == PureWindowsPath("C:/User/SCope data") # Check new values assert cfg["extra setting"] == "value"
""" Main entry point to the SCope API implementation. """ import os from fastapi import FastAPI from scopeserver.config import from_file from scopeserver import message_of_the_day, SCopeServer scope_api = FastAPI() CONFIG = from_file(os.environ.get("SCOPE_CONFIG")) @scope_api.get("/echo/{value}") def echo(value): """ A testing HTTP API endpoint that echoes the request parameter. """ return {"echo": value} scope_legacy = SCopeServer(CONFIG) @scope_api.on_event("startup") def startup(): """ Start the legacy server. """ message_of_the_day(str(CONFIG["data"])) scope_legacy.start_scope_server()
def test_load_malformed_json(): with pytest.raises(JSONDecodeError): config.from_file( Path("test") / Path("data") / Path("malformed_config.json"))
def test_load_from_nonexistant_file(): with pytest.raises(FileNotFoundError): config.from_file("this_file_does_not_exist.txt")