def test_dump_and_get_example(set_env, env: Env): set_env( { "VAR_1": "some string", "VAR_2": "4", "VAR_3": "4.5", "VAR_4": "True,False", "EXTRA_VAR": "dont read this", } ) env.str("VAR_1", default="some default") env.int("VAR_2") env.decimal("VAR_3") env.list("VAR_4", subcast=bool) env.json("NON_EXISTING", default={}) assert env.dump() == { "VAR_1": ParsedValue("some string", "str", True), "VAR_2": ParsedValue(4, "int", False), "VAR_3": ParsedValue(D("4.5"), "decimal", False), "VAR_4": ParsedValue([True, False], "list", False), "NON_EXISTING": ParsedValue({}, "json", True), } assert env.get_example() == ( "NON_EXISTING=Optional[json]\n" "VAR_1=Optional[str]\n" "VAR_2=int\n" "VAR_3=decimal\n" "VAR_4=list\n" )
def test_prefix(set_env, env: Env): set_env({"PREFIX_STRING": "some string"}) assert env.str("PREFIX_STRING") == "some string" env.prefix.append("PREFIX_") assert env.str("STRING") == "some string" env.prefix.pop() assert env.str("PREFIX_STRING") == "some string"
def test_prefixed(set_env, env: Env): set_env({"PF1_PF2_STRING": "some string"}) assert env.str("PF1_PF2_STRING") == "some string" with env.prefixed("PF1_"): assert env.str("PF2_STRING") == "some string" with env.prefixed("PF2_"): assert env.str("STRING") == "some string" assert env.str("PF2_STRING") == "some string" assert env.str("PF1_PF2_STRING") == "some string"
def main() -> None: def shutdown(sig: int, frame_type: FrameType | None) -> None: if conn.websocket is not None and conn.loop is not None: for task in asyncio.all_tasks(conn.loop): task.cancel() coro = conn.websocket.close() asyncio.run_coroutine_threadsafe(coro, conn.loop) env = Env() env.read_env() host = env.str("SHOWDOWN_HOST") port = env.int("SHOWDOWN_PORT") protocol = "wss" if port == 443 else "ws" url = f"{protocol}://{host}:{port}/showdown/websocket" conn = Connection( url=url, username=env.str("USERNAME"), password=env.str("PASSWORD"), avatar=env.str("AVATAR", default=""), statustext=env.str("STATUSTEXT", default=""), rooms=env.list("ROOMS", default=[]), main_room=env.str("MAIN_ROOM"), command_character=env.str("COMMAND_CHARACTER"), administrators=env.list("ADMINISTRATORS", default=[]), webhooks=env.json("WEBHOOKS", default={}), ) signal.signal(signal.SIGINT, shutdown) conn.open_connection()
def test_read_env(env: Env): assert env.read_env("./tests/.env.test") assert env.str("A_STRING") == "blabla"