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 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) 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 _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