From b35004b8b97173d7865c6db6717ca8b2b9db126d Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Mon, 19 May 2025 18:09:32 +0200 Subject: [PATCH] allow 'incldue' and 'override' --- frappyman.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/frappyman.py b/frappyman.py index 0ea4be6..30c3afb 100644 --- a/frappyman.py +++ b/frappyman.py @@ -37,9 +37,12 @@ STICK = 2 class Namespace(dict): - def __init__(self): + def __init__(self, frappymanager, *args): + self.fm = frappymanager + self.args = args self['Node'] = self.node self['Mod'] = self.mod + self['Include'] = self.include for fun in 'Param', 'Command', 'Group': self[fun] = self.dummy self.init() @@ -59,6 +62,12 @@ class Namespace(dict): def dummy(self, *args, **kwds): return None + def include(self, cfg): + local = {} + print('INCLUDE', self.args, cfg, self.fm.get_cfg_file(*self.args, cfg, True)) + with open(self.fm.get_cfg_file(*self.args, cfg, True), encoding='utf-8') as f: + exec(f.read(), self, local) + __builtins__ = builtins @@ -225,19 +234,25 @@ class FrappyManager(ServiceManager): return namespace.description, local.get('sea_cfg', namespace.sea_cfg) def cfg_details(self, ins, service, cfg): - namespace = Namespace() - for cfgdir in self.config_dirs(ins, service): - cfgfile = join(cfgdir, f'{cfg}_cfg.py') - if exists(cfgfile): - return self.get_cfg_details(namespace, cfgfile) + namespace = Namespace(self, ins, service) + if cfgfile: + return self.get_cfg_details(namespace, cfgfile) raise FileNotFoundError(f'{cfg} not found') - def is_cfg(self, ins, service, cfg): + def get_cfg_file(self, ins, service, cfg, lazy=False): + filenames = [f'{cfg}_cfg.py'] + if lazy: + filenames.extend([f'{cfg}.py', cfg]) for cfgdir in self.config_dirs(ins, service): - cfgfile = join(cfgdir, f'{cfg}_cfg.py') - if exists(cfgfile): - return True - return False + for filename in filenames: + cfgfile = join(cfgdir, filename) + if exists(cfgfile): + return cfgfile + print('NOT FOUND', cfg, self.config_dirs(ins, service), filenames) + return None + + def is_cfg(self, ins, service, cfg): + return bool(self.get_cfg_file(ins, service, cfg)) def all_cfg(self, ins, service, details=False): """get available cfg files @@ -255,7 +270,7 @@ class FrappyManager(ServiceManager): all_cfg = set() if not ins: return {} - namespace = Namespace() + namespace = Namespace(self, ins, service) if details: self.frappy2sea = f2s = {} self.sea2frappy = s2f = {} @@ -326,7 +341,7 @@ class FrappyManager(ServiceManager): argdict['service'] = cfg return super().treat_args(argdict, (), extra) if (',' in cfg or cfg.endswith('.cfg') or - self.is_cfg(argdict.get('ins'), argdict.get('service'), cfg)): + self.get_cfg_file(argdict.get('ins'), argdict.get('service'), cfg, True)): return super().treat_args(argdict, (), [cfg] + extra) return super().treat_args(argdict, unknown, extra)