Beispiel #1
0
def _filename_matches(
    filename: str,
    reftime: int,
    inclusion: str,
    exclusion: str,
) -> Tuple[bool, str]:
    date_inclusion = ""
    inclusion_is_regex = False
    exclusion_is_regex = False

    if inclusion.startswith("~"):
        inclusion_is_regex = True
        inclusion = inclusion[1:]
    if exclusion.startswith("~"):
        exclusion_is_regex = True
        exclusion = exclusion[1:]

    inclusion_tmp = fileinfo_process_date(inclusion, reftime)
    if inclusion != inclusion_tmp:
        inclusion = inclusion_tmp
        date_inclusion = inclusion_tmp

    incl_match: Union[bool, Match, None] = None
    if inclusion_is_regex:
        incl_match = regex(inclusion).match(filename)
    else:
        incl_match = fnmatch.fnmatch(filename, inclusion)

    excl_match: Union[bool, Match, None] = None
    if exclusion_is_regex:
        excl_match = regex(exclusion).match(filename)
    else:
        excl_match = fnmatch.fnmatch(filename, exclusion)

    if incl_match and not excl_match:
        return True, date_inclusion
    return False, date_inclusion
Beispiel #2
0
def _match(name: str, pattern: str) -> Union[bool, Match, None]:
    return (regex(pattern[1:]).match(name) if pattern.startswith("~") else  #
            fnmatch.fnmatch(name, pattern))
Beispiel #3
0
def parse_ibm_mq(string_table: StringTable, group_by_object: str) -> Section:
    re_intro = regex(r"^QMNAME\((.*)\)[\s]*STATUS\((.*?)\)[\s]*NOW\((.*)\)")
    re_group = regex(r"^AMQ\d+\w?: [^.]*.")
    re_key = regex(r"[\s]*[A-Z0-9]+\(")
    re_second_column = regex(r" [A-Z0-9]+\(")
    re_key_value = regex(r"([A-Z0-9]+)\((.*)\)[\s]*")

    def record_attribute(s, attributes, parsed):
        pair = re_key_value.match(s)
        if pair is None:
            return
        key = pair.group(1)
        value = pair.group(2).strip()
        attributes[key] = value

    def record_group(qmname, attributes, parsed):
        obj = attributes.get(group_by_object)
        if obj is not None and not obj.startswith(("SYSTEM", "AMQ.MQEXPLORER")):
            obj = "%s:%s" % (qmname, obj)
            parsed.setdefault(obj, {})
            parsed[obj].update(attributes)

    def lookahead(iterable):
        """
        Pass through all values from the given iterable, augmented by the
        information if there are more values to come after the current one
        (True), or if it is the last value (False).
        """
        sentinel = object()
        previous = sentinel
        for value in iter(iterable):
            if previous is not sentinel:
                yield previous, True
            previous = value
        yield previous, False

    parsed: Dict[str, Any] = {}
    attributes: Dict[str, Any] = {}
    for (line,), has_more in lookahead(string_table):
        intro_line = re_intro.match(line)
        if intro_line:
            if attributes:
                record_group(qmname, attributes, parsed)  # type: ignore[has-type]
                attributes.clear()
            qmname = intro_line.group(1)
            qmstatus = intro_line.group(2)
            now = intro_line.group(3)
            parsed[qmname] = {"STATUS": qmstatus, "NOW": now}
            continue
        if re_group.match(line) or not has_more:
            if attributes:
                record_group(qmname, attributes, parsed)
                attributes.clear()
            continue
        if re_key.match(line):
            if re_second_column.match(line[39:]):
                first_half = line[:40]
                second_half = line[40:]
                record_attribute(first_half, attributes, parsed)
                record_attribute(second_half, attributes, parsed)
            else:
                record_attribute(line, attributes, parsed)
    return parsed