jw.pkg: Fix "make check" static code check fallout
The previous commits have put rules for linting and formatting via ruff, yapf, mypy and pyright into place. They are checked with the make check target, and this commit adds the fixes for the target to succeed.
It does some refactoring where type checking dug up dirty bits, and also adds lots of churn in the Python code. To a good deal, that's owed to mere formatting changes. It would have been better to seperate those from syntax and refactoring fixes into multiple commits, so that the interesting changes don't drown in the formatting nose. However, that would have been a lot of additional work only to be thrown away by later commits, hence this commit has a big diff in one piece. The size of the diff is regrettable but hopefully a one-off: What it buys is automatic format checking for CI and predictble formats for smaller diffs in the future.
Rules that "make check" enforces are, in the following order
- Syntax checkers:
- ruff check . - mypy . - pyright- Format check:
- yapf --diff --recursive .The refactoring includes:
- Turn the Result class into a more elaborate object, capable of doing more heavy lifting around stderr and stdout decoding, summarizing outcome, and matching error strings.Aside from fixing broken type checks, this also removes lots of boilerplate calling code which is currently used for handling possible call outcome scenarios. Trying to access an inexistent, decoded string should raise a meaningful exception by itself now, which removes lots of code with case distinctions.- Fix Cmd type hierarchy:
- Add the AbstractCmd class above Cmd. This is necessary because the checker rightfully complains it can't instantiate a Cmd instance where constructor arguments were needed. They never were, but the type used at the instantiating code's location in jw.pkg.App so claims.- Lots of sub- and sub-subcommands are derived from the base class of the invoking command. That provides some properties shared across the ancestor hierarchy of a command, but is semantically unsound. Fix that by introducing jw.pkg.BaseCmd class as a place to provide basic helpers shared across all commands used in a jw.pkg.App's context, and derive all command classes from that afresh. The parent command is still reachable via a common parent property.Formatting changes are conforming to PEP-8, mostly, with minor tweaks. All in all they include the following changes.
- Remove # -*- coding: utf-8 -*-
The line was needed by Python 2 which is not supported anylonger. For Python 3, the default encoding is UTF-8, anyway.- Allow to run "make py-format" without having it produce any changes. It's basically "yapf --in-place --recursive ." with some code style settings, see conf/topdir/pyproject.toml. The settings may be debatable. I've had custom tweaks in place on that target, too, but then again, IDEs would have more hassle to integrate that.- Introduce a 88 character line length limit
- One import per line, reshuffle them semantically, see [tool.isort] in pyproject.toml.- Hide imports needed for type-checking only behind
if TYPE_CHECKING- Spaces around assignments accounts for much churn. Having having no spaces in inline parameter list assignments and default parameter values would arguably be more compact where it's useful. On the other hand, I have not found a code formatter which allows spaces around assignments in parameter lists broken into one per line and that's often better than a wall of text.- Add two spaces before # export, as this seems to be mandated by PEP-8- Use single quotes by default
Signed-off-by: Jan Lindemann <jan@janware.com>
|
|
@ -1,76 +1,119 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, TYPE_CHECKING
|
||||
import asyncio
|
||||
import cProfile
|
||||
import os
|
||||
import sys
|
||||
|
||||
import os, sys, argparse, re, asyncio, cProfile
|
||||
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from .AsyncRunner import AsyncRunner
|
||||
from .log import *
|
||||
from .log import DEBUG, ERR, NOTICE, log, set_log_flags, set_log_level
|
||||
from .Types import LoadTypes
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import TypeVar
|
||||
from collections.abc import Awaitable
|
||||
T = TypeVar("T")
|
||||
from typing import TypeVar
|
||||
|
||||
class App: # export
|
||||
T = TypeVar('T')
|
||||
|
||||
class App: # export
|
||||
|
||||
def _add_arguments(self, parser):
|
||||
self.__parser.add_argument('--log-flags', help='Log flags', default=self.__default_log_flags)
|
||||
self.__parser.add_argument('--log-level', help='Log level', default=self.__default_log_level)
|
||||
self.__parser.add_argument('--log-file', help='Log file', default=self.__default_log_file)
|
||||
self.__parser.add_argument('--backtrace', help='Show exception backtraces', action='store_true', default=self.__back_trace)
|
||||
self.__parser.add_argument('--write-profile', help='Profile code and store output to file', default=None)
|
||||
self.__parser.add_argument(
|
||||
'--log-flags', help = 'Log flags', default = self.__default_log_flags
|
||||
)
|
||||
self.__parser.add_argument(
|
||||
'--log-level', help = 'Log level', default = self.__default_log_level
|
||||
)
|
||||
self.__parser.add_argument(
|
||||
'--log-file', help = 'Log file', default = self.__default_log_file
|
||||
)
|
||||
self.__parser.add_argument(
|
||||
'--backtrace',
|
||||
help = 'Show exception backtraces',
|
||||
action = 'store_true',
|
||||
default = self.__back_trace,
|
||||
)
|
||||
self.__parser.add_argument(
|
||||
'--write-profile',
|
||||
help = 'Profile code and store output to file',
|
||||
default = None,
|
||||
)
|
||||
|
||||
def __init__(self, description: str = '', name_filter: str = '^Cmd.*', modules: None=None, eloop: None=None) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
description: str = '',
|
||||
name_filter: str = '^Cmd.*',
|
||||
modules: list[str] | None = None,
|
||||
eloop: None = None,
|
||||
) -> None:
|
||||
|
||||
def add_cmd_to_parser(cmd, parsers):
|
||||
parser = parsers.add_parser(
|
||||
cmd.name,
|
||||
help = cmd.help,
|
||||
description = cmd.description,
|
||||
formatter_class = argparse.ArgumentDefaultsHelpFormatter,
|
||||
formatter_class = ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
parser.set_defaults(func=cmd.run)
|
||||
parser.set_defaults(func = cmd.run)
|
||||
cmd.add_arguments(parser)
|
||||
cmd.set_parser(parser)
|
||||
return parser
|
||||
|
||||
def add_cmds_to_parser(parent, parser, cmds, all=False):
|
||||
def add_cmds_to_parser(
|
||||
parent: AbstractCmd | App,
|
||||
parser: ArgumentParser,
|
||||
cmds,
|
||||
all = False
|
||||
) -> None:
|
||||
if not cmds:
|
||||
return
|
||||
|
||||
class SubCommand:
|
||||
def __init__(self, cmd: Cmd, parser: Any):
|
||||
|
||||
def __init__(self, cmd: AbstractCmd, parser: Any):
|
||||
self.cmd = cmd
|
||||
self.parser = parser
|
||||
|
||||
title = 'Available subcommands'
|
||||
if hasattr(parent, 'name'):
|
||||
title += ' of ' + getattr(parent, 'name')
|
||||
subparsers = parser.add_subparsers(title=title, metavar='', dest='command')
|
||||
subparsers = parser.add_subparsers(
|
||||
title = title, metavar = '', dest = 'command'
|
||||
)
|
||||
scs: dict[str, SubCommand] = {}
|
||||
for cmd in cmds:
|
||||
cmd.set_parent(parent)
|
||||
scs[cmd.name] = SubCommand(cmd, add_cmd_to_parser(cmd, subparsers))
|
||||
if all:
|
||||
for sc in scs.values():
|
||||
add_cmds_to_parser(sc.cmd, sc.parser, sc.cmd.children, all=all)
|
||||
add_cmds_to_parser(sc.cmd, sc.parser, sc.cmd.children, all = all)
|
||||
return
|
||||
args, unknown = self.__parser.parse_known_args()
|
||||
if args.command in scs:
|
||||
sc = scs[args.command]
|
||||
add_cmds_to_parser(sc.cmd, sc.parser, sc.cmd.children, all=all)
|
||||
add_cmds_to_parser(sc.cmd, sc.parser, sc.cmd.children, all = all)
|
||||
|
||||
from .Cmd import Cmd
|
||||
from .Cmd import AbstractCmd
|
||||
|
||||
self.__args: Namespace|None = None
|
||||
self.__cmdline: str|None = None
|
||||
self.__default_log_flags: str = os.getenv('JW_DEFAULT_LOG_FLAGS', default='stderr,position,prio,color')
|
||||
self.__default_log_level: str|int|None = os.getenv('JW_DEFAULT_LOG_LEVEL', default=NOTICE)
|
||||
self.__default_log_file: str|None = os.getenv('JW_DEFAULT_LOG_FILE', default=None)
|
||||
backtrace: str|bool = os.getenv('JW_DEFAULT_SHOW_BACKTRACE', False)
|
||||
self.__back_trace = True if isinstance(backtrace, str) and backtrace.lower() in ['1', 'true'] else False
|
||||
self.__args: Namespace | None = None
|
||||
self.__cmdline: str | None = None
|
||||
self.__default_log_flags: str = os.getenv(
|
||||
'JW_DEFAULT_LOG_FLAGS', default = 'stderr,position,prio,color'
|
||||
)
|
||||
self.__default_log_level: str | int | None = os.getenv(
|
||||
'JW_DEFAULT_LOG_LEVEL', default = NOTICE
|
||||
)
|
||||
self.__default_log_file: str | None = os.getenv(
|
||||
'JW_DEFAULT_LOG_FILE', default = None
|
||||
)
|
||||
backtrace: str | bool = os.getenv('JW_DEFAULT_SHOW_BACKTRACE', False)
|
||||
self.__back_trace = isinstance(backtrace, str) and backtrace.lower() in {
|
||||
'1',
|
||||
'true',
|
||||
}
|
||||
set_log_flags(self.__default_log_flags)
|
||||
set_log_level(self.__default_log_level)
|
||||
|
||||
|
|
@ -79,10 +122,13 @@ class App: # export
|
|||
if eloop is None:
|
||||
self.__eloop = asyncio.get_event_loop()
|
||||
self.__own_eloop = True
|
||||
self.__async_runner: AsyncRunner|None = None
|
||||
self.__async_runner: AsyncRunner | None = None
|
||||
|
||||
self.__parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
||||
description=description, add_help=False)
|
||||
self.__parser = ArgumentParser(
|
||||
formatter_class = ArgumentDefaultsHelpFormatter,
|
||||
description = description,
|
||||
add_help = False,
|
||||
)
|
||||
self._add_arguments(self.__parser)
|
||||
|
||||
args, unknown = self.__parser.parse_known_args()
|
||||
|
|
@ -91,12 +137,26 @@ class App: # export
|
|||
|
||||
log(DEBUG, '-------------- Running: >' + ' '.join(sys.argv) + '<')
|
||||
|
||||
cmd_classes = LoadTypes(modules if modules else ['__main__'], type_name_filter=name_filter, type_filter=[Cmd])
|
||||
add_all_parsers = '-h' in sys.argv or '--help' in sys.argv or '_ARGCOMPLETE' in os.environ
|
||||
add_cmds_to_parser(self, self.__parser, [cmd_class(self) for cmd_class in cmd_classes], all=add_all_parsers)
|
||||
cmd_classes = LoadTypes(
|
||||
modules if modules else ['__main__'],
|
||||
type_name_filter = name_filter,
|
||||
type_filter = [AbstractCmd], # type: ignore[type-abstract]
|
||||
)
|
||||
add_all_parsers = (
|
||||
'-h' in sys.argv or '--help' in sys.argv or '_ARGCOMPLETE' in os.environ
|
||||
)
|
||||
add_cmds_to_parser(
|
||||
self,
|
||||
self.__parser,
|
||||
[cmd_class(self) for cmd_class in cmd_classes],
|
||||
all = add_all_parsers,
|
||||
)
|
||||
|
||||
# -- Add help only now, wouldn't want to have parse_known_args() exit on --help with subcommands missing
|
||||
self.__parser.add_argument('-h', '--help', action='help', help='Show this help message and exit')
|
||||
# -- Add help only now, wouldn't want to have parse_known_args() exit
|
||||
# on --help with subcommands missing
|
||||
self.__parser.add_argument(
|
||||
'-h', '--help', action = 'help', help = 'Show this help message and exit'
|
||||
)
|
||||
|
||||
def __del__(self):
|
||||
if self.__own_eloop:
|
||||
|
|
@ -105,24 +165,32 @@ class App: # export
|
|||
self.__eloop = None
|
||||
self.__own_eloop = False
|
||||
|
||||
async def __aenter__(self) ->None:
|
||||
return self
|
||||
async def __aenter__(self) -> None:
|
||||
pass
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||
pass
|
||||
|
||||
async def __run(self, argv=None) -> None:
|
||||
async def __run(self, argv = None) -> None:
|
||||
|
||||
try:
|
||||
class NoopCompleter:
|
||||
# Import argcomplete only here to not require it to be compatible
|
||||
# with minimal environments
|
||||
from argcomplete.completers import BaseCompleter
|
||||
|
||||
class NoopCompleter(BaseCompleter):
|
||||
|
||||
def __call__(self, **kwargs):
|
||||
return ()
|
||||
import argcomplete # Don't require it to be compatible with minimal environments
|
||||
argcomplete.autocomplete(self.__parser, default_completer=NoopCompleter())
|
||||
except:
|
||||
|
||||
import argcomplete
|
||||
|
||||
argcomplete.autocomplete(self.__parser, default_completer = NoopCompleter())
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self.__args = self.__parser.parse_args(args=argv)
|
||||
self.__args = self.__parser.parse_args(args = argv)
|
||||
|
||||
set_log_flags(self.__args.log_flags)
|
||||
set_log_level(self.__args.log_level)
|
||||
|
|
@ -152,14 +220,17 @@ class App: # export
|
|||
finally:
|
||||
if pr is not None:
|
||||
pr.disable()
|
||||
log(NOTICE, f'Writing profile statistics to {self.__args.write_profile}')
|
||||
log(
|
||||
NOTICE,
|
||||
f'Writing profile statistics to {self.__args.write_profile}'
|
||||
)
|
||||
pr.dump_stats(self.__args.write_profile)
|
||||
|
||||
if exit_status:
|
||||
sys.exit(exit_status)
|
||||
|
||||
# Run sub-command. Overwrite if you want to do anything before or after
|
||||
async def _run(self, args: argparse.Namespace) -> None:
|
||||
async def _run(self, args: Namespace) -> None | int:
|
||||
return await self.args.func(args)
|
||||
|
||||
def call_async(self, awaitable: Awaitable[T], timeout: float | None = None) -> T:
|
||||
|
|
@ -167,6 +238,8 @@ class App: # export
|
|||
|
||||
@property
|
||||
def eloop(self) -> asyncio.AbstractEventLoop:
|
||||
if self.__eloop is None:
|
||||
raise Exception('Tried to get inexistent event loop from application')
|
||||
return self.__eloop
|
||||
|
||||
@property
|
||||
|
|
@ -178,28 +251,34 @@ class App: # export
|
|||
@property
|
||||
def cmdline(self) -> str:
|
||||
if self.__cmdline is None:
|
||||
import shlex
|
||||
|
||||
with open('/proc/self/cmdline', 'rb') as f:
|
||||
raw = f.read().split(b'\0')[:-1]
|
||||
self.__cmdline = ' '.join(shlex.quote(arg.decode()) for arg in raw)
|
||||
return self.__cmdline
|
||||
|
||||
@property
|
||||
def args(self) -> argparse.Namespace:
|
||||
def args(self) -> Namespace:
|
||||
if self.__args is None:
|
||||
raise Exception('Tried to get inexistent argument list from application')
|
||||
return self.__args
|
||||
|
||||
@property
|
||||
def parser(self) -> argparse.ArgumentParser:
|
||||
def parser(self) -> ArgumentParser:
|
||||
return self.__parser
|
||||
|
||||
def run(self, argv=None) -> None:
|
||||
def run(self, argv = None) -> None:
|
||||
try:
|
||||
ret = self.__eloop.run_until_complete(self.__run(argv)) # type: ignore
|
||||
ret = self.eloop.run_until_complete(self.__run(argv))
|
||||
finally:
|
||||
if self.__async_runner:
|
||||
self.__async_runner.close()
|
||||
self.__async_runner = None
|
||||
return ret
|
||||
|
||||
def run_sub_commands(description = '', name_filter = '^Cmd.*', modules=None, argv=None): # export
|
||||
with App(description, name_filter, modules) as app:
|
||||
return app.run(argv=argv)
|
||||
def run_sub_commands(
|
||||
description = '', name_filter = '^Cmd.*', modules = None, argv = None
|
||||
): # export
|
||||
app = App(description, name_filter, modules)
|
||||
return app.run(argv = argv)
|
||||
|
|
|
|||