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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# ==================================================================================================================== #
# _____ _ _ ____ _ _ _ _ _ #
# _ __ _ |_ _|__ ___ | (_)_ __ __ _ / ___(_) |_| | | |_ _| |__ #
# | '_ \| | | || |/ _ \ / _ \| | | '_ \ / _` || | _| | __| |_| | | | | '_ \ #
# | |_) | |_| || | (_) | (_) | | | | | | (_| || |_| | | |_| _ | |_| | |_) | #
# | .__/ \__, ||_|\___/ \___/|_|_|_| |_|\__, (_)____|_|\__|_| |_|\__,_|_.__/ #
# |_| |___/ |___/ #
# ==================================================================================================================== #
# Authors: #
# Patrick Lehmann #
# #
# License: #
# ==================================================================================================================== #
# Copyright 2026-2026 Patrick Lehmann - Bötzingen, Germany #
# #
# 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. #
# #
# SPDX-License-Identifier: Apache-2.0 #
# ==================================================================================================================== #
#
"""
The command line interface of pyTooling.GitHub, and its :program:`pytooling-github` program.
.. rubric:: Usage
.. code-block:: bash
pytooling-github help
pytooling-github version
.. hint::
See :ref:`high-level help <CLI>` for explanations and usage examples.
"""
from argparse import RawDescriptionHelpFormatter, Namespace
from textwrap import dedent
from typing import ClassVar, NoReturn
from pyTooling.Decorators import export
from pyTooling.Exceptions import ExceptionBase, MissingDependencyError, ToolingException
from pyTooling.Attributes.ArgParse import ArgParseHelperMixin, DefaultHandler, CommandHandler
from pyTooling.Attributes.ArgParse.Flag import FlagArgument
from pyTooling.Attributes.ArgParse.Argument import StringArgument
from pyTooling.TerminalUI import TerminalApplication, Mode
from pyTooling.GitHub.CLI.Pipeline import PipelineHandlers
@export
class Application(TerminalApplication, PipelineHandlers, ArgParseHelperMixin):
"""
The :program:`pytooling-github` program: a terminal application whose commands are declared as attributes.
Every command is a method marked with :class:`~pyTooling.Attributes.ArgParse.CommandHandler`, and the arguments
of a command are the attributes written on that method. A group of related commands is a mixin-class of its own,
which this class inherits from - so a new command is a new mixin and one more base-class, and nothing here has
to know what it does.
"""
HeadLine: ClassVar[str] = "pyTooling.GitHub Service Program" #: Headline printed above every command's output.
def __init__(self) -> None:
"""Initializes the program, its terminal and its command line parser."""
super().__init__(Mode.TextToStdOut_ErrorsToStdErr)
textWidth = min(self.Width, 160)
class HelpFormatter(RawDescriptionHelpFormatter):
"""Nested class widening argparse's help page to the terminal, and its option column to 30 characters."""
def __init__(self, *args, **kwargs) -> None:
"""
Initializes the formatter with the widths this program wants.
:param args: Positional parameters, passed on to :class:`~argparse.RawDescriptionHelpFormatter`.
:param kwargs: Keyword parameters, passed on after the two widths were set.
"""
kwargs["max_help_position"] = 30
kwargs["width"] = textWidth
super().__init__(*args, **kwargs)
ArgParseHelperMixin.__init__(
self,
prog="pytooling-github",
description=dedent(f"""\
'{self.HeadLine}' to work with GitHub pipelines.
"""),
formatter_class=HelpFormatter,
add_help=False
)
def Run(self, enableAutoComplete: bool = True) -> None:
"""
Parses the command line and dispatches to the handler of the command it names.
:param enableAutoComplete: Optional, register the parser with ``argcomplete``, if that package is installed.
Default: ``True``.
"""
ArgParseHelperMixin.Run(self, enableAutoComplete)
@DefaultHandler()
@FlagArgument("-q", "--quiet", dest="quiet", help="Reduce messages to a minimum.")
@FlagArgument("-v", "--verbose", dest="verbose", help="Print out detailed messages.")
@FlagArgument("-d", "--debug", dest="debug", help="Enable debug mode.")
def HandleDefault(self, _: Namespace) -> None:
"""Handle program calls without any command."""
self._PrintHeadline()
self._PrintHelp()
@CommandHandler("help", help="Display help page(s) for the given command name.",
description="Display help page(s) for the given command name.")
@StringArgument(dest="Command", metaName="Command", optional=True, help="Print help page(s) for a command.")
def HandleHelp(self, args: Namespace) -> None:
"""
Handle program calls with command ``help``.
:param args: The parsed command line, whose ``Command`` names the command to print the help page of.
"""
self._PrintHeadline()
self._PrintHelp(args.Command)
@CommandHandler("version", help="Display version information.", description="Display version information.")
def HandleVersion(self, _: Namespace) -> None:
"""Handle program calls with command ``version``."""
from pyTooling import GitHub as DunderModule
self._PrintHeadline()
self._PrintVersion(DunderModule, "pyTooling.GitHub")
@export
def main() -> NoReturn:
"""
Entrypoint to start program execution.
This function is called either from :pycode:`if __name__ == "__main__":` or through the ``console_scripts``
entry point :program:`pytooling-github`, which :file:`setup.py` registers.
It creates the :class:`Application` and runs it in a ``try ... except`` environment, so an exception is reported
as a message and a non-zero exit code rather than as a traceback.
"""
from sys import argv
program = Application()
program.Configure(
verbose=("-v" in argv or "--verbose" in argv),
debug=("-d" in argv or "--debug" in argv),
silent=("-q" in argv or "--quiet" in argv)
)
try:
program.Run()
except MissingDependencyError as ex:
program.PrintMissingDependencyError(ex)
except ToolingException as ex:
program.WriteLineToStdErr(f"{{RED}}[ERROR] {ex}{{NOCOLOR}}".format(**program.Foreground))
if ex.__cause__ is not None:
program.WriteLineToStdErr(f"{{DARK_YELLOW}}Because of: {ex.__cause__}{{NOCOLOR}}".format(**program.Foreground))
for note in getattr(ex, "__notes__", ()) or ():
program.WriteLineToStdErr(f"{{DARK_YELLOW}} [NOTE] {note}{{NOCOLOR}}".format(**program.Foreground))
program.Exit(1)
except ExceptionBase as ex:
program.PrintExceptionBase(ex)
except NotImplementedError as ex:
program.PrintNotImplementedError(ex)
except Exception as ex:
program.PrintException(ex)
if __name__ == "__main__":
main()
|