Repository navigation
Expand file tree
/
Copy pathpilotcli.py
More file actions
86 lines (66 loc) · 2.9 KB
/
Copy pathpilotcli.py
File metadata and controls
86 lines (66 loc) · 2.9 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
# Copyright (C) 2022-2026 Indoc Systems
#
# Contact Indoc Systems for any questions regarding the use of this source code.
from multiprocessing import freeze_support
import click
import pkg_resources
from click.formatting import HelpFormatter
from packaging.version import Version
import app.services.output_manager.error_handler as error_handler
import app.services.output_manager.message_handler as mhandler
from app.commands.entry_point import command_groups, entry_point
from app.services.output_manager.help_page import get_cli_help_message
from app.utils.aggregated import doc, get_latest_cli_version, get_version_compatibility
class CustomHelpFormatter(HelpFormatter):
def __init__(self, *args, **kwargs):
# Set a desired width to avoid wrapping of the help text
kwargs['width'] = 200
super().__init__(*args, **kwargs)
def write_dl(self, rows, col_max=80, col_spacing=2):
if rows:
# get max width of command
widths = max([len(row[0]) for row in rows])
for command, help_doc in rows:
self.write(f'{command:<{widths}} ')
if help_doc:
while len(help_doc) > col_max:
# find the last space within the col_max
last_space = help_doc[:col_max].rfind(' ')
if last_space == -1:
last_space = col_max
last_space += 1
self.write(f'{help_doc[:last_space]}\n')
# filling the new line with indent spacing
self.write(' ' * (widths + col_spacing))
help_doc = help_doc[last_space:]
self.write(f'{help_doc}')
self.write('\n')
click.Context.formatter_class = CustomHelpFormatter
class ComplexCLI(click.MultiCommand):
def format_help_text(self, ctx, formatter):
latest_version, download_url = get_latest_cli_version()
current_version = pkg_resources.get_distribution('app').version
if Version(current_version) < latest_version:
mhandler.SrvOutPutHandler.newer_version_available(latest_version, download_url)
get_version_compatibility(current_version)
click.MultiCommand.format_help_text(self, ctx, formatter)
def list_commands(self, ctx):
rv = command_groups()
rv.sort()
return rv
def get_command(self, ctx, name):
try:
mod = __import__(f'app.commands.{name}', None, None, ['cli'])
except ImportError:
return
return mod.cli
@click.command(cls=ComplexCLI)
@doc(get_cli_help_message())
def cli():
try:
entry_point()
except Exception as e:
error_handler.SrvErrorHandler.default_handle(e, True)
if __name__ == '__main__':
freeze_support() # Add support for multiprocessing after bundling with PyInstaller
cli()