|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * SPDX-FileCopyrightText: Copyright (c) 2022 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <string.h> |
| 28 | + |
| 29 | +#include "extmod/vfs.h" |
| 30 | +#include "lib/oofatfs/ff.h" |
| 31 | +#include "lib/oofatfs/diskio.h" |
| 32 | +#include "py/mpstate.h" |
| 33 | +#include "py/obj.h" |
| 34 | +#include "py/objstr.h" |
| 35 | +#include "py/runtime.h" |
| 36 | +#include "shared-bindings/dotenv/__init__.h" |
| 37 | + |
| 38 | +//| """Functions to manage environment variables from a .env file. |
| 39 | +//| |
| 40 | +//| A subset of the CPython `dotenv library <https://saurabh-kumar.com/python-dotenv/>`_. It does |
| 41 | +//| not support variables or double quotes. |
| 42 | +//| |
| 43 | +//| The simplest way to define keys and values is to put them in single quotes. \ and ' are |
| 44 | +//| escaped by \ in single quotes. Newlines can occur in quotes for multiline values. Comments |
| 45 | +//| start with # and apply for the rest of the line. |
| 46 | +//| |
| 47 | +//| File format example: |
| 48 | +//| |
| 49 | +//| .. code-block:: |
| 50 | +//| |
| 51 | +//| key=value |
| 52 | +//| key2 = value2 |
| 53 | +//| 'key3' = 'value with spaces' |
| 54 | +//| # comment |
| 55 | +//
1E79
| key4 = value3 # comment 2 |
| 56 | +//| 'key5'=value4 |
| 57 | +//| key=value5 # overrides the first one |
| 58 | +//| multiline = 'hello |
| 59 | +//| world |
| 60 | +//| how are you?' |
| 61 | +//| |
| 62 | +//| """ |
| 63 | +//| |
| 64 | +//| import typing |
| 65 | + |
| 66 | +//| def get_key(dotenv_path: str, key_to_get: str) -> Optional[str]: |
| 67 | +//| """Get the value for the given key from the given .env file. If the key occurs multiple |
| 68 | +//| times in the file, then the last value will be returned. |
| 69 | +//| |
| 70 | +//| Returns None if the key isn't found or doesn't have a value.""" |
| 71 | +//| ... |
| 72 | +//| |
| 73 | +STATIC mp_obj_t _dotenv_get_key(mp_obj_t path_in, mp_obj_t key_to_get_in) { |
| 74 | + return common_hal_dotenv_get_key(mp_obj_str_get_str(path_in), |
| 75 | + mp_obj_str_get_str(key_to_get_in)); |
| 76 | +} |
| 77 | +MP_DEFINE_CONST_FUN_OBJ_2(dotenv_get_key_obj, _dotenv_get_key); |
| 78 | + |
| 79 | +//| def load_dotenv() -> None: |
| 80 | +//| """Does nothing in CircuitPython because os.getenv will automatically read .env when |
| 81 | +//| available. |
| 82 | +//| |
| 83 | +//| Present in CircuitPython so CPython-compatible code can use it without error.""" |
| 84 | +//| ... |
| 85 | +//| |
| 86 | +STATIC mp_obj_t dotenv_load_dotenv(void) { |
| 87 | + return mp_const_none; |
| 88 | +} |
| 89 | +MP_DEFINE_CONST_FUN_OBJ_0(dotenv_load_dotenv_obj, dotenv_load_dotenv); |
| 90 | + |
| 91 | +STATIC const mp_rom_map_elem_t dotenv_module_globals_table[] = { |
| 92 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_dotenv) }, |
| 93 | + |
| 94 | + { MP_ROM_QSTR(MP_QSTR_get_key), MP_ROM_PTR(&dotenv_get_key_obj) }, |
| 95 | + { MP_ROM_QSTR(MP_QSTR_load_dotenv), MP_ROM_PTR(&dotenv_load_dotenv_obj) }, |
| 96 | +}; |
| 97 | + |
| 98 | +STATIC MP_DEFINE_CONST_DICT(dotenv_module_globals, dotenv_module_globals_table); |
| 99 | + |
| 100 | +const mp_obj_module_t dotenv_module = { |
| 101 | + .base = { &mp_type_module }, |
| 102 | + .globals = (mp_obj_dict_t *)&dotenv_module_globals, |
| 103 | +}; |
| 104 | + |
| 105 | +MP_REGISTER_MODULE(MP_QSTR_dotenv, dotenv_module, CIRCUITPY_DOTENV); |
0 commit comments