From 174c6fba16e9dde3fc097aea96637fb61fd0a1ff Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 11 Sep 2026 20:47:20 +0200 Subject: [PATCH 1/2] cmds.projects.CmdCreateFile: Fix spurious trailing newline create-file and create-pkg-config both print the rendered template with a newline appended over the original template, owed to using print() instead of sys.stdout.write(). Fix that. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/cmds/projects/CmdCreateFile.py | 4 +++- .../jw/pkg/cmds/projects/CmdCreatePkgConfig.py | 15 ++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/python/jw/pkg/cmds/projects/CmdCreateFile.py b/src/python/jw/pkg/cmds/projects/CmdCreateFile.py index dfe5b95f..dcd5c944 100644 --- a/src/python/jw/pkg/cmds/projects/CmdCreateFile.py +++ b/src/python/jw/pkg/cmds/projects/CmdCreateFile.py @@ -1,3 +1,5 @@ +import sys + from argparse import ArgumentParser, ArgumentTypeError, Namespace from enum import Enum, auto from typing import override @@ -132,4 +134,4 @@ class CmdCreateFile(Cmd): # export method = getattr(self, 'render_' + args.format, None) if method is None: # Should be prevented by choices=[] but keeps linter happy raise Exception(f'Unsupported output format {args.format}') - print(method(args.module, args.field)) + sys.stdout.write(method(args.module, args.field)) diff --git a/src/python/jw/pkg/cmds/projects/CmdCreatePkgConfig.py b/src/python/jw/pkg/cmds/projects/CmdCreatePkgConfig.py index f5dab4a5..7c0d9684 100644 --- a/src/python/jw/pkg/cmds/projects/CmdCreatePkgConfig.py +++ b/src/python/jw/pkg/cmds/projects/CmdCreatePkgConfig.py @@ -1,5 +1,7 @@ from __future__ import annotations +import sys + from typing import TYPE_CHECKING, override from .Cmd import Cmd, Parent @@ -74,16 +76,19 @@ class CmdCreatePkgConfig(Cmd): # export ] ) + extra: list[str] = [] if args.cflags is not None: - contents += f'Cflags: {args.cflags}\n' + extra.append(f'Cflags: {args.cflags}') if args.libflags is not None: - contents += f'Libs: {args.libflags}\n' + extra.append(f'Libs: {args.libflags}') val = merged.get('requires_run') if val is not None: - contents += f'Requires: {self.__cleanup_requires(val)}' + extra.append(f'Requires: {self.__cleanup_requires(val)}') val = merged.get('requires_build') if val is not None: - contents += (f'Requires.private: {self.__cleanup_requires(val)}') + extra.append(f'Requires.private: {self.__cleanup_requires(val)}') # not sure what to do with requires_devel - print(contents) + if extra: + contents += '\n'.join(extra) + sys.stdout.write(contents) -- 2.55.0 From a197e9c50d94ca8607b068944c48b597aab674db Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sat, 12 Sep 2026 17:56:40 +0200 Subject: [PATCH 2/2] cmds.projects.CmdCreateFile: Fix --format help The possible values of --format are defined by an Enum which is never really used as such. Derive them from class introspection instead, i.e. offer all formats that have a corresponding render_() name. Also, turn the option into real argparse-backed choices, and make the argument mandatory, because that reflects the reality of the implementation - there is no default. Signed-off-by: Jan Lindemann --- .../jw/pkg/cmds/projects/CmdCreateFile.py | 27 +++++++++++-------- .../integration/jw-pkg/help/test-expected.txt | 5 ++-- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/python/jw/pkg/cmds/projects/CmdCreateFile.py b/src/python/jw/pkg/cmds/projects/CmdCreateFile.py index dcd5c944..754f42cb 100644 --- a/src/python/jw/pkg/cmds/projects/CmdCreateFile.py +++ b/src/python/jw/pkg/cmds/projects/CmdCreateFile.py @@ -1,8 +1,12 @@ +from __future__ import annotations + import sys from argparse import ArgumentParser, ArgumentTypeError, Namespace -from enum import Enum, auto -from typing import override +from typing import TYPE_CHECKING, override + +if TYPE_CHECKING: + from typing import Iterable from ...lib.log import WARNING, log from ...lib.version.base import Syntax @@ -17,10 +21,6 @@ def key_value(s: str) -> tuple[str, str]: raise ArgumentTypeError('Expected KEY=VALUE') return key, value -# TODO: Put the more elaborate stuff into lib -class Fmt(Enum): - Pyright = auto() - class CmdCreateFile(Cmd): # export def __jw_required( @@ -50,6 +50,11 @@ class CmdCreateFile(Cmd): # export ret = [module, *ret] return ret + @property + def __format_choices(self) -> Iterable[str]: + p = 'render_' + return [name.removeprefix(p) for name in dir(self) if name.startswith(p)] + def __render( self, template_name: str, @@ -100,12 +105,12 @@ class CmdCreateFile(Cmd): # export @override def add_arguments(self, parser: ArgumentParser) -> None: super().add_arguments(parser) + format_choices = self.__format_choices parser.add_argument( '--format', - help = ( - 'Output format, for example: ' - ', '.join([fmt.name.lower() for fmt in Fmt]) - ) + choices = format_choices, + required = True, + help = 'Output format, one of: ' + ', '.join(format_choices) ) parser.add_argument( '--search-path', @@ -132,6 +137,6 @@ class CmdCreateFile(Cmd): # export @override async def _run(self, args: Namespace) -> None: method = getattr(self, 'render_' + args.format, None) - if method is None: # Should be prevented by choices=[] but keeps linter happy + if method is None: # choices already restricts this; keeps the linter happy raise Exception(f'Unsupported output format {args.format}') sys.stdout.write(method(args.module, args.field)) diff --git a/test/integration/jw-pkg/help/test-expected.txt b/test/integration/jw-pkg/help/test-expected.txt index d5c43eff..4ac25287 100644 --- a/test/integration/jw-pkg/help/test-expected.txt +++ b/test/integration/jw-pkg/help/test-expected.txt @@ -332,7 +332,7 @@ List available commands options: -h, --help show this help message and exit ============= Running: jw-pkg.py -t ../../../.. --log-level info projects create-file --help -usage: jw-pkg.py projects create-file [-h] [--format FORMAT] +usage: jw-pkg.py projects create-file [-h] --format {pyright,tmpl} [--search-path SEARCH_PATH] [--template-name TEMPLATE_NAME] [--quote] [-f KEY=VALUE] @@ -345,7 +345,8 @@ positional arguments: options: -h, --help show this help message and exit - --format FORMAT pyright (default: None) + --format {pyright,tmpl} + Output format, one of: pyright, tmpl --search-path SEARCH_PATH Template search path, colon separated (default: /etc/opt/jw-pkg/templates) -- 2.55.0