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 <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-16 13:25:32 +02:00
commit ef21dc1b1e
2 changed files with 19 additions and 18 deletions

View file

@ -418,6 +418,23 @@ class App(Base):
self.__distro = ret self.__distro = ret
return self.__distro 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): 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) return self.__find_dir(name, search_subdirs, search_absdirs, pretty)

View file

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import re
from argparse import Namespace, ArgumentParser from argparse import Namespace, ArgumentParser
from ...lib.log import * from ...lib.log import *
@ -10,22 +9,7 @@ from ..CmdProjects import CmdProjects
class CmdInfo(Cmd): # export class CmdInfo(Cmd): # export
def __init__(self, parent: CmdProjects) -> None: def __init__(self, parent: CmdProjects) -> None:
super().__init__(parent, 'info', help='Print information about operating system this script runs on') super().__init__(parent, 'info', help='Print information about target platform')
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
def add_arguments(self, parser: ArgumentParser) -> None: def add_arguments(self, parser: ArgumentParser) -> None:
super().add_arguments(parser) super().add_arguments(parser)
@ -35,4 +19,4 @@ class CmdInfo(Cmd): # export
help=f'Format string, expanding macros {macro_names}') help=f'Format string, expanding macros {macro_names}')
async def _run(self, args: Namespace) -> None: async def _run(self, args: Namespace) -> None:
print(self._expand_macros(args.format)) print(self.app.distro_info(args.format))