From f5ab0d99b1a773fd33b8624b92a3ab486a64dd4d Mon Sep 17 00:00:00 2001 From: Stanislav Pankevich Date: Wed, 22 May 2024 21:19:50 +0200 Subject: [PATCH] backend/sdoc: instantiate doc/grammar meta models at start time This avoids instantiating the meta model each time an SDoc or an SGra file is read. --- strictdoc/backend/sdoc/error_handling.py | 2 +- .../backend/sdoc/grammar/grammar_builder.py | 15 ++++----- strictdoc/backend/sdoc/grammar_reader.py | 31 ++++++++++--------- strictdoc/backend/sdoc/reader.py | 18 ++++++----- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/strictdoc/backend/sdoc/error_handling.py b/strictdoc/backend/sdoc/error_handling.py index fe1101c81..3960ad525 100644 --- a/strictdoc/backend/sdoc/error_handling.py +++ b/strictdoc/backend/sdoc/error_handling.py @@ -365,7 +365,7 @@ def default_view_doesnt_exist( filename=filename, ) - def to_print_message(self): + def to_print_message(self) -> str: message = "" message += f"error: could not parse file: {self.file_path}.\n" message += f"Semantic error: {self.title}\n" diff --git a/strictdoc/backend/sdoc/grammar/grammar_builder.py b/strictdoc/backend/sdoc/grammar/grammar_builder.py index 9fdd463f8..be16ce41d 100644 --- a/strictdoc/backend/sdoc/grammar/grammar_builder.py +++ b/strictdoc/backend/sdoc/grammar/grammar_builder.py @@ -1,4 +1,3 @@ -# mypy: disable-error-code="no-untyped-call,no-untyped-def" from strictdoc.backend.sdoc.grammar.grammar import ( DOCUMENT_GRAMMAR, FREE_TEXT_PARSER_GRAMMAR, @@ -16,12 +15,11 @@ class SDocGrammarBuilder: @staticmethod - def _prep_grammar(grammar): - grammar = (grammar).replace("'\\n'", "'\n'") - return grammar + def _prep_grammar(grammar: str) -> str: + return grammar.replace("'\\n'", "'\n'") @staticmethod - def create_grammar(): + def create_grammar() -> str: grammar = SDocGrammarBuilder._prep_grammar( DOCUMENT_GRAMMAR + GRAMMAR_GRAMMAR @@ -33,20 +31,20 @@ def create_grammar(): return grammar @staticmethod - def create_free_text_grammar(): + def create_free_text_grammar() -> str: return SDocGrammarBuilder._prep_grammar( FREE_TEXT_PARSER_GRAMMAR + TEXT_TYPES_GRAMMAR ) @staticmethod - def create_grammar_grammar(): + def create_grammar_grammar() -> str: grammar = SDocGrammarBuilder._prep_grammar( GRAMMAR_WRAPPER + GRAMMAR_GRAMMAR + STRICTDOC_BASIC_TYPE_SYSTEM ) return grammar @staticmethod - def create_raw_grammar(): + def create_raw_grammar() -> str: grammar = ( DOCUMENT_GRAMMAR + GRAMMAR_GRAMMAR @@ -55,5 +53,4 @@ def create_raw_grammar(): + TEXT_TYPES_GRAMMAR + STRICTDOC_BASIC_TYPE_SYSTEM ) - return grammar diff --git a/strictdoc/backend/sdoc/grammar_reader.py b/strictdoc/backend/sdoc/grammar_reader.py index b5fe63213..8b186bfc5 100644 --- a/strictdoc/backend/sdoc/grammar_reader.py +++ b/strictdoc/backend/sdoc/grammar_reader.py @@ -1,4 +1,3 @@ -# mypy: disable-error-code="no-untyped-call,no-untyped-def" import sys import traceback from typing import Optional @@ -21,25 +20,29 @@ class SDocGrammarReader: - def __init__(self, path_to_output_root) -> None: - self.path_to_output_root = path_to_output_root + meta_model = metamodel_from_str( + SDocGrammarBuilder.create_grammar_grammar(), + classes=GRAMMAR_MODELS + [DocumentGrammarWrapper], + use_regexp_group=True, + ) - @staticmethod - def read(input_string, file_path=None) -> DocumentGrammar: - meta_model = metamodel_from_str( - SDocGrammarBuilder.create_grammar_grammar(), - classes=GRAMMAR_MODELS + [DocumentGrammarWrapper], - use_regexp_group=True, - ) + def __init__(self, path_to_output_root: str) -> None: + self.path_to_output_root: str = path_to_output_root - meta_model.register_obj_processors( + @staticmethod + def read( + input_string: str, file_path: Optional[str] = None + ) -> DocumentGrammar: + SDocGrammarReader.meta_model.register_obj_processors( { "GrammarElement": preserve_source_location_data, } ) - grammar_wrapper: DocumentGrammarWrapper = meta_model.model_from_str( - input_string, file_name=file_path + grammar_wrapper: DocumentGrammarWrapper = ( + SDocGrammarReader.meta_model.model_from_str( + input_string, file_name=file_path + ) ) grammar: DocumentGrammar = grammar_wrapper.grammar grammar.parent = None @@ -52,7 +55,7 @@ def read(input_string, file_path=None) -> DocumentGrammar: return grammar - def read_from_file(self, file_path) -> DocumentGrammar: + def read_from_file(self, file_path: str) -> DocumentGrammar: unpickled_content: Optional[DocumentGrammar] = assert_optional_cast( PickleCache.read_from_cache(file_path, self.path_to_output_root), DocumentGrammar, diff --git a/strictdoc/backend/sdoc/reader.py b/strictdoc/backend/sdoc/reader.py index 4bc5c66c3..812d05b0b 100644 --- a/strictdoc/backend/sdoc/reader.py +++ b/strictdoc/backend/sdoc/reader.py @@ -17,22 +17,24 @@ class SDReader: + meta_model = metamodel_from_str( + SDocGrammarBuilder.create_grammar(), + classes=DOCUMENT_MODELS, + use_regexp_group=True, + ) + def __init__(self, path_to_output_root: str = "NOT_RELEVANT"): self.path_to_output_root = path_to_output_root @staticmethod def _read(input_string, file_path=None): - meta_model = metamodel_from_str( - SDocGrammarBuilder.create_grammar(), - classes=DOCUMENT_MODELS, - use_regexp_group=True, - ) - parse_context = ParseContext(path_to_sdoc_file=file_path) processor = SDocParsingProcessor(parse_context=parse_context) - meta_model.register_obj_processors(processor.get_default_processors()) + SDReader.meta_model.register_obj_processors( + processor.get_default_processors() + ) - document: SDocDocument = meta_model.model_from_str( + document: SDocDocument = SDReader.meta_model.model_from_str( input_string, file_name=file_path ) parse_context.document_reference.set_document(document)