def get_metadata(item_name, namespace):
    """
    This function will return the Metadata object associated with the
    specified Item.

    Examples:
        .. code-block::

            # Get Metadata object from an Item's namespace
            get_metadata("Item_Name", "Namespace_Name")

    Args:
        item_name (string): name of the Item
        namespace (string): name of the namespace

    Returns:
        Metadata object or None: Metadata object containing the namespace
        ``value`` and ``configuration`` dictionary, but will be ``None`` if
        the namespace or Item do not exist
    """
    log.debug("get_metadata: Item [{}], namespace [{}]".format(
        item_name, namespace))
    metadata = metadata_registry.get(MetadataKey(namespace, item_name))
    '''
    # this might be interesting, but I haven't come across a need for it
    if metadata is None:
        return {"value": None, "configuration": None}
    else:
        return metadata
    '''
    return metadata_registry.get(MetadataKey(namespace, item_name))
Пример #2
0
def set_metadata(
        item_name,  # type: str
        namespace,  # type: str
        configuration,  # type: t.Mapping[str, t.Any]
        value=None,  # type: str
        overwrite=False  # type: bool
):
    # type: (...) -> None
    """
    This function creates or modifies Item metadata, optionally overwriting
    the existing data. If not overwriting, the provided keys and values will
    be overlaid on top of the existing keys and values.

    Examples:
        .. code-block::

            # Add/change metadata in an Item's namespace (only overwrites existing keys and "value" is optional)
            set_metadata("Item_Name", "Namespace_Name", {"Key_1": "key 1 value", "Key_2": 2, "Key_3": False}, "namespace_value")

            # Overwrite metadata in an Item's namespace with new data
            set_metadata("Item_Name", "Namespace_Name", {"Key_5": 5}, overwrite=True)

    Args:
        item_name (str): name of the Item
        namespace (str): name of the namespace
        configuration (dict): ``configuration`` dictionary to add to the
            namespace
        value (str): either the new namespace value or ``None``
        overwrite (bool): if ``True``, existing namespace data will be
            discarded
    """
    if overwrite:
        remove_metadata(item_name, namespace)
    metadata = get_metadata(item_name, namespace)
    if metadata is None or overwrite:
        LOG.trace(
            u"set_metadata: adding or overwriting metadata namespace with 'value: {}, configuration: {}': Item '{}', namespace '{}'"
            .format(value, configuration, item_name, namespace))
        METADATA_REGISTRY.add(
            Metadata(MetadataKey(namespace, item_name), value, configuration))
    else:
        if value is None:
            value = metadata.value
        new_configuration = dict(metadata.configuration).copy()
        new_configuration.update(configuration)
        LOG.trace(
            u"set_metadata: setting metadata namespace to 'value: {}, configuration: {}': Item '{}', namespace '{}'"
            .format(value, new_configuration, item_name, namespace))
        METADATA_REGISTRY.update(
            Metadata(MetadataKey(namespace, item_name), value,
                     new_configuration))
Пример #3
0
def set_metadata(item_name,
                 namespace,
                 configuration,
                 value=None,
                 overwrite=False):
    """
    This function creates or modifies Item metadata, optionally overwriting
    the existing data. If not overwriting, the provided keys and values will
    be overlaid on top of the existing keys and values.

    Examples:
        .. code-block::

            # Add/change metadata in an Item's namespace (only overwrites existing keys and "value" is optional)
            set_metadata("Item_Name", "Namespace_Name", {"Key_1": "key 1 value", "Key_2": 2, "Key_3": False}, "namespace_value")

            # Overwrite metadata in an Item's namespace with new data
            set_metadata("Item_Name", "Namespace_Name", {"Key_5": 5}, overwrite=True)

    Args:
        item_name (string): name of the Item
        namespace (string): name of the namespace
        configuration (dict): ``configuration`` dictionary to add to the
            namespace
        value (string): either the new namespace value or ``None``
        overwrite (bool): if ``True``, existing namespace data will be
            discarded
    """
    if overwrite:
        remove_metadata(item_name, namespace)
    metadata = get_metadata(item_name, namespace)
    if metadata is None or overwrite:
        log.debug(
            "set_metadata: adding or overwriting metadata namespace with [value: {}, configuration: {}]: Item [{}], namespace [{}]"
            .format(value, configuration, item_name, namespace))
        metadata_registry.add(
            Metadata(MetadataKey(namespace, item_name), value, configuration))
    else:
        if value is None:
            value = metadata.value
        new_configuration = dict(metadata.configuration).copy()
        new_configuration.update(configuration)
        log.debug(
            "set_metadata: setting metadata namespace to [value: {}, configuration: {}]: Item [{}], namespace [{}]"
            .format(value, new_configuration, item_name, namespace))
        metadata_registry.update(
            Metadata(MetadataKey(namespace, item_name), value,
                     new_configuration))
