Skip to content

Commit

Permalink
refactor(example): ♻️ implementation done
Browse files Browse the repository at this point in the history
  • Loading branch information
miragecentury committed Aug 8, 2024
1 parent 5c9696d commit 59bb386
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
25 changes: 23 additions & 2 deletions src/python_factory/core/app/abstracts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@
Provides the abstract class for the application.
"""

from abc import ABC
from typing import cast
from typing import Generic, TypeVar, cast, get_args

import injector

from .config import AppConfigAbstract
from .fastapi_abstract import FastAPIAbstract

APP_T = TypeVar("APP_T", bound="AppBase") # pylint: disable=invalid-name
CONFIG_T = TypeVar("CONFIG_T", bound=AppConfigAbstract) # pylint: disable=invalid-name


class AppBase(FastAPIAbstract):
"""
Application abstract class.
"""

PACKAGE_NAME: str = ""

def __init__(self, config: AppConfigAbstract) -> None:

if self.PACKAGE_NAME == "":
raise ValueError(
"The package name must be set in the concrete application class."
)

self._config: AppConfigAbstract = config
FastAPIAbstract.__init__(self=cast(FastAPIAbstract, self), config=self._config)

Expand All @@ -23,3 +35,12 @@ def get_config(self) -> AppConfigAbstract:
Get the application configuration.
"""
return self._config


class GenericAppModule(Generic[APP_T, CONFIG_T], injector.Module):

def configure(self, binder: injector.Binder) -> None:
app_concrete_class, _ = get_args(
self.__orig_bases__[0] # type: ignore[attr-defined]
)
binder.bind(interface=app_concrete_class, to=app_concrete_class)
2 changes: 0 additions & 2 deletions src/python_factory/core/app/abstracts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@


class AppConfigAbstract(FastAPIConfigAbstract):

package_name: str
environment: EnvironmentEnum
23 changes: 16 additions & 7 deletions src/python_factory/example/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import injector

from python_factory.core.app import AppBase, EnvironmentEnum
from python_factory.core.app import AppBase
from python_factory.core.app.abstracts import GenericAppModule
from python_factory.core.utils.importlib import get_path_file_in_package
from python_factory.core.utils.yaml_reader import YamlFileReader

from .config import AppConfig

Expand All @@ -14,24 +17,30 @@ class App(AppBase):
Concrete application class.
"""

PACKAGE_NAME: str = "python_factory.example"

def __init__(self, config: injector.Inject[AppConfig]) -> None:
super().__init__(config=config)


class AppInjectorModule(injector.Module):
def configure(self, binder: injector.Binder) -> None:
binder.bind(App, to=App)
class AppModule(GenericAppModule[App, AppConfig]):

@injector.singleton
@injector.provider
def app_config_provider(self) -> AppConfig:
def provider_for_app_config(self) -> AppConfig:
return AppConfig(
environment=EnvironmentEnum.DEVELOPMENT,
**YamlFileReader(
file_path=get_path_file_in_package(
filename="application.yaml", package=App.PACKAGE_NAME
),
yaml_base_key="application",
use_environment_injection=True,
).read()
)


def app_factory() -> App:
"""
Provides the application factory.
"""
return injector.Injector(modules=[AppInjectorModule]).get(interface=App)
return injector.Injector(modules=[AppModule]).get(interface=App)
1 change: 0 additions & 1 deletion src/python_factory/example/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@


class AppConfig(AppConfigAbstract):
package_name: str = "python_factory.example"
title: str = "Python Factory Example"
description: str = "An example application for Python Factory."
8 changes: 8 additions & 0 deletions src/python_factory/example/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
application:
title: Python Factory Example
description: An example application for Python Factory
version: 0.1.0
environment: ${ENVIRONMENT:development}
debug: ${DEBUG:false}
reload: ${RELOAD:false}

0 comments on commit 59bb386

Please sign in to comment.