diff --git a/texla/Parser/TreeExplorer.py b/texla/Parser/TreeExplorer.py index e6ff2b7..49f054a 100644 --- a/texla/Parser/TreeExplorer.py +++ b/texla/Parser/TreeExplorer.py @@ -1,122 +1,124 @@ import logging class TreeExplorer: """ The TreeExplorer class is an utility to navigate and extract information from the tree of parsed blocks. For example it is useful to extract the tree of the parents of a block for debugging reasons. It is useful also in rendering to localize blocks inside the document. """ def __init__(self, root_block): """ The constructor needs a root_block to begin the tree""" self.root_block = root_block self.blocks = {'@': root_block} - self.block_names = {} + self.block_names = {"default":[]} #registering blocks by id self.register_blocks(root_block.ch_blocks) + self.register_block_names() @staticmethod def create_tree_from_children(block): #first of all we need the root_block current = block while True: if current.parent_block is None: root_block = current break current = current.parent_block #now we can return a new TreeExplorer #constructed from the found root. return TreeExplorer(root_block) def register_blocks(self, blocks): """This methods reads all the blocks tree from the root_block and created a dictionary with id:block""" for block in blocks: self.blocks[block.id] = block if block.N_chblocks > 0: self.register_blocks(block.ch_blocks) + def register_block_names(self): + """This function registers the block_names, creating + a dictionary with blocks groups by type""" + for bl in self.blocks.values(): + if not bl in self.block_names: + self.block_names[bl.block_name] = [] + if not bl in self.block_names[bl.block_name]: + self.block_names[bl.block_name].append(bl) + def update_blocks_register(self): """This methods update the blocks' ids register recalling register_blocks with the root_block""" self.register_blocks(self.root_block.ch_blocks) + self.register_block_names() def get_parents_list(self, block): """This method returns the list of the parent blocks of the requested block """ if isinstance(block, str): block = self.blocks[block] parents = [] current = block while True: if current == self.root_block: break parents.append(current.parent_block) current = current.parent_block parents.reverse() return parents def get_parents_list_ids(self, block): parents = self.get_parents_list(block) return [x.id for x in parents] def get_block(self, blockid): return self.blocks.get(blockid) def print_tree(self, block, filter_list=None): """This methods prints a beautified tree starting from block parameter and his children. If filter_list is present only the block with the id in the list are printed. It returns a list of output strings""" output = [] if filter_list is None or block.id in filter_list: lstr = ". "* (block.tree_depth+1) output.append(lstr+ ". "+ " "+"_"*40 ) output.append(lstr+ "#"+"---"+ ">|ID : {}".format(block.id)) output.append(lstr+ ". "+ " |block_name : {}". format(block.block_name)) output.append(lstr+ ". "+ " |attributes: ") for at,attr in block.attributes.items(): output.append(lstr+ ". " + " | - "+ "{} : {}". format(at, attr)) output.append(lstr+ ". "+ " |content : {}". format(block.content)) output.append(lstr+ ". ."+"\u203E"*40+"\n") output = "\n".join(output) #iterating on the block children for bl in block.ch_blocks: output += self.print_tree(bl, filter_list) return output def print_tree_to_blocks(self, blocks): """This methods print the tree of parents of the list of blocks passed as parameter. First of all it gets all the parents ids and then prints the tree using the list as filter.""" fl = [] for bl in blocks: fl+= self.get_parents_list_ids(bl) if isinstance(bl, str): fl.append(bl) else: fl.append(bl.id) return self.print_tree(self.root_block, filter_list=fl) def print_tree_to_block(self, block): return self.print_tree_to_blocks([block]) def print_all_tree(self): return self.print_tree(self.root_block) - - def register_block_names(self): - """This function registers the block_names, creating - a dictionary with blocks groups by type""" - self.block_names.clear() - for bl in self.blocks.values(): - if not bl in self.block_names: - self.block_names[bl.block_name] = [] - self.block_names[bl.block_name].append(bl) diff --git a/texla/Reporter.py b/texla/Reporter.py index 9b3be4f..33de1e4 100644 --- a/texla/Reporter.py +++ b/texla/Reporter.py @@ -1,69 +1,67 @@ from .Parser.TreeExplorer import TreeExplorer import logging class Reporter: def __init__(self, tree): self.tree_explorer = tree - #registering the block_names - self.tree_explorer.register_block_names() self.not_parsed_blocks = self.tree_explorer.block_names["default"] self.not_rendered_blocks = [] self.not_parsed_types = {} self.not_rendered_types = {} #collecting not parsed block types for bl in self.not_parsed_blocks: if bl.type not in self.not_parsed_types: self.not_parsed_types[bl.type] = [] self.not_parsed_types[bl.type].append(bl) #references to labels not defined self.missing_anchors = {} def add_not_rendered_block(self, block): """This method saves a block that is not rendered by the Renderer.""" self.not_rendered_blocks.append(block) if not block.block_name in self.not_rendered_types: self.not_rendered_types[block.block_name] = [] self.not_rendered_types[block.block_name].append(block) def add_missing_anchor(self, label, refs): """This methods saves the references list to a missing label""" if label not in self.missing_anchors: self.missing_anchors[label] = [] self.missing_anchors[label] += refs def print_report(self, console=True): logging.info('\033[0;34m############### TEXLA REPORT ###############\033[0m') s = [] s.append("\n- NOT PARSED blocks:") for bl, v in self.not_parsed_types.items(): s.append("\t- {} : {}".format(bl, len(v))) s.append("\n- NOT RENDERED blocks:") for bl, v in self.not_rendered_types.items(): s.append("\t- {} : {}".format(bl, len(v))) s.append("\n- Missing labels:") for lb in self.missing_anchors: s.append("\t- {}".format(lb)) text= "\n".join(s) if console: logging.info(text) #saving to file also the block trees with open("debug/texla_report.txt",'w') as file: t = ["############### TEXLA REPORT ###############"] t.append("\n- NOT PARSED blocks:") for bl, v in self.not_parsed_types.items(): t.append("\t- {} : {}".format(bl, len(v))) t.append("\n- NOT PARSED blocks details:") t.append(self.tree_explorer.print_tree_to_blocks(self.not_parsed_blocks)) t.append("\n- NOT RENDERED blocks:") for bl, v in self.not_rendered_types.items(): t.append("\t- {} : {}".format(bl, len(v))) t.append("\n- NOT RENDERED blocks details:") t.append(self.tree_explorer.print_tree_to_blocks(self.not_rendered_blocks)) t.append("\n- Missing labels details:") for lb, refs in self.missing_anchors.items(): t.append("\t- {}".format(lb)) for ref in refs: t.append("\t\t- {}".format(ref)) file.write("\n".join(t))