Exemple #1
0
def _install_hier(hier, parent, prefix, name):
    """ Install hierarchy info into the proper table """
    namev = name.split()
    canon = prefix + utils.normalize_token(namev[0])
    if parent:
        hier[canon] = parent
    return canon
Exemple #2
0
def _install_toplev(toplev, prefix, elem):
    """ Install toplevel info into the proper table """
    elemv = elem.split()
    canon = prefix + utils.normalize_token(elemv[0])
    toplev.append(canon)
Exemple #3
0
def _install_alias(table, prefix, name):
    """ Install aliases info into the proper table """
    namev = name.split()
    for name in namev[1:]:
        table[prefix + utils.normalize_token(name)] = prefix + \
          utils.normalize_token(namev[0])
def parse(payload, person=None, since=None, until=None):
    """ Processes the email payload """

    index = 0  # Just in case we don't enter the loop

    state = __new_state()
    started = 0
    for index, line in enumerate(payload.splitlines()):

        line = line.strip()
        logging.debug('%d: << "%s"', index, line)
        tokens = line.split()

        if len(tokens) == 1:
            if tokens[0].lower() == "todo" or tokens[0] == "--":
                logging.debug("%d: end of parsing", index)
                person, since, until = None, None, None
                started = 0
                continue

        if len(tokens) == 0:
            if not started:
                logging.debug("%d: skipping empty line", index)
                continue
            __end_of_record(index, state)
            state = __new_state()
            started = 0  # We're now in the middle of nowhere
            logging.debug("%d: resetting state: %s", index, state)
            continue

        #
        # Weekly/mbox.py uses the string ".from <person> since <since>
        # until <until>" to signal that the sender changed and to indicate
        # the reporting period.
        #
        if (
            len(tokens) == 6
            and tokens[0].lower() == ".from"
            and tokens[1].startswith("@")
            and tokens[2].lower() == "since"
            and tokens[4].lower() == "until"
        ):
            logging.debug("%d: person: %s -> %s", index, person, tokens[1])
            person = tokens[1]
            person = person[1:]
            since = tokens[3]
            until = tokens[5]
            continue

        if not started and not person and not since and not until:
            logging.debug("%d: not started yet", index)
            continue

        # Make sure we have person, since and until
        if not person:
            raise RuntimeError("%d: person is not set" % index)
        if not since:
            raise RuntimeError("%d: since is not set" % index)
        if not until:
            raise RuntimeError("%d: until is not set" % index)

        started = 1  # we're now parsing the weekly
        if not state["initialized"]:
            sys.stdout.write('    {"people:%s": {"weekly:did": {\n' % person)
            sys.stdout.write('        "weekly:since": "%s",\n' % since)
            sys.stdout.write('        "weekly:until": "%s",\n' % until)
            state["initialized"] = 1
        state["lines"].append(line)

        for token in tokens:

            # Token 'laundering': get rid of that pesky punctuation
            while token and token[0] in ("("):
                token = token[1:]
            while token and token[-1] in (";", ":", ".", ",", "-", ")"):
                token = token[:-1]
            if not token:
                continue

            # Work around english possessive
            if token.endswith("'s"):
                token = token[:-2]

            if token.startswith("%"):
                token = token[1:]
                logging.debug("%d: activity: %s", index, token)
                token = utils.normalize_token(token)
                state["activity"] = token
            elif token.startswith("$"):
                token = token[1:]
                logging.debug("%d: project: %s", index, token)
                token = utils.normalize_token(token)
                state["project"] = token
            elif token.startswith("#"):
                token = token[1:]
                logging.debug("%d: tag: %s", index, token)
                if token.startswith("-"):
                    token = utils.normalize_token(token)
                    if token in state["tags"]:
                        logging.debug("%d: remove tag: %s", index, token)
                        state["tags"].remove(token)
                else:
                    token = utils.normalize_token(token)
                    logging.debug("%d: add tag: %s", index, token)
                    state["tags"].add(token)
            elif token.startswith("@"):
                token = token[1:]
                logging.debug("%d: handle: %s", index, token)
                if token.startswith("-"):
                    token = utils.normalize_token(token)
                    if token in state["handles"]:
                        logging.debug("%d: remove handle: %s", index, token)
                        state["handles"].remove(token)
                else:
                    token = utils.normalize_token(token)
                    logging.debug("%d: add handle: %s", index, token)
                    state["handles"].add(token)
            elif token.startswith("["):
                pass  # We deal with it later; because '[16 h]' is two tokens
            else:
                pass  # nothing

    if started:
        __end_of_record(index, state)