mirror of
ssh://git.janware.com/janware/proj/jw-pkg
synced 2026-04-24 09:13:37 +02:00
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:
parent
d1df9f0ac7
commit
ef21dc1b1e
2 changed files with 19 additions and 18 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue