From ef21dc1b1eaf8e979651164846b0892cbefe80c6 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Thu, 16 Apr 2026 13:25:32 +0200 Subject: [PATCH] App.distro_info(): Add method Add a method distro_info() to App, essentially providing CmdInfo's macro-expansion of platform information to other interested code. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/App.py | 17 +++++++++++++++++ src/python/jw/pkg/cmds/distro/CmdInfo.py | 20 ++------------------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/python/jw/pkg/App.py b/src/python/jw/pkg/App.py index f22caf19..62b2aef8 100644 --- a/src/python/jw/pkg/App.py +++ b/src/python/jw/pkg/App.py @@ -418,6 +418,23 @@ class App(Base): self.__distro = ret return self.__distro + def distro_info(self, fmt: str) -> str: + ret = fmt + for macro in re.findall("%{([A-Za-z_-]+)}", fmt): + try: + name = 'distro_' + macro.replace('-', '_') + if name == 'distro_info': + continue + val = getattr(self, name) + patt = r'%{' + macro + r'}' + if ret.find(patt) == -1: + continue + ret = ret.replace(patt, val) + except Exception as e: + log(ERR, f'Failed to expand macro "{macro}" inside "{fmt}": {str(e)}') + raise + return ret + def find_dir(self, name: str, search_subdirs: list[str]=[], search_absdirs: list[str]=[], pretty: bool=True): return self.__find_dir(name, search_subdirs, search_absdirs, pretty) diff --git a/src/python/jw/pkg/cmds/distro/CmdInfo.py b/src/python/jw/pkg/cmds/distro/CmdInfo.py index db4a6ee3..821812ac 100644 --- a/src/python/jw/pkg/cmds/distro/CmdInfo.py +++ b/src/python/jw/pkg/cmds/distro/CmdInfo.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -import re from argparse import Namespace, ArgumentParser from ...lib.log import * @@ -10,22 +9,7 @@ from ..CmdProjects import CmdProjects class CmdInfo(Cmd): # export def __init__(self, parent: CmdProjects) -> None: - super().__init__(parent, 'info', help='Print information about operating system this script runs on') - - def _expand_macros(self, string: str) -> str: - ret = string - for macro in re.findall("%{([A-Za-z_-]+)}", string): - try: - name = 'distro_' + macro.replace('-', '_') - val = getattr(self.app, name) - patt = r'%{' + macro + r'}' - if ret.find(patt) == -1: - continue - ret = ret.replace(patt, val) - except Exception as e: - log(ERR, f'Failed to expand macro "{macro}" inside "{string}": {str(e)}') - raise - return ret + super().__init__(parent, 'info', help='Print information about target platform') def add_arguments(self, parser: ArgumentParser) -> None: super().add_arguments(parser) @@ -35,4 +19,4 @@ class CmdInfo(Cmd): # export help=f'Format string, expanding macros {macro_names}') async def _run(self, args: Namespace) -> None: - print(self._expand_macros(args.format)) + print(self.app.distro_info(args.format))