Finished handling crossed sestarttokens

This commit is contained in:
Maximilien Rigaut 2022-06-01 04:41:46 +02:00
parent e822c12162
commit df71a13096
No known key found for this signature in database
GPG Key ID: 4F14B23709ADA5B1
5 changed files with 90 additions and 32 deletions

View File

@ -11,6 +11,7 @@
# 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, LonelyOptionalError # noqa
@ -149,7 +150,7 @@ class BaseContainer:
print(ident + "> " + str(e))
print("")
def to_container(self): # idea: additional parameters for encapsulation
def to_container(self, filter_func=None): # idea: additional parameters for encapsulation
return self
@ -341,8 +342,15 @@ class ContextManager:
Index of the pile where the encapsulation must begin.
end : int
Index of the pile where the encapsulation must end.
Returns
-------
list[(BaseContainer|None)]
"""
# print(start, end, self.pile[start:end+1])
# Enabling this will allow us to debug the pile
# print(f"Step : [{end}] - encapsulation:")
# print(" Base:", self.parsed_list)
# print(" >>> Pile:", self.pile)
try:
pile_start = self.pile[start]
_ = self.pile[end]
@ -350,17 +358,19 @@ class ContextManager:
raise IndexError("Start index must be lower than end index")
except IndexError:
log_exception(
KeyError(f"Indexes ({start}:{end}) must be in the pile range ({len(self.pile)})."),
IndexError(f"Indexes ({start}:{end}) must be in the pile range ({len(self.pile)})."),
level="CRITICAL"
)
try:
container = _to_container[pile_start.label]() # noqa : F821 # TODO: fix bad error handling on missmatched SE
container = _to_container[pile_start.label]() # noqa : F821
except KeyError:
log_exception(
KeyError(f"Element {self.pile[start].label} not in dictionary of tokens-containers correspondences."),
KeyError(f"Element {pile_start.label} not in dictionary of tokens-containers correspondences."),
level="CRITICAL"
)
except AttributeError as error:
rich.inspect(error)
rich.inspect(self.pile)
if pile_start is None:
for e, r in zip(self.pile, self.parsed_list):
log_message(f'{e} - {r}')
@ -372,13 +382,15 @@ class ContextManager:
raise error
for i in range(start, end):
if self.pile[i]:
# container.add(self.pile[i].to_container()) # This line transform the self modifiying containers # MONITOR # noqa
container.add(self.pile[i]) # TODO: ignore the first and last element, or ignore the self modifying tokens # noqa : F821
container.add(self.pile[i].to_container(lambda x: x.label==pile_start.label)) # This line transform the self modifiying containers # MONITOR # noqa
#container.add(self.pile[i]) # TODO: ignore the first and last element, or ignore the self modifying tokens # noqa : F821
self.pile[i] = None
container.add(self.pile[end])
self.pile[end] = None
self.pile[start] = container
# print(" <<< Pile:", self.pile)
def _add_matched(self, label, index):
if label not in self.matched_elements:
self.matched_elements[label] = []

View File

@ -10,7 +10,6 @@
import logging
import traceback
import sys
from bootstraparse.modules.tools import __GLk # __GFi, __GFu, __GL

View File

@ -16,7 +16,7 @@ from collections import namedtuple
import rich
from bootstraparse.modules.error_mngr import MismatchedContainerError
from bootstraparse.modules import context_mngr as cm
import pyparsing as pp
import regex # future: remove regex
@ -74,9 +74,15 @@ class SemanticType:
def counterpart(self): # noqa # FUTURE: Check for static counterpart implementation
return None
def to_container(self):
def to_container(self, filter_func=None):
"""
Function called by the context manager to know how to deal with the encapsulation of the token.
Parameters
----------
filter_func: (function | None)
A function that takes a token and returns a boolean. If the token is not valid, it should return False.
Returns
-------
SemanticType
@ -106,8 +112,11 @@ class ExplicitSemanticType(SemanticType):
def to_markup(self):
return f'<{self.label} />'
def to_container(self):
return TextToken(self.content[0])
def to_container(self, filter_func=None):
"""Check if we are trying to encapsulate this very token, don't do anything"""
if filter_func and filter_func(self):
return self
return cm.TextContainer([TextToken(self.content[0])])
class FinalSemanticType(SemanticType):
@ -118,7 +127,7 @@ class FinalSemanticType(SemanticType):
def counterpart(self): # noqa
return None
def to_container(self):
def to_container(self, filter_func=None):
return self
@ -126,8 +135,12 @@ class OpenedSemanticType(SemanticType):
"""
Semantic type used to signify it has a matching end component and needs to be matched.
"""
def to_container(self):
return self
def to_container(self, filter_func=None):
if filter_func is None:
raise MismatchedContainerError(self)
elif filter_func(self):
return self
raise MismatchedContainerError(self)
class ClosedSemanticType(SemanticType):
@ -164,7 +177,7 @@ class ImageToken(SemanticType):
class TextToken(EmptySemanticType):
label = "text"
def to_container(self):
def to_container(self, filter_func=None):
return self
@ -293,7 +306,7 @@ class BlockQuoteAuthorToken(SemanticType):
class Linebreak(ExplicitSemanticType):
label = "linebreak"
def to_container(self):
def to_container(self, filter_func=None):
return self

View File

