make 'frappy cli' work
This commit is contained in:
@@ -98,6 +98,7 @@ class Config:
|
||||
"""merge info from linsetools.cfg and instrument.cfg"""
|
||||
this = None
|
||||
single_instrument = None
|
||||
instrument = None
|
||||
ins_config = None
|
||||
|
||||
def __init__(self, instrument=None):
|
||||
@@ -132,6 +133,8 @@ class Config:
|
||||
if not self.this:
|
||||
self.this = self.single_instrument
|
||||
|
||||
if self.single_instrument: # TODO: do we need this?
|
||||
instrument = self.single_instrument
|
||||
if instrument:
|
||||
self.instance = instrument or 'this'
|
||||
self.instrument = self.this if self.instance == 'this' else self.instance
|
||||
@@ -220,12 +223,13 @@ class MarcheControl:
|
||||
port = 8124
|
||||
otherarg = None
|
||||
argmap = {k: 'action' for k in ('start', 'restart', 'stop', 'gui', 'run', 'cli', 'list', 'listcfg', 'running')}
|
||||
configcls = Config
|
||||
|
||||
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.config.instrument # SINQ instruments
|
||||
if not (Path('/home') / self.user).is_dir():
|
||||
self.user = user or list(self.config.instruments)[0] # SINQ instruments
|
||||
if not (Path('/home') / (self.user or 'undefined')).is_dir():
|
||||
self.user = 'l_samenv'
|
||||
if port is not None:
|
||||
self.port = port
|
||||
@@ -294,7 +298,7 @@ class MarcheControl:
|
||||
|
||||
@classmethod
|
||||
def do_on_ins(cls, args):
|
||||
config = Config()
|
||||
config = cls.configcls()
|
||||
argdict = config.parse_args(cls.argmap, args, cls.otherarg)
|
||||
instrument = argdict.pop('instrument', None)
|
||||
if instrument is None:
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import re
|
||||
import os
|
||||
from socket import gethostname, gethostbyname
|
||||
from pathlib import Path
|
||||
from .base import MarcheControl, Logger, ArgError, write_content, Config
|
||||
from .normalizeuri import normalizeuri
|
||||
|
||||
|
||||
WRAPPER_CFG = """interface = '{port}'
|
||||
@@ -15,6 +17,9 @@ class FrappyConfig(Config):
|
||||
log = None
|
||||
process_file = None
|
||||
SERVICE = {0: 'main', 1: 'stick'}
|
||||
instrument = None
|
||||
frappy_ports = ()
|
||||
frappy_servers = ()
|
||||
|
||||
def __init__(self, instrument=None, host='localhost'):
|
||||
super().__init__(instrument)
|
||||
@@ -23,13 +28,11 @@ class FrappyConfig(Config):
|
||||
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])))
|
||||
self.frappy_servers = [f'{host}:{p}' for p in self.frappy_ports]
|
||||
if 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])))
|
||||
self.frappy_servers = [f'{host}:{p}' for p in self.frappy_ports]
|
||||
|
||||
def service_from_uri(self, uri):
|
||||
try:
|
||||
@@ -54,6 +57,7 @@ class FrappyControl(MarcheControl):
|
||||
services = {k: 'service' for k in ('main', 'stick', 'addons')}
|
||||
argmap = dict(MarcheControl.argmap, all='service', **services)
|
||||
otherarg = 'cfg'
|
||||
configcls = FrappyConfig
|
||||
|
||||
def __init__(self, instance, host='localhost', port=None, user=None, config=None):
|
||||
super().__init__(instance, host, port, user, config or FrappyConfig(instance))
|
||||
@@ -149,8 +153,8 @@ class FrappyControl(MarcheControl):
|
||||
result = {}
|
||||
for cfgfile in Path(self.config.wrapperdir).glob('*_cfg.py'):
|
||||
ins, _, cfg = cfgfile.stem[:-4].rpartition('-')
|
||||
ins = ins or self.config.single_instrument
|
||||
if ins != self.config.instrument:
|
||||
ins = ins or self.config.this
|
||||
if ins != self.config.this:
|
||||
continue
|
||||
match = WRAPPER_PAT.match(cfgfile.read_text())
|
||||
if match:
|
||||
@@ -181,17 +185,16 @@ class FrappyControl(MarcheControl):
|
||||
def cli(self):
|
||||
self.get_cfg_info()
|
||||
from frappy.client.interactive import init, interact
|
||||
# from frappy.protocol.discovery import scan
|
||||
# if ins == self.single_ins:
|
||||
# all_nodes = {}
|
||||
# for node in nodes:
|
||||
# host, port = node.split(':')
|
||||
# if host == 'localhost':
|
||||
# host = gethostname()
|
||||
# all_nodes[gethostbyname(host), int(port)] = node
|
||||
# for a in scan():
|
||||
# all_nodes.setdefault((a.address, a.port), f'{a.hostname}:{a.port}')
|
||||
# nodes = list(all_nodes.values())
|
||||
from frappy.protocol.discovery import scan
|
||||
boxes = self.config.sections['boxes']
|
||||
found = {}
|
||||
for ans in scan():
|
||||
uri = normalizeuri(f'{ans.hostname}:{ans.port}')
|
||||
found[uri] = ans
|
||||
for cfg, uri in boxes.items():
|
||||
if uri in found:
|
||||
self.cfg_info[uri] = cfg
|
||||
print(self.cfg_info)
|
||||
init(*self.cfg_info)
|
||||
interact(appname=self.config.instrument)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user