diff --git a/base.py b/base.py index acab412..841127e 100644 --- a/base.py +++ b/base.py @@ -98,6 +98,7 @@ class Config: """merge info from linsetools.cfg and instrument.cfg""" this = None single_instrument = None + ins_config = None def __init__(self, instrument=None): configfile = Path('/sq_sw/linse/etc/linsetools.cfg') @@ -215,15 +216,15 @@ def wait_status(cl, service): return True -class MarcheControl(Config): +class MarcheControl: port = 8124 otherarg = None argmap = {k: 'action' for k in ('start', 'restart', 'stop', 'gui', 'run', 'cli', 'list', 'listcfg', 'running')} - def __init__(self, instrument=None, host='localhost', port=None, user=None): - super().__init__(instrument) + def __init__(self, instrument=None, host='localhost', port=None, user=None, config=None): + self.config = config or Config(instrument) self.host = host - self.user = user or self.instrument # SINQ instruments + self.user = user or self.config.instrument # SINQ instruments if not (Path('/home') / self.user).is_dir(): self.user = 'l_samenv' if port is not None: diff --git a/frappy.py b/frappy.py index c2351a6..0c84edd 100644 --- a/frappy.py +++ b/frappy.py @@ -14,13 +14,18 @@ WRAPPER_PAT = re.compile(r"interface\s=\s*'(\d*)'\s*\n") class FrappyConfig(Config): log = None process_file = None + SERVICE = {0: 'main', 1: 'stick'} - def init(self, host='localhost'): + def __init__(self, instrument=None, host='localhost'): + super().__init__(instrument) superconfig = self.sections['superfrappy'] self.wrapperdir = superconfig.get('wrapperdir') if not Path(self.wrapperdir).is_dir(): raise ValueError(f'{self.wrapperdir} does not exist') self.cfgdirs = superconfig.get('cfgdirs') + if not self.ins_config: + raise ValueError(repr(instrument)) + print(self.ins_config) frappy_ports = self.ins_config.get('frappy_ports', '0') ports = frappy_ports.split('-') self.frappy_ports = list(range(int(ports[0]), int(ports[-1]))) @@ -29,7 +34,7 @@ class FrappyConfig(Config): def service_from_uri(self, uri): try: idx = self.frappy_servers.index(uri) - return {0: 'main', 1: 'stick'}.get(idx, 'addons') + return self.SERVICE.get(idx, 'addons') except ValueError: return '' @@ -50,19 +55,16 @@ class FrappyControl(MarcheControl): argmap = dict(MarcheControl.argmap, all='service', **services) otherarg = 'cfg' - def __init__(self, instance, host='localhost', port=None, user=None): - super().__init__(instance, host, port, user) - FrappyConfig.init(self, host) + def __init__(self, instance, host='localhost', port=None, user=None, config=None): + super().__init__(instance, host, port, user, config or FrappyConfig(instance)) self.get_cfg_info() # do we need to update this from time to time? - service_from_uri = FrappyConfig.service_from_uri - def get_service(self, instance): - return f'frappy.{instance}' if self.instance == 'this' else f'frappy.{self.instrument}-{instance}' + return f'frappy.{instance}' if self.config.instance == 'this' else f'frappy.{self.config.instrument}-{instance}' def wrapper_file(self, cfg): - prefix = "" if self.single_instrument else f'{self.instrument}-' - return Path(self.wrapperdir) / f'{prefix}{cfg}_cfg.py' + prefix = "" if self.config.single_instrument else f'{self.config.instrument}-' + return Path(self.config.wrapperdir) / f'{prefix}{cfg}_cfg.py' def cfg_file(self, cfgdirs, service, cfg): """find config file @@ -77,7 +79,7 @@ class FrappyControl(MarcheControl): services = [service] if service else list(self.services) services.append('') - cfgdirs = cfgdirs or self.cfgdirs + cfgdirs = cfgdirs or self.config.cfgdirs for servicedir in services: for cfgdir in cfgdirs.split(':'): @@ -90,21 +92,21 @@ class FrappyControl(MarcheControl): def get_std_port(self, service): if service == 'main': - return self.frappy_ports[0] + return self.config.frappy_ports[0] if service == 'stick': - return self.frappy_ports[1] - return self.frappy_ports[2:] + return self.config.frappy_ports[1] + return self.config.frappy_ports[2:] def get_local_ports(self, run_state=None): """return dict of (on, busy, cfg)""" self.get_cfg_info() run_state = run_state or self.status('frappy') result = {} - prefix = '' if self.single_instrument else f'{self.instrument}-' + prefix = '' if self.config.single_instrument else f'{self.config.instrument}-' for host_port, cfg in self.cfg_info.items(): host, port = host_port.split(':') if host == 'localhost': - on, busy, _ = run_state.get(prefix+cfg, (0,0,0)) + on, busy, _ = run_state.get(prefix+cfg, (0, 0, 0)) if on or busy: result.setdefault(int(port), []).append((on, busy, prefix+cfg)) return {sorted(v)[-1][-1]: k for k, v in result.items()} @@ -125,7 +127,7 @@ class FrappyControl(MarcheControl): def add_frappy_service(self, service, cfg, port, log=None): if log is None: log = Logger('info') - service, cfgfile = self.cfg_file(self.cfgdirs, service, cfg) + service, cfgfile = self.cfg_file(self.config.cfgdirs, service, cfg) if not port: if not service: raise ArgError('service is not given and can not be determined from cfg file location') @@ -145,10 +147,10 @@ class FrappyControl(MarcheControl): def get_cfg_info(self): """get info from wrapper dir""" result = {} - for cfgfile in Path(self.wrapperdir).glob('*_cfg.py'): + for cfgfile in Path(self.config.wrapperdir).glob('*_cfg.py'): ins, _, cfg = cfgfile.stem[:-4].rpartition('-') - ins = ins or self.single_instrument - if ins != self.instrument: + ins = ins or self.config.single_instrument + if ins != self.config.instrument: continue match = WRAPPER_PAT.match(cfgfile.read_text()) if match: @@ -172,7 +174,7 @@ class FrappyControl(MarcheControl): on, busy, _ = run_state.pop(ins_cfg) if on: # port = ports.get(cfg) - kind = (self.frappy_ports + [port]).index(port) + kind = (self.config.frappy_ports + [port]).index(port) result[cfg] = kind, f'localhost:{port}' return result @@ -191,7 +193,7 @@ class FrappyControl(MarcheControl): # all_nodes.setdefault((a.address, a.port), f'{a.hostname}:{a.port}') # nodes = list(all_nodes.values()) init(*self.cfg_info) - interact(appname=self.instrument) + interact(appname=self.config.instrument) def gui(self): self.get_cfg_info() @@ -242,7 +244,7 @@ class FrappyControl(MarcheControl): self.sea2frappy = s2f = {} self.list_info = list_info = {} for servicedir in [service] if service else self.services: - for cfgdir in self.cfgdirs.split(':'): + for cfgdir in self.config.cfgdirs.split(':'): cfgdir = Path(cfgdir) / servicedir for cfgfile in cfgdir.glob('*_cfg.py'): cfg = cfgfile.name[:-7] @@ -266,7 +268,7 @@ class FrappyControl(MarcheControl): return all_cfg def listcfg(self, service='', prt=print): - self.all_cfg(self.instrument, service, True) + self.all_cfg(self.config.instrument, service, True) seacfgpat = re.compile(r'(.*)(\.config|\.stick|\.addon)') keylen = max((max(len(k) for k in cfgs) for cfgs in self.list_info.values()), default=1) ambiguous = set()