Пример #4
0
def remove_metadata(item_name, namespace=None):
    """
    This function removes the Item metadata for the specified namepsace or for
    all namespaces.

    Examples:
        .. code-block::

            # Remove a namespace from an Item
            remove_metadata("Item_Name", "Namespace_Name")

            # Remove ALL namespaces from an Item
            remove_metadata("Item_Name")

    Args:
        item_name (str): name of the item
        namespace (str): name of the namespace or ``None``, which will
            remove metadata in all namespaces for the specified Item
    """
    if namespace is None:
        LOG.debug(u"remove_metadata (all): Item '{}'".format(item_name))
        METADATA_REGISTRY.removeItemMetadata(item_name)
    else:
        LOG.debug(u"remove_metadata: Item '{}', namespace '{}'".format(
            item_name, namespace))
        METADATA_REGISTRY.remove(MetadataKey(namespace, item_name))
Пример #5
0
def remove_metadata(item_name, namespace=None):
    """
    This function removes the Item metadata for the specified namepsace or for
    all namespaces.

    Examples:
        .. code-block::

            # Remove a namespace from an Item
            remove_metadata("Item_Name", "Namespace_Name")

            # Remove ALL namespaces from an Item
            remove_metadata("Item_Name")

    Args:
        item_name (string): name of the item
        namespace (string): name of the namespace or ``None``, which will
            remove metadata in all namespaces for the specified Item
    """
    if namespace is None:
        log.debug("remove_metadata (all): Item [{}]".format(item_name))
        metadata_registry.removeItemMetadata(item_name)
    else:
        log.debug("remove_metadata: Item [{}], namespace [{}]".format(
            item_name, namespace))
        metadata_registry.remove(MetadataKey(namespace, item_name))
Пример #6
0
 def write(self,
           value='',
           configuration={}
           ):  #writes the value and conifuration for the namespace
     MetadataRegistry.add(
         Metadata(MetadataKey(self.namespace, self.item_name), str(value),
                  configuration))
Пример #7
0
def get_metadata(item_name, namespace):
    """
    This function will return the Metadata object associated with the
    specified Item.

    Examples:
        .. code-block::

            # Get Metadata object from an Item's namespace
            get_metadata("Item_Name", "Namespace_Name")

    Args:
        item_name (str): name of the Item
        namespace (str): name of the namespace

    Returns:
        Metadata object or None: Metadata object containing the namespace
        ``value`` and ``configuration`` dictionary, but will be ``None`` if
        the namespace or the Item does not exist
    """
    log.debug("get_metadata: Item [{}], namespace [{}]".format(
        item_name, namespace))
    metadata = metadata_registry.get(MetadataKey(namespace, item_name))
    return metadata_registry.get(MetadataKey(namespace, item_name))
