-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
75 additions
and
40 deletions.
There are no files selected for viewing
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
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
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,47 @@ | ||
from typing import TYPE_CHECKING, Dict | ||
|
||
from src.couch_potato.errors import RegistryError | ||
|
||
if TYPE_CHECKING: | ||
from src.couch_potato import CouchPotato | ||
from src.couch_potato._model import BaseModel | ||
|
||
|
||
class ModelRegistry: | ||
def __init__(self): | ||
self._models: Dict[str, "BaseModel"] = {} | ||
|
||
def register(self, model) -> None: | ||
if model.__name__ in self._models: | ||
raise RegistryError(f"Model with name {model.__name__} already registered") | ||
self._models[model.__name__] = model | ||
|
||
|
||
class InstanceRegistry: | ||
def __init__(self): | ||
self._instances: Dict["CouchPotato", ModelRegistry] = {} | ||
|
||
def register(self, instance) -> None: | ||
if instance in self._instances: | ||
raise RegistryError("Instance already registered") | ||
self._instances[instance] = ModelRegistry() | ||
|
||
def contains(self, instance) -> bool: | ||
return instance in self._instances.keys() | ||
|
||
def get_model_registry(self, instance) -> ModelRegistry: | ||
if not self.contains(instance): | ||
raise RegistryError("Instance is not registered") | ||
return self._instances[instance] | ||
|
||
|
||
_model_registry = ModelRegistry() | ||
_instance_registry = InstanceRegistry() | ||
|
||
|
||
def get_model_registry(): | ||
return _model_registry | ||
|
||
|
||
def get_instance_registry(): | ||
return _instance_registry |
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
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