-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path_main.py
More file actions
74 lines (62 loc) · 2.4 KB
/
_main.py
File metadata and controls
74 lines (62 loc) · 2.4 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
# Copyright 2016 Red Hat, Inc.
#
# 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.
"""
Highest level runner.
"""
# isort: STDLIB
from typing import Callable
# isort: THIRDPARTY
import justbytes as jb
from ._error_reporting import handle_error
from ._errors import StratisCliActionError, StratisCliEnvironmentError
from ._parser import PARSER
def run() -> Callable:
"""
Generate a function that parses arguments and executes.
"""
# Set default configuration parameters for display of sizes, i.e., values
# that are generally given in bytes or some multiple thereof.
jb.Config.set_display_config(jb.DisplayConfig(show_approx_str=False))
def the_func(command_line_args):
"""
Run according to the arguments passed.
"""
namespace = PARSER.parse_args(command_line_args)
post_parser = getattr(namespace, "post_parser", None)
if post_parser is not None:
post_parser(namespace).verify(namespace, PARSER)
try:
try:
namespace.func(namespace)
# Keyboard Interrupt is recaught at the outermost possible layer.
# It is outside the regular execution of the program, so it is
# handled only there; it is just reraised here.
# The same holds for SystemExit, except that it must be allowed
# to propagate to the interpreter, i.e., it should not be recaught
# anywhere.
except (
BrokenPipeError,
KeyboardInterrupt,
SystemExit,
StratisCliEnvironmentError,
) as err:
raise err
except BaseException as err:
raise StratisCliActionError(command_line_args, namespace) from err
except StratisCliActionError as err:
if namespace.propagate:
raise
handle_error(err)
return 0
return the_func