added print_all methods for debugging, added container logic

This commit is contained in:
vicky 2022-05-27 19:59:07 +02:00
parent bd308d71ca
commit 5c936a603d
1 changed files with 138 additions and 43 deletions

View File

@ -11,10 +11,11 @@
# container = BaseContainer() # container = BaseContainer()
# container[number] -> The number element in the content # container[number] -> The number element in the content
# "class_insert" >> container[number] -> Get an element from one of the mapped methods # "class_insert" >> container[number] -> Get an element from one of the mapped methods
import rich import rich
from bootstraparse.modules import syntax from bootstraparse.modules import syntax
from bootstraparse.modules.error_mngr import CannotBeContainedError from bootstraparse.modules.error_mngr import MismatchedContainerError, log_exception, log_message
from bootstraparse.modules.syntax import EncapsulatedSemanticType
class BaseContainer: class BaseContainer:
@ -95,6 +96,17 @@ class BaseContainer:
) )
) )
def print_all(self, ident=''):
print(ident + self.class_name())
for e in self:
if isinstance(e, BaseContainer):
e.print_all(ident + ' ')
else:
print(ident + str(e))
def to_container(self):
return self
class BaseContainerWithOptionals(BaseContainer): class BaseContainerWithOptionals(BaseContainer):
def __init__(self): def __init__(self):
@ -126,7 +138,6 @@ class BaseContainerWithOptionals(BaseContainer):
# Define containers all the Enhanced text elements, divs, headers, list and any element that can be a container # Define containers all the Enhanced text elements, divs, headers, list and any element that can be a container
# TODO: List all elements and their specialties (custom span id, header level, etc.)
class TextContainer(BaseContainer): class TextContainer(BaseContainer):
pass pass
@ -147,41 +158,43 @@ class EtStrikethroughContainer(BaseContainer):
pass pass
class IlLinkContainer(BaseContainer): class EtCustomSpanContainer(BaseContainer):
pass
class EtUlistContainer(BaseContainer):
pass
class EtOlistContainer(BaseContainer):
pass
class HyperLinkContainer(BaseContainer):
def __init__(self): def __init__(self):
self.map += {'url': ""} self.map += {'url': ""}
pass pass
class IlImageContainer(BaseContainer): # class IlImageContainer(BaseContainer):
# pass
class SeContainer(BaseContainer):
pass pass
class SeDivContainer(BaseContainer): class HeaderContainer(BaseContainer):
pass
class SeArticleContainer(BaseContainer):
pass
class SeAsideContainer(BaseContainer):
pass
class SeSectionContainer(BaseContainer):
pass
class SeHeaderContainer(BaseContainer):
def __init__(self): def __init__(self):
self.map += {'header_level': ""} super().__init__()
self.map['header_level'] = ''
pass pass
class SeDisplayContainer(BaseContainer): class DisplayContainer(BaseContainer):
def __init__(self): def __init__(self):
self.map += {'display_level': ""} super().__init__()
self.map['display_level'] = ""
pass pass
@ -191,22 +204,47 @@ class TableMainContainer(BaseContainer):
class TableHeadContainer(BaseContainer): class TableHeadContainer(BaseContainer):
def __init__(self): def __init__(self):
self.map += {'colspan': ""} super().__init__()
self.map['colspan'] = ""
pass pass
class TableRowContainer(BaseContainer): class TableRowContainer(BaseContainer):
def __init__(self): def __init__(self):
self.map += {'colspan': ""} super().__init__()
self.map['colspan'] = ""
pass pass
class TableCellContainer(BaseContainer): class TableCellContainer(BaseContainer):
def __init__(self): def __init__(self):
self.map += {'colspan': ""} super().__init__()
self.map['colspan'] = ""
pass pass
"""
Dictionary of all correspondences between tokens and containers.
"""
_to_container = {
"text": TextContainer,
"text:em": EtEmContainer,
"text:strong": EtStrongContainer,
"text:underline": EtUnderlineContainer,
"text:strikethrough": EtStrikethroughContainer,
"text:custom_span": EtCustomSpanContainer,
"list:ulist": EtUlistContainer,
"list:olist": EtOlistContainer,
"header": HeaderContainer,
"display": DisplayContainer,
"se:start:div": SeContainer,
"se:start:article": SeContainer,
"se:start:aside": SeContainer,
"se:start:section": SeContainer,
"hyperlink": HyperLinkContainer,
}
class ContextManager: class ContextManager:
""" """
Class in charge of piling all the parsed elements and then encapsulating them inside one another. Class in charge of piling all the parsed elements and then encapsulating them inside one another.
@ -221,6 +259,54 @@ class ContextManager:
""" """
self.output = [] self.output = []
self.parsed_list = parsed_list self.parsed_list = parsed_list
self.pile = []
self.matched_elements = {}
def __iter__(self):
for e in self.pile:
if e:
yield e
def encapsulate(self, start, end):
"""
Method to encapsulate a number of tokens together as a final container object
and replace all elements in the pile with None, except the first one which becomes the container.
Parameters
----------
start : int
Index of the pile where the encapsulation must begin.
end : int
Index of the pile where the encapsulation must end.
"""
try:
pile_start = self.pile[start]
except KeyError:
log_exception(
KeyError(f"Index {start} not in pile range."),
level="CRITICAL"
)
try:
container = _to_container[pile_start.label]()
except KeyError:
log_exception(
KeyError(f"Element {self.pile[start].label} not in dictionary of tokens-containers correspondences."),
level="CRITICAL"
)
for i in range(start, end):
if self.pile[i]:
container.add(self.pile[i].to_container())
self.pile[i] = None
container.add(self.pile[end])
self.pile[end] = None
self.pile[start] = container
def _add_matched(self, label, index):
if label not in self.matched_elements:
self.matched_elements[label] = []
self.matched_elements[label] += [index]
def _get_matched(self, label):
return self.matched_elements[label].pop()
def __call__(self): def __call__(self):
""" """
@ -231,25 +317,32 @@ class ContextManager:
list[BaseContainer] list[BaseContainer]
Returns the pile entirely processed as a list of containers. Returns the pile entirely processed as a list of containers.
""" """
pile = [] # self.pile = self.parsed_list.copy()
matched_elements = {} line_number = 1
line_number = 0 for index, token in enumerate(self.parsed_list):
for token in self.parsed_list: self.pile.append(token)
try: try:
if token.counterpart(): if isinstance(token, syntax.Linebreak):
if token.counterpart() in matched_elements: line_number += 1
self.encapsulate(pile[matched_elements[token.counterpart()]:]) elif isinstance(token, syntax.FinalSemanticType):
elif isinstance(token, EncapsulatedSemanticType): self.encapsulate(index, index)
raise CannotBeContainedError elif token.counterpart() in self.matched_elements:
else: self.encapsulate(self._get_matched(token.counterpart()), index)
pile += token elif isinstance(token, syntax.ClosedSemanticType):
matched_elements += token raise MismatchedContainerError(token, line_number)
else: else:
pile += token self._add_matched(token.label, index)
except CannotBeContainedError as e: except MismatchedContainerError as e:
print(e) print(e)
def print_all(self):
for e in self:
if isinstance(e, BaseContainer):
e.print_all()
else:
print(str(e))
# Defines all methods to manage the context of the parser as a file is being parsed # Defines all methods to manage the context of the parser as a file is being parsed
# Builds all containers as the parser is parsing the file # Builds all containers as the parser is parsing the file
@ -266,7 +359,9 @@ if __name__ == "__main__":
# header 1 # # header 1 #
! display 1 !""" ! display 1 !"""
) )
test = parser.parse_line(io_string) test = parser.parse_line(io_string)
ctx = ContextManager(test) ctx = ContextManager(test)
ctx() ctx()
# rich.print(test) print('----------------------------------')
ctx.print_all()