base_hook.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. __package__ = 'archivebox.plugantic'
  2. import json
  3. from typing import List, Literal
  4. from pydantic import BaseModel, ConfigDict, Field, computed_field
  5. HookType = Literal['CONFIG', 'BINPROVIDER', 'BINARY', 'EXTRACTOR', 'REPLAYER', 'CHECK', 'ADMINDATAVIEW']
  6. hook_type_names: List[HookType] = ['CONFIG', 'BINPROVIDER', 'BINARY', 'EXTRACTOR', 'REPLAYER', 'CHECK', 'ADMINDATAVIEW']
  7. class BaseHook(BaseModel):
  8. """
  9. A Plugin consists of a list of Hooks, applied to django.conf.settings when AppConfig.read() -> Plugin.register() is called.
  10. Plugin.register() then calls each Hook.register() on the provided settings.
  11. each Hook.regsiter() function (ideally pure) takes a django.conf.settings as input and returns a new one back.
  12. or
  13. it modifies django.conf.settings in-place to add changes corresponding to its HookType.
  14. e.g. for a HookType.CONFIG, the Hook.register() function places the hook in settings.CONFIG (and settings.HOOKS)
  15. An example of an impure Hook would be a CHECK that modifies settings but also calls django.core.checks.register(check).
  16. In practice any object that subclasses BaseHook and provides a .register() function can behave as a Hook.
  17. setup_django() -> imports all settings.INSTALLED_APPS...
  18. # django imports AppConfig, models, migrations, admins, etc. for all installed apps
  19. # django then calls AppConfig.ready() on each installed app...
  20. builtin_plugins.npm.NpmPlugin().AppConfig.ready() # called by django
  21. builtin_plugins.npm.NpmPlugin().register(settings) ->
  22. builtin_plugins.npm.NpmConfigSet().register(settings)
  23. plugantic.base_configset.BaseConfigSet().register(settings)
  24. plugantic.base_hook.BaseHook().register(settings, parent_plugin=builtin_plugins.npm.NpmPlugin())
  25. ...
  26. ...
  27. Both core ArchiveBox code and plugin code depend on python >= 3.10 and django >= 5.0 w/ sqlite and a filesystem.
  28. Core ArchiveBox code can depend only on python and the pip libraries it ships with, and can never depend on plugin code / node / other binaries.
  29. Plugin code can depend on archivebox core, other django apps, other pip libraries, and other plugins.
  30. Plugins can provide BinProviders + Binaries which can depend on arbitrary other binaries / package managers like curl / wget / yt-dlp / etc.
  31. The execution interface between plugins is simply calling builtinplugins.npm.... functions directly, django handles
  32. importing all plugin code. There is no need to manually register methods/classes, only register to call
  33. impure setup functions or provide runtime state.
  34. settings.CONFIGS / settings.BINPROVIDERS / settings.BINARIES /... etc. are reserved for dynamic runtime state only.
  35. This state is exposed to the broader system in a flat namespace, e.g. CONFIG.IS_DOCKER=True, or BINARIES = [
  36. ..., Binary('node', abspath='/usr/local/bin/node', version='22.2.0'), ...
  37. ]
  38. """
  39. model_config = ConfigDict(
  40. extra="allow",
  41. arbitrary_types_allowed=True,
  42. from_attributes=True,
  43. populate_by_name=True,
  44. validate_defaults=True,
  45. validate_assignment=False,
  46. revalidate_instances="subclass-instances",
  47. )
  48. # verbose_name: str = Field()
  49. @computed_field
  50. @property
  51. def id(self) -> str:
  52. return self.__class__.__name__
  53. @computed_field
  54. @property
  55. def hook_module(self) -> str:
  56. return f'{self.__module__}.{self.__class__.__name__}'
  57. hook_type: HookType = Field()
  58. def register(self, settings, parent_plugin=None):
  59. """Load a record of an installed hook into global Django settings.HOOKS at runtime."""
  60. self._plugin = parent_plugin # for debugging only, never rely on this!
  61. # assert json.dumps(self.model_json_schema(), indent=4), f"Hook {self.hook_module} has invalid JSON schema."
  62. # record installed hook in settings.HOOKS
  63. settings.HOOKS[self.id] = self
  64. # print("REGISTERED HOOK:", self.hook_module)