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>
This commit is contained in:
parent
8c5c98c95a
commit
6db73873e7
97 changed files with 3229 additions and 1893 deletions
|
|
@ -1,17 +1,16 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ..Cmd import Cmd as Base
|
||||
from ...CmdBase import CmdBase
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..CmdPosix import CmdPosix
|
||||
from ..CmdPosix import CmdPosix as Parent
|
||||
|
||||
class Cmd(Base): # export
|
||||
class Cmd(CmdBase): # export
|
||||
|
||||
def __init__(self, parent: CmdPosix, name: str, help: str) -> None:
|
||||
def __init__(self, parent: Parent, name: str, help: str) -> None:
|
||||
super().__init__(parent, name, help)
|
||||
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
|
|
|
|||
|
|
@ -1,34 +1,56 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...lib.util import copy
|
||||
from .Cmd import Cmd
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..CmdPosix import CmdPosix
|
||||
from argparse import Namespace, ArgumentParser
|
||||
from argparse import ArgumentParser, Namespace
|
||||
|
||||
class CmdCopy(Cmd): # export
|
||||
from ..CmdPosix import CmdPosix
|
||||
|
||||
class CmdCopy(Cmd): # export
|
||||
|
||||
def __init__(self, parent: CmdPosix) -> None:
|
||||
super().__init__(parent, 'copy', help="Copy files")
|
||||
super().__init__(parent, 'copy', help = 'Copy files')
|
||||
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
super().add_arguments(parser)
|
||||
parser.add_argument('src', help='Source file URI')
|
||||
parser.add_argument('dst', help='Destination file URI')
|
||||
parser.add_argument('-o', '--owner', default=None, help='Destination file owner')
|
||||
parser.add_argument('-g', '--group', default=None, help='Destination file group')
|
||||
parser.add_argument('-m', '--mode', default=None, help='Destination file mode')
|
||||
parser.add_argument('-F', '--fixed-strings', action='store_true',
|
||||
help='Don\'t expand platform.expand_macros macros in <src> and <dst>')
|
||||
parser.add_argument('src', help = 'Source file URI')
|
||||
parser.add_argument('dst', help = 'Destination file URI')
|
||||
parser.add_argument(
|
||||
'-o', '--owner', default = None, help = 'Destination file owner'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-g', '--group', default = None, help = 'Destination file group'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-m', '--mode', default = None, help = 'Destination file mode'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-F',
|
||||
'--fixed-strings',
|
||||
action = 'store_true',
|
||||
help = "Don't expand platform.expand_macros macros in <src> and <dst>",
|
||||
)
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
|
||||
def __expand(url: str) -> str:
|
||||
if args.fixed_strings:
|
||||
return url
|
||||
return self.app.distro.expand_macros(url)
|
||||
await copy(__expand(args.src), __expand(args.dst),
|
||||
owner=args.owner, group=args.group, mode=int(args.mode, 0))
|
||||
ret = self.app.distro.expand_macros(url)
|
||||
if not isinstance(ret, str):
|
||||
raise Exception(
|
||||
f'Expanding macros in "{url}" returned unexpected ret "{ret}"'
|
||||
)
|
||||
return ret
|
||||
|
||||
await copy(
|
||||
__expand(args.src),
|
||||
__expand(args.dst),
|
||||
owner = args.owner,
|
||||
group = args.group,
|
||||
mode = int(args.mode, 0),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from argparse import ArgumentParser, Namespace
|
||||
|
||||
from argparse import Namespace, ArgumentParser
|
||||
from ..CmdPosix import CmdPosix as Parent
|
||||
from .Cmd import Cmd as Base
|
||||
|
||||
from .Cmd import Cmd
|
||||
from ..CmdPosix import CmdPosix
|
||||
class CmdTar(Base): # export
|
||||
|
||||
class CmdTar(Cmd): # export
|
||||
|
||||
def __init__(self, parent: CmdPosix) -> None:
|
||||
super().__init__(parent, 'tar', help='Handle tar archives')
|
||||
def __init__(self, parent: Parent) -> None:
|
||||
super().__init__(parent, 'tar', help = 'Handle tar archives')
|
||||
self.load_subcommands()
|
||||
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
|
|
|
|||
|
|
@ -1,27 +1,29 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from argparse import Namespace, ArgumentParser
|
||||
from argparse import ArgumentParser
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from ..Cmd import Cmd as Base
|
||||
from ....CmdBase import CmdBase
|
||||
from ....lib.FileContext import FileContext
|
||||
from ....lib.ProcFilterGpg import ProcFilterGpg
|
||||
from ....lib.TarIo import TarIo
|
||||
from ..CmdTar import CmdTar as Parent
|
||||
|
||||
from ....lib.TarIo import TarIo
|
||||
from ....lib.ProcFilterGpg import ProcFilterGpg
|
||||
from ....lib.FileContext import FileContext
|
||||
|
||||
class Cmd(Base): # export
|
||||
class Cmd(CmdBase): # export
|
||||
|
||||
def __init__(self, parent: Parent, name: str, help: str) -> None:
|
||||
super().__init__(parent, name, help)
|
||||
self.__tar_io: None = None
|
||||
|
||||
@asynccontextmanager
|
||||
async def ctx(self, **kwargs) -> TarIo:
|
||||
async with TarIo.create(src=self.app.args.archive_path, **kwargs) as ret:
|
||||
ret.src.add_proc_filter(FileContext.Direction.In, ProcFilterGpg(ec=self.app.exec_context))
|
||||
async def ctx(self, **kwargs) -> AsyncIterator[TarIo]:
|
||||
async with TarIo.create(src = self.app.args.archive_path, **kwargs) as ret:
|
||||
ret.src.add_proc_filter(
|
||||
FileContext.Direction.In, ProcFilterGpg(ec = self.app.exec_context)
|
||||
)
|
||||
yield ret
|
||||
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
super().add_arguments(parser)
|
||||
parser.add_argument('-f', '--archive-path', required=True, help='Archive path')
|
||||
parser.add_argument(
|
||||
'-f', '--archive-path', required = True, help = 'Archive path'
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,27 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
from argparse import ArgumentParser, Namespace
|
||||
|
||||
from argparse import Namespace, ArgumentParser
|
||||
from ....lib.log import DEBUG, log
|
||||
from .Cmd import Cmd, Parent
|
||||
|
||||
from .Cmd import Cmd
|
||||
from ..CmdTar import CmdTar
|
||||
class CmdExtract(Cmd): # export
|
||||
|
||||
from ....lib.FileContext import FileContext
|
||||
from ....lib.log import *
|
||||
|
||||
class CmdExtract(Cmd): # export
|
||||
|
||||
def __init__(self, parent: CmdTar) -> None:
|
||||
super().__init__(parent, 'x', help="Extract a tar archive")
|
||||
def __init__(self, parent: Parent) -> None:
|
||||
super().__init__(parent, 'x', help = 'Extract a tar archive')
|
||||
|
||||
def add_arguments(self, parser: ArgumentParser) -> None:
|
||||
super().add_arguments(parser)
|
||||
parser.add_argument('dst', help='Destination root URI')
|
||||
parser.add_argument('dst', help = 'Destination root URI')
|
||||
|
||||
async def _run(self, args: Namespace) -> None:
|
||||
async with self.ctx(dst=args.dst) as ctx:
|
||||
async with self.ctx(dst = args.dst) as ctx:
|
||||
paths = await ctx.extract(ctx.dst.root)
|
||||
log(DEBUG, f'Extracted {len(paths)} files')
|
||||
|
|
|
|||
Loading…
Reference in a new issue