コード例 #1
0
def test_make_iterparsers2():
    exp = Martel.HeaderFooter("dataset", {},
                              Martel.Group("header", Martel.Re(r"(a*\R)*")),
                              RecordReader.Until, ("b",),
                              
                              Martel.Group("record", Martel.Re(r"(b*\R)*")),
                              RecordReader.Until, ("c",),
                              
                              Martel.Group("footer", Martel.Re(r"(c*\R)*")),
                              RecordReader.Everything, (),)
    
    iterator = exp.make_iterator("record")
    assert isinstance(iterator, IterParser.IterHeaderFooter), iterator
    lines = ["a"
             "aa",
             "aaaaaaa",
             "b",
             "bb",
             "bbbb",
             "bbbbbbbb",
             "bbbbbbbbbbbbbbbb",
             "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
             "cccc",
             "cc",
             "c",
             ]
    
    text = "\n".join(lines) + "\n"

    i = 0
    for rec in iterator.iterateString(text):
        i = i + 1
    assert i == 1, i
コード例 #2
0
def test_missing_end2():
    # Same as the test_missing_end1 but using HeaderFooter
    exp = Martel.HeaderFooter("dataset", {},
                              None, None, None,
                              Martel.Group("record", Martel.Re(r"(b*\R)*a\R")),
                              RecordReader.EndsWith, ("a",),
                              None, None, None
                              )
    lines = [
        "bbb",
        "bb",
        "a",
        "b",
        "a",
        "a",
        ]
    text = "\n".join(lines) + "\n"

    iterator = exp.make_iterator("record")
    # This should work
    for x in iterator.iterateString(text):
        pass

    # This should not
    lines.append("c")
    text = "\n".join(lines) + "\n"
    try:
        for x in iterator.iterateString(text):
            pass
        raise AssertionError
    except Parser.ParserPositionException, exc:
        assert exc.pos == 15, exc.pos
コード例 #3
0
def test_header_footer_parser():
    # Check that I can pass same tag names in the header, record and
    # footer but not have them collide.

    header_format = Martel.Re("(?P<term?pos=header>a+)\R")
    record_format = Martel.Re("(?P<term?pos=body>b+)\R")
    footer_format = Martel.Re("(?P<term?pos=footer>c+)\R")

    format = Martel.HeaderFooter(
        "all",
        {"state": "New Mexico"},
        header_format,
        RecordReader.CountLines,
        (1, ),
        record_format,
        RecordReader.CountLines,
        (1, ),
        footer_format,
        RecordReader.CountLines,
        (1, ),
    )

    parser = format.make_parser()
    grab = GrabElements()
    parser.setContentHandler(grab)
    parser.parseString("a\nbb\nbb\nccc\n")
    elements = grab.elements
    assert len(elements) == 5, len(elements)
    check_element(elements[0], ("all", {"state": "New Mexico"}))
    check_element(elements[1], ("term", {"pos": "header"}))
    check_element(elements[2], ("term", {"pos": "body"}))
    check_element(elements[3], ("term", {"pos": "body"}))
    check_element(elements[4], ("term", {"pos": "footer"}))
コード例 #4
0
def test_header_footer7():
    exp = Martel.HeaderFooter("dataset", {}, None, None, None,
                              Martel.Re("a(?P<b>b*)a\R"),
                              RecordReader.CountLines, (1, ), None, None, None)
    lines = [
        "aa",
        "aBbbba",
        "abba",
    ]
    text = "\n".join(lines) + "\n"

    try:
        for info in exp.make_iterator("b").iterateString(text):
            pass
    except Parser.ParserPositionException, exc:
        assert exc.pos == 4, exc.pos
コード例 #5
0
def test_header_footer3():
    exp = Martel.HeaderFooter("dataset", {}, None, None, None,
                              Martel.Re("a(?P<b>b*)a\R"),
                              RecordReader.CountLines, (1, ), None, None, None)
    lines = [
        "aa",
        "aba",
        "abba",
    ]
    text = "\n".join(lines) + "\n"

    i = 0
    for info in exp.make_iterator("b").iterateString(text):
        assert len(info["b"]) == 1
        assert len(info["b"][0]) == i, (info["b"][0], i)
        i = i + 1
    assert i == 3, i
コード例 #6
0
def test_header_footer5():
    exp = Martel.HeaderFooter(
        "dataset",
        {},
        None,
        None,
        None,
        Martel.Re("a(?P<b>b*)a\R"),
        RecordReader.CountLines,
        (1, ),
        Martel.Re("end\R"),
        RecordReader.CountLines,
        (1, ),
    )
    lines = ["aa", "aba", "abba", "END"]
    text = "\n".join(lines) + "\n"

    try:
        for info in exp.make_iterator("b").iterateString(text):
            assert len(info["b"]) == 1
    except Parser.ParserPositionException, exc:
        assert exc.pos == 12, exc.pos
コード例 #7
0
def test_header_footer4():
    # Test that the errors are correct
    exp = Martel.HeaderFooter(
        "dataset",
        {},
        Martel.Re("header\R"),
        RecordReader.CountLines,
        (1, ),
        Martel.Re("a(?P<b>b*)a\R"),
        RecordReader.CountLines,
        (1, ),
        Martel.Re("end\R"),
        RecordReader.CountLines,
        (1, ),
    )
    lines = ["HEADER", "aa", "aba", "abba", "end"]
    text = "\n".join(lines) + "\n"

    try:
        for info in exp.make_iterator("b").iterateString(text):
            pass
    except Parser.ParserPositionException, exc:
        assert exc.pos == 0
コード例 #8
0
def test_missing_end3():
    # This one is missing the footer
    exp = Martel.HeaderFooter("dataset", {},
                              None, None, None,
                              
                              Martel.Group("record", Martel.Re(r"(b*\R)*a\R")),
                              RecordReader.EndsWith, ("a",),

                              Martel.Group("footer", Martel.Re(r"c\R")),
                              RecordReader.CountLines, (1,)
                              )
    lines = [
        "bbb",
        "bb",
        "a",
        "b",
        "a",
        "a",
        "c",  # This will be removed for the test
        ]
    text = "\n".join(lines) + "\n"

    iterator = exp.make_iterator("record")
    # This should work
    for x in iterator.iterateString(text):
        pass

    # This should not
    lines.pop()
    text = "\n".join(lines) + "\n"
    try:
        for x in iterator.iterateString(text):
            pass
        raise AssertionError
    except Parser.ParserPositionException, exc:
        assert exc.pos == 15, exc.pos