Skip to content

Commit a058295

Browse files
committed
_environ: update documentation
1 parent 47339d4 commit a058295

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

shared-bindings/_environ/__init__.c

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,47 +35,55 @@
3535
#include "py/runtime.h"
3636
#include "shared-bindings/_environ/__init__.h"
3737

38-
//| """Functions to manage environment variables from a .env file.
38+
//| """Functions to manage environment variables from a settings.toml file.
3939
//|
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/>`_.
4241
//|
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`.
4944
//|
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.
5064
//|
5165
//| File format example:
5266
//|
5367
//| .. code-block::
5468
//|
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\\"\\\\"
5874
//| # 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"
6877
//|
6978
//| """
7079
//|
7180
//| import typing
7281
//|
7382

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.
7785
//|
78-
//| Returns None if the key isn't found or doesn't have a value."""
86+
//| Returns None if the key isn't found"""
7987
//| ...
8088
//|
8189
STATIC mp_obj_t __environ_get_key(mp_obj_t path_in, mp_obj_t key_to_get_in) {

0 commit comments

Comments
 (0)