Skip to content

Commit ef2bfdb

Browse files
committed
dotenv becomes settings.toml
1 parent 748cb92 commit ef2bfdb

File tree

31 files changed

+642
-566
lines changed

31 files changed

+642
-566
lines changed

devices/ble_hci/common-hal/_bleio/Adapter.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
#include "shared-bindings/_bleio/ScanEntry.h"
5050
#include "shared-bindings/time/__init__.h"
5151

52-
#if CIRCUITPY_DOTENV
53-
#include "shared-module/dotenv/__init__.h"
52+
#if CIRCUITPY_ENVIRON
53+
#include "shared-bindings/_environ/__init__.h"
5454
#endif
5555

5656
#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
@@ -284,15 +284,15 @@ char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0
284284
STATIC void bleio_adapter_hci_init(bleio_adapter_obj_t *self) {
285285
mp_int_t name_len = 0;
286286

287-
#if CIRCUITPY_DOTENV
288-
char ble_name[32];
289-
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
290-
if (name_len > 0) {
291-
self->name = mp_obj_new_str(ble_name, (size_t)name_len);
287+
#if CIRCUITPY_ENVIRON
288+
mp_obj_t name = common_hal__environ_get_key("CIRCUITPY_BLE_NAME");
289+
if (name != mp_const_none) {
290+
mp_arg_validate_type_string(name, MP_QSTR_CIRCUITPY_BLE_NAME);
291+
self->name = name;
292292
}
293293
#endif
294294

295-
if (name_len <= 0) {
295+
if (!self->name) {
296296
name_len = sizeof(default_ble_name);
297297
bt_addr_t addr;
298298
hci_check_error(hci_read_bd_addr(&addr));

docs/environment.rst

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,36 @@ variables are commonly used to store "secrets" such as Wi-Fi passwords and API
66
keys. This method *does not* make them secure. It only separates them from the
77
code.
88

9-
CircuitPython supports these by mimicking the `dotenv <https://github.com/theskumar/python-dotenv>`_
10-
CPython library. Other languages such as Javascript, PHP and Ruby also have
11-
dotenv libraries.
9+
CircuitPython supports these by parsing a subset of the `toml <https://toml.io/>`_ file format internally.
1210

13-
These libraries store environment variables in a ``.env`` file. Here is a simple
14-
example:
11+
Here is a simple example:
1512

1613
.. code-block:: bash
1714
18-
KEY1='value1'
15+
KEY1="value1"
1916
# Comment
20-
KEY2='value2
21-
is multiple lines'
17+
KEY2="value2\ncontains a newline"
2218
23-
CircuitPython uses the ``.env`` at the drive root (no folder) as the environment.
19+
[SECTION] # Only values in the "root table" are parsed
20+
SECTION_VALUE = ... # so this value cannot be seen by getenv
21+
22+
CircuitPython uses the ``settings.toml`` at the drive root (no folder) as the environment.
2423
User code can access the values from the file using `os.getenv()`. It is
2524
recommended to save any values used repeatedly in a variable because `os.getenv()`
26-
will parse the ``/.env`` on every access.
25+
will parse the ``settings.toml`` file contents on every access.
26+
27+
Details of the toml language subset
28+
-----------------------------------
29+
30+
* The content is required to be in UTF-8 encoding
31+
* The supported data types are string and integer
32+
* Only basic strings are supported, not triple-quoted strings
33+
* Only integers supported by strtol. (no 0o, no 0b, no underscores 1_000, 011
34+
is 9, not 11)
35+
* Only bare keys are supported
36+
* Duplicate keys are not diagnosed.
37+
* Comments are supported
38+
* Only values from the "root table" can be retrieved
2739

2840
CircuitPython behavior
2941
----------------------

docs/workflows.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ connection, the central device can discover two default services. One for file t
4646
CircuitPython specifically that includes serial characteristics.
4747

4848
To change the default BLE advertising name without (or before) running user code, the desired name
49-
can be put in the `/.env` file. The key is `CIRCUITPY_BLE_NAME`. It's limited to approximately
49+
can be put in the `settings.toml` file. The key is `CIRCUITPY_BLE_NAME`. It's limited to approximately
5050
30 characters depending on the port's settings and will be truncated if longer.
5151

5252
### File Transfer API
@@ -69,21 +69,21 @@ Read-only characteristic that returns the UTF-8 encoded version string.
6969

7070
## Web
7171

72-
The web workflow is depends on adding Wi-Fi credentials into the `/.env` file. The keys are
72+
The web workflow is depends on adding Wi-Fi credentials into the `settings.toml` file. The keys are
7373
`CIRCUITPY_WIFI_SSID` and `CIRCUITPY_WIFI_PASSWORD`. Once these are defined, CircuitPython will
7474
automatically connect to the network and start the webserver used for the workflow. The webserver
7575
is on port 80 unless overridden by `CIRCUITPY_WEB_API_PORT`. It also enables MDNS.
7676

77-
Here is an example `/.env`:
77+
Here is an example `/settings.toml`:
7878

7979
```bash
8080
# To auto-connect to Wi-Fi
81-
CIRCUITPY_WIFI_SSID='scottswifi'
82-
CIRCUITPY_WIFI_PASSWORD='secretpassword'
81+
CIRCUITPY_WIFI_SSID="scottswifi"
82+
CIRCUITPY_WIFI_PASSWORD="secretpassword"
8383

8484
# To enable modifying files from the web. Change this too!
8585
# Leave the User field blank in the browser.
86-
CIRCUITPY_WEB_API_PASSWORD='passw0rd'
86+
CIRCUITPY_WEB_API_PASSWORD="passw0rd"
8787

8888
CIRCUITPY_WEB_API_PORT=80
8989
```
@@ -124,7 +124,7 @@ All file system related APIs are protected by HTTP basic authentication. It is *
124124
hopefully prevent some griefing in shared settings. The password is sent unencrypted so do not reuse
125125
a password with something important. The user field is left blank.
126126

127-
The password is taken from `/.env` with the key `CIRCUITPY_WEB_API_PASSWORD`. If this is unset, the
127+
The password is taken from `settings.toml` with the key `CIRCUITPY_WEB_API_PASSWORD`. If this is unset, the
128128
server will respond with `403 Forbidden`. When a password is set, but not provided in a request, it
129129
will respond `401 Unauthorized`.
130130

locale/circuitpython.pot

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,10 @@ msgstr ""
10021002
msgid "File exists"
10031003
msgstr ""
10041004

1005+
#: shared-module/dotenv/__init__.c
1006+
msgid "File not found"
1007+
msgstr ""
1008+
10051009
#: ports/atmel-samd/common-hal/canio/Listener.c
10061010
#: ports/espressif/common-hal/canio/Listener.c
10071011
#: ports/stm/common-hal/canio/Listener.c
@@ -1184,6 +1188,7 @@ msgid "Internal define error"
11841188
msgstr ""
11851189

11861190
#: ports/espressif/common-hal/paralleldisplay/ParallelBus.c
1191+
#: shared-module/dotenv/__init__.c
11871192
msgid "Internal error"
11881193
msgstr ""
11891194

@@ -1230,6 +1235,11 @@ msgstr ""
12301235
msgid "Invalid bits per value"
12311236
msgstr ""
12321237

1238+
#: shared-module/dotenv/__init__.c
1239+
#, c-format
1240+
msgid "Invalid byte %.*s"
1241+
msgstr ""
1242+
12331243
#: ports/atmel-samd/common-hal/imagecapture/ParallelImageCapture.c
12341244
#, c-format
12351245
msgid "Invalid data_pins[%d]"
@@ -1260,10 +1270,18 @@ msgstr ""
12601270
msgid "Invalid state"
12611271
msgstr ""
12621272

1273+
#: shared-module/dotenv/__init__.c
1274+
msgid "Invalid unicode escape"
1275+
msgstr ""
1276+
12631277
#: shared-bindings/aesio/aes.c
12641278
msgid "Key must be 16, 24, or 32 bytes long"
12651279
msgstr ""
12661280

1281+
#: shared-module/dotenv/__init__.c
1282+
msgid "Key not found"
1283+
msgstr ""
1284+
12671285
#: shared-module/is31fl3741/FrameBuffer.c
12681286
msgid "LED mappings must match display size"
12691287
msgstr ""

ports/espressif/bindings/esp32_camera/Camera.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
//| """
6464
//| Configure and initialize a camera with the given properties
6565
//|
66-
//| This driver requires that the ``CIRCUITPY_RESERVED_PSRAM`` in ``/.env`` be large enough to hold the camera frambuffer(s). Generally, boards with built-in cameras will have a default setting that is large enough. If the constructor raises a MemoryError or an IDFError, this probably indicates the setting is too small and should be increased.
66+
//| This driver requires that the ``CIRCUITPY_RESERVED_PSRAM`` in ``settings.toml`` be large enough to hold the camera frambuffer(s). Generally, boards with built-in cameras will have a default setting that is large enough. If the constructor raises a MemoryError or an IDFError, this probably indicates the setting is too small and should be increased.
6767
//|
6868
//|
6969
//| .. important::

ports/espressif/bindings/espidf/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ STATIC mp_obj_t espidf_get_total_psram(void) {
132132
MP_DEFINE_CONST_FUN_OBJ_0(espidf_get_total_psram_obj, espidf_get_total_psram);
133133

134134
//| def get_reserved_psram() -> int:
135-
//| """Returns number of bytes of psram reserved for use by esp-idf, either a board-specific default value or the value defined in ``/.env``."""
135+
//| """Returns number of bytes of psram reserved for use by esp-idf, either a board-specific default value or the value defined in ``settings.toml``."""
136136
//|
137137
STATIC mp_obj_t espidf_get_reserved_psram(void) {
138138
return MP_OBJ_NEW_SMALL_INT(common_hal_espidf_get_reserved_psram());

ports/espressif/common-hal/_bleio/Adapter.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
#include "esp_bt.h"
6060
#include "esp_nimble_hci.h"
6161

62-
#if CIRCUITPY_DOTENV
63-
#include "shared-module/dotenv/__init__.h"
62+
#if CIRCUITPY_ENVIRON
63+
#include "shared-module/_environ/__init__.h"
6464
#endif
6565

6666
bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
@@ -101,28 +101,24 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
101101
ble_hs_cfg.sync_cb = _on_sync;
102102
// ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
103103

104-
#if CIRCUITPY_DOTENV
105-
mp_int_t name_len = 0;
106-
char ble_name[32];
107-
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
108-
if (name_len > 0) {
109-
if (name_len > MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH) {
110-
name_len = MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH;
111-
}
112-
ble_name[name_len] = '\0';
104+
#if CIRCUITPY_ENVIRON
105+
char ble_name[1 + MYNEWT_VAL_BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH];
106+
_environ_err_t result = _environ_get_key_str("CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name));
107+
if (result == ENVIRON_OK) {
113108
ble_svc_gap_device_name_set(ble_name);
114-
} else {
109+
} else
110+
#else
111+
{
115112
ble_svc_gap_device_name_set("CIRCUITPY");
116113
}
117-
#else
118-
ble_svc_gap_device_name_set("CIRCUITPY");
119114
#endif
120115

121-
// Clear all of the internal connection objects.
122-
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
123-
bleio_connection_internal_t *connection = &bleio_connections[i];
124-
// Reset connection.
125-
connection->conn_handle = BLEIO_HANDLE_INVALID;
116+
{// Clear all of the internal connection objects.
117+
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
118+
bleio_connection_internal_t *connection = &bleio_connections[i];
119+
// Reset connection.
120+
connection->conn_handle = BLEIO_HANDLE_INVALID;
121+
}
126122
}
127123

128124
cp_task = xTaskGetCurrentTaskHandle();

ports/espressif/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#include "shared-bindings/microcontroller/RunMode.h"
5656
#include "shared-bindings/rtc/__init__.h"
5757
#include "shared-bindings/socketpool/__init__.h"
58-
#include "shared-module/dotenv/__init__.h"
58+
#include "shared-module/_environ/__init__.h"
5959

6060
#include "peripherals/rmt.h"
6161
#include "peripherals/timer.h"
@@ -519,7 +519,7 @@ void port_idle_until_interrupt(void) {
519519
void port_post_boot_py(bool heap_valid) {
520520
if (!heap_valid && filesystem_present()) {
521521
mp_int_t reserved;
522-
if (dotenv_get_key_int("/.env", "CIRCUITPY_RESERVED_PSRAM", &reserved)) {
522+
if (_environ_get_key_int("CIRCUITPY_RESERVED_PSRAM", &reserved) == ENVIRON_OK) {
523523
common_hal_espidf_set_reserved_psram(reserved);
524524
}
525525
common_hal_espidf_reserve_psram();

ports/nrf/common-hal/_bleio/Adapter.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
#include "shared-bindings/_bleio/ScanEntry.h"
5353
#include "shared-bindings/time/__init__.h"
5454

55-
#if CIRCUITPY_DOTENV
56-
#include "shared-module/dotenv/__init__.h"
55+
#if CIRCUITPY_ENVIRON
56+
#include "shared-bindings/_environ/__init__.h"
5757
#endif
5858

5959
#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
@@ -345,12 +345,11 @@ STATIC void bleio_adapter_reset_name(bleio_adapter_obj_t *self) {
345345

346346
mp_int_t name_len = 0;
347347

348-
#if CIRCUITPY_DOTENV
349-
char ble_name[32];
350-
name_len = dotenv_get_key("/.env", "CIRCUITPY_BLE_NAME", ble_name, sizeof(ble_name) - 1);
351-
if (name_len > 0) {
352-
ble_name[name_len] = '\0';
353-
common_hal_bleio_adapter_set_name(self, (char *)ble_name);
348+
#if CIRCUITPY_ENVIRON
349+
mp_obj_t ble_name = common_hal__environ_get_key("CIRCUITPY_BLE_NAME");
350+
if (ble_name != mp_const_none) {
351+
common_hal_bleio_adapter_set_name(self, mp_obj_str_get_str(ble_name));
352+
return;
354353
}
355354
#endif
356355

ports/unix/variants/coverage/mpconfigvariant.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SRC_BITMAP := \
3333
shared-bindings/aesio/__init__.c \
3434
shared-bindings/bitmaptools/__init__.c \
3535
shared-bindings/displayio/Bitmap.c \
36-
shared-bindings/dotenv/__init__.c \
36+
shared-bindings/_environ/__init__.c \
3737
shared-bindings/rainbowio/__init__.c \
3838
shared-bindings/traceback/__init__.c \
3939
shared-bindings/util.c \
@@ -45,7 +45,7 @@ SRC_BITMAP := \
4545
shared-module/displayio/Bitmap.c \
4646
shared-module/displayio/ColorConverter.c \
4747
shared-module/displayio/ColorConverter.c \
48-
shared-module/dotenv/__init__.c \
48+
shared-module/_environ/__init__.c \
4949
shared-module/rainbowio/__init__.c \
5050
shared-module/traceback/__init__.c \
5151
shared-module/zlib/__init__.c \
@@ -56,7 +56,7 @@ CFLAGS += \
5656
-DCIRCUITPY_AESIO=1 \
5757
-DCIRCUITPY_BITMAPTOOLS=1 \
5858
-DCIRCUITPY_DISPLAYIO_UNIX=1 \
59-
-DCIRCUITPY_DOTENV=1 \
59+
-DCIRCUITPY_ENVIRON=1 \
6060
-DCIRCUITPY_GIFIO=1 \
6161
-DCIRCUITPY_RAINBOWIO=1 \
6262
-DCIRCUITPY_TRACEBACK=1 \

0 commit comments

Comments
 (0)