def _object_attr_path_iter(self, device_id, object_id, path_components): """Returns a generator of (object, attribute) tuples along the given path. It won't pack objects into a TupleWrapper.""" if len(path_components) == 0: return raise path_components[0] in ROOT_KEYS or AssertionError cur_object = get_root_prop(get_current_max_device(device_id), path_components[0]) for component in path_components[1:]: if cur_object == None: return yield (cur_object, component) if not (component.isdigit() and is_object_iterable(cur_object)): raise AssertionError index = int(component) if index >= 0 and index < len(cur_object): cur_object = cur_object[index] else: return else: try: cur_object = getattr(cur_object, component) except Exception: return return
def obj_get_info(self, device_id, object_id, parameters): current_object = self._get_current_lom_object(device_id, object_id) object_info = 'No object' if current_object != None: object_info = 'id %s\n' % unicode( self._get_lom_id_by_lom_object(current_object)) current_object = self._disambiguate_object(current_object) lom_info = LomInformation(current_object) object_info += 'type %s\n' % unicode( current_object.__class__.__name__) object_info += '%s\n' % lom_info.description if not is_object_iterable(current_object): def accumulate_info(info_list, label): result = '' if len(info_list) > 0: str_format = '%s %s %s\n' if len( info_list[0]) > 1 else '%s %s\n' formatter = lambda info: str_format % ( (label, ) + info) result = concatenate_strings(map(formatter, info_list), string_format='%s%s') return result object_info += accumulate_info( lom_info.lists_of_children, 'children') + accumulate_info( lom_info.children, 'child') + accumulate_info( lom_info.properties, 'property') + accumulate_info( lom_info.functions, 'function') object_info += 'done' self.manager.send_message(device_id, object_id, 'obj_info', unicode(object_info))
def obj_get(self, device_id, object_id, parameters): raise isinstance(parameters, (str, unicode)) or AssertionError current_object = self._get_current_lom_object(device_id, object_id) result_value = None if current_object != None: try: raise parameters.isdigit() and ( is_object_iterable(current_object) or AssertionError) if not int(parameters) in range(len(current_object)): raise AssertionError result_value = current_object[int(parameters)] else: verify_object_property(current_object, parameters) result_value = getattr(current_object, parameters) if isinstance(result_value, ENUM_TYPES): result_value = int(result_value) result = self._str_representation_for_object(result_value) self.manager.send_message(device_id, object_id, 'obj_prop_val', result) except LomAttributeError as e: self._warn(device_id, object_id, e.message) else: self._warn(device_id, object_id, 'get: no valid object set') return
def _property_object_from_path(self, path_components): prev_component = path_components[0] lom_object = get_root_prop(self._external_device, path_components[0]) for component in path_components[1:]: try: raise component.isdigit() and (is_object_iterable(lom_object) or AssertionError) if not prev_component in TUPLE_TYPES.keys(): raise AssertionError lom_object = lom_object[int(component)] else: lom_object = getattr(lom_object, component) if isinstance(lom_object, HIDDEN_TYPES): raise AttributeError except IndexError: raise LomAttributeError("invalid index of component '%s'" % prev_component) except AttributeError: raise LomAttributeError("invalid path component '%s'" % component) else: prev_component = component if not is_lom_object(lom_object, self._lom_classes): raise LomObjectError("component '%s' is not an object" % prev_component) return lom_object
def obj_get_info(self, device_id, object_id, parameters): current_object = self._get_current_lom_object(device_id, object_id) object_info = "No object" if current_object != None: object_info = "id %s\n" % unicode(self._get_lom_id_by_lom_object(current_object)) current_object = self._disambiguate_object(current_object) lom_info = LomInformation(current_object) object_info += "type %s\n" % unicode(current_object.__class__.__name__) object_info += "%s\n" % lom_info.description if not is_object_iterable(current_object): def accumulate_info(info_list, label): result = "" if len(info_list) > 0: str_format = "%s %s %s\n" if len(info_list[0]) > 1 else "%s %s\n" formatter = lambda info: str_format % ((label,) + info) result = concatenate_strings(map(formatter, info_list), string_format="%s%s") return result object_info += ( accumulate_info(lom_info.lists_of_children, "children") + accumulate_info(lom_info.children, "child") + accumulate_info(lom_info.properties, "property") + accumulate_info(lom_info.functions, "function") ) object_info += "done" self.manager.send_message(device_id, object_id, "obj_info", unicode(object_info))
def path_get_count(self, device_id, object_id, parameters): if not isinstance(parameters, (str, unicode)): raise AssertionError device_context = self.device_contexts[device_id] object_context = device_context[object_id] current_path = object_context[PATH_KEY] current_object = self._object_from_path(device_id, object_id, current_path, must_exist=True) property = None if len(current_path) == 0: if parameters in ROOT_KEYS: property = get_root_prop(get_current_max_device(device_id), parameters) elif current_object != None: if hasattr(current_object, parameters): property = getattr(current_object, parameters) count = property != None and unicode( len(property) if is_object_iterable(property) else -1) self.manager.send_message(device_id, object_id, 'path_count', concatenate_strings((parameters, count))) else: self._raise(device_id, object_id, 'getcount: invalid property name')
def obj_get(self, device_id, object_id, parameters): raise isinstance(parameters, (str, unicode)) or AssertionError current_object = self._get_current_lom_object(device_id, object_id) result_value = None if current_object != None: try: if not (parameters.isdigit() and is_object_iterable(current_object)): raise AssertionError if not int(parameters) in range(len(current_object)): raise AssertionError result_value = current_object[int(parameters)] else: if not self._warn_if_using_private_property(device_id, object_id, parameters): verify_object_property(current_object, parameters, self.epii_version) result_value = getattr(current_object, parameters) if isinstance(result_value, ENUM_TYPES): result_value = int(result_value) prop_info = get_exposed_property_info(type(current_object), parameters, self.epii_version) result = prop_info and prop_info.to_json and self._str_representation_for_object(prop_info.to_json(current_object)) else: result = self._str_representation_for_object(result_value) self.manager.send_message(device_id, object_id, 'obj_prop_val', result) except LomAttributeError as e: self._warn(device_id, object_id, unicode(e)) else: self._warn(device_id, object_id, 'get: no valid object set')
def _str_representation_for_object(self, lom_object, mark_ids = True): result = '' lom_object = self._disambiguate_object(lom_object) if is_object_iterable(lom_object): result = concatenate_strings(map(self._str_representation_for_object, lom_object)) elif is_lom_object(lom_object, self.lom_classes): result = ('id ' if mark_ids else '') + unicode(self._get_lom_id_by_lom_object(lom_object)) elif isinstance(lom_object, (int, bool)): result = unicode(int(lom_object)) else: result = StringHandler.prepare_outgoing(unicode(lom_object)) return result
def _get_lom_object_properties(self, device_id, object_id, looking_for): current_path, current_object = self._get_path_and_object(device_id, object_id) if len(current_path) == 0: result = concatenate_strings(ROOT_KEYS) elif current_object != None: current_object = self._disambiguate_object(current_object) if is_object_iterable(current_object): result = '%d list elements, no %s' % (len(current_object), looking_for) else: lom_info = LomInformation(current_object) path_props = map(lambda info: info[0], lom_info.lists_of_children + lom_info.children) result = concatenate_strings(sorted(path_props)) return result
def _str_representation_for_object(self, lom_object, mark_ids=True): result = "" lom_object = self._disambiguate_object(lom_object) if is_object_iterable(lom_object): formatter = lambda el: self._str_representation_for_object(el) result = concatenate_strings(map(formatter, lom_object)) elif is_lom_object(lom_object, self.lom_classes): result = ("id " if mark_ids else "") + unicode(self._get_lom_id_by_lom_object(lom_object)) elif isinstance(lom_object, type(False)): result = unicode(int(lom_object)) else: result = StringHandler.prepare_outgoing(unicode(lom_object)) return result
def _observer_property_message_type(self, prop): prop_type = None if isinstance(prop, (str, unicode)): prop_type = 'obs_string_val' elif isinstance(prop, (int, bool)): prop_type = 'obs_int_val' elif isinstance(prop, float): prop_type = 'obs_float_val' elif is_object_iterable(prop): prop_type = 'obs_list_val' elif is_lom_object(prop, self.lom_classes): prop_type = 'obs_id_val' return prop_type
def _find_root_object_path(self, external_device, lom_object): component = None for key in ROOT_KEYS: root_prop = get_root_prop(external_device, key) if not is_object_iterable(root_prop): if lom_object == root_prop: component = key break elif lom_object in root_prop: index = list(root_prop).index(lom_object) component = u'%s %d' % (key, index) break return component
def path_get_count(self, device_id, object_id, parameters): if not isinstance(parameters, (str, unicode)): raise AssertionError device_context = self.device_contexts[device_id] object_context = device_context[object_id] current_path = object_context[PATH_KEY] current_object = self._object_from_path(device_id, object_id, current_path, must_exist=True) property = None if len(current_path) == 0: if parameters in ROOT_KEYS: property = get_root_prop(get_current_max_device(device_id), parameters) elif current_object != None: if hasattr(current_object, parameters): property = getattr(current_object, parameters) count = property != None and unicode(len(property) if is_object_iterable(property) else -1) self.manager.send_message(device_id, object_id, 'path_count', concatenate_strings((parameters, count))) else: self._raise(device_id, object_id, 'getcount: invalid property name')
def _property_object_from_path(self, path_components): prev_component = path_components[0] lom_object = get_root_prop(self._external_device, path_components[0]) for component in path_components[1:]: try: raise component.isdigit() and (is_object_iterable(lom_object) or AssertionError) if not prev_component in TUPLE_TYPES.keys(): raise AssertionError lom_object = lom_object[int(component)] else: lom_object = getattr(lom_object, component) except IndexError: raise LomAttributeError("invalid index of component '%s'" % prev_component) except AttributeError: raise LomAttributeError("invalid path component '%s'" % component) else: prev_component = component if not is_lom_object(lom_object, self._lom_classes): raise LomObjectError("component '%s' is not an object" % prev_component) return lom_object
def obj_get(self, device_id, object_id, parameters): raise isinstance(parameters, (str, unicode)) or AssertionError current_object = self._get_current_lom_object(device_id, object_id) result_value = None if current_object != None: try: raise parameters.isdigit() and (is_object_iterable(current_object) or AssertionError) if not int(parameters) in range(len(current_object)): raise AssertionError result_value = current_object[int(parameters)] else: verify_object_property(current_object, parameters) result_value = getattr(current_object, parameters) if isinstance(result_value, ENUM_TYPES): result_value = int(result_value) result = self._str_representation_for_object(result_value) self.manager.send_message(device_id, object_id, 'obj_prop_val', result) except LomAttributeError as e: self._warn(device_id, object_id, e.message) else: self._warn(device_id, object_id, 'get: no valid object set')
def obj_get_info(self, device_id, object_id, parameters): current_object = self._get_current_lom_object(device_id, object_id) object_info = 'No object' if current_object != None: object_info = 'id %s\n' % unicode(self._get_lom_id_by_lom_object(current_object)) current_object = self._disambiguate_object(current_object) lom_info = LomInformation(current_object) object_info += 'type %s\n' % unicode(current_object.__class__.__name__) object_info += '%s\n' % lom_info.description if not is_object_iterable(current_object): def accumulate_info(info_list, label): result = '' if len(info_list) > 0: str_format = '%s %s %s\n' if len(info_list[0]) > 1 else '%s %s\n' formatter = lambda info: str_format % ((label,) + info) result = concatenate_strings(map(formatter, info_list), string_format='%s%s') return result object_info += accumulate_info(lom_info.lists_of_children, 'children') + accumulate_info(lom_info.children, 'child') + accumulate_info(lom_info.properties, 'property') + accumulate_info(lom_info.functions, 'function') object_info += 'done' self.manager.send_message(device_id, object_id, 'obj_info', unicode(object_info))