From 7f24587c73dc3ec17082a981f0dad36d35a58a43 Mon Sep 17 00:00:00 2001 From: Leo Schleier <43878374+leoschleier@users.noreply.github.com> Date: Mon, 1 Jan 2024 18:21:28 +0100 Subject: [PATCH] feat: Add config loading (#11) * build: Update dependencies and ruff settings * feat: Add io functions for reading config files * feat: Add function for loading a config * test: Add test cases for io functions * feat: Add safe loading of config * feat Add test cases for config module * docs: Add section on config files to README * chore: Add ruff cache to .gitignore * refactor: Rename get and set function in config.py * style: Use pattern matching instead of if-statements * docs: Update package docstring * refactor: Remove file-existence check * fix: Fix pyright issues --- .gitignore | 5 +- docs/README.md | 24 +++++ poetry.lock | 190 ++++++++++++++++++++++++++++++++-- pyproject.toml | 4 + src/pit/viper/__init__.py | 37 +++++-- src/pit/viper/_io.py | 94 +++++++++++++++++ src/pit/viper/config.py | 135 ++++++++++++++++++++++++ tests/data/config/config.json | 11 ++ tests/data/config/config.toml | 8 ++ tests/data/config/config.yaml | 8 ++ tests/test__io.py | 39 +++++++ tests/test_config.py | 52 ++++++++++ 12 files changed, 592 insertions(+), 15 deletions(-) create mode 100644 src/pit/viper/_io.py create mode 100644 src/pit/viper/config.py create mode 100644 tests/data/config/config.json create mode 100644 tests/data/config/config.toml create mode 100644 tests/data/config/config.yaml create mode 100644 tests/test__io.py create mode 100644 tests/test_config.py diff --git a/.gitignore b/.gitignore index 3cc4e1d..2a4aea2 100644 --- a/.gitignore +++ b/.gitignore @@ -163,4 +163,7 @@ cython_debug/ #.idea/ # Test data -!tests/data/.env \ No newline at end of file +!tests/data/.env + +# Ruff +.ruff_cache \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index 5a0b642..a0cdbaa 100644 --- a/docs/README.md +++ b/docs/README.md @@ -33,6 +33,30 @@ from pit import viper env = viper.auto_env() ``` +### Config Files + +Config files are another config source for `pit-viper`. The package supports +loading, accessing, and setting defaults for a config. Supported file formats +are JSON, TOML, and YAML. + +```python +from pathlib import Path +from pit import viper + +MY_CONFIG_DIR = Path() / "config" + +viper.set_config_path(MY_CONFIG_DIR) +viper.set_config_name("my_config") +viper.set_config_type("toml") + +viper.set("foo", "default-value") + +viper.load_config() + +bar = viper.get("foo") +nested_parameter = viper.get("my.nested.parameter") +``` + ## Development We use [Poetry](https://github.com/python-poetry/poetry) for packaging and diff --git a/poetry.lock b/poetry.lock index da7e158..efc2c1e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,5 +1,59 @@ # This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. +[[package]] +name = "black" +version = "23.10.1" +description = "The uncompromising code formatter." +optional = false +python-versions = ">=3.8" +files = [ + {file = "black-23.10.1-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:ec3f8e6234c4e46ff9e16d9ae96f4ef69fa328bb4ad08198c8cee45bb1f08c69"}, + {file = "black-23.10.1-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:1b917a2aa020ca600483a7b340c165970b26e9029067f019e3755b56e8dd5916"}, + {file = "black-23.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9c74de4c77b849e6359c6f01987e94873c707098322b91490d24296f66d067dc"}, + {file = "black-23.10.1-cp310-cp310-win_amd64.whl", hash = "sha256:7b4d10b0f016616a0d93d24a448100adf1699712fb7a4efd0e2c32bbb219b173"}, + {file = "black-23.10.1-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:b15b75fc53a2fbcac8a87d3e20f69874d161beef13954747e053bca7a1ce53a0"}, + {file = "black-23.10.1-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:e293e4c2f4a992b980032bbd62df07c1bcff82d6964d6c9496f2cd726e246ace"}, + {file = "black-23.10.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7d56124b7a61d092cb52cce34182a5280e160e6aff3137172a68c2c2c4b76bcb"}, + {file = "black-23.10.1-cp311-cp311-win_amd64.whl", hash = "sha256:3f157a8945a7b2d424da3335f7ace89c14a3b0625e6593d21139c2d8214d55ce"}, + {file = "black-23.10.1-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:cfcce6f0a384d0da692119f2d72d79ed07c7159879d0bb1bb32d2e443382bf3a"}, + {file = "black-23.10.1-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:33d40f5b06be80c1bbce17b173cda17994fbad096ce60eb22054da021bf933d1"}, + {file = "black-23.10.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:840015166dbdfbc47992871325799fd2dc0dcf9395e401ada6d88fe11498abad"}, + {file = "black-23.10.1-cp38-cp38-win_amd64.whl", hash = "sha256:037e9b4664cafda5f025a1728c50a9e9aedb99a759c89f760bd83730e76ba884"}, + {file = "black-23.10.1-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:7cb5936e686e782fddb1c73f8aa6f459e1ad38a6a7b0e54b403f1f05a1507ee9"}, + {file = "black-23.10.1-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:7670242e90dc129c539e9ca17665e39a146a761e681805c54fbd86015c7c84f7"}, + {file = "black-23.10.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5ed45ac9a613fb52dad3b61c8dea2ec9510bf3108d4db88422bacc7d1ba1243d"}, + {file = "black-23.10.1-cp39-cp39-win_amd64.whl", hash = "sha256:6d23d7822140e3fef190734216cefb262521789367fbdc0b3f22af6744058982"}, + {file = "black-23.10.1-py3-none-any.whl", hash = "sha256:d431e6739f727bb2e0495df64a6c7a5310758e87505f5f8cde9ff6c0f2d7e4fe"}, + {file = "black-23.10.1.tar.gz", hash = "sha256:1f8ce316753428ff68749c65a5f7844631aa18c8679dfd3ca9dc1a289979c258"}, +] + +[package.dependencies] +click = ">=8.0.0" +mypy-extensions = ">=0.4.3" +packaging = ">=22.0" +pathspec = ">=0.9.0" +platformdirs = ">=2" + +[package.extras] +colorama = ["colorama (>=0.4.3)"] +d = ["aiohttp (>=3.7.4)"] +jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] +uvloop = ["uvloop (>=0.15.2)"] + +[[package]] +name = "click" +version = "8.1.7" +description = "Composable command line interface toolkit" +optional = false +python-versions = ">=3.7" +files = [ + {file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"}, + {file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"}, +] + +[package.dependencies] +colorama = {version = "*", markers = "platform_system == \"Windows\""} + [[package]] name = "colorama" version = "0.4.6" @@ -22,17 +76,54 @@ files = [ {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, ] +[[package]] +name = "mypy-extensions" +version = "1.0.0" +description = "Type system extensions for programs checked with the mypy type checker." +optional = false +python-versions = ">=3.5" +files = [ + {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"}, + {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, +] + [[package]] name = "packaging" -version = "23.1" +version = "23.2" description = "Core utilities for Python packages" optional = false python-versions = ">=3.7" files = [ - {file = "packaging-23.1-py3-none-any.whl", hash = "sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61"}, - {file = "packaging-23.1.tar.gz", hash = "sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f"}, + {file = "packaging-23.2-py3-none-any.whl", hash = "sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"}, + {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, +] + +[[package]] +name = "pathspec" +version = "0.11.2" +description = "Utility library for gitignore style pattern matching of file paths." +optional = false +python-versions = ">=3.7" +files = [ + {file = "pathspec-0.11.2-py3-none-any.whl", hash = "sha256:1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20"}, + {file = "pathspec-0.11.2.tar.gz", hash = "sha256:e0d8d0ac2f12da61956eb2306b69f9469b42f4deb0f3cb6ed47b9cce9996ced3"}, +] + +[[package]] +name = "platformdirs" +version = "3.11.0" +description = "A small Python package for determining appropriate platform-specific dirs, e.g. a \"user data dir\"." +optional = false +python-versions = ">=3.7" +files = [ + {file = "platformdirs-3.11.0-py3-none-any.whl", hash = "sha256:e9d171d00af68be50e9202731309c4e658fd8bc76f55c11c7dd760d023bda68e"}, + {file = "platformdirs-3.11.0.tar.gz", hash = "sha256:cf8ee52a3afdb965072dcc652433e0c7e3e40cf5ea1477cd4b3b1d2eb75495b3"}, ] +[package.extras] +docs = ["furo (>=2023.7.26)", "proselint (>=0.13)", "sphinx (>=7.1.1)", "sphinx-autodoc-typehints (>=1.24)"] +test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4)", "pytest-cov (>=4.1)", "pytest-mock (>=3.11.1)"] + [[package]] name = "pluggy" version = "1.3.0" @@ -50,13 +141,13 @@ testing = ["pytest", "pytest-benchmark"] [[package]] name = "pytest" -version = "7.4.2" +version = "7.4.3" description = "pytest: simple powerful testing with Python" optional = false python-versions = ">=3.7" files = [ - {file = "pytest-7.4.2-py3-none-any.whl", hash = "sha256:1d881c6124e08ff0a1bb75ba3ec0bfd8b5354a01c194ddd5a0a870a48d99b002"}, - {file = "pytest-7.4.2.tar.gz", hash = "sha256:a766259cfab564a2ad52cb1aae1b881a75c3eb7e34ca3779697c23ed47c47069"}, + {file = "pytest-7.4.3-py3-none-any.whl", hash = "sha256:0d009c083ea859a71b76adf7c1d502e4bc170b80a8ef002da5806527b9591fac"}, + {file = "pytest-7.4.3.tar.gz", hash = "sha256:d989d136982de4e3b29dabcc838ad581c64e8ed52c11fbe86ddebd9da0818cd5"}, ] [package.dependencies] @@ -68,7 +159,92 @@ pluggy = ">=0.12,<2.0" [package.extras] testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] +[[package]] +name = "pyyaml" +version = "6.0.1" +description = "YAML parser and emitter for Python" +optional = false +python-versions = ">=3.6" +files = [ + {file = "PyYAML-6.0.1-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:d858aa552c999bc8a8d57426ed01e40bef403cd8ccdd0fc5f6f04a00414cac2a"}, + {file = "PyYAML-6.0.1-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:fd66fc5d0da6d9815ba2cebeb4205f95818ff4b79c3ebe268e75d961704af52f"}, + {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:69b023b2b4daa7548bcfbd4aa3da05b3a74b772db9e23b982788168117739938"}, + {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:81e0b275a9ecc9c0c0c07b4b90ba548307583c125f54d5b6946cfee6360c733d"}, + {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba336e390cd8e4d1739f42dfe9bb83a3cc2e80f567d8805e11b46f4a943f5515"}, + {file = "PyYAML-6.0.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:326c013efe8048858a6d312ddd31d56e468118ad4cdeda36c719bf5bb6192290"}, + {file = "PyYAML-6.0.1-cp310-cp310-win32.whl", hash = "sha256:bd4af7373a854424dabd882decdc5579653d7868b8fb26dc7d0e99f823aa5924"}, + {file = "PyYAML-6.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d"}, + {file = "PyYAML-6.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:6965a7bc3cf88e5a1c3bd2e0b5c22f8d677dc88a455344035f03399034eb3007"}, + {file = "PyYAML-6.0.1-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:f003ed9ad21d6a4713f0a9b5a7a0a79e08dd0f221aff4525a2be4c346ee60aab"}, + {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:42f8152b8dbc4fe7d96729ec2b99c7097d656dc1213a3229ca5383f973a5ed6d"}, + {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:062582fca9fabdd2c8b54a3ef1c978d786e0f6b3a1510e0ac93ef59e0ddae2bc"}, + {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d2b04aac4d386b172d5b9692e2d2da8de7bfb6c387fa4f801fbf6fb2e6ba4673"}, + {file = "PyYAML-6.0.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:e7d73685e87afe9f3b36c799222440d6cf362062f78be1013661b00c5c6f678b"}, + {file = "PyYAML-6.0.1-cp311-cp311-win32.whl", hash = "sha256:1635fd110e8d85d55237ab316b5b011de701ea0f29d07611174a1b42f1444741"}, + {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, + {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, + {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, + {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, + {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, + {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, + {file = "PyYAML-6.0.1-cp312-cp312-win_amd64.whl", hash = "sha256:0d3304d8c0adc42be59c5f8a4d9e3d7379e6955ad754aa9d6ab7a398b59dd1df"}, + {file = "PyYAML-6.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47"}, + {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1fe35611261b29bd1de0070f0b2f47cb6ff71fa6595c077e42bd0c419fa27b98"}, + {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:704219a11b772aea0d8ecd7058d0082713c3562b4e271b849ad7dc4a5c90c13c"}, + {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:afd7e57eddb1a54f0f1a974bc4391af8bcce0b444685d936840f125cf046d5bd"}, + {file = "PyYAML-6.0.1-cp36-cp36m-win32.whl", hash = "sha256:fca0e3a251908a499833aa292323f32437106001d436eca0e6e7833256674585"}, + {file = "PyYAML-6.0.1-cp36-cp36m-win_amd64.whl", hash = "sha256:f22ac1c3cac4dbc50079e965eba2c1058622631e526bd9afd45fedd49ba781fa"}, + {file = "PyYAML-6.0.1-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:b1275ad35a5d18c62a7220633c913e1b42d44b46ee12554e5fd39c70a243d6a3"}, + {file = "PyYAML-6.0.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:18aeb1bf9a78867dc38b259769503436b7c72f7a1f1f4c93ff9a17de54319b27"}, + {file = "PyYAML-6.0.1-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:596106435fa6ad000c2991a98fa58eeb8656ef2325d7e158344fb33864ed87e3"}, + {file = "PyYAML-6.0.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:baa90d3f661d43131ca170712d903e6295d1f7a0f595074f151c0aed377c9b9c"}, + {file = "PyYAML-6.0.1-cp37-cp37m-win32.whl", hash = "sha256:9046c58c4395dff28dd494285c82ba00b546adfc7ef001486fbf0324bc174fba"}, + {file = "PyYAML-6.0.1-cp37-cp37m-win_amd64.whl", hash = "sha256:4fb147e7a67ef577a588a0e2c17b6db51dda102c71de36f8549b6816a96e1867"}, + {file = "PyYAML-6.0.1-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:1d4c7e777c441b20e32f52bd377e0c409713e8bb1386e1099c2415f26e479595"}, + {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a0cd17c15d3bb3fa06978b4e8958dcdc6e0174ccea823003a106c7d4d7899ac5"}, + {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:28c119d996beec18c05208a8bd78cbe4007878c6dd15091efb73a30e90539696"}, + {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7e07cbde391ba96ab58e532ff4803f79c4129397514e1413a7dc761ccd755735"}, + {file = "PyYAML-6.0.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:49a183be227561de579b4a36efbb21b3eab9651dd81b1858589f796549873dd6"}, + {file = "PyYAML-6.0.1-cp38-cp38-win32.whl", hash = "sha256:184c5108a2aca3c5b3d3bf9395d50893a7ab82a38004c8f61c258d4428e80206"}, + {file = "PyYAML-6.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:1e2722cc9fbb45d9b87631ac70924c11d3a401b2d7f410cc0e3bbf249f2dca62"}, + {file = "PyYAML-6.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9eb6caa9a297fc2c2fb8862bc5370d0303ddba53ba97e71f08023b6cd73d16a8"}, + {file = "PyYAML-6.0.1-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:c8098ddcc2a85b61647b2590f825f3db38891662cfc2fc776415143f599bb859"}, + {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5773183b6446b2c99bb77e77595dd486303b4faab2b086e7b17bc6bef28865f6"}, + {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b786eecbdf8499b9ca1d697215862083bd6d2a99965554781d0d8d1ad31e13a0"}, + {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc1bf2925a1ecd43da378f4db9e4f799775d6367bdb94671027b73b393a7c42c"}, + {file = "PyYAML-6.0.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:04ac92ad1925b2cff1db0cfebffb6ffc43457495c9b3c39d3fcae417d7125dc5"}, + {file = "PyYAML-6.0.1-cp39-cp39-win32.whl", hash = "sha256:faca3bdcf85b2fc05d06ff3fbc1f83e1391b3e724afa3feba7d13eeab355484c"}, + {file = "PyYAML-6.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:510c9deebc5c0225e8c96813043e62b680ba2f9c50a08d3724c7f28a747d1486"}, + {file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"}, +] + +[[package]] +name = "ruff" +version = "0.1.3" +description = "An extremely fast Python linter, written in Rust." +optional = false +python-versions = ">=3.7" +files = [ + {file = "ruff-0.1.3-py3-none-macosx_10_7_x86_64.whl", hash = "sha256:b46d43d51f7061652eeadb426a9e3caa1e0002470229ab2fc19de8a7b0766901"}, + {file = "ruff-0.1.3-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:b8afeb9abd26b4029c72adc9921b8363374f4e7edb78385ffaa80278313a15f9"}, + {file = "ruff-0.1.3-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ca3cf365bf32e9ba7e6db3f48a4d3e2c446cd19ebee04f05338bc3910114528b"}, + {file = "ruff-0.1.3-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:4874c165f96c14a00590dcc727a04dca0cfd110334c24b039458c06cf78a672e"}, + {file = "ruff-0.1.3-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:eec2dd31eed114e48ea42dbffc443e9b7221976554a504767ceaee3dd38edeb8"}, + {file = "ruff-0.1.3-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:dc3ec4edb3b73f21b4aa51337e16674c752f1d76a4a543af56d7d04e97769613"}, + {file = "ruff-0.1.3-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2e3de9ed2e39160800281848ff4670e1698037ca039bda7b9274f849258d26ce"}, + {file = "ruff-0.1.3-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:1c595193881922cc0556a90f3af99b1c5681f0c552e7a2a189956141d8666fe8"}, + {file = "ruff-0.1.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0f75e670d529aa2288cd00fc0e9b9287603d95e1536d7a7e0cafe00f75e0dd9d"}, + {file = "ruff-0.1.3-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:76dd49f6cd945d82d9d4a9a6622c54a994689d8d7b22fa1322983389b4892e20"}, + {file = "ruff-0.1.3-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:918b454bc4f8874a616f0d725590277c42949431ceb303950e87fef7a7d94cb3"}, + {file = "ruff-0.1.3-py3-none-musllinux_1_2_i686.whl", hash = "sha256:d8859605e729cd5e53aa38275568dbbdb4fe882d2ea2714c5453b678dca83784"}, + {file = "ruff-0.1.3-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:0b6c55f5ef8d9dd05b230bb6ab80bc4381ecb60ae56db0330f660ea240cb0d4a"}, + {file = "ruff-0.1.3-py3-none-win32.whl", hash = "sha256:3e7afcbdcfbe3399c34e0f6370c30f6e529193c731b885316c5a09c9e4317eef"}, + {file = "ruff-0.1.3-py3-none-win_amd64.whl", hash = "sha256:7a18df6638cec4a5bd75350639b2bb2a2366e01222825562c7346674bdceb7ea"}, + {file = "ruff-0.1.3-py3-none-win_arm64.whl", hash = "sha256:12fd53696c83a194a2db7f9a46337ce06445fb9aa7d25ea6f293cf75b21aca9f"}, + {file = "ruff-0.1.3.tar.gz", hash = "sha256:3ba6145369a151401d5db79f0a47d50e470384d0d89d0d6f7fab0b589ad07c34"}, +] + [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "b0d5b834150d6369acf5574d31fe9130043ea989811c47c7b2829741ce009e4f" +content-hash = "297cf6aa4fcfc9ed4cc5b80a08081d3a786e02d5675401f3dc90206db0ee63a6" diff --git a/pyproject.toml b/pyproject.toml index 22a8081..04bbaea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,9 +26,12 @@ packages = [{include = "pit", from = "src"}] [tool.poetry.dependencies] python = "^3.11" +pyyaml = "^6.0.1" [tool.poetry.group.dev.dependencies] pytest = "^7.4.2" +black = "^23.10.1" +ruff = "^0.1.3" [build-system] requires = ["poetry-core"] @@ -47,6 +50,7 @@ pythonVersion = "3.11" [tool.ruff] select = ["ALL"] +ignore = ["ANN401"] # Use of typing.Any fixable = ["ALL"] fix = true line-length = 79 diff --git a/src/pit/viper/__init__.py b/src/pit/viper/__init__.py index edb9249..c5dc4ed 100644 --- a/src/pit/viper/__init__.py +++ b/src/pit/viper/__init__.py @@ -1,17 +1,40 @@ """Pit-viper offers configuration management capabilities. -This package currently provides one main function, `auto_env`, -which reads a `.env` file and sets the corresponding environment -variables in the current process. This can be useful for configuring -applications or scripts that rely on environment variables. +This package supports accessing parameters from environment variables, +and configuration files. Usage: >>> from pit import viper + >>> from pathlib import Path + >>> >>> viper.auto_env() + >>> + >>> viper.set_config_path(Path() / "config") + >>> viper.set_config_name("my_config") + >>> viper.set_config_type("toml") + >>> viper.set("foo", "default-value") + >>> + >>> viper.load_config() + >>> foo = viper.get("foo") -For more information, see the documentation for the `auto_env` -function. +For more information, see the https://github.com/leoschleier/pit-viper. """ +from pit.viper.config import get_conf as get +from pit.viper.config import ( + load_config, + set_config_name, + set_config_path, + set_config_type, +) +from pit.viper.config import set_conf as set # noqa: A001 from pit.viper.env import auto_env -__all__ = ["auto_env"] +__all__ = [ + "auto_env", + "get", + "load_config", + "set", + "set_config_name", + "set_config_path", + "set_config_type", +] diff --git a/src/pit/viper/_io.py b/src/pit/viper/_io.py new file mode 100644 index 0000000..6faa284 --- /dev/null +++ b/src/pit/viper/_io.py @@ -0,0 +1,94 @@ +"""Internal IO functions for viper.""" +import json +import tomllib +from pathlib import Path +from typing import Any + +import yaml + + +def read_config(path: Path) -> dict[str, Any]: + """Read a config file. + + This function supports configs from JSON, TOML, and YAML files. + + Parameters + ---------- + path : Path + Path to the config file. + + Returns + ------- + dict[str, Any] + Config + + Raises + ------ + ValueError + Raised if the config file extension is not supported. + """ + file_extension = path.suffix if path.suffix else path.name + + match file_extension: + case ".json": + return _read_json(path) + case ".toml": + return _read_toml(path) + case ".yml" | ".yaml": + return _read_yaml(path) + case _: + msg = f"Config file extension {file_extension} not supported." + raise ValueError(msg) + + + + +def _read_json(path: Path) -> dict[str, Any]: + """Read a JSON file. + + Parameters + ---------- + path : Path + Path to the JSON file. + + Returns + ------- + dict[str, Any] + JSON file contents. + """ + with path.open("rb") as stream: + return json.load(stream) + + +def _read_toml(path: Path) -> dict[str, Any]: + """Read a TOML file. + + Parameters + ---------- + path : Path + Path to the TOML file. + + Returns + ------- + dict[str, Any] + TOML file contents. + """ + with path.open("rb") as stream: + return tomllib.load(stream) + + +def _read_yaml(path: Path) -> dict[str, Any]: + """Read a YAML file. + + Parameters + ---------- + path : Path + Path to the YAML file. + + Returns + ------- + dict[str, Any] + YAML file contents. + """ + with path.open("rb") as stream: + return yaml.safe_load(stream) diff --git a/src/pit/viper/config.py b/src/pit/viper/config.py new file mode 100644 index 0000000..cca576e --- /dev/null +++ b/src/pit/viper/config.py @@ -0,0 +1,135 @@ +"""Load config from file.""" +from pathlib import Path +from typing import Any + +from pit.viper import _io + +_config_path: Path | None = None +_config_name: str = "" +_config_type: str = "" +_config: dict[str, Any] = {} + + +def set_config_name(name: str) -> None: + """Set the config name. + + Parameters + ---------- + name : str + Name of the config file. + """ + global _config_name # noqa: PLW0603 + _config_name = name + + +def set_config_path(path: Path) -> None: + """Set the config path. + + Parameters + ---------- + path : Path + Path to the config file or the directory containing the config + file. + """ + global _config_path # noqa: PLW0603 + _config_path = path + + +def set_config_type(type_: str) -> None: + """Set the config type. + + Supported config formats: json, toml, yaml, yml. + + Parameters + ---------- + type_ : str + Config format. + """ + global _config_type # noqa: PLW0603 + _config_type = type_.lower() + + +def get_conf(key: str, default: Any = None) -> Any: + """Get a value from the config. + + Parameters + ---------- + key : str + Key to get the value for. + default : Any, optional + Default value to return if the key is not found, by default None + + Returns + ------- + Any + Value for the key. + """ + keys = key.split(".") + value = _config + + for k in keys: + if isinstance(value, dict): + value = value.get(k, default) + + return value + + +def set_conf(key: str, value: Any) -> None: + """Set a value in the config. + + Parameters + ---------- + key : str + Key to set the value for. + value : Any + Value to set. + """ + keys = key.split(".") + config = _config + + for k in keys[:-1]: + if isinstance(config, dict): + config = config.setdefault(k, {}) + + config[keys[-1]] = value + + +def load_config() -> None: + """Load the config from the config file. + + Raises + ------ + FileNotFoundError + Raised if the config path is not set or the path does not exist. + """ + if not _config_path: + msg = "Config path not set." + raise FileNotFoundError(msg) + + config_file = ( + f"{_config_name}.{_config_type}" if _config_type else _config_name + ) + config_path = _config_path / config_file + + config = _io.read_config(config_path) + for key, value in config.items(): + _safe_set(key, value) + + +def _safe_set(key: str, value: Any) -> None: + """Set a value in the config without overwriting nested defaults. + + Parameters + ---------- + key : str + Key to set the value for. + value : Any + Value to set. + """ + if isinstance(value, dict): + for k, v in value.items(): # pyright: ignore [reportUnknownVariableType] + _safe_set(f"{key}.{k}", v) + else: + set_conf(key, value) + + diff --git a/tests/data/config/config.json b/tests/data/config/config.json new file mode 100644 index 0000000..05dab99 --- /dev/null +++ b/tests/data/config/config.json @@ -0,0 +1,11 @@ +{ + "foo": "bar", + "test": { + "nested": { + "a": 0, + "b": 1.1, + "c": true, + "d": "test-string" + } + } +} \ No newline at end of file diff --git a/tests/data/config/config.toml b/tests/data/config/config.toml new file mode 100644 index 0000000..2de85ec --- /dev/null +++ b/tests/data/config/config.toml @@ -0,0 +1,8 @@ +foo = "bar" + +[test] +[test.nested] +a = 0 +b = 1.1 +c = true +d = "test-string" diff --git a/tests/data/config/config.yaml b/tests/data/config/config.yaml new file mode 100644 index 0000000..630300a --- /dev/null +++ b/tests/data/config/config.yaml @@ -0,0 +1,8 @@ +foo: bar + +test: + nested: + a: 0 + b: 1.1 + c: true + d: test-string \ No newline at end of file diff --git a/tests/test__io.py b/tests/test__io.py new file mode 100644 index 0000000..ac2b975 --- /dev/null +++ b/tests/test__io.py @@ -0,0 +1,39 @@ +"""Test the _io module.""" +from pathlib import Path +from typing import Final + +import pytest +from pit.viper import _io + +CONFIG_DIR: Final = Path(__file__).parent / "data" / "config" +CONFIG_JSON: Final = CONFIG_DIR / "config.json" +CONFIG_TOML: Final = CONFIG_DIR / "config.toml" +CONFIG_YAML: Final = CONFIG_DIR / "config.yaml" + +EX_CONFIG = { + "foo": "bar", + "test": { + "nested": { + "a": 0, + "b": 1.1, + "c": True, + "d": "test-string", + }, + }, +} + + +@pytest.mark.parametrize( + "config_path", + [CONFIG_JSON, CONFIG_TOML, CONFIG_YAML], +) +def test_read_config(config_path: Path) -> None: + """Test reading a config file. + + Parameters + ---------- + config_path : Path + Path to the config file. + """ + config = _io.read_config(config_path) + assert config == EX_CONFIG diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..e586565 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,52 @@ +"""Test the config module.""" +from pathlib import Path + +from pit import viper + +CONFIG_DIR = Path(__file__).parent / "data" / "config" +CONFIG_NAME = "config" +CONFIG_TYPE = "toml" + +EX_CONFIG = { + "foo": "bar", + "test.nested.a": 0, + "test.nested.b": 1.1, + "test.nested.c": True, + "test.nested.d": "test-string", +} + + +def test_load_config() -> None: + """Test loading a config file.""" + viper.set_config_path(CONFIG_DIR) + viper.set_config_name(CONFIG_NAME) + viper.set_config_type(CONFIG_TYPE) + viper.load_config() + + for key, value in EX_CONFIG.items(): + assert viper.get(key) == value + + +def test_load_config_with_defaults() -> None: + """Test loading a config file with defaults.""" + viper.set_config_path(CONFIG_DIR) + viper.set_config_name(CONFIG_NAME) + viper.set_config_type(CONFIG_TYPE) + + # Parameters set before loading the config only serve as defaults + # and can be overwritten on load. + viper.set("foo", "default bar") + viper.set("test.nested.e", "default-string") + + viper.load_config() + + # Parameters set after loading the config will overwrite the + # respective values. + viper.set("test.nested.d", "overwritten-string") + + for key, value in EX_CONFIG.items(): + if key != "test.nested.d": + assert viper.get(key) == value + + assert viper.get("test.nested.d") == "overwritten-string" + assert viper.get("test.nested.e") == "default-string"