Commit 29d181bf authored by Wout De Nolf's avatar Wout De Nolf

Custom parameters can be added with `mgrproxy.SetParameters(jsonstring)`....

Custom parameters can be added with `mgrproxy.SetParameters(jsonstring)`. These have priority over parameters and labels. No "remote" support for custom parameters.
parent 7062974a
......@@ -44,7 +44,6 @@ __docformat__ = 'restructuredtext'
import tango
import sys
import ast
# Add additional import
#----- PROTECTED REGION ID(MetadataManager.additionnal_import) ENABLED START -----#
import os.path
......@@ -157,11 +156,20 @@ class MetadataManager (tango.LatestDeviceImpl):
self.dataRootEventId = None
self.dataRoot = ''
self.datasetParentLocation = ''
self.datasetParamMap = dict()
# Priority of dataset metadata keys
# (highest number is highest priority)
# 1. parameters(local): from attributes of this device
# 2. labels: from device property
# 3. parameters(local): from attributes of other devices
# 4. custom: added with SetParameters
# 5. sample:
# 6. globals: proposal, beamline, etc.
self.datasetParamMap = dict() # priority (local:1, remote:3)
self.parameterDefinitionMap = dict()
self.sampleParamMap = dict()
self.attributeProxyMap = dict()
self.labels_map = dict()
self.labels_map = dict() # priority (2)
self.customParamMap = dict() # priority (4)
self.sampleParamMap = dict() # priority (5)
self.attr_datafileList_read = []
self.attr_datasetNameList_read = []
self.msgList = deque(maxlen=20)
......@@ -352,8 +360,6 @@ class MetadataManager (tango.LatestDeviceImpl):
reader = ICATParameterReader(self.get_name(), self.authenticationPlugin, self.username, self.password, self.server, self.port)
return str(reader.compare(self.parameters, self.labels))
# returns the sample parameters set by the SetSampleParameters command as key value pairs
# the list is dynamically created on each call
# returns "None" if list is empty
......@@ -512,8 +518,6 @@ class MetadataManager (tango.LatestDeviceImpl):
tomo = tomodbFile(self.attr_proposal_read, self.beamlineID.lower(), argin)
self.client.sendObject(tomo)
#----- PROTECTED REGION END -----# // MetadataManager.IngestTomodbXML
def is_IngestTomodbXML_allowed(self):
self.debug("In is_IngestTomodbXML_allowed()")
......@@ -588,6 +592,7 @@ class MetadataManager (tango.LatestDeviceImpl):
for datasetParam in self.datasetParamMap:
self.datasetParamMap[datasetParam] = ''
self.customParamMap = dict()
except:
traceback.print_exc(file=sys.stdout)
......@@ -679,7 +684,6 @@ class MetadataManager (tango.LatestDeviceImpl):
sys.excepthook(*sys.exc_info())
pass
def notifyInfo(self, message):
self.sendNotification("notification", "info", message)
......@@ -834,34 +838,28 @@ class MetadataManager (tango.LatestDeviceImpl):
self.warn_stream("Error retrieving poll value: %s" %e)
beat = 0
def SetParameters(self, python_dict):
d = ast.literal_eval(python_dict)
class AlreadySuppliedParameterDefinition(ParameterDefinition):
def __init__(self, name, value):
super().__init__(name)
self.value = value
def isRemote(self):
# we fool the thing to think it is a remote param => will call 'buildValue'
return True
def buildValue(self, *args, **kwargs):
return self.value
for k, v in d.items():
# k in our case is the parameter name (like: "TOMO_comment" for example or "Sample_name")
# v is supposed to be the parameter value
d[k] = AlreadySuppliedParameterDefinition(k, v)
# this will overwrite existing parameter definition map... Is it a problem?
self.parameterDefinitionMap = d
def SetParameters(self, json_string):
"""This will overwrite ICAT key-value pairs defined
by the "parameters" and "labels" device properties,
but not the sample parameters.
"""
self.customParamMap = json.loads(json_string)
original = set(self.parameters) | set(self.labels)
new = set(self.customParamMap.keys())
overwritten = len(original & new)
original = len(original) + len(self.sampleParamMap) - overwritten
new = len(new) - overwritten
self.info(f"Add parameters ({new} new, {overwritten} overwritten, {original} original)")
# Gets the parameters defined on the parameters list in the tango device
# Return List<Parameter>
def getParameters(self):
"""Remote and custom parameters.
:returns list(parameter):
"""
try:
notallowed = self.notallowed_parameters
parameters = []
# add the linked parameters (need to get the value)
# async reads of every proxy
......@@ -886,17 +884,35 @@ class MetadataManager (tango.LatestDeviceImpl):
for n, d in self.parameterDefinitionMap.items():
if d.isRemote():
v = d.buildValue(self.attributeProxyMap, values)
if v:
if v and n not in notallowed:
parameters.append(parameter(n, v))
except:
traceback.print_exc(file=sys.stdout)
#return dts
traceback.print_exc(file=sys.stdout)
notallowed = self.notallowed_custom
parameters += list(parameter(n, v) for n, v in self.customParamMap.items()
if n not in notallowed)
return parameters
@property
def notallowed_parameters(self):
return set(self.labels_map.keys()) | set(self.customParamMap.keys()) | set(self.sampleParamMap.keys())
@property
def notallowed_labels(self):
return set(self.customParamMap.keys()) | set(self.sampleParamMap.keys())
@property
def notallowed_custom(self):
return set(self.sampleParamMap.keys())
def CreateDataset(self, complete=True):
# Avoid name collisions:
notallowed = self.notallowed_parameters
datasetParamMap = {k:v for k,v in self.datasetParamMap.items()
if k not in notallowed}
notallowed = self.notallowed_labels
labels_map = {k:v for k,v in self.labels_map.items()
if k not in notallowed}
return self.metadataWorker.createDataset(
complete,
self.attr_proposal_read,
......@@ -905,8 +921,8 @@ class MetadataManager (tango.LatestDeviceImpl):
self.attr_dataFolder_read,
self.startDate,
self.endDate,
self.labels_map,
self.datasetParamMap,
labels_map,
datasetParamMap,
self.attr_sampleName_read,
self.sampleParamMap,
self.attr_datafileList_read)
......@@ -1171,13 +1187,12 @@ class MetadataManagerClass(tango.DeviceClass):
[tango.DevVoid, "none"]],
'userComment':
[[tango.DevString, "Message"],
[tango.DevVoid, "none"]]
}
[tango.DevVoid, "none"]],
"SetParameters": [
[tango.DevString, "String representation of Python dictionary"],
[tango.DevString, "JSON-representation of a parameter dictionary (overwrites)"],
[tango.DevVoid, "none"],
],
}
# Attribute definitions
attr_list = {
......
......@@ -102,6 +102,13 @@ class MetadataWorker():
def toDictionary(self, dataset, sampleParamMap, initial=None):
d = dict()
# add parameters and labels
for p in dataset.get_parameter():
d[p.get_name()] = p.get_value()
# add parameters from sample
for key in sampleParamMap:
d[key] = sampleParamMap[key]
# add main proposal/sample/dataset parameters
d['proposal'] = dataset.get_investigation()
d['beamlineID'] = dataset.get_instrument()
d['datasetName'] = dataset.get_name()
......@@ -109,13 +116,6 @@ class MetadataWorker():
d['startDate'] = dataset.get_startDate()
d['endDate'] = dataset.get_endDate()
d['sampleName'] = dataset.get_sample().get_name()
for p in dataset.get_parameter():
d[p.get_name()] = p.get_value()
# add parameters from sample
for key in sampleParamMap:
d[key] = sampleParamMap[key]
return d
def addParametersToDataSet(self, dataset, parameters):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment