Skip to content

Feature/manager #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
bed12e0
Initial mockup
FasterSpeeding Aug 5, 2024
fd4d1a1
Allow configs to define loaders and unloaders
FasterSpeeding Aug 6, 2024
1fce99d
Switch to just allowing configs to add type IDs to load
FasterSpeeding Aug 6, 2024
70b978c
Reformat PR code
always-on-duty[bot] Aug 6, 2024
c077114
Load config classes from package metadata
FasterSpeeding Aug 7, 2024
fb774ae
Some cleanup/improvements
FasterSpeeding Aug 7, 2024
5817730
Reformat PR code
always-on-duty[bot] Aug 7, 2024
6c4f172
Improve entrypoint naming
FasterSpeeding Aug 7, 2024
bb7b237
More documentation
FasterSpeeding Aug 7, 2024
2cedc63
Small fixes
FasterSpeeding Aug 8, 2024
fa0e17f
Create manager.md
FasterSpeeding Aug 8, 2024
198b499
Update and rename manager.md to managed.md
FasterSpeeding Aug 8, 2024
7521521
Update managed.md
FasterSpeeding Aug 8, 2024
c8c5517
Reformat PR code
always-on-duty[bot] Aug 8, 2024
726795a
Small fixes
FasterSpeeding Aug 8, 2024
eb3ee16
Reformat PR code
always-on-duty[bot] Aug 8, 2024
02b478c
Linting fixes
FasterSpeeding Aug 8, 2024
cfda553
Introduce TypeConfig class for libraries
FasterSpeeding Aug 11, 2024
783fcec
Set Manager as a type dep
FasterSpeeding Aug 11, 2024
d513421
Add decorator utility methods
FasterSpeeding Aug 11, 2024
6d01c50
Small doc improvements
FasterSpeeding Aug 11, 2024
b9afb88
Fixes
FasterSpeeding Aug 11, 2024
ef67886
Some small improvementsw
FasterSpeeding Aug 13, 2024
c83f986
post-rebase fixes
FasterSpeeding Nov 18, 2024
4258316
Remove locking and u[grade to 3.11+
FasterSpeeding Nov 24, 2024
cdfee36
Reformat PR code
always-on-duty[bot] Nov 24, 2024
7f4cbd0
Doc heading fixes
FasterSpeeding Nov 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions alluka/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from . import _types # pyright: ignore[reportPrivateUsage]
from . import _visitor
from . import abc as alluka
from .managed import _index

if typing.TYPE_CHECKING:
from typing import Self
Expand Down Expand Up @@ -168,7 +169,7 @@ def __init__(self, *, introspect_annotations: bool = True) -> None:
self._descriptors: weakref.WeakKeyDictionary[
alluka.CallbackSig[typing.Any], dict[str, _types.InjectedTuple]
] = weakref.WeakKeyDictionary()
self._introspect_annotations = introspect_annotations
self._introspect_annotations = introspect_annotations # TODO: deprecate
static_context = _context.Context(self)
self._make_context: collections.Callable[[Self], alluka.Context] = lambda _: static_context
self._type_dependencies: dict[type[typing.Any], typing.Any] = {alluka.Client: self, Client: self}
Expand All @@ -180,7 +181,11 @@ def _build_descriptors(self, callback: alluka.CallbackSig[typing.Any], /) -> dic
except KeyError:
pass

# TODO: introspect_annotations=self._introspect_annotations
descriptors = _index.GLOBAL_INDEX.get_descriptors(callback)
if descriptors is not None:
self._descriptors[callback] = descriptors
return descriptors

descriptors = self._descriptors[callback] = _visitor.Callback(callback).accept(_visitor.ParameterVisitor())
return descriptors

Expand Down
8 changes: 4 additions & 4 deletions alluka/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ class InjectedTypes(int, enum.Enum):
"""


InjectedTuple = typing.Union[
tuple[typing.Literal[InjectedTypes.CALLBACK], InjectedCallback],
tuple[typing.Literal[InjectedTypes.TYPE], InjectedType],
]
InjectedTuple = (
tuple[typing.Literal[InjectedTypes.CALLBACK], InjectedCallback]
| tuple[typing.Literal[InjectedTypes.TYPE], InjectedType]
)
"""Type of the tuple used to describe an injected value."""

_TypeT = type[_T]
Expand Down
80 changes: 80 additions & 0 deletions alluka/managed/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
# BSD 3-Clause License
#
# Copyright (c) 2020-2024, Faster Speeding
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Utility class for loading type dependencies based on configuration."""

from __future__ import annotations

__all__: list[str] = ["ConfigFile", "Manager", "PluginConfig", "TypeConfig"]

import typing

from . import _index
from ._config import ConfigFile as ConfigFile
from ._config import PluginConfig as PluginConfig # noqa: TC002
from ._config import TypeConfig as TypeConfig # noqa: TC002
from ._manager import Manager as Manager

if typing.TYPE_CHECKING:
from collections import abc as collections


_GLOBAL_INDEX = _index.GLOBAL_INDEX

register_config: collections.Callable[[type[PluginConfig]], None] = _GLOBAL_INDEX.register_config
"""Register a plugin configuration class.

!!! warning
Libraries should register custom configuration classes using package
entry-points tagged with the `"alluka.managed"` group.

Parameters
----------
config_cls : type[PluginConfig]
The plugin configuration class to register.

Raises
------
RuntimeError
If the configuration class' ID is already registered.
"""

register_type: collections.Callable[[TypeConfig[typing.Any]], None] = _GLOBAL_INDEX.register_type
"""Register the procedures for creating and destroying a type dependency.

!!! warning
Libraries should register custom type procedure objects using package
entry-points tagged with the `"alluka.managed"` group.

Parameters
----------
type_info : yuyo.managed.TypeConfig[typing.Any]
The type dependency's runtime procedures.
"""
Loading
Loading