allow 'incldue' and 'override'

This commit is contained in:
2025-05-19 18:09:32 +02:00
parent 9a673afb21
commit b35004b8b9

View File

@ -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):
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')
for filename in filenames:
cfgfile = join(cfgdir, filename)
if exists(cfgfile):
return True
return False
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)