def read(self, path): '''Read - to be mapped on a JSON RPC read for a this entity argument is a json rpc path. ''' (yin_form, data) = yin_path(path) needs_adjust = True cps_obj = None try: cps_obj = cps_utils.CPSObject(yin_form, data=data) except ValueError: return None k = [cps_obj.get()] res_list = [] cps.get(k, res_list) result = [] for element in res_list: if needs_adjust: yin_form = prep_path(yin_form, element) needs_adjust = False result.append(convert_result(yin_form, element)) cps_type = cps.type(yin_form) try: if cps_type["attribute_type"] == "list": if data != {}: if len(result) == 0: return None else: return result[0] else: return result return result[0] except IndexError: return None
def _do_convert_result(in_path, element_path, value): '''Recursive worker for result conversion - returns a key-data pair''' regexp = re.compile("(.*)(" + in_path + "/)(.*)") cps_type = cps.type(element_path) rma = regexp.match(element_path) mod_pref = rma.group(1) key = rma.group(3) if len(mod_pref) > 0: key = ":".join((_reverse_mod_map(mod_pref[:-1]), key)) if cps_type["attribute_type"] == "leaf-list": ylist = [] for element in value: list_val = cps_utils.cps_attr_types_map.from_data( element_path, element) ylist.append(GLOBAL_MAP.from_cps(element_path, list_val)) result = {key: ylist} elif cps_type["attribute_type"] == "list": ylist = [] for element in value.values(): list_elem = {} for (subkey, subvalue) in element.items(): list_elem.update( _do_convert_result(element_path, subkey, subvalue)) ylist.append(list_elem) result = {key: ylist} elif cps_type["attribute_type"] == "container": container = {} for (subkey, subvalue) in value.items(): container.update(_do_convert_result(element_path, subkey, subvalue)) result = {key: container} else: final = cps_utils.cps_attr_types_map.from_data(element_path, value) result = {key: GLOBAL_MAP.from_cps(element_path, final)} return result
def find_type(self, key, val): if key in self.types: t = self.types[key] else: try: data_type = cps.type(key) except: data_type = {} if 'data_type' in data_type: t = data_type['data_type'] else: t = self.guess_type_for_len(val) if t is None: t = 'bytearray' return t
def _is_in_cps(key): '''Check if key is in CPS''' try: return cps.type(key) is not None except Exception: return False
# LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS # FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. # # See the Apache Version 2.0 License for specific language governing # permissions and limitations under the License. # import cps import sys if __name__ == '__main__': del sys.argv[0] #delete this program's name from the args for __arg in sys.argv: __obj_def = cps.info(__arg) if not 'names' in __obj_def: print('Failed to translate %s to a object.' % __arg) continue __names = __obj_def['names'].keys() for __name in __names: __elem = cps.type(__name) if 'data_type' not in __elem: print('Failed to get object details for %s' % __name) continue print('Name: %s\nData Type: %s\nID: %s\nAtrribute Type: %s\n\n' \ % (__elem['name'],__elem['data_type'],__elem['id'],__elem['attribute_type']) )