Skip to content

Commit

Permalink
refactory / allow other settings libraries than pydantic_settings (#9)
Browse files Browse the repository at this point in the history
Changes:

- split base.py
- don't require settings being a pydantic settings object
- add get_value_from_settings helper for handling attribute based
  settings as well dictionaries
- bump version
- apply basic styling
- fix docs
- fix ruff integration
- reformat files
- add python 3.13 to gh-actions
  • Loading branch information
devkral authored Nov 22, 2024
1 parent ed06052 commit 6f3fbc1
Show file tree
Hide file tree
Showing 18 changed files with 967 additions and 686 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ on:
push:
branches:
- "**"
pull_request:
branches: ["main", "develop", "release"]
jobs:
tests:
name: "Python ${{ matrix.python-version }}"
runs-on: "ubuntu-latest"
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
steps:
- uses: "actions/checkout@v4"
- uses: "actions/setup-python@v5"
Expand Down
9 changes: 9 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Release notes


## Version 0.1.0

### Changed

- Internals refactored. `base.py` is splitted now in multiple submodules.
- Allow different settings than pydantic_settings.
- Switch to semantic versioning.

## Version 0.0.9

### Added
Expand Down
46 changes: 40 additions & 6 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

``` shell
pip install monkay
# or with pydantic_settings
# pip install monkay[settings]
```

### Usage
Expand Down Expand Up @@ -81,12 +79,9 @@ There is also a method:
Which can be used to clear the caches.


##### Monkey


#### Using settings

Settings can be an initialized pydantic settings variable or a class.
Settings pointed at via the string can be an initialized settings variable or a class.
When pointing to a class the class is automatically called without arguments.

Let's do the configuration like Django via environment variable:
Expand Down Expand Up @@ -145,6 +140,45 @@ monkay = Monkay(
)
```

##### Other settings libraries

Here we just use pydantic_settings. But settings can be everything from a dictionary, to a dataclass.
Only requirement is that names are resolvable as attributes or as keys.`


``` python title="explicit_settings.py"
from typing import TypedDict

class Settings(TypedDict):
preloads: list[str]
extensions: list[Any]
foo: str

settings = Settings(preloads=[], extensions=[], foo="hello")
# or just a dictionary
# settings = {"preloads": [], "extensions": [], "foo": "hello"}
```

and

``` python title="__init__.py"
import os
from monkay import Monkay, get_value_from_settings
monkay = Monkay(
globals(),
with_extensions=True,
with_instance=True,
settings_path=os.environ.get("MONKAY_SETTINGS", "example.default.path.settings:settings"),
settings_preloads_name="preloads",
settings_extensions_name="extensions",
)

# attribute grabber with fallback to items
get_value_from_settings(monkay.settings, "foo")
```



#### Pathes

Like shown in the examples pathes end with a `:` for an attribute. But sometimes a dot is nicer.
Expand Down
50 changes: 50 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,59 @@ site_name: Monkay
site_description: The ultimate preload, settings, lazy import manager..
site_url: https://dymmond.github.io/monkay

theme:
name: "material"
language: en
palette:
- scheme: "default"
primary: "light green"
accent: "red"
media: "(prefers-color-scheme: light)"
toggle:
icon: "material/lightbulb"
name: "Switch to dark mode"
- scheme: "slate"
media: "(prefers-color-scheme: dark)"
primary: "green"
accent: "red"
toggle:
icon: "material/lightbulb-outline"
name: "Switch to light mode"

features:
- search.suggest
- search.highlight
- content.tabs.link
- content.code.copy
- content.code.annotate
- content.tooltips
- content.code.select
- navigation.indexes
- navigation.path
- navigation.tabs


nav:
- Home: index.md
- Tutorial: tutorial.md
- Helpers: helpers.md
- Testing: testing.md
- Specials: specials.md
- Release Notes: release-notes.md

markdown_extensions:
- attr_list
- toc:
permalink: true
- mdx_include:
base_path: docs
- admonition
- extra
- pymdownx.superfences:
custom_fences:
- name: mermaid
class: mermaid
format: !!python/name:pymdownx.superfences.fence_code_format ""
- pymdownx.tabbed:
alternate_style: true
- md_in_html
2 changes: 1 addition & 1 deletion monkay/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2024-present alex <devkral@web.de>
#
# SPDX-License-Identifier: BSD-3-Clauses
__version__ = "0.0.9"
__version__ = "0.1.0"
15 changes: 12 additions & 3 deletions monkay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
# SPDX-License-Identifier: BSD-3-Clauses

from .base import (
InGlobalsDict,
absolutify_import,
get_value_from_settings,
load,
load_any,
)
from .core import (
Monkay,
)
from .types import (
PRE_ADD_LAZY_IMPORT_HOOK,
DeprecatedImport,
ExtensionProtocol,
Monkay,
load,
load_any,
)

__all__ = [
Expand All @@ -20,4 +27,6 @@
"load",
"load_any",
"absolutify_import",
"InGlobalsDict",
"get_value_from_settings",
]
Loading

0 comments on commit 6f3fbc1

Please sign in to comment.