-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Packaging: Prepare src layout migration
- Dependency injection - Service container - Service wiring Change-Id: I058ef5706b803d8a415ab088559f893288ab7c48
- Loading branch information
1 parent
e8d3632
commit 20517c1
Showing
6 changed files
with
161 additions
and
44 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
This is the module for the class for all languages. | ||
""" | ||
|
||
|
||
class LanguageTag: | ||
""" | ||
The LanguageTag class deals with language data. | ||
""" | ||
|
||
_tag: str | ||
""" | ||
_tag (str): The BCP 47 language subtag of the LanguageTag object. | ||
""" | ||
|
||
def __init__(self, language_tag) -> None: | ||
pass | ||
|
||
def get_bcp_47_tag(self) -> str | None: | ||
pass | ||
|
||
def get_discord_code(self) -> str | None: | ||
pass | ||
|
||
def get_fallbacks(self) -> list | None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
This is the module for creating the objects for all languages. | ||
""" | ||
|
||
# Local imports | ||
from .language_tag import LanguageTag | ||
|
||
|
||
class LanguageTagFactory: | ||
""" | ||
The LanguageTagFactory class deals with LanguageTag object creations. | ||
""" | ||
|
||
_tags: dict = {} | ||
""" | ||
_tags (dict): The LanguageTag objects. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
pass | ||
|
||
def get_tag(self) -> LanguageTag | None: | ||
pass | ||
|
||
def get_tag_by_discord_code(self) -> LanguageTag | None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
Module for SCAICT-uwu service locator. | ||
""" | ||
|
||
# Standard imports | ||
import importlib | ||
import sys | ||
|
||
|
||
class ServiceContainer: | ||
""" | ||
Service locator for SCAICT-uwu core services. | ||
Refer to service_wiring.py for the default implementations. | ||
""" | ||
|
||
_services: dict = {} | ||
""" | ||
""" | ||
|
||
_service_instantiators: dict = {} | ||
""" | ||
""" | ||
|
||
_services_being_created: dict = {} | ||
""" | ||
""" | ||
|
||
def load_wiring_files(self, wiring_modules: list) -> None: | ||
for module_name in wiring_modules: | ||
module = importlib.import_module(module_name) | ||
self.apply_wiring(module.get_wiring()) | ||
|
||
def apply_wiring(self, service_instantiators) -> None: | ||
for name, instantiator in service_instantiators: | ||
self.define_service(name, instantiator) | ||
|
||
def get_service_names(self) -> list: | ||
# Convert dict_keys to list | ||
return list(self._service_instantiators.keys()) | ||
|
||
def has_service(self, name: str) -> bool: | ||
return name in self._service_instantiators | ||
|
||
def define_service(self, name: str, instantiator) -> None: | ||
if self.has_service(name): | ||
sys.exit("ServiceAlreadyDefinedException $name") | ||
|
||
self._service_instantiators[name] = instantiator | ||
|
||
def redefine_service(self, name: str, instantiator) -> None: | ||
if not self.has_service(name): | ||
sys.exit("NoSuchServiceException $name") | ||
|
||
if name in self._services: | ||
sys.exit("CannotReplaceActiveServiceException $name") | ||
|
||
self._service_instantiators[name] = instantiator | ||
|
||
def create_service(self, name: str): | ||
if not self.has_service(name): | ||
sys.exit("NoSuchServiceException $name") | ||
|
||
if not name in self._services_being_created: | ||
sys.exit( | ||
"RecursiveServiceDependencyException " | ||
+ "Circular dependency when creating service!" | ||
) | ||
|
||
self._services_being_created[name] = True | ||
|
||
service = self._service_instantiators[name](self) | ||
|
||
return service | ||
|
||
def get_service(self, name: str): | ||
if not name in self._services: | ||
self._services[name] = self.create_service(name) | ||
|
||
return self._services[name] | ||
|
||
# Service helper functions | ||
|
||
def get_config(self): | ||
return self.get_service("Config") | ||
|
||
def get_language_tag(self): | ||
return self.get_service("LanguageTag") | ||
|
||
def get_language_tag_factory(self): | ||
return self.get_service("LanguageTagFactory") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
Module for SCAICT-uwu default service implementations. | ||
""" | ||
|
||
from .service_container import ServiceContainer | ||
|
||
|
||
def get_wiring() -> dict: | ||
""" | ||
Get the service implementation functions. | ||
""" | ||
return {"Config": get_config} | ||
|
||
|
||
def get_config(service_container: ServiceContainer): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters