Example #1
0
def _check_walker(template_fname, template_defaults, template_key, comment, template_parsed, expected):
    fil = _apply_to_template(template_fname, template_defaults, {template_key: comment})
    if isinstance(expected, type):
        try:
            list(walk(fil))
            ok_(False, "Expected exception %s" % expected)
        except expected:
            ok_(True)
    else:
        walked = list(walk(fil))

        expected_parsed = []
        for elem in template_parsed:
            if isinstance(elem, tuple):
                # not a template holder?  goes in as is.
                expected_parsed.append(elem)
            elif elem == template_key:
                # substitute in what we expect for the comment
                for e in expected:
                    expected_parsed.append(e)
            else:
                # it's a template key we don't care about, skip.
                pass

        eq_(expected_parsed, walked)
Example #2
0
def _check_walker(template_fname, template_defaults, template_key, comment,
                  template_parsed, expected):
    fil = _apply_to_template(template_fname, template_defaults,
                             {template_key: comment})
    if isinstance(expected, type):
        try:
            list(walk(fil))
            ok_(False, "Expected exception %s" % expected)
        except expected:
            ok_(True)
    else:
        walked = list(walk(fil))

        expected_parsed = []
        for elem in template_parsed:
            if isinstance(elem, tuple):
                # not a template holder?  goes in as is.
                expected_parsed.append(elem)
            elif elem == template_key:
                # substitute in what we expect for the comment
                for e in expected:
                    expected_parsed.append(e)
            else:
                # it's a template key we don't care about, skip.
                pass

        eq_(expected_parsed, walked)
Example #3
0
def _find_candidates(input_f, line_number):
    cur_old_fname = None
    cur_new_fname = None
    cur_old_line_num = None
    cur_new_line_num = None
    cur_line_num = 0
    cur_old_line = None
    cur_new_line = None

    for item in walk(input_f):
        cur_old_fname = _maybe_update_old_fname(item, cur_old_fname)
        cur_new_fname = _maybe_update_new_fname(item, cur_new_fname)
        cur_old_line_num = _maybe_update_old_line_num(item, cur_old_line_num)
        cur_new_line_num = _maybe_update_new_line_num(item, cur_new_line_num)
        cur_line_num = _update_line_num(item, cur_line_num)
        cur_old_line = _maybe_update_old_line(item, cur_old_line)
        cur_new_line = _maybe_update_new_line(item, cur_new_line)

        if cur_line_num >= line_number:
            return ((cur_new_fname,
                      _safe_decr(cur_new_line_num),
                      cur_new_line),
                    (cur_old_fname,
                     _safe_decr(cur_old_line_num),
                     cur_old_line))

    return tuple()
Example #4
0
def _find_candidates(input_f, line_number):
    """
    If @line_number is a line number in the diffscuss file in
    @input_f, return a list of two Candidate tuples:

    - one for the new version of the source in the diffscuss file

    - one for the old version of the source in the diffscuss file

    The new version is always listed first.

    If line_number ends up being greater than the number of lines in
    input_f, returns an empty list.
    """
    cur_old_fname = None
    cur_new_fname = None
    cur_old_line_num = None
    cur_new_line_num = None
    cur_old_line = None
    cur_new_line = None

    for (index, item) in enumerate(walk(input_f)):
        # walk the diffscuss file line by line, maintaing the updated
        # file names, line numbers, and line texts for both the old
        # and new versions of the source
        cur_old_fname = _maybe_update_old_fname(item, cur_old_fname)
        cur_new_fname = _maybe_update_new_fname(item, cur_new_fname)
        cur_old_line_num = _maybe_update_old_line_num(item, cur_old_line_num)
        cur_new_line_num = _maybe_update_new_line_num(item, cur_new_line_num)
        cur_old_line = _maybe_update_old_line(item, cur_old_line)
        cur_new_line = _maybe_update_new_line(item, cur_new_line)
        cur_line_num = index + 1

        # once we've walked past the line number in the diffscuss
        # file, maintaining the file / line number context for both
        # old and new versions of the source as we went, we have our
        # candidates for matching source.
        if cur_line_num >= line_number:
            return [
                Candidate(fname=cur_new_fname,
                          line_num=_safe_decr(cur_new_line_num),
                          line_text=cur_new_line),
                Candidate(fname=cur_old_fname,
                          line_num=_safe_decr(cur_old_line_num),
                          line_text=cur_old_line)
            ]

    return list()
