forked from datalad/git-lad
140 lines
3.6 KiB
Python
140 lines
3.6 KiB
Python
import enum
|
|
import sys
|
|
|
|
import rich_click as click
|
|
from loguru import logger as loguru_logger
|
|
|
|
import git_lad.commands as cmds
|
|
from git_lad.cmd_resources import (
|
|
CmdResources,
|
|
ProgressMode,
|
|
ResultRenderMode,
|
|
)
|
|
|
|
|
|
class LogLevel(enum.StrEnum):
|
|
TRACE = 'TRACE'
|
|
DEBUG = 'DEBUG'
|
|
INFO = 'INFO'
|
|
SUCCESS = 'SUCCESS'
|
|
WARNING = 'WARNING'
|
|
ERROR = 'ERROR'
|
|
CRITICAL = 'CRITICAL'
|
|
|
|
|
|
def _gitlad_pdb_excepthook(type_, value, tb):
|
|
import pdb # noqa: PLC0415, T100
|
|
import traceback # noqa: PLC0415
|
|
|
|
traceback.print_exception(type_, value, tb)
|
|
print() # noqa: T201
|
|
pdb.post_mortem(tb)
|
|
|
|
|
|
def _gitlad_std_excepthook(type_, value, tb): # noqa: ARG001
|
|
# TODO: option to show a traceback maybe via ENV var switch
|
|
click.secho(f'Unexpected {type_.__name__}: {value}', fg='red', err=True)
|
|
sys.exit(1)
|
|
|
|
|
|
@click.group()
|
|
@click.option(
|
|
'--debug',
|
|
'debug',
|
|
is_flag=True,
|
|
help="""\
|
|
Drop into debugger when an unexpected error occurs.
|
|
""",
|
|
)
|
|
@click.option(
|
|
'--show-log',
|
|
'show_log',
|
|
is_flag=False,
|
|
default=None,
|
|
type=click.Choice(LogLevel, case_sensitive=False),
|
|
show_envvar=True,
|
|
help="""\
|
|
Show log messages from employed operations.
|
|
By default, this developer-targeted information is not shown,
|
|
but it can provide useful information for investigating unexpected
|
|
behavior. When enabled, log messages at or above the given level
|
|
will be written to STDERR.""",
|
|
)
|
|
@click.option(
|
|
'--progress',
|
|
'progress',
|
|
is_flag=False,
|
|
default=ProgressMode.AUTO,
|
|
type=click.Choice(ProgressMode, case_sensitive=False),
|
|
show_default=True,
|
|
help="""\
|
|
Progress reporting mode. With <off>, no progress reporting is enabled. This
|
|
does not imply no output, but turns off additional, non-default processing and
|
|
outputs done for the sake of progress reporting alone (also see --quiet for
|
|
individual commands). <rich> reports progress via a built in progress display
|
|
geared towards interactive (terminal) sessions. With <auto> an appropriate
|
|
progress reporting mode is selected based on runtime conditions.
|
|
""",
|
|
)
|
|
@click.option(
|
|
'--render-results',
|
|
'result_render_mode',
|
|
is_flag=False,
|
|
default=ResultRenderMode.AUTO,
|
|
type=click.Choice(ResultRenderMode, case_sensitive=False),
|
|
show_default=True,
|
|
help="""\
|
|
This option is a no-op at this point.
|
|
""",
|
|
)
|
|
@click.pass_context
|
|
def git_lad(
|
|
ctx,
|
|
*,
|
|
debug: bool = False,
|
|
show_log: LogLevel | None = None,
|
|
progress: ProgressMode,
|
|
result_render_mode: ResultRenderMode,
|
|
):
|
|
"""\
|
|
A lean command line interface for the DataLad world
|
|
|
|
This tool is in its infancy. Only a small subset of DataLad's
|
|
functionality is provided. But if it has what is needed,
|
|
it should be leaner and faster, and feel more modern at the
|
|
same time.
|
|
"""
|
|
sys.excepthook = _gitlad_pdb_excepthook if debug else _gitlad_std_excepthook
|
|
|
|
if show_log is not None:
|
|
# kill the default
|
|
loguru_logger.remove()
|
|
# set the requested level
|
|
loguru_logger.add(sys.stderr, level=str(show_log))
|
|
# enable log message from the core lib
|
|
loguru_logger.enable('datalad_core')
|
|
|
|
# ctx here is coming from the test running
|
|
cmdres = CmdResources(progress, result_render_mode)
|
|
if ctx.obj and 'progress_handler' in ctx.obj:
|
|
cmdres.progress_handler = ctx.obj['progress_handler']
|
|
ctx.obj = ctx.with_resource(cmdres)
|
|
|
|
|
|
for cmd in dir(cmds):
|
|
if cmd.startswith('_'):
|
|
continue
|
|
git_lad.add_command(getattr(cmds, cmd))
|
|
|
|
|
|
def main():
|
|
git_lad(
|
|
auto_envvar_prefix='GITLAD',
|
|
show_default=True,
|
|
# raises instead of exiting
|
|
# standalone_mode=False,
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|