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