Esempio n. 1
0
    def set_uuid(self, nodetype):
        """Create and set a new UUID, for a given type of node.

        :param nodetype: Type of node
        :return: The new UUID
        """
        uuid = UUID.u()
        self[NodeType.get_id(nodetype)] = uuid
        return uuid
Esempio n. 2
0
def check(nodetype, **kwargs):
    """Get status of a task or template (etc.).

    :param nodetype: Type of thing you are looking for
    :type nodetype: str, See ``NodeType`` for defined values
    :param state: Only look at nodes in the given state (default=DONE, None for any)
    :type state: str
    :param multiple: If True, return all matching items; otherwise group by nodetype and, if given, name.
                     Return only the last record for each grouping.
    :type multiple: bool
    :param names: List of names (may be regular expressions) of nodes to look for.
    :type names: list of str, or str
    :param program_id: Only checks the nodes for the specifed program
    :param template_id: only checks the nodes that belong to the template with
            the specified template_id.

    :return: list(LogStatus). See parameter `multiple`.
    """
    if _log is None:
        raise NotInitializedError("check")
    if not NodeType.is_known(nodetype):
        raise ValueError(
            "Unknown nodetype {}, not in {}".format(nodetype, NodeType.known()))
    node_id_key = NodeType.get_id(nodetype)
    check_nodetype = nodetype != NodeType.ANY
    check_state = 'state' in kwargs and kwargs['state'] is not None
    multiple = False
    in_template_id = None
    in_program_id = None
    if 'template_id' in kwargs:
        in_template_id = kwargs['template_id']
    if 'program_id' in kwargs:
        in_program_id = kwargs['program_id']
    if not in_program_id:
        in_program_id = _program_uuid
    if 'multiple' in kwargs:
        multiple = kwargs['multiple']
    name_filters = []
    names = None
    if 'names' in list(kwargs.keys()):
        names = kwargs['names']
        if isinstance(names, str):
            names = [names]
        if not names:
            names = ['']
        for name in names:
            if not name.startswith('^'):
                name = '.*' + name
            name_filters.append(re.compile(name))

    def fltr(rec):

        if in_program_id and not in_program_id == rec.get(Keyword.PROGRAM_UID, None):
            return False
        if rec.get(Keyword.NODETYPE, None) is None or rec.get(Keyword.NAME, None) is None:
            return False
        if check_nodetype and not rec.get(Keyword.NODETYPE, None).startswith(nodetype):
            return False
        if in_template_id and not in_template_id.replace(" ", "+") == rec.get(Keyword.TMPL_UID, None):
            return False
        if check_state:
            log_state = rec.get(Keyword.STATE, None)
            if log_state is not None and kwargs['state'] != log_state:
                return False
        if name_filters:
            return any([nf.match(rec.get(Keyword.NAME, "")) for nf in name_filters])
        else:
            return True

    # Let's keep the results in order that the
    # nodes are created in (i.e. when they are logged as new)
    result = OrderedDict()
    keylist = [Keyword.NODETYPE]
    keylist.append(Keyword.NAME)
    keylist.append(node_id_key)
    if _log and _log.is_file:  # XXX: Put this logic in BaseLogger subclasses
        with open(_log.path, 'rb') as f:
            linenum = 1
            try:
                for rec in Reader(f):
                    if fltr(rec):
                        key = '#'.join([rec.get(k, '') for k in keylist])
                        if multiple:
                            # keep a list per name
                            _dict_append(result, key, rec)
                        else:
                            # keep only 1 per name
                            result[key] = rec
                    linenum += 1
            except ValueError as err:
                # add line number to error before re-raising
                raise ValueError("Line {:d}: {}".format(linenum, err))
    else:
        raise NotImplementedError(
            'Only know how to search files, not {}'.format(_log.path))
    if result:
        if multiple:
            flattened = itertools.chain.from_iterable(iter(result.values()))
            return list(map(LogStatus, flattened))
        else:
            return list(map(LogStatus, list(result.values())))
    else:
        return None
Esempio n. 3
0
 def get_uuid(self, nodetype):
     return self[NodeType.get_id(nodetype)]