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