1
0

base.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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
  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,
  13. task: Callable[P, list[tuple[str, str]] | tuple[str, str] | None],
  14. /,
  15. *args: P.args,
  16. **kwargs: P.kwargs,
  17. ) -> None:
  18. """Add a task to the pre-compile context.
  19. Args:
  20. task: The task to add.
  21. args: The arguments to pass to the task
  22. kwargs: The keyword arguments to pass to the task
  23. """
  24. class PreCompileContext(CommonContext):
  25. """Context for pre-compile hooks."""
  26. add_save_task: AddTaskProtcol
  27. add_modify_task: Callable[[str, Callable[[str], str]], None]
  28. class Plugin:
  29. """Base class for all plugins."""
  30. def get_frontend_development_dependencies(
  31. self, **context: Unpack[CommonContext]
  32. ) -> list[str] | set[str] | tuple[str, ...]:
  33. """Get the NPM packages required by the plugin for development.
  34. Args:
  35. context: The context for the plugin.
  36. Returns:
  37. A list of packages required by the plugin for development.
  38. """
  39. return []
  40. def get_frontend_dependencies(
  41. self, **context: Unpack[CommonContext]
  42. ) -> list[str] | set[str] | tuple[str, ...]:
  43. """Get the NPM packages required by the plugin.
  44. Args:
  45. context: The context for the plugin.
  46. Returns:
  47. A list of packages required by the plugin.
  48. """
  49. return []
  50. def get_static_assets(
  51. self, **context: Unpack[CommonContext]
  52. ) -> Sequence[tuple[Path, str | bytes]]:
  53. """Get the static assets required by the plugin.
  54. Args:
  55. context: The context for the plugin.
  56. Returns:
  57. A list of static assets required by the plugin.
  58. """
  59. return []
  60. def get_stylesheet_paths(self, **context: Unpack[CommonContext]) -> Sequence[str]:
  61. """Get the paths to the stylesheets required by the plugin relative to the styles directory.
  62. Args:
  63. context: The context for the plugin.
  64. Returns:
  65. A list of paths to the stylesheets required by the plugin.
  66. """
  67. return []
  68. def pre_compile(self, **context: Unpack[PreCompileContext]) -> None:
  69. """Called before the compilation of the plugin.
  70. Args:
  71. context: The context for the plugin.
  72. """