Corrected minor errors on optionals
This commit is contained in:
parent
ab3332fedf
commit
c13e1524e3
|
|
@ -11,10 +11,9 @@
|
|||
# container = BaseContainer()
|
||||
# container[number] -> The number element in the content
|
||||
# "class_insert" >> container[number] -> Get an element from one of the mapped methods
|
||||
import rich
|
||||
|
||||
from bootstraparse.modules import syntax, error_mngr, export
|
||||
from bootstraparse.modules.error_mngr import MismatchedContainerError, log_exception, log_message # noqa
|
||||
from bootstraparse.modules.error_mngr import MismatchedContainerError, log_exception, log_message, LonelyOptionalError # noqa
|
||||
|
||||
|
||||
class BaseContainer:
|
||||
|
|
@ -24,12 +23,14 @@ class BaseContainer:
|
|||
type = None
|
||||
subtype = None
|
||||
|
||||
def __init__(self, content=None):
|
||||
def __init__(self, content=None, optionals=None, others=None):
|
||||
if content is None:
|
||||
content = []
|
||||
if others is None:
|
||||
self.others = {}
|
||||
self.content = content
|
||||
self.map = {}
|
||||
self.optionals = None
|
||||
self.optionals = optionals
|
||||
|
||||
def get_content(self, exm):
|
||||
output = ""
|
||||
|
|
@ -41,8 +42,8 @@ class BaseContainer:
|
|||
def get_optionals(self):
|
||||
return self.optionals
|
||||
|
||||
def get_others(self): # noqa
|
||||
return {}
|
||||
def get_others(self):
|
||||
return self.others
|
||||
|
||||
def export(self, exm):
|
||||
"""
|
||||
|
|
@ -103,6 +104,17 @@ class BaseContainer:
|
|||
for e, f in zip(self, other):
|
||||
if e != f:
|
||||
return False
|
||||
|
||||
if self.get_optionals() != other.get_optionals():
|
||||
return False
|
||||
|
||||
if len(self.get_others()) != len(other.get_others()):
|
||||
return False
|
||||
|
||||
for k, v in self.get_others().items():
|
||||
if v != other.get_others()[k]:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def __invert__(self):
|
||||
|
|
@ -110,7 +122,7 @@ class BaseContainer:
|
|||
|
||||
def __str__(self):
|
||||
return '{} ({})'.format(", ".join(map(str, self.content)),
|
||||
", ".join(map(str, self.get_optionals())) if self.get_optionals() != [] else "")
|
||||
", ".join(map(str, self.get_optionals())) if self.get_optionals() else "")
|
||||
|
||||
def __repr__(self):
|
||||
representation = {}
|
||||
|
|
@ -119,11 +131,13 @@ class BaseContainer:
|
|||
representation[type(content)] += 1
|
||||
else:
|
||||
representation[type(content)] = 1
|
||||
return "{}({})".format(
|
||||
return "{}({}){}{}".format(
|
||||
self.class_name(),
|
||||
", ".join(
|
||||
[f'{index.__name__}: {value}' for index, value in representation.items()]
|
||||
)
|
||||
),
|
||||
f" Opts[{len(self.get_optionals().content)}]" if self.get_optionals() else "",
|
||||
f" Otrs[{len(self.get_others())}]" if self.get_others() else ""
|
||||
)
|
||||
|
||||
def print_all(self, ident=''):
|
||||
|
|
@ -139,43 +153,6 @@ class BaseContainer:
|
|||
return self
|
||||
|
||||
|
||||
class BaseContainerWithOptionals(BaseContainer):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.map['html_insert'] = self.fetch_html_insert
|
||||
self.map['class_insert'] = self.fetch_class_insert
|
||||
|
||||
def get_optionals(self):
|
||||
rich.inspect(self.content)
|
||||
rich.inspect(self.content[0])
|
||||
return []
|
||||
|
||||
def get_others(self):
|
||||
return None
|
||||
|
||||
def fetch_html_insert(self):
|
||||
"""
|
||||
Returns a list of all html_inserts, and an empty list if none exist.
|
||||
:rtype: list
|
||||
"""
|
||||
rlist = []
|
||||
for o in self.get_optionals():
|
||||
if o.label == 'optional:insert':
|
||||
rlist += [o]
|
||||
return rlist
|
||||
|
||||
def fetch_class_insert(self):
|
||||
"""
|
||||
Returns a list of all class_inserts, and an empty list if none exist.
|
||||
:rtype: list
|
||||
"""
|
||||
rlist = []
|
||||
for o in self.get_optionals():
|
||||
if o.label == 'optional:class':
|
||||
rlist += [o]
|
||||
return rlist
|
||||
|
||||
|
||||
# Define containers all the Enhanced text elements, divs, headers, list and any element that can be a container
|
||||
class TextContainer(BaseContainer):
|
||||
def export(self, _):
|
||||
|
|
@ -229,8 +206,8 @@ class HyperLinkContainer(BaseContainer):
|
|||
type = "inline_elements"
|
||||
subtype = "link"
|
||||
|
||||
def __init__(self, content=None):
|
||||
super().__init__(content)
|
||||
def __init__(self, content=None, optionals=None, others=None):
|
||||
super().__init__(content, optionals, others)
|
||||
self.map['url'] = ""
|
||||
pass
|
||||
|
||||
|
|
@ -241,6 +218,7 @@ class HyperLinkContainer(BaseContainer):
|
|||
|
||||
class SeContainer(BaseContainer):
|
||||
type = "structural_elements"
|
||||
|
||||
def export(self, exm):
|
||||
self.subtype = self[0].content[0]
|
||||
return super().export(exm)
|
||||
|
|
@ -250,8 +228,8 @@ class HeaderContainer(BaseContainer):
|
|||
type = "structural_elements"
|
||||
subtype = "header"
|
||||
|
||||
def __init__(self, content=None):
|
||||
super().__init__(content)
|
||||
def __init__(self, content=None, optionals=None, others=None):
|
||||
super().__init__(content, optionals, others)
|
||||
self.map['header_level'] = ''
|
||||
pass
|
||||
|
||||
|
|
@ -260,8 +238,8 @@ class DisplayContainer(BaseContainer):
|
|||
type = "structural_elements"
|
||||
subtype = "display"
|
||||
|
||||
def __init__(self, content=None):
|
||||
super().__init__(content)
|
||||
def __init__(self, content=None, optionals=None, others=None):
|
||||
super().__init__(content, optionals, others)
|
||||
self.map['display_level'] = ""
|
||||
pass
|
||||
|
||||
|
|
@ -274,8 +252,8 @@ class TableSeparatorContainer(BaseContainer):
|
|||
# type = "table"
|
||||
# subtype = "t_head"
|
||||
#
|
||||
# def __init__(self, content=None):
|
||||
# super().__init__(content)
|
||||
# def __init__(self, content=None, optionals=None, others=None):
|
||||
# super().__init__(content, optionals, others)
|
||||
# self.map['colspan'] = ""
|
||||
# pass
|
||||
|
||||
|
|
@ -284,8 +262,8 @@ class TableRowContainer(BaseContainer):
|
|||
type = "table"
|
||||
subtype = "t_row"
|
||||
|
||||
def __init__(self, content=None):
|
||||
super().__init__(content)
|
||||
def __init__(self, content=None, optionals=None, others=None):
|
||||
super().__init__(content, optionals, others)
|
||||
self.map['colspan'] = ""
|
||||
pass
|
||||
|
||||
|
|
@ -294,8 +272,8 @@ class TableCellContainer(BaseContainer):
|
|||
type = "table"
|
||||
subtype = "t_cell"
|
||||
|
||||
def __init__(self, content=None):
|
||||
super().__init__(content)
|
||||
def __init__(self, content=None, optionals=None, others=None):
|
||||
super().__init__(content, optionals, others)
|
||||
self.map['colspan'] = ""
|
||||
pass
|
||||
|
||||
|
|
@ -348,7 +326,7 @@ class ContextManager:
|
|||
self.pile = []
|
||||
self.matched_elements = {}
|
||||
self.dict_lookahead = {
|
||||
# "list:ulist": ["list:ulist"],
|
||||
"list:ulist": ["list:ulist"],
|
||||
"list:olist": ["list:olist"],
|
||||
"table:row": ["table:separator", "table:row"] # TODO: Implement tables
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import pytest
|
||||
# import rich
|
||||
import rich
|
||||
|
||||
from bootstraparse.modules import context_mngr, export
|
||||
from bootstraparse.modules import syntax as sy #sy.SemanticType, sy.TextToken, sy.Linebreak, sy.StructuralElementStartToken, sy.StructuralElementEndToken, sy.EtUlistToken, sy.EtOlistToken # noqa
|
||||
|
|
@ -34,6 +35,14 @@ _opts = sy.OptionalToken([
|
|||
]),
|
||||
sy.OptionalInsertToken(["var='test', number=11"]),
|
||||
])
|
||||
_others = {
|
||||
"header_level": "1",
|
||||
"display_level": "1",
|
||||
"col_span": "1",
|
||||
"row_span": "1",
|
||||
"url": "1",
|
||||
"test": "1",
|
||||
}
|
||||
|
||||
_token_list_with_expected_result = [
|
||||
[
|
||||
|
|
@ -401,20 +410,20 @@ _token_list_with_expected_result = [
|
|||
[
|
||||
sy.EtOlistToken([
|
||||
sy.TextToken(["a"]),
|
||||
_opts,
|
||||
]),
|
||||
_opts,
|
||||
sy.EtUlistToken([
|
||||
sy.TextToken(["b"]),
|
||||
_opts,
|
||||
]),
|
||||
_opts,
|
||||
],
|
||||
[
|
||||
context_mngr.EtOlistContainer([
|
||||
sy.EtOlistToken([sy.TextToken(["a"]), _opts]),
|
||||
]),
|
||||
sy.EtOlistToken([sy.TextToken(["a"])]),
|
||||
], _opts),
|
||||
context_mngr.EtOlistContainer([
|
||||
sy.EtUlistToken([sy.TextToken(["b"]), _opts]),
|
||||
]),
|
||||
sy.EtUlistToken([sy.TextToken(["b"])]),
|
||||
], _opts),
|
||||
],
|
||||
__GLk(1), # TODO : Add all optionals
|
||||
],
|
||||
|
|
@ -500,7 +509,6 @@ def test_matched_functions(base_cm):
|
|||
|
||||
def test_context_mngr():
|
||||
base = context_mngr.BaseContainer()
|
||||
assert base is not None
|
||||
assert base.class_name() == 'BaseContainer'
|
||||
base.add('test')
|
||||
assert base[0] == 'test'
|
||||
|
|
@ -516,44 +524,61 @@ def test_context_mngr():
|
|||
base.add("test4")
|
||||
assert base[0:2] == ['test3', 'test2']
|
||||
assert base.__getslice__(0, 2) == ['test3', 'test2']
|
||||
assert base == ['test3', 'test2', 'test4']
|
||||
|
||||
base2 = context_mngr.BaseContainer(["test3", "test2", "test4"])
|
||||
assert base == base2
|
||||
assert base != ['test3', 'test2', 'test5']
|
||||
assert base != ['test3', 'test2']
|
||||
assert type(repr(base)) == str
|
||||
assert type(str(base)) == str
|
||||
assert ~base == []
|
||||
assert ~base is None
|
||||
base.map = {'test': lambda: "test3"}
|
||||
assert base >> "test" == 'test3'
|
||||
assert base != 1
|
||||
assert hasattr(base, "get_others")
|
||||
assert hasattr(base, "get_optionals")
|
||||
assert base.get_optionals() is None
|
||||
assert base.get_others() is not None
|
||||
base2.others = _others
|
||||
assert base2 != base
|
||||
base.others = _others.copy()
|
||||
assert base == base2
|
||||
base.optionals = _opts
|
||||
assert base != base2
|
||||
base2.optionals = _opts
|
||||
assert base == base2
|
||||
base.others["test"] = "test3"
|
||||
assert base != base2
|
||||
base.debug_map()
|
||||
|
||||
|
||||
def test_base_container_with_optionals():
|
||||
base = context_mngr.BaseContainerWithOptionals()
|
||||
assert base is not None
|
||||
|
||||
assert base.class_name() == 'BaseContainerWithOptionals'
|
||||
assert base.debug_map() is None
|
||||
|
||||
base.add('test')
|
||||
assert base[0] == 'test'
|
||||
assert len(base) == 1
|
||||
|
||||
assert base.fetch_html_insert() == []
|
||||
|
||||
class OI:
|
||||
label = 'optional:insert'
|
||||
|
||||
class CI:
|
||||
label = 'optional:class'
|
||||
|
||||
i, c = OI(), CI()
|
||||
base.optionals = [i, c]
|
||||
assert base.fetch_html_insert() == [i]
|
||||
assert base.fetch_class_insert() == [c]
|
||||
#
|
||||
# def test_base_container_with_optionals():
|
||||
# base = context_mngr.BaseContainer()
|
||||
# assert base is not None
|
||||
#
|
||||
# assert base.class_name() == 'BaseContainerWithOptionals'
|
||||
# assert base.debug_map() is None
|
||||
#
|
||||
# base.add('test')
|
||||
# assert base[0] == 'test'
|
||||
# assert len(base) == 1
|
||||
#
|
||||
# assert base.fetch_html_insert() == []
|
||||
#
|
||||
# class OI:
|
||||
# label = 'optional:insert'
|
||||
#
|
||||
# class CI:
|
||||
# label = 'optional:class'
|
||||
#
|
||||
# i, c = OI(), CI()
|
||||
# base.optionals = [i, c]
|
||||
# assert base.fetch_html_insert() == [i]
|
||||
# assert base.fetch_class_insert() == [c]
|
||||
|
||||
|
||||
def test_print():
|
||||
base = context_mngr.BaseContainerWithOptionals()
|
||||
base = context_mngr.BaseContainer()
|
||||
assert base is not None
|
||||
base.print_all()
|
||||
base.content = [context_mngr.BaseContainer(), context_mngr.BaseContainer(), "test", 3, context_mngr.BaseContainer()]
|
||||
|
|
@ -598,3 +623,32 @@ def test_export_error():
|
|||
em = export.ExportManager(None, None)
|
||||
with pytest.raises(SystemExit):
|
||||
context_mngr.TextContainer([sy.TextToken(['e']), None]).export(em)
|
||||
|
||||
|
||||
def test_get_last_container_in_pile(base_cm):
|
||||
base_cm()
|
||||
rich.inspect(base_cm.pile[3])
|
||||
rich.inspect(base_cm.get_last_container_in_pile(3))
|
||||
assert base_cm.get_last_container_in_pile(3) == base_cm.pile[2] # Always returns element before
|
||||
assert base_cm.get_last_container_in_pile(2) == base_cm.pile[1]
|
||||
base_cm.pile[2] = None
|
||||
assert base_cm.get_last_container_in_pile(2) == base_cm.pile[1]
|
||||
assert base_cm.get_last_container_in_pile(3) == base_cm.pile[1]
|
||||
base_cm.pile[1] = None
|
||||
assert base_cm.get_last_container_in_pile(2) == base_cm.pile[0]
|
||||
assert base_cm.get_last_container_in_pile(3) == base_cm.pile[0]
|
||||
assert base_cm.get_last_container_in_pile(4) == base_cm.pile[3]
|
||||
base_cm.pile[3] = None
|
||||
assert base_cm.get_last_container_in_pile(3) == base_cm.pile[0]
|
||||
assert base_cm.get_last_container_in_pile(4) == base_cm.pile[0]
|
||||
assert base_cm.get_last_container_in_pile(2) == base_cm.pile[0]
|
||||
tk = sy.TextToken(['e'])
|
||||
tk.line_number = 1
|
||||
base_cm.pile[1] = tk
|
||||
with pytest.raises(SystemExit):
|
||||
base_cm.get_last_container_in_pile(3)
|
||||
|
||||
base_cm.pile[1] = None
|
||||
base_cm.pile[0] = None
|
||||
with pytest.raises(SystemExit):
|
||||
base_cm.get_last_container_in_pile(3)
|
||||
|
|
|
|||
|
|
@ -37,7 +37,6 @@ _list_classes_expected_value = [
|
|||
]),
|
||||
"<strong>test3</strong>",
|
||||
__GLk(1),
|
||||
__XF,
|
||||
],
|
||||
[
|
||||
context_mngr.EtUnderlineContainer([
|
||||
|
|
@ -47,7 +46,6 @@ _list_classes_expected_value = [
|
|||
]),
|
||||
"<u>test4</u>",
|
||||
__GLk(1),
|
||||
__XF,
|
||||
],
|
||||
[
|
||||
context_mngr.EtStrikethroughContainer([
|
||||
|
|
@ -57,7 +55,6 @@ _list_classes_expected_value = [
|
|||
]),
|
||||
"<s>test5</s>",
|
||||
__GLk(1),
|
||||
__XF,
|
||||
],
|
||||
[
|
||||
context_mngr.EtCustomSpanContainer([
|
||||
|
|
@ -67,7 +64,6 @@ _list_classes_expected_value = [
|
|||
]),
|
||||
"<span>test6</span>",
|
||||
__GLk(1),
|
||||
__XF,
|
||||
],
|
||||
[
|
||||
context_mngr.EtUlistContainer([
|
||||
|
|
@ -199,6 +195,7 @@ _zipped_list_classes_expected_value = [
|
|||
|
||||
@pytest.mark.parametrize("container, export_v, line", _zipped_list_classes_expected_value)
|
||||
def test_container_export_value(container, export_v, line):
|
||||
print(line)
|
||||
em = export.ExportManager(None, None)
|
||||
assert isinstance(container, context_mngr.BaseContainer)
|
||||
assert container.export(em) == export_v
|
||||
|
|
|
|||
Loading…
Reference in New Issue