forked from crs4/rocrate-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
112 lines (99 loc) · 3.79 KB
/
main.py
File metadata and controls
112 lines (99 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright (c) 2024-2026 CRS4
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import rich_click as click
from rocrate_validator.utils import log as logging
from rocrate_validator.cli.utils import running_in_jupyter
from rocrate_validator.utils.io_helpers.output.console import Console
from rocrate_validator.utils.io_helpers.output.pager import SystemPager
from rocrate_validator.utils.versioning import get_version
# set up logging
logger = logging.getLogger(__name__)
__all__ = ["cli", "click"]
@click.group(invoke_without_command=True)
@click.rich_config(help_config=click.RichHelpConfiguration(text_markup="rich"))
@click.option(
'--debug',
is_flag=True,
help="Enable debug logging",
default=False
)
@click.option(
'-v',
'--version',
is_flag=True,
help="Show the version of the rocrate-validator package",
default=False
)
@click.option(
'-y',
'--no-interactive',
is_flag=True,
help="Disable interactive mode",
default=False
)
@click.option(
'--disable-color',
is_flag=True,
help="Disable colored console output",
default=False
)
@click.pass_context
def cli(ctx: click.Context, debug: bool, version: bool, disable_color: bool, no_interactive: bool):
ctx.ensure_object(dict)
# determine if the console is interactive
interactive = sys.stdout.isatty() and not no_interactive and not running_in_jupyter()
console = Console(no_color=disable_color or not interactive, interactive=interactive)
# pass the console to subcommands through the click context, after configuration
ctx.obj['console'] = console
ctx.obj['pager'] = SystemPager()
ctx.obj['interactive'] = interactive
try:
# Check the python version
# from rocrate_validator.utils import check_python_version, get_min_python_version
# if not check_python_version():
# console.print(
# "\n[bold][red]ERROR:[/red] A Python version "
# f"{'.'.join([str(_) for _ in get_min_python_version()])} or newer is required ! [/bold]")
# sys.exit(1)
# If the version flag is set, print the version and exit
if version:
console.print(
f"[bold]rocrate-validator [cyan]{get_version()}[/cyan][/bold]")
sys.exit(0)
# Set the log level
logging.basicConfig(level=logging.DEBUG if debug else logging.WARNING)
# If no subcommand is provided, invoke the default command
if ctx.invoked_subcommand is None:
# If no subcommand is provided, invoke the default command
from rocrate_validator.cli.commands.validate import validate
ctx.invoke(validate)
else:
logger.debug("Command invoked: %s", ctx.invoked_subcommand)
except Exception as e:
console.print(
f"\n\n[bold][[red]FAILED[/red]] Unexpected error: {e} !!![/bold]\n", style="white")
console.print("""This error may be due to a bug.
Please report it to the issue tracker
along with the following stack trace:
""")
console.print_exception()
sys.exit(2)
if __name__ == "__main__":
try:
cli()
except Exception as e:
if logger.isEnabledFor(logging.DEBUG):
logger.exception(f"An unexpected error occurred: {e}")
exit(2)