def get_devices(self, gateway_id=None): """ Return a dictionary of devices for a given device_type. Can be restricted to a specific gateway_id if it's provided. :return: """ # if gateway_id is None: # gateway_id = self.gateway_id search_attributes = [{ "field": "device_type_id", "value": self.device_type_id, "limiter": 1, }] try: results = do_search_instance( search_attributes, self._Parent._Devices.devices, self._Parent._class_storage_search_fields) if results["was_found"]: devices = {} for device_id, device in results['values'].items(): if gateway_id is not None: if device.gateway_id != gateway_id: continue devices[device_id] = device return devices else: return {} except YomboWarning as e: raise KeyError(f"Get devices had problems: {e}")
def get_devices(self, gateway_id=None): """ Return a dictionary of devices for a given device_type :return: """ # if gateway_id is None: # gateway_id = self.gateway_id attrs = [{ 'field': 'device_type_id', 'value': self.device_type_id, 'limiter': 1, }] try: found, key, item, ratio, others = do_search_instance( attrs, self._DeviceTypes._Devices.devices, self._DeviceTypes.device_type_search_attributes) if found: devices = {} for item in others: device = item['value'] if gateway_id is not None: if device.gateway_id != gateway_id: continue devices[device['device_id']] = device return devices else: return {} except YomboWarning as e: raise KeyError('Get devices had problems: %s' % e)
def get_meta(self, node_requested, node_type=None, limiter=None, status=None): """ Performs the actual search. .. note:: Can use the built in methods below or use get_meta/get to include 'node_type' limiter: >>> self._Nodes['13ase45'] or: >>> self._Nodes['numeric'] :raises YomboWarning: For invalid requests. :raises KeyError: When item requested cannot be found. :param node_requested: The node ID or node label to search for. :type node_requested: string :param limiter_override: Default: .89 - A value between .5 and .99. Sets how close of a match it the search should be. :type limiter_override: float :param status: Deafult: 1 - The status of the node to check for. :type status: int :return: Pointer to requested node. :rtype: dict """ if limiter is None: limiter = .89 if limiter > .99999999: limiter = .99 elif limiter < .10: limiter = .10 if status is None: status = 1 if node_requested in self.nodes: item = self.nodes[node_requested] if item.status != status: raise KeyError("Requested node found, but has invalid status: %s" % item.status) return item else: attrs = [ { 'field': 'node_id', 'value': node_requested, 'limiter': limiter, }, { 'field': 'machine_label', 'value': node_requested, 'limiter': limiter, } ] try: # logger.debug("Get is about to call search...: %s" % node_requested) # found, key, item, ratio, others = self._search(attrs, operation="highest") found, key, item, ratio, others = do_search_instance(attrs, self.nodes, self.node_search_attributes, limiter=limiter, operation="highest") # logger.debug("found node by search: others: {others}", others=others) if node_type is not None: for other in others: if other['value'].node_type == node_type and other['ratio'] > limiter: return other['value'] else: if found: return item raise KeyError("Node not found: %s" % node_requested) except YomboWarning as e: raise KeyError('Searched for %s, but had problems: %s' % (node_requested, e))
def get(self, command_requested, limiter=None, status=None, command_list=None): """ Looks for commands by it's id, label, and machine_label. .. note:: Modules shouldn't use this function. Use the built in reference to find commands: >>> self._Commands['sz45q3423'] or: >>> self._Commands['on'] :raises YomboWarning: For invalid requests. :raises KeyError: When item requested cannot be found. :raises ValueError: When input value is invalid. :param command_requested: The command ID, label, and machine_label to search for. :type command_requested: string :param limiter_override: Default: .89 - A value between .5 and .99. Sets how close of a match it the search should be. :type limiter_override: float :param status: Deafult: 1 - The status of the command to check for. :type status: int :return: Pointer to requested command. :rtype: dict """ if inspect.isclass(command_requested): if isinstance(command_requested, Command): return command_requested else: raise ValueError("Passed in an unknown object") if limiter is None: limiter = .89 if limiter > .99999999: limiter = .99 elif limiter < .10: limiter = .10 if command_requested in self.commands: item = self.commands[command_requested] if status is not None and item.status != status: raise KeyError( "Requested command found, but has invalid status: %s" % item.status) return item else: attrs = [{ 'field': 'command_id', 'value': command_requested, 'limiter': limiter, }, { 'field': 'label', 'value': command_requested, 'limiter': limiter, }, { 'field': 'machine_label', 'value': command_requested, 'limiter': limiter, }] try: if command_list is not None: commands = command_list else: commands = self.commands logger.debug("Get is about to call search...: %s" % command_requested) found, key, item, ratio, others = do_search_instance( attrs, commands, self.command_search_attributes, limiter=limiter, operation="highest") logger.debug("found command by search: {command_id}", command_id=key) if found: return item else: raise KeyError("Command not found: %s" % command_requested) except YomboWarning as e: raise KeyError('Searched for %s, but had problems: %s' % (command_requested, e))
def search_advanced(self, search_attributes, limiter=None, max_results=None, required_field=None, required_value=None, ignore_field=None, ignore_value=None): """ An advanced search that accepts an array if items to search for. Allows searching multiple attributes and different attributes. It's suggested to use search() for basic searching. This performs a fuzzy search and can be adjust using the limiter argument. By default, the searched items must be at least 90% (.90) match. .. note:: Can use the built in methods below or use search/get to include "item_type" limiter: >>> self._Nodes["13ase45"] or: >>> self._Nodes["numeric"] :raises YomboWarning: For invalid requests. :raises KeyError: When item requested cannot be found. :param search_attributes: A string to search for. Will look at the id's, machine_label, label, item_type, destination :type search_attributes: list, dict :param limiter: Default: .90 - A value between .5 and .99. Sets how close of a match it the search should be. :type limiter: float :param max_results: How many results to return, defaults to 0 to find items above the limiter. :type max_results: int :param required_field: If set, this field will be required to match. Typically used for status. :type required_field: str :param required_value: Any value to require :type required_value: mixed :param ignore_field: If set, and this field matches the result, the result will be dropped. :type ignore_field: str :param ignore_value: Any value to force a result to be dropped. :type ignore_value: mixed :return: Pointer to requested item. :rtype: dict """ items_to_search = getattr(self, self._class_storage_attribute_name) try: # logger.debug("item.search() is about to call do_search_instance...: %s" % item_requested) results = do_search_instance( search_attributes, items_to_search, allowed_keys=self._class_storage_search_fields, limiter=limiter, max_results=max_results, required_field=required_field, required_value=required_value, ignore_field=ignore_field, ignore_value=ignore_value, ) # logger.debug("found item by search: others: {others}", others=others) if results["was_found"]: return results raise KeyError(f"item not found.") except YomboWarning as e: raise KeyError( f"Searched for {self._class_storage_attribute_name}, but had problems: {e}" )
def get(self, input_type_requested, limiter=None, status=None): """ Performs the actual search. .. note:: Modules shouldn't use this function. Use the built in reference to find input types: >>> self._InputTypes['13ase45'] or: >>> self._InputTypes['numeric'] :raises YomboWarning: For invalid requests. :raises KeyError: When item requested cannot be found. :param input_type_requested: The input type ID or input type label to search for. :type input_type_requested: string :param limiter_override: Default: .89 - A value between .5 and .99. Sets how close of a match it the search should be. :type limiter_override: float :param status: Deafult: 1 - The status of the input type to check for. :type status: int :return: Pointer to requested input type. :rtype: dict """ if limiter is None: limiter = .89 if limiter > .99999999: limiter = .99 elif limiter < .10: limiter = .10 if input_type_requested in self.input_types: item = self.input_types[input_type_requested] if status is not None and item.status != status: raise KeyError( "Requested input type found, but has invalid status: %s" % item.status) return item else: attrs = [{ 'field': 'input_type_id', 'value': input_type_requested, 'limiter': limiter, }, { 'field': 'label', 'value': input_type_requested, 'limiter': limiter, }, { 'field': 'machine_label', 'value': input_type_requested, 'limiter': limiter, }] try: # logger.debug("Get is about to call search...: %s" % input_type_requested) # found, key, item, ratio, others = self._search(attrs, operation="highest") found, key, item, ratio, others = do_search_instance( attrs, self.input_types, self.input_type_search_attributes, limiter=limiter, operation="highest") # logger.debug("found input type by search: {input_type_id}", input_type_id=key) if found: return item else: raise KeyError("Input type not found: %s" % input_type_requested) except YomboWarning as e: raise KeyError('Searched for %s, but had problems: %s' % (input_type_requested, e))
def get(self, cron_task_requested, limiter=None, status=None): """ Looks for a cron task by it's id and label. .. note:: Modules shouldn't use this function. Use the built in reference to find cron tasks: >>> self._CronTab['129da137ab9318'] or: >>> self._CronTab['module.mymodule.mycron'] :raises YomboWarning: For invalid requests. :raises KeyError: When item requested cannot be found. :param cron_task_requested: The cron task ID or cron task label to search for. :type cron_task_requested: string :param limiter_override: Default: .89 - A value between .5 and .99. Sets how close of a match it the search should be. :type limiter_override: float :param status: Deafult: 1 - The status of the cron task to check for. :type status: int :return: Pointer to requested cron tab. :rtype: dict """ if limiter is None: limiter = .89 if limiter > .99999999: limiter = .99 elif limiter < .10: limiter = .10 if cron_task_requested in self.cron_tasks: item = self.cron_tasks[cron_task_requested] if status is not None and item.status != status: raise KeyError( "Requested cron tab found, but has invalid status: %s" % item.status) return item else: attrs = [{ 'field': 'cron_task_id', 'value': cron_task_requested, 'limiter': limiter, }, { 'field': 'label', 'value': cron_task_requested, 'limiter': limiter, }] try: # logger.debug("Get is about to call search...: %s" % cron_task_requested) found, key, item, ratio, others = do_search_instance( attrs, self.cron_tasks, self.cron_task_search_attributes, limiter=limiter, operation="highest") # logger.debug("found cron tab by search: {cron_task_id}", cron_task_id=key) if found: return item else: raise KeyError("Cron tab not found: %s" % cron_task_requested) except YomboWarning as e: raise KeyError('Searched for %s, but had problems: %s' % (cron_task_requested, e))