base.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Base class for all plugins."""
  2. from collections.abc import Callable, Sequence
  3. from pathlib import Path
  4. from typing import ParamSpec, Protocol, TypedDict, TypeVarTuple
  5. from typing_extensions import Unpack
  6. class CommonContext(TypedDict):
  7. """Common context for all plugins."""
  8. P = ParamSpec("P")
  9. class AddTaskProtcol(Protocol):
  10. """Protocol for adding a task to the pre-compile context."""
  11. def __call__(
  12. self, task: Callable[P, tuple[str, str]], /, *args: P.args, **kwargs: P.kwargs
  13. ) -> None:
  14. """Add a task to the pre-compile context.
  15. Args:
  16. task: The task to add.
  17. args: The arguments to pass to the task
  18. kwargs: The keyword arguments to pass to the task
  19. """
  20. class PreCompileContext(CommonContext):
  21. """Context for pre-compile hooks."""
  22. add_task: AddTaskProtcol
  23. Types = TypeVarTuple("Types")
  24. class Plugin:
  25. """Base class for all plugins."""
  26. def get_frontend_development_dependancies(
  27. self, **context: Unpack[CommonContext]
  28. ) -> list[str] | set[str] | tuple[str, ...]:
  29. """Get the NPM packages required by the plugin for development.
  30. Args:
  31. context: The context for the plugin.
  32. Returns:
  33. A list of packages required by the plugin for development.
  34. """
  35. return []
  36. def get_frontend_dependancies(
  37. self, **context: Unpack[CommonContext]
  38. ) -> list[str] | set[str] | tuple[str, ...]:
  39. """Get the NPM packages required by the plugin.
  40. Args:
  41. context: The context for the plugin.
  42. Returns:
  43. A list of packages required by the plugin.
  44. """
  45. return []
  46. def get_static_assets(
  47. self, **context: Unpack[CommonContext]
  48. ) -> Sequence[tuple[Path, str | bytes]]:
  49. """Get the static assets required by the plugin.
  50. Args:
  51. context: The context for the plugin.
  52. Returns:
  53. A list of static assets required by the plugin.
  54. """
  55. return []
  56. def get_stylesheet_paths(self, **context: Unpack[CommonContext]) -> Sequence[str]:
  57. """Get the paths to the stylesheets required by the plugin relative to the styles directory.
  58. Args:
  59. context: The context for the plugin.
  60. Returns:
  61. A list of paths to the stylesheets required by the plugin.
  62. """
  63. return []
  64. def pre_compile(self, **context: Unpack[PreCompileContext]) -> None:
  65. """Called before the compilation of the plugin.
  66. Args:
  67. context: The context for the plugin.
  68. """