Example #5
0
def _parse(listing):
    fname = os.path.basename(listing)

    extractors = [OrigAuthorExtractor(),
                  PostedAtExtractor(),
                  LastCommentExtractor(),
                  LineCounter([walker.DIFF_HEADER, walker.DIFF]),
                  NumCommentsExtractor(),
                  TopAuthorsExtractor()]

    with open(listing, 'rb') as fil:
        for line_tup in walker.walk(fil):
            for extractor in extractors:
                extractor.process_line(line_tup)

    return [fname] + [e.get() for e in extractors]
Example #6
0
def _find_base_target_idx(orig_diff, path):
    logging.debug("Finding base target index for %s", path)
    looking_for_range_line = False

    for (i, tagged_line) in enumerate(walk(StringIO(orig_diff))):
        logging.debug("Checking at index %d tagged line %s", i, tagged_line)
        assert (tagged_line[0] not in [COMMENT_HEADER, COMMENT_BODY])
        if looking_for_range_line and _is_range_line(tagged_line):
            logging.debug("Found range line at index %d", i)
            return i
        if _is_target_path(tagged_line, path):
            logging.debug(
                "Found path %s at index %s, now looking for range line", path,
                i)
            looking_for_range_line = True
    logging.info("Could not find path %s in diff", path)
    return None
Example #7
0
def _find_candidates(input_f, line_number):
    """
    If @line_number is a line number in the diffscuss file in
    @input_f, return a list of two Candidate tuples:

    - one for the new version of the source in the diffscuss file

    - one for the old version of the source in the diffscuss file

    The new version is always listed first.

    If line_number ends up being greater than the number of lines in
    input_f, returns an empty list.
    """
    cur_old_fname = None
    cur_new_fname = None
    cur_old_line_num = None
    cur_new_line_num = None
    cur_old_line = None
    cur_new_line = None

    for (index, item) in enumerate(walk(input_f)):
        # walk the diffscuss file line by line, maintaing the updated
        # file names, line numbers, and line texts for both the old
        # and new versions of the source
        cur_old_fname = _maybe_update_old_fname(item, cur_old_fname)
        cur_new_fname = _maybe_update_new_fname(item, cur_new_fname)
        cur_old_line_num = _maybe_update_old_line_num(item, cur_old_line_num)
        cur_new_line_num = _maybe_update_new_line_num(item, cur_new_line_num)
        cur_old_line = _maybe_update_old_line(item, cur_old_line)
        cur_new_line = _maybe_update_new_line(item, cur_new_line)
        cur_line_num = index + 1

        # once we've walked past the line number in the diffscuss
        # file, maintaining the file / line number context for both
        # old and new versions of the source as we went, we have our
        # candidates for matching source.
        if cur_line_num >= line_number:
            return [Candidate(fname=cur_new_fname,
                              line_num=_safe_decr(cur_new_line_num),
                              line_text=cur_new_line),
                    Candidate(fname=cur_old_fname,
                              line_num=_safe_decr(cur_old_line_num),
                              line_text=cur_old_line)]

    return list()
Example #8
0
def _find_base_target_idx(orig_diff, path):
    logging.debug("Finding base target index for %s", path)
    looking_for_range_line = False

    for (i, tagged_line) in enumerate(walk(StringIO(orig_diff))):
        logging.debug("Checking at index %d tagged line %s",
                      i, tagged_line)
        assert(tagged_line[0] not in [COMMENT_HEADER, COMMENT_BODY])
        if looking_for_range_line and _is_range_line(tagged_line):
            logging.debug("Found range line at index %d", i)
            return i
        if _is_target_path(tagged_line, path):
            logging.debug("Found path %s at index %s, now looking for range line",
                          path, i)
            looking_for_range_line = True
    logging.info("Could not find path %s in diff", path)
    return None
Example #9
0
def _parse(listing):
    fname = os.path.basename(listing)

    extractors = [
        OrigAuthorExtractor(),
        PostedAtExtractor(),
        LastCommentExtractor(),
        LineCounter([walker.DIFF_HEADER, walker.DIFF]),
        NumCommentsExtractor(),
        TopAuthorsExtractor()
    ]

    with open(listing, 'rb') as fil:
        for line_tup in walker.walk(fil):
            for extractor in extractors:
                extractor.process_line(line_tup)

    return [fname] + [e.get() for e in extractors]
Example #10
0
def strip_diffscuss(fil, outfil):
    for (elem_type, elem) in walk(fil):
        if elem_type == DIFF:
            outfil.write(elem)