8773a9cmodule-08: add ex2
3 files changed, +140, -0
author
mogi <git@mogiya.dev> on 23-05-2026 01:08 UTC+1
committer
mogi <git@mogiya.dev> on 23-05-2026 01:08 UTC+1
parent55c0670
  @@ -0,0 +1,5 @@
 1+MATRIX_MODE=development
 2+DATABASE_URL=mepretendingihaveadbtoconnectto
 3+API_KEY=imsocoolwithanapikeyandeverything
 4+LOG_LEVEL=DEBUG
 5+ZION_ENDPOINT=ah_yes_i_remember
  @@ -0,0 +1 @@
 1+.env
      @@ -0,0 +1,134 @@
     1+import os
     2+import sys
     3+from dotenv import load_dotenv
     4+
     5+
     6+REQUIRED_KEYS = [
     7+    "MATRIX_MODE",
     8+    "DATABASE_URL",
     9+    "API_KEY",
    10+    "LOG_LEVEL",
    11+    "ZION_ENDPOINT",
    12+]
    13+
    14+
    15+DEFAULTS = {
    16+    "MATRIX_MODE": "development",
    17+    "LOG_LEVEL": "DEBUG",
    18+    "ZION_ENDPOINT": "what_the_helly_is_zion",
    19+}
    20+
    21+
    22+VALID_MODES = {"development", "production"}
    23+
    24+
    25+def load_configuration() -> tuple[dict[str, str | None], bool]:
    26+    env_path = ".env"
    27+    env_loaded = os.path.isfile(env_path)
    28+    load_dotenv(env_path, override=False)
    29+
    30+    config = {key: os.getenv(key, DEFAULTS.get(key)) for key in REQUIRED_KEYS}
    31+    return config, env_loaded
    32+
    33+
    34+def find_missing(config: dict[str, str | None]) -> list[str]:
    35+    return [key for key, value in config.items() if value in (None, "")]
    36+
    37+
    38+def mask(value, mode):
    39+    if not value:
    40+        return "[NOT SET]"
    41+    if mode == "production":
    42+        return "******"
    43+    return value
    44+
    45+
    46+def report(
    47+    config: dict[str, str | None], env_loaded: bool, missing: list[str]
    48+) -> None:
    49+    mode = config.get("MATRIX_MODE") or "development"
    50+
    51+    print("ORACLE STATUS: Reading the Matrix...")
    52+    print()
    53+
    54+    if missing:
    55+        print("[WARN] Missing configuration values:")
    56+        for key in missing:
    57+            print(f"   - {key}")
    58+        print()
    59+        if mode == "production":
    60+            print("[ERROR] Production mode requires every variable to be set.")
    61+            sys.exit(1)
    62+
    63+    if mode not in VALID_MODES:
    64+        print(
    65+            f"[WARN] Unknown MATRIX_MODE '{mode}' "
    66+            "(expected 'development' or 'production')."
    67+        )
    68+        print()
    69+
    70+    print("Configuration loaded:")
    71+    print(f"   Mode: {mode}")
    72+
    73+    db_url = config.get("DATABASE_URL")
    74+    if db_url:
    75+        if mode == "development":
    76+            print(f"   Database: Connected to local instance -> {db_url}")
    77+        else:
    78+            print(
    79+                f"   Database: Connected to production instance "
    80+                f"-> {mask(db_url, mode)}"
    81+            )
    82+    else:
    83+        print("   Database: [NOT CONFIGURED]")
    84+
    85+    api_key = config.get("API_KEY")
    86+    if api_key:
    87+        print(f"   API Access: Authenticated (key={mask(api_key, mode)})")
    88+    else:
    89+        print("   API Access: [NO KEY PROVIDED]")
    90+
    91+    print(f"   Log Level: {config.get('LOG_LEVEL')}")
    92+
    93+    zion = config.get("ZION_ENDPOINT")
    94+    if zion:
    95+        print(f"   Zion Network: Online -> {zion}")
    96+    else:
    97+        print("   Zion Network: [OFFLINE]")
    98+
    99+    print()
   100+    print("Environment security check:")
   101+    print("   [OK] No hardcoded secrets detected")
   102+    if env_loaded:
   103+        print("   [OK] .env file properly configured")
   104+    else:
   105+        print("   [WARN] No .env file found (using environment/defaults)")
   106+    print("   [OK] Production overrides available")
   107+
   108+    print()
   109+    if mode == "development":
   110+        print("DEV MODE")
   111+    elif mode == "production":
   112+        print("PROD MODE")
   113+
   114+    print()
   115+    print("The Oracle sees all configurations.")
   116+
   117+
   118+def main() -> None:
   119+    try:
   120+        config, env_loaded = load_configuration()
   121+        missing = find_missing(config)
   122+        report(config, env_loaded, missing)
   123+    except SystemExit:
   124+        raise
   125+    except Exception as exc:
   126+        print(
   127+            f"[ERROR] The Oracle cannot read the Matrix: {exc}",
   128+            file=sys.stderr,
   129+        )
   130+        sys.exit(1)
   131+
   132+
   133+if __name__ == "__main__":
   134+    main()