Added a couple steps toward table completion & tools

This commit is contained in:
Maximilien Rigaut 2022-06-15 12:38:31 +02:00
parent 8358070ff2
commit 7f6a09097b
No known key found for this signature in database
GPG Key ID: 4F14B23709ADA5B1
6 changed files with 95 additions and 63 deletions

View File

@ -229,17 +229,19 @@ class BaseContainer:
f" Otrs[{len(self.get_others())}]" if self.get_others() else ""
)
def print_all(self, ident=''):
def print_all(self, ident=0):
"""
Prints the container and all its content.
"""
print(ident + self.class_name())
import rich
rich.print(" " * ident + self.class_name() + ':')
for e in self:
if isinstance(e, BaseContainer):
e.print_all(ident + ' ')
e.print_all(ident + 1)
if isinstance(e, syntax.SemanticType):
e.print_all(ident + 1)
else:
print(ident + "> " + str(e))
print("")
rich.print(" "*ident + "> " + str(e))
def to_container(self, filter_func=None): # noqa : C901
"""
@ -426,6 +428,7 @@ _to_container = {
"table:row": TableRowContainer,
"table:cell": TableCellContainer, # Should be an error I beleive ?
"table:separator": TableSeparatorContainer, # Should be an error I beleive ?
"table:cell_size": TableCellContainer,
}
@ -459,6 +462,8 @@ class ContextManager:
self.dict_lookahead_reparse = [
"list:ulist",
"list:olist",
"table:row",
"table:cell",
]
self.contextualised = False
@ -705,16 +710,19 @@ class ContextManager:
i -= 1
log_exception(LonelyOptionalError(self.pile[index], None), level="CRITICAL")
def print_all(self):
def print_all(self, ident=0):
"""
Prints the pile.
"""
import rich
for e in self:
if isinstance(e, BaseContainer):
e.print_all()
e.print_all(ident + 1)
elif isinstance(e, syntax.SemanticType):
e.print_all(ident + 1)
else:
print("> " + str(e))
print("Matched elements:" + str(self.matched_elements))
rich.print(" " * ident + str(e))
rich.print("Matched elements:" + str(self.matched_elements))
# Defines all methods to manage the context of the parser as a file is being parsed

View File

@ -240,11 +240,11 @@ class ContextConverter:
return f"ContextConverter[{self.destination}] <{len(self.pile)}> " \
f"Status: {'initialized' if self.io_initialized else 'uninitialized'}"
def print_all(self):
def print_all(self, ident=0):
"""
Prints the entirety of the converted pile
"""
print(self)
rich.print(self)
def __eq__(self, other):
return str(self) == str(other)

View File

@ -86,6 +86,21 @@ class SemanticType:
def __ne__(self, other):
return not self.__eq__(other)
def print_all(self, ident=0):
"""
Print all lines of the token.
"""
import rich
rich.print(" "*ident + self.__class__.__name__ + ":")
if self.content:
for elt in self.content:
if isinstance(elt, cm.BaseContainer):
elt.print_all(ident+1)
elif isinstance(elt, SemanticType):
elt.print_all(ident+1)
else:
rich.print(" "*(ident+1) + str(elt))
def counterpart(self): # noqa # FUTURE: Check for static counterpart implementation
"""
Function called by the context manager to know how to deal with the encapsulation of the token.
@ -283,16 +298,16 @@ class TableRowToken(FinalSemanticType, TokensToMatch):
label = "table:row"
class TableCellToken(SemanticType):
class TableCellToken(FinalSemanticType):
label = "table:cell"
class TableSeparatorToken(SemanticType):
class TableSeparatorToken(FinalSemanticType):
label = "table:separator"
class TableCellSizeToken(SemanticType):
label = "table:cell_size"
class TableCellSizeToken(FinalSemanticType):
label = ":cell_size"
class OptionalToken(SemanticType):
@ -502,9 +517,8 @@ se_start = (pps('<<') + structural_elements).add_parse_action(of_type(Structural
se_end = (structural_elements + pps('>>')).add_parse_action(of_type(StructuralElementEndToken)) + pp.Opt(optional)
se = se_end | se_start # Structural element
table_row = pp.OneOrMore(
# pp.Regex(r'\|(\d)?')('table_colspan') +
(pp.Combine(pps('|') + pp.Word(pp.nums))('table_colspan').add_parse_action(of_type(TableCellSizeToken)) | pps('|')) +
pp.SkipTo('|')('table_cell').add_parse_action(reparse(enhanced_text), of_type(TableCellToken))
((pp.Combine(pps('|') + pp.Word(pp.nums))('table_colspan').add_parse_action(of_type(TableCellSizeToken)) | pps('|')) +
pp.SkipTo('|').add_parse_action(reparse(enhanced_text))('table_cell')).add_parse_action(of_type(TableCellToken))
).add_parse_action(of_type(TableRowToken)) + pps('|') + pp.Opt(optional)
table_separator = pp.OneOrMore(
pps('|') + pp.Word(':-')

View File

@ -315,8 +315,8 @@ _token_list_with_expected_result = [
[
[
sy.TableRowToken([
sy.TableCellSizeToken(["2"]),
sy.TableCellToken([
"2",
sy.TextToken(["a"]),
]),
sy.TableCellToken([
@ -326,11 +326,18 @@ _token_list_with_expected_result = [
],
[
context_mngr.TableRowContainer([
context_mngr.TableCellContainer([
context_mngr.TextContainer([sy.TextToken(["a"])]),
], others={"colspan": "2"}),
context_mngr.TableCellContainer([
context_mngr.TextContainer([sy.TextToken(["b"])]),
sy.TableRowToken([
context_mngr.TableCellContainer([
sy.TableCellToken([
"2",
context_mngr.TextContainer([sy.TextToken(["a"])]),
]),
]),
context_mngr.TableCellContainer([
sy.TableCellToken([
context_mngr.TextContainer([sy.TextToken(["b"])]),
]),
]),
]),
]),
],
@ -340,38 +347,46 @@ _token_list_with_expected_result = [
[
[
sy.TableRowToken([
sy.TableCellSizeToken(["2"]),
sy.TableCellToken([
sy.TableCellSizeToken(["2"]),
sy.TextToken(["a"]),
]),
sy.TableCellSizeToken(["3"]),
sy.TableCellToken([
sy.TableCellSizeToken(["3"]),
sy.TextToken(["b"]),
]),
]),
],
[
context_mngr.TableRowContainer([
context_mngr.TableCellContainer([ # MONITOR # The colspan must be taken into account
context_mngr.TextContainer([sy.TextToken(["a"])]),
]),
context_mngr.TableCellContainer([
context_mngr.TextContainer([sy.TextToken(["b"])]),
sy.TableRowToken([
context_mngr.TableCellContainer([ # MONITOR # The colspan must be taken into account
sy.TableCellToken([
sy.TableCellSizeToken(["2"]),
context_mngr.TextContainer([sy.TextToken(["a"])]),
]),
]),
context_mngr.TableCellContainer([
sy.TableCellToken([
sy.TableCellSizeToken(["3"]),
context_mngr.TextContainer([sy.TextToken(["b"])]),
]),
]),
]),
]),
],
__GLk(1),
__XF,
],
[
[
sy.TableSeparatorToken(["---", "---"]),
],
[
error_mngr.MismatchedContainerError,
],
__GLk(1),
],
# [
# [
# sy.TableSeparatorToken(["---", "---"]),
# ],
# [
# error_mngr.MismatchedContainerError,
# ],
# __GLk(1),
# ],
[
[
sy.TableRowToken([
@ -626,10 +641,13 @@ def test_container():
def test_context_call(init_list, expected, file_line):
print(f"Executing tests @{file_line}")
ctx = context_mngr.ContextManager(init_list) # noqa : F841
# ctx.print_all()
if isinstance(expected[0], context_mngr.BaseContainer) or isinstance(expected[0], sy.SemanticType):
for i, e in zip(ctx(), expected):
rich.inspect(i)
rich.inspect(e)
# rich.inspect(i)
# rich.inspect(e)
i.print_all()
e.print_all()
assert i == e
else:
with pytest.raises(expected[0]):

View File

@ -458,33 +458,26 @@ dict_advanced_syntax_input_and_expected_output = {
), __GLk()),
("|2 Text1 | Text2 |", (
sy.TableRowToken([
sy.TableCellSizeToken(['2']),
sy.TableCellToken([sy.TextToken(["Text1 "])]),
sy.TableCellToken([sy.TableCellSizeToken(["2"]), sy.TextToken(["Text1 "])]),
sy.TableCellToken([sy.TextToken(["Text2 "])]),
]),
), __GLk()),
("|2 Text1 |3 Text2 |", (
sy.TableRowToken([
sy.TableCellSizeToken(['2']),
sy.TableCellToken([sy.TextToken(["Text1 "])]),
sy.TableCellSizeToken(['3']),
sy.TableCellToken([sy.TextToken(["Text2 "])]),
sy.TableCellToken([sy.TableCellSizeToken(["2"]), sy.TextToken(["Text1 "])]),
sy.TableCellToken([sy.TableCellSizeToken(["3"]), sy.TextToken(["Text2 "])]),
]),
), __GLk()),
("|2 Text1 |3 Text2 |4 Text3 |", (
sy.TableRowToken([
sy.TableCellSizeToken(['2']),
sy.TableCellToken([sy.TextToken(["Text1 "])]),
sy.TableCellSizeToken(['3']),
sy.TableCellToken([sy.TextToken(["Text2 "])]),
sy.TableCellSizeToken(['4']),
sy.TableCellToken([sy.TextToken(["Text3 "])]),
sy.TableCellToken([sy.TableCellSizeToken(["2"]), sy.TextToken(["Text1 "])]),
sy.TableCellToken([sy.TableCellSizeToken(["3"]), sy.TextToken(["Text2 "])]),
sy.TableCellToken([sy.TableCellSizeToken(["4"]), sy.TextToken(["Text3 "])]),
]),
), __GLk()),
("|3 Text1 | Text2 | {var='test', number=11}", (
("|2 Text1 | Text2 | {var='test', number=11}", (
sy.TableRowToken([
sy.TableCellSizeToken(['3']),
sy.TableCellToken([sy.TextToken(["Text1 "])]),
sy.TableCellToken([sy.TableCellSizeToken(["2"]), sy.TextToken(["Text1 "])]),
sy.TableCellToken([sy.TextToken(["Text2 "])]),
]),
sy.OptionalToken([
@ -517,8 +510,7 @@ dict_advanced_syntax_input_and_expected_output = {
), __GLk(),),
("|2 Text1 | Text2 |", (
sy.TableRowToken([
sy.TableCellSizeToken(['2']),
sy.TableCellToken([sy.TextToken(["Text1 "])]),
sy.TableCellToken([sy.TableCellSizeToken(["2"]), sy.TextToken(["Text1 "])]),
sy.TableCellToken([sy.TextToken(["Text2 "])]),
]),
), __GLk()),
@ -662,8 +654,8 @@ list_of_text_input_and_readable_output = [
# Tables
("| Text1 | Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,<table:cell = 'Text2 ' />' />"),
("| Text1 |3 Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,<table:cell_size = '3' "
"/>,<table:cell = 'Text2 ' />' />"),
("| Text1 |3 Text2 |", "table", "<table:row = '<table:cell = 'Text1 ' />,<table:cell = '<:cell_size = '3' "
"/>,Text2 ' />' />"),
("|---|---|", "table_separator", "<table:separator = '---,---' />"),
("|:--|-:-|--:|--:|", "table_separator", "<table:separator = ':--,-:-,--:,--:' />"),

View File

@ -28,8 +28,8 @@ _type_counterpart_container = [
(syntax.HyperlinkToken, None, "self", tools.__GLk(1)), # Shouldn't be an error
(syntax.TableToken, None, "error", tools.__GLk(1)),
(syntax.TableRowToken, None, "self", tools.__GLk(1)),
(syntax.TableCellToken, None, "error", tools.__GLk(1)),
(syntax.TableSeparatorToken, None, "error", tools.__GLk(1)),
(syntax.TableCellToken, None, "self", tools.__GLk(1)),
(syntax.TableSeparatorToken, None, "self", tools.__GLk(1)),
(syntax.BlockQuoteToken, None, "error", tools.__GLk(1)),
(syntax.BlockQuoteAuthorToken, None, "error", tools.__GLk(1)),
(syntax.CodeToken, "code", "token", tools.__GLk(1)),