Пример #8
0
def get_metadata(item_name, namespace):
    # type: (str, str) -> t.Union[Metadata, None]
    """
    This function will return the Metadata object associated with the
    specified Item.

    Examples:
        .. code-block::

            # Get Metadata object from an Item's namespace
            get_metadata("Item_Name", "Namespace_Name")

    Args:
        item_name (str): name of the Item
        namespace (str): name of the namespace

    Returns:
        Metadata object or None: Metadata object containing the namespace
        ``value`` and ``configuration`` dictionary, but will be ``None`` if
        the namespace or the Item does not exist
    """
    LOG.trace(u"get_metadata: Item '{}', namespace '{}'".format(
        item_name, namespace))
    return METADATA_REGISTRY.get(MetadataKey(namespace, item_name))
Пример #9
0
 def remove(self):  #removes the namespace
     MetadataRegistry.remove(MetadataKey(self.namespace, self.item_name))
Пример #10
0
 def read_raw(
     self
 ):  # reads the value and configuration for the namespace, generally not called outside of class
     return MetadataRegistry.get(MetadataKey(
         self.namespace,
         self.item_name))  # returns None if namespace does not exist
Пример #11
0
    def __init__(self,item_registry,config):
        semantic_tags = {}
        f = io.open("/openhab/python/shared/semantic/config/tags_de.txt", "r", encoding="utf-8")
        lines = f.readlines()
        for line in lines:
            keys,synonyms = line.strip().split("=")
            keys = keys.split("_")
            synonyms = synonyms.lower().split(",")
            type = keys[0]
            name = keys[-1]
            
            semantic_tags[name] = [type,synonyms]

        semantic_tags["Location"] = ["Location",[]]
        semantic_tags["Equipment"] = ["Equipment",[]]
        semantic_tags["Point"] = ["Point",[]]
        semantic_tags["Property"] = ["Property",[]]

        # build semantic items
        self.semantic_items = {}
        for item in item_registry.getItems():
            semantic_type = "Group" if item.getType() == "Group" else "Item"
            semantic_properties = []
            tags_search = []
            item_tags = item.getTags()
            for item_tag in item_tags:
                if item_tag in semantic_tags:
                    _semantic_type,_tags_search = semantic_tags[item_tag]
                    tags_search += _tags_search
                    if _semantic_type in ["Location","Equipment","Point"]:
                        semantic_type = _semantic_type
                    elif _semantic_type == "Property":
                        semantic_properties.append(item_tag)
                  
            #[u'semantics', u'synonyms']
            synonyms = METADATA_REGISTRY.get(MetadataKey("synonyms", item.getName()))
            synonym_search = synonyms.getValue().lower().split(",") if synonyms is not None else []
            synonym_search = map(unicode.strip, synonym_search)
            
            answer = METADATA_REGISTRY.get(MetadataKey("answer", item.getName()))
            answer = answer.getValue() if answer is not None else None

            semantic_item = SemanticItem(item,semantic_type,semantic_properties,tags_search,synonym_search,answer)
            self.semantic_items[semantic_item.item.getName()] = semantic_item

        # prepare semantic locations and children
        self.root_locations = []
        for semantic_item in self.semantic_items.values():
            if semantic_item.item.getType() == "Group":
                children = semantic_item.item.getMembers()
                for item in children:
                    semantic_item.children.append(self.semantic_items[item.getName()])

                if semantic_item.getSemanticType() == "Location":
                    if len(semantic_item.item.getGroupNames()) == 0:
                        self.root_locations.append(semantic_item)
                        
        # prepare parents
        for semantic_item in self.semantic_items.values():
            for semantic_children in semantic_item.children:
                semantic_children.parents.append(semantic_item)

        # prepare regex matcher
        self.semantic_search_part_regex = {}
        self.semantic_search_full_regex = {}
        for semantic_item in self.semantic_items.values():
            for search_term in semantic_item.search_terms:
                if search_term in self.semantic_search_part_regex:
                    continue
                self.semantic_search_part_regex[search_term] = re.compile(config["main"]["phrase_part_matcher"].format(search_term))
                self.semantic_search_full_regex[search_term] = re.compile(config["main"]["phrase_full_matcher"].format(search_term))