From d6ec5d9afad2dc8a8e6ef060763b1cdf91c48277 Mon Sep 17 00:00:00 2001 From: vicky Date: Mon, 28 Mar 2022 19:50:39 +0200 Subject: [PATCH] added BaseContainer and attached methods --- src/bootstraparse/modules/context_mngr.py | 71 ++++++++++++++++++++--- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/src/bootstraparse/modules/context_mngr.py b/src/bootstraparse/modules/context_mngr.py index a7e471a..dc346d4 100644 --- a/src/bootstraparse/modules/context_mngr.py +++ b/src/bootstraparse/modules/context_mngr.py @@ -1,14 +1,71 @@ # Interprets where the parser is located and resolves errors class BaseContainer: - pass - # Must define __init__, __str__, __repr__ and a print_all method - # will contain all sub-tokens and/or sub-containers - # Defines a method to get all token elements (__iter__) and a method to get a specific element (__getitem__) - # Defines a method to get the number of elements (__len__) - # Defines a method to get the name of the container (__name__) - # Defines a method to the optionals inside the container ( ? .optional()) + """ + Creates container holding all the elements from the start of a parsed element to its end. + """ + def __init__(self): + self.content = [] + self.optionals = [] + + def add(self, other): + self.content.append(other) + + def class_name(self): + return self.__class__.__name__ + + def __len__(self): + return len(self.content) + + def __iter__(self): + for content in self.content: + yield content + + def __getitem__(self, item): + return self.content[item] + + def __setitem__(self, key, value): + self.content[key] = value + + def __getslice__(self, start, end): + return self.content[start:end] + + def __eq__(self, other): + if len(self) != len(other): + return False + for e, f in zip(self, other): + if e != f: + return False + return True + + def __invert__(self): + return self.optionals + + def __str__(self): + return '{} ({})'.format(", ".join(map(str, self.content)), + ", ".join(map(str, self.optionals)) if self.optionals != [] else "") + + def __repr__(self): + representation = {} + for content in self.content: + if type(content) in representation: + representation[type(content)] += 1 + else: + representation[type(content)] = 1 + return "{}({})".format( + self.class_name(), + ", ".join( + [f'{index.__name__}: {value}' for index, value in representation.items()] + ) + ) + # Define containers all the Enhanced text elements, divs, headers, list and any element that can be a container +class TextContainer(BaseContainer): + pass + + +class EtEmContainer(BaseContainer): + pass class ContextManager: