|
35 | 35 | #include "py/runtime.h"
|
36 | 36 | #include "shared-bindings/_environ/__init__.h"
|
37 | 37 |
|
38 |
| -//| """Functions to manage environment variables from a .env file. |
| 38 | +//| """Functions to manage environment variables from a settings.toml file. |
39 | 39 | //|
|
40 |
| -//| A subset of the CPython `_environ library <https://saurabh-kumar.com/python-_environ/>`_. It does |
41 |
| -//| not support variables or double quotes. |
| 40 | +//| This library can read a subset of `toml files <https://toml.io/>`_. |
42 | 41 | //|
|
43 |
| -//| Keys and values may be put in single quotes. |
44 |
| -//| ``\`` and ``'`` are escaped by ``\`` in single quotes. Newlines can occur in quotes for multiline values. |
45 |
| -//| Comments start with ``#`` and apply for the rest of the line. |
46 |
| -//| A ``#`` immediately following an ``=`` is part of the value, not the start of a comment, |
47 |
| -//| and a ``#`` embedded in a value without whitespace will be part of that value. |
48 |
| -//| This corresponds to how assignments and comments work in most Unix shells. |
| 42 | +//| It is recommended to not use this module directly. Instead, retrieve string |
| 43 | +//| values using `os.getenv`. |
49 | 44 | //|
|
| 45 | +//| Due to technical limitations it probably also accepts some files that are |
| 46 | +//| not valid TOML files; bugs of this nature are subject to change (i.e., be |
| 47 | +//| fixed) without the usual deprecation period for incompatible changes. |
| 48 | +//| |
| 49 | +//| Details of the format understood by this module: |
| 50 | +//| * the content is required to be in UTF-8 encoding |
| 51 | +//| * the supported data types are string and integer |
| 52 | +//| * only integers and basic strings are supported |
| 53 | +//| * only integers supported by strtol are supported (no 0o, no 0b, no |
| 54 | +//| underscores 1_000, 011 is 9, not 11) |
| 55 | +//| * In quoted strings, all standard eescape codes, including ``\\uxxxx`` |
| 56 | +//| and ``\\Uxxxxxxxx``, are supported |
| 57 | +//| * only bare keys are supported |
| 58 | +//| * duplicate keys are not diagnosed. |
| 59 | +//| * comments are supported |
| 60 | +//| * only values from the "root table" can be retrieved (parsing ends when it |
| 61 | +//| encounters a line starting with [) |
| 62 | +//| * due to technical limitations, the content of multi-line |
| 63 | +//| strings can erroneously be parsed as a value. |
50 | 64 | //|
|
51 | 65 | //| File format example:
|
52 | 66 | //|
|
53 | 67 | //| .. code-block::
|
54 | 68 | //|
|
55 |
| -//| key=value |
56 |
| -//| key2 = value2 |
57 |
| -//| 'key3' = 'value with spaces' |
| 69 | +//| str_key="Hello world" # with trailing comment |
| 70 | +//| int_key = 7 |
| 71 | +//| unicode_key="👨" |
| 72 | +//| unicode_key2="\\U0001f468" # same as above |
| 73 | +//| escape_codes="supported, including \\r\\n\\"\\\\" |
58 | 74 | //| # comment
|
59 |
| -//| key4 = value3 # comment 2 |
60 |
| -//| 'key5'=value4 |
61 |
| -//| key=value5 # overrides the first one |
62 |
| -//| multiline = 'hello |
63 |
| -//| world |
64 |
| -//| how are you?' |
65 |
| -//| # The #'s below will be included in the value. They do not start a comment. |
66 |
| -//| key6=#value |
67 |
| -//| key7=abc#def |
| 75 | +//| [subtable] |
| 76 | +//| subvalue="cannot retrieve this using _environ or getenv" |
68 | 77 | //|
|
69 | 78 | //| """
|
70 | 79 | //|
|
71 | 80 | //| import typing
|
72 | 81 | //|
|
73 | 82 |
|
74 |
| -//| def get_key(_environ_path: str, key_to_get: str) -> Optional[str]: |
75 |
| -//| """Get the value for the given key from the given .env file. If the key occurs multiple |
76 |
| -//| times in the file, then the last value will be returned. |
| 83 | +//| def get_key(_environ_path: str, key_to_get: str) -> Union[str, int, None]: |
| 84 | +//| """Get the value for the given key from the given .env file. |
77 | 85 | //|
|
78 |
| -//| Returns None if the key isn't found or doesn't have a value.""" |
| 86 | +//| Returns None if the key isn't found""" |
79 | 87 | //| ...
|
80 | 88 | //|
|
81 | 89 | STATIC mp_obj_t __environ_get_key(mp_obj_t path_in, mp_obj_t key_to_get_in) {
|
|
0 commit comments