licenses_app.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright (c) Jupyter Development Team.
  2. # Distributed under the terms of the Modified BSD License.
  3. """A license reporting CLI
  4. Mostly ready-to-use, the downstream must provide the location of the application's
  5. static resources. Licenses from an app's federated_extensions will also be discovered
  6. as configured in `labextensions_path` and `extra_labextensions_path`.
  7. from traitlets import default
  8. from jupyterlab_server import LicensesApp
  9. class MyLicensesApp(LicensesApp):
  10. version = "0.1.0"
  11. @default("static_dir")
  12. def _default_static_dir(self):
  13. return "my-static/"
  14. class MyApp(JupyterApp, LabConfig):
  15. ...
  16. subcommands = dict(
  17. licenses=(MyLicensesApp, MyLicensesApp.description.splitlines()[0])
  18. )
  19. """
  20. from typing import Any
  21. from jupyter_core.application import JupyterApp, base_aliases, base_flags
  22. from traitlets import Bool, Enum, Instance, Unicode
  23. from ._version import __version__
  24. from .config import LabConfig
  25. from .licenses_handler import LicensesManager
  26. class LicensesApp(JupyterApp, LabConfig):
  27. """A license management app."""
  28. version = __version__
  29. description = """
  30. Report frontend licenses
  31. """
  32. static_dir = Unicode("", config=True, help="The static directory from which to show licenses")
  33. full_text = Bool(False, config=True, help="Also print out full license text (if available)")
  34. report_format = Enum(
  35. ["markdown", "json", "csv"], "markdown", config=True, help="Reporter format"
  36. )
  37. bundles_pattern = Unicode(".*", config=True, help="A regular expression of bundles to print")
  38. licenses_manager = Instance(LicensesManager)
  39. aliases = {
  40. **base_aliases,
  41. "bundles": "LicensesApp.bundles_pattern",
  42. "report-format": "LicensesApp.report_format",
  43. }
  44. flags = {
  45. **base_flags,
  46. "full-text": (
  47. {"LicensesApp": {"full_text": True}},
  48. "Print out full license text (if available)",
  49. ),
  50. "json": (
  51. {"LicensesApp": {"report_format": "json"}},
  52. "Print out report as JSON (implies --full-text)",
  53. ),
  54. "csv": (
  55. {"LicensesApp": {"report_format": "csv"}},
  56. "Print out report as CSV (implies --full-text)",
  57. ),
  58. }
  59. def initialize(self, *args: Any, **kwargs: Any) -> None:
  60. """Initialize the app."""
  61. super().initialize(*args, **kwargs)
  62. self.init_licenses_manager()
  63. def init_licenses_manager(self) -> None:
  64. """Initialize the license manager."""
  65. self.licenses_manager = LicensesManager(
  66. parent=self,
  67. )
  68. def start(self) -> None:
  69. """Start the app."""
  70. report = self.licenses_manager.report(
  71. report_format=self.report_format,
  72. full_text=self.full_text,
  73. bundles_pattern=self.bundles_pattern,
  74. )[0]
  75. print(report)
  76. self.exit(0)