Total Complexity | 2 |
Total Lines | 42 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
1 | """ |
||
2 | byceps.cli.command.import_roles |
||
3 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
4 | |||
5 | Import roles and their assigned permissions from a TOML file. |
||
6 | |||
7 | :Copyright: 2014-2024 Jochen Kupperschmidt |
||
8 | :License: Revised BSD (see `LICENSE` file for details) |
||
9 | """ |
||
10 | |||
11 | from pathlib import Path |
||
12 | |||
13 | import click |
||
14 | from flask.cli import with_appcontext |
||
15 | |||
16 | from byceps.services.authz import impex_service |
||
17 | |||
18 | |||
19 | _DEFAULT_DATA_FILE = Path('scripts') / 'data' / 'roles.toml' |
||
20 | |||
21 | |||
22 | @click.command() |
||
23 | @click.option( |
||
24 | '-f', |
||
25 | '--file', |
||
26 | 'data_file', |
||
27 | type=click.Path(exists=True, dir_okay=False, path_type=Path), |
||
28 | default=_DEFAULT_DATA_FILE, |
||
29 | ) |
||
30 | @with_appcontext |
||
31 | def import_roles(data_file: Path) -> None: |
||
32 | """Import authorization roles.""" |
||
33 | _import_roles(data_file) |
||
34 | |||
35 | |||
36 | def _import_roles(data_file: Path) -> None: |
||
37 | click.echo('Importing roles ... ', nl=False) |
||
38 | role_counts = impex_service.import_roles(data_file) |
||
39 | click.secho('done. ', fg='green', nl=False) |
||
40 | click.secho( |
||
41 | f'Imported {role_counts.imported} roles, ' |
||
42 | f'skipped {role_counts.skipped} roles.', |
||
44 |