From 6eda8a2b70e05407360b9f88e34cacd05ea0d8e0 Mon Sep 17 00:00:00 2001 From: Quintijn Hoogenboom Date: Thu, 17 Nov 2022 15:05:02 +0100 Subject: [PATCH] Bug fix languages (#17) * testing started of this feature (test_vocola_header_new_file.py) * improved the include messages for new vcl files --- src/vocola2/__init__.py | 2 +- src/vocola2/_vocola_main.py | 24 +++++- src/vocola2/natlinkvocolastartup.py | 2 +- tests/test_vocola_header_new_file.py | 113 +++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 tests/test_vocola_header_new_file.py diff --git a/src/vocola2/__init__.py b/src/vocola2/__init__.py index 0577bbf..1ecaabb 100644 --- a/src/vocola2/__init__.py +++ b/src/vocola2/__init__.py @@ -1,6 +1,6 @@ """Vocola""" -__version__ = '3.1.4' # bug fix +__version__ = '3.1.5' # bug fix ## now in versions 3.1. series # __version__ = '2.9.6' # sendkeys again from Vocola, but via dtactions. # __version__ = '2.9.5' # make ready for improved pip diff --git a/src/vocola2/_vocola_main.py b/src/vocola2/_vocola_main.py index 37ad52c..919a767 100644 --- a/src/vocola2/_vocola_main.py +++ b/src/vocola2/_vocola_main.py @@ -45,8 +45,8 @@ import natlink from natlinkcore import natlinkutils from natlinkcore import readwritefile -import VocolaUtils -import natlinkvocolastartup # was natlinkstartup in natlinkmain... +from vocola2 import VocolaUtils +from vocola2 import natlinkvocolastartup # was natlinkstartup in natlinkmain... from vocola2.exec.vcl2py.main import main_routine # main.main_routine compile function from importlib.metadata import version from pathlib import Path @@ -106,6 +106,7 @@ if language != 'enx': print(f' language: "{language}"') if status.getVocolaTakesLanguages(): + # addition to comment line in new .vcl files () (self.openCommandFile) VocolaUserLanguageDirectory = os.path.join(VocolaUserDirectory, language) if not os.path.exists(VocolaUserLanguageDirectory): os.mkdir(VocolaUserLanguageDirectory) @@ -457,9 +458,10 @@ def openCommandFile(self, file, comment): path = self.FindExistingCommandFile(file) if not path: + after_comment = self.get_after_comment_new_vcl_file() path = commandFolder + '\\' + file rwfile = readwritefile.ReadWriteFile() - rwfile.writeAnything(path, f'# {comment} \n\n') + rwfile.writeAnything(path, f'# {comment}{after_comment}\n') # with open(path, 'w', encoding='ascii') as fp: # fp.write(f'# {comment} \n\n') @@ -481,7 +483,21 @@ def openCommandFile(self, file, comment): # os.spawnv(os.P_NOWAIT, prog, [prog, path]) natlink.execScript("AppBringUp \"" + path + "\", \"" + path + "\"") - + def get_after_comment_new_vcl_file(self): + """get language dependent and unimacro actions dependent start + of a new vcl command file + """ + language_comment_addition = '' # at new command file + include_unimacro_line = '' + if status.getVocolaTakesLanguages(): + if language != 'enx': + language_comment_addition = f' (language: {language})' + + if status.getVocolaTakesUnimacroActions(): + include_unimacro_line = 'include Unimacro.vch;\n' + if status.getVocolaTakesLanguages() and language != 'enx': + include_unimacro_line = 'include ..\\Unimacro.vch;\n' + return f'{language_comment_addition}\n{include_unimacro_line}\n' ########################################################################### # # diff --git a/src/vocola2/natlinkvocolastartup.py b/src/vocola2/natlinkvocolastartup.py index e141e92..f8a5530 100644 --- a/src/vocola2/natlinkvocolastartup.py +++ b/src/vocola2/natlinkvocolastartup.py @@ -99,7 +99,7 @@ def create_new_language_subdirectory_if_needed(): commandFolder = None if VocolaEnabled and status.getVocolaTakesLanguages(): - if language != 'enx ' and commandFolder: + if language != 'enx' and commandFolder: uDir = commandFolder uDir2 = os.path.join(uDir, language) if not os.path.isdir(uDir2): diff --git a/tests/test_vocola_header_new_file.py b/tests/test_vocola_header_new_file.py new file mode 100644 index 0000000..80afb7b --- /dev/null +++ b/tests/test_vocola_header_new_file.py @@ -0,0 +1,113 @@ +from pathlib import Path +import pytest +from natlinkcore import natlinkstatus +from natlinkcore.loader import * + +status = natlinkstatus.NatlinkStatus() + +thisDir = Path(__file__).parent +thisFileName = Path(__file__).stem + ".py" + + +## taken from natlinkcore, test_loader: + +class MockLoggingHandler(logging.Handler): + """Mock logging handler to check for expected logs.""" + + def __init__(self, *args, **kwargs): + self.messages: Dict[str, List[str]] = {} + self.reset() + logging.Handler.__init__(self, *args, **kwargs) + + def emit(self, record): + self.messages[record.levelname.lower()].append(record.getMessage()) + + def reset(self): + self.messages = { + 'debug': [], + 'info': [], + 'warning': [], + 'error': [], + 'critical': [], + } + +@pytest.fixture() +def empty_config(): + config = NatlinkConfig.get_default_config() + return config + +@pytest.fixture() +def logger(): + _logger = logging.Logger('natlink') + _logger.setLevel(logging.DEBUG) + log_handler = MockLoggingHandler() + _logger.addHandler(log_handler) + _logger.messages = log_handler.messages + _logger.reset = lambda: log_handler.reset() + return _logger + +## comment out one of the two functions... +def test_get_content_new_vcl_file(empty_config, logger, monkeypatch): + """see if the header of a new _vcl_file is correct with the options + + """ + # patching the language property + main = NatlinkMain(logger, empty_config) + main.config = empty_config + main.language = 'enx' + assert main.language == 'enx' + # main.language = 'nld' + # assert main.language == 'nld' + + ## after this setting and imports of voc_main, the setting is not affected any more... + ## language is a property in main (loader.NatlinkMain) and natlinkstatus.NatlinkStatus() (linked to main). + ## so first test with lines 59 and 60 commented, testing 'enx'. + ## then uncomment these 2 lines, and test with 'nld', which stands for every language being not 'enx' + + monkeypatch.setattr(main, "trigger_load", lambda: None) + + from vocola2 import _vocola_main + voc_main = _vocola_main.ThisGrammar() + + ## True, True, different result for 'enx' and other languages: + monkeypatch.setattr(status, "getVocolaTakesUnimacroActions", lambda: True) + monkeypatch.setattr(status, "getVocolaTakesLanguages", lambda: True) + + after_text = voc_main.get_after_comment_new_vcl_file() + if main.language == 'enx': + assert after_text == '\ninclude Unimacro.vch;\n\n' + else: + assert after_text == ' (language: nld)\ninclude ..\\Unimacro.vch;\n\n' + + + ## False, False, same for different languages as set above. + monkeypatch.setattr(status, "getVocolaTakesUnimacroActions", lambda: False) + monkeypatch.setattr(status, "getVocolaTakesLanguages", lambda: False) + after_text = voc_main.get_after_comment_new_vcl_file() + assert after_text == '\n\n' + + # same for different languages: + monkeypatch.setattr(status, "getVocolaTakesUnimacroActions", lambda: True) + monkeypatch.setattr(status, "getVocolaTakesLanguages", lambda: False) + after_text = voc_main.get_after_comment_new_vcl_file() + assert after_text == '\ninclude Unimacro.vch;\n\n' + + ## different for 'enx' and other languages: + monkeypatch.setattr(status, "getVocolaTakesUnimacroActions", lambda: False) + monkeypatch.setattr(status, "getVocolaTakesLanguages", lambda: True) + after_text = voc_main.get_after_comment_new_vcl_file() + if main.language == 'enx': + assert after_text == '\n\n' + else: + assert after_text == ' (language: nld)\n\n' + + + + + + + + + +if __name__ == "__main__": + pytest.main([__file__])