From a44a32e48a73a51248adc4631ca09ca49515d22c Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Sun, 14 Feb 2021 18:22:31 +0000 Subject: [PATCH] moved code that deals with sets of PVInfo into separate file/class --- morioc/morioc.py | 12 ++++++------ morioc/pvinfo.py | 31 ------------------------------ morioc/pvinfoset.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 morioc/pvinfoset.py diff --git a/morioc/morioc.py b/morioc/morioc.py index 58dc6c1..869408c 100644 --- a/morioc/morioc.py +++ b/morioc/morioc.py @@ -4,7 +4,7 @@ from threading import Thread from pcaspy import SimpleServer, Driver from context import Context -from pvinfo import make_pvinfos, make_pvdb, make_values +from pvinfoset import PVInfoSet from managed import delete_managed @@ -57,16 +57,16 @@ class MorIOC(Context): def serve(self, **kwargs): - pvis = make_pvinfos(kwargs, parse_string=False) - pvdb = make_pvdb(pvis) + pvis = PVInfoSet(kwargs, parse_string=False) + pvdb = pvis.pvdb() self.createPV(pvdb) - pvvs = make_values(pvis) + pvvs = pvis.values() self.setParams(**pvvs) def host(self, **kwargs): - pvis = make_pvinfos(kwargs, parse_string=True) - pvdb = make_pvdb(pvis) + pvis = PVInfoSet(kwargs, parse_string=True) + pvdb = pvis.pvdb() self.createPV(pvdb) diff --git a/morioc/pvinfo.py b/morioc/pvinfo.py index 2a57b85..0bdd173 100644 --- a/morioc/pvinfo.py +++ b/morioc/pvinfo.py @@ -1,5 +1,3 @@ -from managed import is_managed, get_managed_dtype - #PV data type: enum, string, char, float or int DTYPES = { @@ -10,15 +8,6 @@ DTYPES = { -def make_values(pvinfos): - return {pvi.name: pvi.value for pvi in pvinfos} - - -def make_pvinfos(kwargs, parse_string=True): - return [PVInfo(*args, parse_string=parse_string) for args in kwargs.items()] - - - class PVInfo: def __init__(self, name, data, parse_string=True): @@ -59,23 +48,3 @@ def infer_type(value, parse_string=True): #TODO arrays? strings? -def make_pvdb(pvinfos): - pvdb = {} - for pvi in pvinfos: - name = pvi.name - - if is_managed(name): - dtype = pvi.dtype - managed_dtype = get_managed_dtype(name) - if dtype != managed_dtype: - print(f"dtype for {name} changed:", managed_dtype, "->", dtype) #TODO what should be done if this happens? -# server.deletePV(name) #? - continue # no need to re-create - - print("will create", name) - pvdb[name] = pvi.to_dict() - - return pvdb - - - diff --git a/morioc/pvinfoset.py b/morioc/pvinfoset.py new file mode 100644 index 0000000..71a6e28 --- /dev/null +++ b/morioc/pvinfoset.py @@ -0,0 +1,47 @@ +from pvinfo import PVInfo +from managed import is_managed, get_managed_dtype + + +class PVInfoSet(set): + + def __init__(self, data, parse_string=True): + super().__init__( + PVInfo(*args, parse_string=parse_string) for args in data.items() + ) + + def values(self): + return make_values(self) + + def pvdb(self): + return make_pvdb(self) + + + +def make_pvinfos(data, parse_string=True): + return [PVInfo(*args, parse_string=parse_string) for args in data.items()] + + +def make_values(pvinfos): + return {pvi.name: pvi.value for pvi in pvinfos} + + +def make_pvdb(pvinfos): + pvdb = {} + for pvi in pvinfos: + name = pvi.name + + if is_managed(name): + dtype = pvi.dtype + managed_dtype = get_managed_dtype(name) + if dtype != managed_dtype: + print(f"dtype for {name} changed:", managed_dtype, "->", dtype) #TODO what should be done if this happens? +# server.deletePV(name) #TODO ??? + continue # no need to re-create + + print("will create", name) + pvdb[name] = pvi.to_dict() + + return pvdb + + +