"""volve_dia.ddr.rendered: the publisher HTML and chandra's blocks reduce to the same `Rendered` report."""
from volve_dia.ddr.rendered import Rendered, Table, parse_html, parse_ocr
from volve_dia.ocr import LABEL_MAP, Block
HTML = """
| | |
| Status: | normal |
| Hole Dia (): | |
| Rig Name: | MÆRSK INSPIRER |
Installed the BOP.
Tested it.
| Start time | Remark |
| 00:00 | Moved the BOP.
|
| 03:30 | Tested. |
"""
EXPECTED = Rendered(
header={"wellbore": "15/9-F-12", "period": "2007-12-31 00:00 - 2008-01-01 00:00"},
fields={"status": "normal", "hole dia ()": "", "rig name": "MÆRSK INSPIRER"},
texts={"Summary of activities (24 Hours)": "Installed the BOP.\nTested it."},
tables={"Operations": Table(["Start time", "Remark"], [["00:00", "Moved the BOP."], ["03:30", "Tested."]]),
"Casing Liner Tubing": Table(None, [["Start Time", ""], ["Type", "liner"]])},
)
def block(page, label, html, text=None):
return Block(page=page, label=LABEL_MAP.get(label.lower(), "paragraph"), raw_label=label, text=text or html,
bbox=[], reading_order=0, html=html)
OPS_HEAD = "| Start time | Remark |
"
BLOCKS = [
block(1, "Page-Header", "Summary report
",
"Summary report\nWellbore: 15/9-F-12 Period: 2007-12-31 00:00 - 2008-01-01 00:00"),
block(1, "Table", "| Status: normal |
| Hole Dia (): | |
"
"| Rig Name: | MÆRSK INSPIRER |
"),
block(1, "Section-Header", "Summary of activities (24 Hours)"),
block(1, "Text", "Installed the BOP.
", "Installed the BOP."),
block(2, "Text", "Tested it.
", "Tested it."), # the paragraph goes on
block(2, "Text", "Operations
", "Operations"), # bold throughout: a title
block(2, "Table", f"{OPS_HEAD}| 00:00 | Moved the BOP. |
"),
block(3, "Page-Footer", "Page 2 of 3
", "Page 2 of 3"),
block(3, "Table", f""), # header repeated
block(3, "Section-Header", "Casing Liner Tubing"),
block(3, "Table", ""),
block(4, "Table", ""), # a data row read as a header
]
def test_parse_html():
assert parse_html(HTML) == EXPECTED
def test_parse_ocr_undoes_the_page_layout():
r = parse_ocr(BLOCKS)
assert (r.header, r.fields, r.texts, r.tables) == (EXPECTED.header, EXPECTED.fields, EXPECTED.texts,
EXPECTED.tables)
assert r.notes == ["table continued: Operations",
"table continued (header row kept as data): Casing Liner Tubing"]
def test_parse_ocr_repairs_the_table_structure():
period = "2007-12-31 00:00 - 2008-01-01 00:00"
blocks = [
block(1, "Table", "| Summary report | Printed: 2008-10-04 00:00 |
"
f"| Wellbore: 15/9-F-12 | Period: {period} |
"
"| Status: | normal |
"), # header lines as rows
block(1, "Section-Header", "Operations"),
block(1, "Table", f"{OPS_HEAD}| 00:00 | Moved |
"
" | the BOP. |
"), # a two-line cell as rows
block(2, "Table", ""), # a column split
]
r = parse_ocr(blocks)
assert r.header == {"wellbore": "15/9-F-12", "period": period, "other": "Printed: 2008-10-04 00:00"}
assert r.fields == {"status": "normal"}
assert r.tables == {"Operations": Table(["Start time", "Remark"], [["00:00", "Moved\nthe BOP."],
["03:30", "ok", "Tested."]])}
assert r.notes == ["table continued, columns differ: Operations", "rows merged into the row above: Operations (1)"]
def test_round_trip():
assert Rendered.from_dict(EXPECTED.to_dict()) == EXPECTED