def teardown(self): stop_tcp_server('127.0.0.1', TEST_PORT) if self.p is not None: self.p.join() if self.DEBUG: from DHParser import log log.suspend_logging()
def test_logging(self): # try: # log_dir() # assert False, "AttributeError should be raised when log_dir() is called outside " \ # "a logging context." # except AttributeError: # pass res = log_dir() if res: suspend_logging() start_logging(self.LOGDIR) assert not os.path.exists(self.LOGDIR), \ "Log dir should be created lazily!" dirname = log_dir() # print(type(dirname), dirname) assert dirname == self.LOGDIR assert is_logging( ), "is_logging() should return True, if logging is on" save_log_dir = suspend_logging() assert not is_logging(), \ "is_logging() should return False, if innermost logging context " \ "has logging turned off." resume_logging(save_log_dir) assert is_logging(), "is_logging() should return True after logging off " \ "context has been left" info_path = os.path.join(self.LOGDIR, 'info.txt') assert os.path.exists(info_path), "an 'info.txt' file should be " \ "created within a newly created log dir" # cleanup os.remove(info_path) os.rmdir(self.LOGDIR)
def logging_task(self): log_dir() assert is_logging(), "Logging should be on inside logging context" save_log_dir = suspend_logging() assert not is_logging( ), "Logging should be off outside logging context" resume_logging(save_log_dir) # TODO: Some race condition occurs here, but which and why??? # Maybe: Some other thread has created logdir but not yet info.txt # Solution: Just return True, cause log_dir() does not guarantee # existence of 'info.txt', anyway... return True
def load_compiler_suite(compiler_suite: str) -> \ Tuple[PreprocessorFactoryFunc, ParserFactoryFunc, TransformerFactoryFunc, CompilerFactoryFunc]: """ Extracts a compiler suite from file or string `compiler_suite` and returns it as a tuple (preprocessor, parser, ast, compiler). Returns: 4-tuple (preprocessor function, parser class, ast transformer function, compiler class) """ global RX_SECTION_MARKER assert isinstance(compiler_suite, str) source = load_if_file(compiler_suite) dhpath = relative_path(os.path.dirname('.'), DHPARSER_PARENTDIR) imports = DHPARSER_IMPORTS.format(dhparser_parentdir=dhpath) if is_python_code(compiler_suite): sections = split_source(compiler_suite, source) _, imports, preprocessor_py, parser_py, ast_py, compiler_py, _ = sections # TODO: Compile in one step and pick parts from namespace later ? preprocessor = compile_python_object(imports + preprocessor_py, r'get_(?:\w+_)?preprocessor$') parser = compile_python_object(imports + parser_py, r'get_(?:\w+_)?grammar$') ast = compile_python_object(imports + ast_py, r'get_(?:\w+_)?transformer$') else: # Assume source is an ebnf grammar. # Is there really any reasonable application case for this? lg_dir = suspend_logging() compiler_py, messages, _ = compile_source(source, None, get_ebnf_grammar(), get_ebnf_transformer(), get_ebnf_compiler(compiler_suite, source)) resume_logging(lg_dir) if has_errors(messages): raise DefinitionError(only_errors(messages), source) preprocessor = get_ebnf_preprocessor parser = get_ebnf_grammar ast = get_ebnf_transformer compiler = compile_python_object(imports + compiler_py, r'get_(?:\w+_)?compiler$') if callable(preprocessor) and callable(parser) and callable(Callable) and callable(compiler): return preprocessor, parser, ast, compiler raise ValueError('Could not generate compiler suite from source code!')
def grammar_instance(grammar_representation) -> Tuple[Grammar, str]: """ Returns a grammar object and the source code of the grammar, from the given `grammar`-data which can be either a file name, ebnf-code, python-code, a Grammar-derived grammar class or an instance of such a class (i.e. a grammar object already). """ if isinstance(grammar_representation, str): # read grammar grammar_src = load_if_file(grammar_representation) if is_python_code(grammar_src): parser_py = grammar_src # type: str messages = [] # type: List[Error] else: lg_dir = suspend_logging() result, messages, _ = compile_source( grammar_src, None, get_ebnf_grammar(), get_ebnf_transformer(), get_ebnf_compiler()) parser_py = cast(str, result) resume_logging(lg_dir) if has_errors(messages): raise DefinitionError(only_errors(messages), grammar_src) imports = DHPARSER_IMPORTS.format( dhparser_parentdir=relative_path('.', DHPARSER_PARENTDIR)) grammar_class = compile_python_object(imports + parser_py, r'\w+Grammar$') if inspect.isclass(grammar_class) and issubclass(grammar_class, Grammar): parser_root = grammar_class() else: raise ValueError('Could not compile or Grammar class!') else: # assume that dsl_grammar is a ParserHQ-object or Grammar class grammar_src = '' if isinstance(grammar_representation, Grammar): parser_root = grammar_representation else: # assume ``grammar_representation`` is a grammar class and get the root object parser_root = grammar_representation() return parser_root, grammar_src