Single source of truth for the DEBUG flag. Checked here, the lowest-level module, so every other module can share one definition (env var first, then env_file) without circular imports. Accepts True/TRUE and optionally-quoted values so a stray capital doesn't half-enable debug acros
()
| 6 | |
| 7 | |
| 8 | def is_debug() -> bool: |
| 9 | """Single source of truth for the DEBUG flag. |
| 10 | |
| 11 | Checked here, the lowest-level module, so every other module can share one |
| 12 | definition (env var first, then env_file) without circular imports. Accepts |
| 13 | True/TRUE and optionally-quoted values so a stray capital doesn't half-enable |
| 14 | debug across the stack. |
| 15 | """ |
| 16 | # 1. Check environment |
| 17 | if os.environ.get("DEBUG", "").lower() == "true": |
| 18 | return True |
| 19 | |
| 20 | # 2. Check env_file manually |
| 21 | if os.path.exists(str(ENV_FILE)): |
| 22 | try: |
| 23 | with open(str(ENV_FILE), "r", encoding="utf-8") as f: |
| 24 | for line in f: |
| 25 | # Robust check for DEBUG=true (handles spaces, optional quotes, etc.) |
| 26 | line = line.strip() |
| 27 | if not line or line.startswith("#"): |
| 28 | continue |
| 29 | if "DEBUG" in line and "=" in line: |
| 30 | key, val = [p.strip() for p in line.split("=", 1)] |
| 31 | if key == "DEBUG": |
| 32 | return val.strip("'\"").lower() == "true" |
| 33 | except Exception: |
| 34 | # If the env file cannot be read for any reason, fail closed and |
| 35 | # leave DEBUG disabled by returning False below. |
| 36 | pass |
| 37 | return False |
| 38 | |
| 39 | |
| 40 | # Setup handlers |
no outgoing calls
no test coverage detected