Skip to content

Commit

Permalink
feat: add register and serve functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexismanuel committed Jul 8, 2024
1 parent 36ad62b commit 5f35559
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ditto/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
from __future__ import annotations
import inspect
from functools import wraps


def inject(func):
@wraps(func)
def wrapper(*args, **kwargs):
sig = inspect.signature(func)
for name, param in sig.parameters.items():
if name not in kwargs and param.default == inspect.Parameter.empty:
kwargs[name] = ServiceRegistry.get_instance().get(name)
return func(*args, **kwargs)

return wrapper


def register(service):
registry = ServiceRegistry.get_instance()
service_class = service if inspect.isclass(service) else service.__class__
base_class = service_class.__bases__.get(0, service_class)
registry.register(base_class, service)


class ServiceRegistry:
Expand All @@ -22,3 +43,4 @@ def get_instance(cls) -> ServiceRegistry:
if cls.instance is None:
cls.instance = cls()
return cls.instance

0 comments on commit 5f35559

Please sign in to comment.