use frappy.config for details when listing cfg files
bare sea_cfg = ... is ignored
This commit is contained in:
76
frappyman.py
76
frappyman.py
@@ -26,6 +26,7 @@ import builtins
|
|||||||
from glob import glob
|
from glob import glob
|
||||||
from socket import gethostbyname, gethostname
|
from socket import gethostbyname, gethostname
|
||||||
from itertools import zip_longest
|
from itertools import zip_longest
|
||||||
|
from pathlib import Path
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from os.path import join, isdir, basename, expanduser, exists
|
from os.path import join, isdir, basename, expanduser, exists
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
@@ -37,39 +38,20 @@ MAIN = 1
|
|||||||
STICK = 2
|
STICK = 2
|
||||||
|
|
||||||
|
|
||||||
class Namespace(dict):
|
class Config:
|
||||||
def __init__(self, frappymanager, *args):
|
log = None
|
||||||
self.fm = frappymanager
|
process_file = None
|
||||||
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()
|
|
||||||
|
|
||||||
def init(self):
|
@classmethod
|
||||||
self.description = ''
|
def get(cls, cfgfile):
|
||||||
self.sea_cfg = None
|
if not cls.process_file:
|
||||||
|
import logging
|
||||||
def node(self, equipment_id, description, *args, **kwds):
|
from frappy.config import process_file
|
||||||
self.description = description
|
from frappy.lib import generalConfig
|
||||||
|
generalConfig.init()
|
||||||
def mod(self, name, cls, description, config=None, **kwds):
|
cls.log = logging.getLogger('frappyman')
|
||||||
cls = getattr(cls, '__name__', cls)
|
cls.process_file = process_file
|
||||||
if cls.endswith('SeaClient'):
|
return cls.process_file(Path(cfgfile), cls.log)
|
||||||
self.sea_cfg = config
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
SEAEXT = {'main': '.config', 'stick': '.stick'}
|
SEAEXT = {'main': '.config', 'stick': '.stick'}
|
||||||
@@ -238,19 +220,24 @@ class FrappyManager(ServiceManager):
|
|||||||
interact()
|
interact()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_cfg_details(namespace, cfgfile):
|
def get_cfg_details(cfgfile):
|
||||||
# get sea_cfg option from frappy cfg file
|
mods = Config.get(cfgfile)
|
||||||
namespace.init()
|
node = mods.pop('node') or {}
|
||||||
local = {}
|
sea_cfg = None
|
||||||
with open(cfgfile, encoding='utf-8') as f:
|
for mod, config in mods.items():
|
||||||
exec(f.read(), namespace, local)
|
cls = config['cls']
|
||||||
return namespace.description, local.get('sea_cfg', namespace.sea_cfg)
|
cls = getattr(cls, '__name__', cls)
|
||||||
|
if cls.endswith('SeaClient'):
|
||||||
|
try:
|
||||||
|
sea_cfg = config['config']['value']
|
||||||
|
except KeyError:
|
||||||
|
sea_cfg = None
|
||||||
|
return node.get('description', '').strip(), sea_cfg
|
||||||
|
|
||||||
def cfg_details(self, ins, service, cfgfile):
|
def cfg_details(self, ins, service, cfgfile):
|
||||||
namespace = Namespace(self, ins, service)
|
|
||||||
if cfgfile:
|
if cfgfile:
|
||||||
return self.get_cfg_details(namespace, cfgfile)
|
return self.get_cfg_details(cfgfile)
|
||||||
raise FileNotFoundError(f'{cfg} not found')
|
raise FileNotFoundError(f'{cfgfile} not found')
|
||||||
|
|
||||||
def get_cfg_file(self, ins, service, cfg, lazy=False):
|
def get_cfg_file(self, ins, service, cfg, lazy=False):
|
||||||
filenames = [f'{cfg}_cfg.py']
|
filenames = [f'{cfg}_cfg.py']
|
||||||
@@ -283,7 +270,6 @@ class FrappyManager(ServiceManager):
|
|||||||
all_cfg = set()
|
all_cfg = set()
|
||||||
if not ins:
|
if not ins:
|
||||||
return {}
|
return {}
|
||||||
namespace = Namespace(self, ins, service)
|
|
||||||
if details:
|
if details:
|
||||||
self.frappy2sea = f2s = {}
|
self.frappy2sea = f2s = {}
|
||||||
self.sea2frappy = s2f = {}
|
self.sea2frappy = s2f = {}
|
||||||
@@ -294,7 +280,9 @@ class FrappyManager(ServiceManager):
|
|||||||
cfg = basename(cfgfile)[:-7]
|
cfg = basename(cfgfile)[:-7]
|
||||||
if details:
|
if details:
|
||||||
try:
|
try:
|
||||||
desc, sea_cfg = self.get_cfg_details(namespace, cfgfile)
|
desc, sea_cfg = self.get_cfg_details(cfgfile)
|
||||||
|
except TypeError:
|
||||||
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sea_cfg = None
|
sea_cfg = None
|
||||||
desc = repr(e)
|
desc = repr(e)
|
||||||
|
|||||||
Reference in New Issue
Block a user