@ -1,8 +1,7 @@
import pytest
# import rich
import rich
from bootstraparse.modules import context_mngr, export
from bootstraparse.modules import context_mngr, export, error_mngr
from bootstraparse.modules import syntax as sy #sy.SemanticType, sy.TextToken, sy.Linebreak, sy.StructuralElementStartToken, sy.StructuralElementEndToken, sy.EtUlistToken, sy.EtOlistToken # noqa
from bootstraparse.modules.tools import __GLk
__XF = pytest.mark.xfail
@ -427,6 +426,36 @@ _token_list_with_expected_result = [
],
__GLk(1), # TODO : Add all optionals
],
[ # Testing error handling
[
sy.StructuralElementStartToken(["div"]),
sy.StructuralElementStartToken(["aside"]),
sy.TextToken(["a"]),
sy.StructuralElementEndToken(["div"]),
sy.StructuralElementEndToken(["aside"]),
],
[
error_mngr.MismatchedContainerError,
],
__GLk(1),
],
[
[
sy.EtStrongToken(['**']),
sy.TextToken(["a"]),
sy.EtEmToken(['*']),
sy.EtStrongToken(['**']),
],
[
context_mngr.EtStrongContainer([
sy.EtStrongToken(['**']),
context_mngr.TextContainer([sy.TextToken(["a"])]),
context_mngr.TextContainer([sy.TextToken(["*"])]),
sy.EtStrongToken(['**']),
]),
],
__GLk(1),
],
]
_zipped_token_list_with_expected_result = [
@ -467,17 +496,17 @@ def test_encapsulate(base_cm): # Robustness is tested in test_container_export_
def test_encapsulate_bad_index(base_cm):
with pytest.raises(SystemExit):
with pytest.raises(IndexError):
base_cm.encapsulate(1, 44)
with pytest.raises(SystemExit):
with pytest.raises(IndexError):
base_cm.encapsulate(33, 1)
with pytest.raises(SystemExit):
with pytest.raises(IndexError):
base_cm.encapsulate(2, 1)
base_cm.encapsulate(1, 2)
with pytest.raises(SystemExit):
with pytest.raises(AttributeError):
base_cm.encapsulate(2, 2)
base_cm.pile[0] = {}
@ -487,7 +516,7 @@ def test_encapsulate_bad_index(base_cm):
def test_encapsulate_bad_cm(base_cm):
base_cm.pile[1] = sy.SemanticType([1])
with pytest.raises(SystemExit):
with pytest.raises(KeyError):
base_cm.encapsulate(1, 2)
@ -601,13 +630,18 @@ def test_container():
@pytest.mark.parametrize("init_list, expected, file_line", _zipped_token_list_with_expected_result)
def test_context_call(init_list, expected, file_line):
print(f"Executing tests @{file_line}")
ctx = context_mngr.ContextManager(init_list) # noqa : F841
assert ctx() == expected
if isinstance(expected[0], context_mngr.BaseContainer) or isinstance(expected[0], sy.SemanticType):
assert ctx() == expected
else:
with pytest.raises(expected[0]):
ctx()
def test_content_call_raises():
ctx = context_mngr.ContextManager([sy.StructuralElementEndToken(["1"])])
with pytest.raises(SystemExit):
with pytest.raises(error_mngr.MismatchedContainerError):
ctx()
@ -621,7 +655,7 @@ def test_container_export(container, base_cm):
def test_export_error():
em = export.ExportManager(None, None)
with pytest.raises(SystemExit):
with pytest.raises(TypeError):
context_mngr.TextContainer([sy.TextToken(['e']), None]).export(em)
@ -645,10 +679,10 @@ def test_get_last_container_in_pile(base_cm):
tk = sy.TextToken(['e'])
tk.line_number = 1
base_cm.pile[1] = tk
with pytest.raises(SystemExit):
with pytest.raises(error_mngr.LonelyOptionalError):
base_cm.get_last_container_in_pile(3)
base_cm.pile[1] = None
base_cm.pile[0] = None
with pytest.raises(SystemExit):
with pytest.raises(error_mngr.LonelyOptionalError):
base_cm.get_last_container_in_pile(3)

View File

@ -1,6 +1,6 @@
import pytest
from bootstraparse.modules import syntax, error_mngr, tools
from bootstraparse.modules import syntax, error_mngr, tools, context_mngr
__XF = pytest.mark.xfail
@ -19,7 +19,7 @@ _type_counterpart_container = [
(syntax.EtStrongToken, 'text:strong', "token", tools.__GLk(1)),
(syntax.EtUnderlineToken, 'text:underline', "token", tools.__GLk(1)),
(syntax.EtStrikethroughToken, 'text:strikethrough', "token", tools.__GLk(1)),
(syntax.EtCustomSpanToken, None, "self", tools.__GLk(1), __XF), # Not sure what to do with custom spans
(syntax.EtCustomSpanToken, None, "error", tools.__GLk(1)),
(syntax.EtUlistToken, None, "self", tools.__GLk(1)),
(syntax.EtOlistToken, None, "self", tools.__GLk(1)),
(syntax.HeaderToken, None, "self", tools.__GLk(1)),
@ -54,7 +54,7 @@ def test_counterpart_container(type_n, expected_counterpart, expected_container,
elif expected_container == "self":
assert token.to_container() == token
elif expected_container == "token":
assert isinstance(token.to_container(), syntax.TextToken)
assert isinstance(token.to_container(), context_mngr.TextContainer)
else:
raise ValueError("Invalid expected_container value: {}".format(expected_container))
except Exception as e: