Skip to content

Commit 904eb99

Browse files
author
Thomas Goodwin
committed
Modified property handling to send more meta data for fields, etc. and a helper function to handle converting that structure to the REDHAWK-usable one
1 parent d97bdb1 commit 904eb99

4 files changed

Lines changed: 88 additions & 40 deletions

File tree

rest/component.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ def get(self, domain, application, component):
6464
@gen.coroutine
6565
def put(self, domain, application, component):
6666
data = json.loads(self.request.body)
67-
68-
changes = {}
69-
for p in data['properties']:
70-
changes[p['id']] = p['value']
67+
json_props = data.get('properties', [])
68+
changes = self.unformat_properties(json_props)
7169

7270
yield self.redhawk.component_configure(domain, application, component, changes)

rest/device.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def put(self, domainName, managerId, deviceId):
6868
'deallocate' : self.redhawk.device_deallocate
6969
}
7070
data = json.loads(self.request.body)
71-
changes = {}
72-
for p in data['properties']:
73-
changes[p['id']] = p['value']
71+
json_props = data.get('properties', [])
72+
changes = self.unformat_properties(json_props)
73+
7474
cb = PUT_METHODS.get(data['method'], None)
7575
try:
7676
r, message = yield cb(domainName, managerId, deviceId, changes)

rest/fei.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def put(self, domain_name, dev_mgr_id, dev_id, int_name, allocation_id):
103103
port = self.find_port(int_name, dev)
104104
# FEITunerHandler supports 1 PUT: Tune.
105105
try:
106-
for prop in data['properties']:
106+
props = PropertyHelper.unformat_properties(data['properties'])
107+
for prop in props:
107108
self.set_attribute(port, str(allocation_id), prop['id'], prop['value'])
108109
except Exception as e:
109110
self._handle_request_exception(e)

rest/helper.py

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
PortHelper -- Convert Port information into dict
2626
"""
2727

28-
from ossie.utils.prop_helpers import Property
28+
from ossie.utils.prop_helpers import Property, simpleProperty, structProperty, structSequenceProperty, sequenceProperty
2929

3030

3131
class PropertyHelper(object):
3232

33+
""" For going from REDHAWK to JSON """
3334
@staticmethod
3435
def format_properties(properties):
3536
if not properties:
@@ -41,47 +42,95 @@ def format_properties(properties):
4142

4243
@staticmethod
4344
def _corba_properties(properties):
44-
prop_dict = []
45+
prop_list = []
4546
for prop in properties:
4647
ret = PropertyHelper.__any(prop.value)
4748
ret['id'] = prop.id
48-
prop_dict.append(ret)
49+
prop_list.append(ret)
4950

50-
return prop_dict
51+
return prop_list
5152

5253
@staticmethod
5354
def _property_set(properties):
54-
prop_dict = []
55-
for prop in properties:
56-
# BugFix: write-only props cannot be queried, here's a way to feed
57-
# a non-destructive value in place.
58-
val = None if ('writeonly' == prop.mode or 'message' in prop.kinds) else prop.queryValue()
59-
60-
if prop.type not in ['struct', 'structSeq']:
61-
if type(val) == list:
62-
prop_type = 'simpleSeq'
63-
else:
64-
prop_type = 'simple'
55+
def _handle_simple(prop):
56+
try:
57+
val = prop.queryValue()
58+
except:
59+
val = None
60+
61+
if hasattr(prop, 'clean_name'):
62+
clean_name = prop.clean_name
6563
else:
66-
prop_type = prop.type
67-
68-
prop_json = {
69-
'id': prop.id,
70-
'name': prop.clean_name,
71-
'value': val,
72-
'scaType': prop_type,
73-
'mode': prop.mode,
74-
'kinds': prop.kinds
64+
clean_name = prop.id.split('::')[-1]
65+
66+
p_dict = {
67+
'id' : prop.id,
68+
'name' : clean_name,
69+
'scaType' : 'simple',
70+
'value' : val,
71+
'type' : prop.type,
72+
'mode' : prop.mode,
73+
'kinds' : prop.kinds
7574
}
7675

77-
if prop_type == 'simple':
78-
prop_json['type'] = prop.type;
79-
80-
if '_enums' in dir(prop) and prop._enums:
81-
prop_json['enumerations'] = prop._enums
82-
83-
prop_dict.append(prop_json)
84-
return prop_dict
76+
if hasattr(prop, '_enums') and prop._enums:
77+
p_dict['enumerations'] = prop._enums
78+
return p_dict
79+
80+
def _handle_simpleseq(prop):
81+
p_dict = _handle_simple(prop)
82+
p_dict['scaType'] = 'simpleSeq'
83+
return p_dict
84+
85+
def _handle_struct(prop):
86+
p_dict = _handle_simple(prop)
87+
p_dict.pop('type', None)
88+
p_dict['value'] = []
89+
for _, m in prop.members.iteritems():
90+
m_dict = _handle_simple(m)
91+
p_dict['value'].append( m_dict )
92+
p_dict['scaType'] = 'struct'
93+
return p_dict
94+
95+
def _handle_structseq(prop):
96+
p_dict = _handle_simple(prop)
97+
p_dict['value'] = []
98+
for idx, _ in enumerate(prop):
99+
element_dict = _handle_struct(prop[idx])
100+
p_dict['value'].append( element_dict )
101+
p_dict['scaType'] = 'structSeq'
102+
return p_dict
103+
104+
prop_list = []
105+
for prop in properties:
106+
if type(prop) == simpleProperty:
107+
prop_list.append( _handle_simple(prop) )
108+
elif type(prop) == sequenceProperty:
109+
prop_list.append( _handle_simpleseq(prop) )
110+
elif type(prop) == structProperty:
111+
prop_list.append( _handle_struct(prop) )
112+
elif type(prop) == structSequenceProperty:
113+
prop_list.append( _handle_structseq(prop) )
114+
return prop_list
115+
116+
""" Going from JSON to REDHAWK Properties """
117+
@staticmethod
118+
def unformat_properties(properties):
119+
def _handle_value(val):
120+
out_val = val
121+
if type(val) == list:
122+
out_val = []
123+
for v in val:
124+
if type(v) == dict:
125+
out_val.append({v['id']: _handle_value(v['value'])})
126+
else:
127+
out_val.append(v)
128+
return out_val
129+
130+
props_dict = {}
131+
for prop in properties:
132+
props_dict[prop['id']] = _handle_value(prop['value'])
133+
return props_dict
85134

86135
###############################################
87136
# Private

0 commit comments

Comments
 (0)