2525PortHelper -- 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
3131class 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