|
| 1 | +Dotenv (.env) Parsing |
| 2 | +===================== |
| 3 | + |
| 4 | +``script_vars.load_vars_from_json`` ingests flat JSON, but nothing read the |
| 5 | +de-facto 12-factor ``.env`` file. This parses ``KEY=VALUE`` lines — honouring |
| 6 | +``export`` prefixes, single/double quoting, escapes, and inline comments — into |
| 7 | +a plain dict that can feed a config layer, with no ``python-dotenv`` dependency. |
| 8 | + |
| 9 | +Pure standard library (``re``); imports no ``PySide6``. ``parse_dotenv`` is a |
| 10 | +pure string-to-dict function, and the loader merges into a caller-supplied |
| 11 | +mapping rather than mutating ``os.environ``, so it is safe and deterministic. |
| 12 | + |
| 13 | +Headless API |
| 14 | +------------ |
| 15 | + |
| 16 | +.. code-block:: python |
| 17 | +
|
| 18 | + from je_auto_control import parse_dotenv, load_dotenv, dotenv_values, dump_dotenv |
| 19 | +
|
| 20 | + values = parse_dotenv('PLAIN=hello\nexport TOKEN="a\\nb" # comment') |
| 21 | + # {"PLAIN": "hello", "TOKEN": "a\nb"} |
| 22 | +
|
| 23 | + config = {} |
| 24 | + load_dotenv(".env", config) # merge file into a dict |
| 25 | + load_dotenv(".env.local", config, override=True) |
| 26 | +
|
| 27 | +``parse_dotenv`` skips blanks and ``#`` comment lines, strips an optional |
| 28 | +``export`` prefix, validates keys, and resolves values: single-quoted values |
| 29 | +are literal, double-quoted values process ``\n`` / ``\t`` / ``\\`` / ``\"`` |
| 30 | +escapes, and unquoted values drop a trailing `` #`` comment and surrounding |
| 31 | +whitespace. ``dotenv_values`` reads and parses a file; ``load_dotenv`` merges a |
| 32 | +file into an explicit ``env`` mapping (keeping existing keys unless |
| 33 | +``override``); ``dump_dotenv`` serialises a mapping back to ``.env`` text, |
| 34 | +quoting values that need it. |
| 35 | + |
| 36 | +Executor commands |
| 37 | +----------------- |
| 38 | + |
| 39 | +``AC_parse_dotenv`` parses ``text`` into ``{values}``; ``AC_load_dotenv`` reads |
| 40 | +a file at ``path`` into a fresh ``{values}`` dict. Both are exposed as MCP tools |
| 41 | +(``ac_parse_dotenv`` / ``ac_load_dotenv``) and as Script Builder commands under |
| 42 | +**Data**. |
0 commit comments