change is_registered and is_ready into private model fields

This commit is contained in:
Nick Sweeting 2024-09-22 19:27:00 -07:00
parent 8f38f70e4a
commit 7f05026022
No known key found for this signature in database
2 changed files with 15 additions and 16 deletions

View file

@ -60,18 +60,18 @@ class BaseHook(BaseModel):
ignored_types=(TaskWrapper, ),
)
hook_type: ClassVar[HookType] # e.g. = 'CONFIG'
# verbose_name: str = Field()
is_registered: bool = False
is_ready: bool = False
_is_registered: bool = False
_is_ready: bool = False
@computed_field
@property
def id(self) -> str:
return self.__class__.__name__
@computed_field
@property
def hook_module(self) -> str:
"""e.g. builtin_plugins.singlefile.apps.SinglefileConfigSet"""
@ -91,7 +91,6 @@ class BaseHook(BaseModel):
def plugin_dir(self) -> Path:
return Path(inspect.getfile(self.__class__)).parent.resolve()
hook_type: HookType = Field()
def register(self, settings, parent_plugin=None):
"""Load a record of an installed hook into global Django settings.HOOKS at runtime."""
@ -102,10 +101,10 @@ class BaseHook(BaseModel):
# record installed hook in settings.HOOKS
settings.HOOKS[self.id] = self
if settings.HOOKS[self.id].is_registered:
if settings.HOOKS[self.id]._is_registered:
raise Exception(f"Tried to run {self.hook_module}.register() but its already been called!")
settings.HOOKS[self.id].is_registered = True
settings.HOOKS[self.id]._is_registered = True
# print("REGISTERED HOOK:", self.hook_module)
@ -114,7 +113,7 @@ class BaseHook(BaseModel):
assert self.id in settings.HOOKS, f"Tried to ready hook {self.hook_module} but it is not registered in settings.HOOKS."
if settings.HOOKS[self.id].is_ready:
if settings.HOOKS[self.id]._is_ready:
raise Exception(f"Tried to run {self.hook_module}.ready() but its already been called!")
settings.HOOKS[self.id].is_ready = True
settings.HOOKS[self.id]._is_ready = True

View file

@ -34,8 +34,8 @@ class BasePlugin(BaseModel):
# All the hooks the plugin will install:
hooks: List[InstanceOf[BaseHook]] = Field(default=[])
is_registered: bool = False
is_ready: bool = False
_is_registered: bool = False
_is_ready: bool = False
@computed_field
@property
@ -114,13 +114,13 @@ class BasePlugin(BaseModel):
### Mutate django.conf.settings... values in-place to include plugin-provided overrides
settings.PLUGINS[self.id] = self
if settings.PLUGINS[self.id].is_registered:
if settings.PLUGINS[self.id]._is_registered:
raise Exception(f"Tried to run {self.plugin_module}.register() but its already been called!")
for hook in self.hooks:
hook.register(settings, parent_plugin=self)
settings.PLUGINS[self.id].is_registered = True
settings.PLUGINS[self.id]._is_registered = True
# print('√ REGISTERED PLUGIN:', self.plugin_module)
def ready(self, settings=None):
@ -131,16 +131,16 @@ class BasePlugin(BaseModel):
settings = django_settings
assert (
self.id in settings.PLUGINS and settings.PLUGINS[self.id].is_registered
self.id in settings.PLUGINS and settings.PLUGINS[self.id]._is_registered
), f"Tried to run plugin.ready() for {self.plugin_module} but plugin is not yet registered in settings.PLUGINS."
if settings.PLUGINS[self.id].is_ready:
if settings.PLUGINS[self.id]._is_ready:
raise Exception(f"Tried to run {self.plugin_module}.ready() but its already been called!")
for hook in self.hooks:
hook.ready(settings)
settings.PLUGINS[self.id].is_ready = True
settings.PLUGINS[self.id]._is_ready = True
# @validate_call
# def install_binaries(self) -> Self: