From 804b7ee3d7e2e6679f692fa62362eaca97993d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=81?= Date: Mon, 30 Nov 2020 17:54:33 +0300 Subject: [PATCH] Xml style comments can be added to the file after joining regex or raw formats if comment = "xml". fixed #47 --- calculate/templates/format/base_format.py | 48 ++++++++++----- calculate/templates/format/raw_format.py | 2 +- .../templates/format/xml_gconf_format.py | 29 +--------- calculate/templates/format/xml_xfce_format.py | 29 +--------- calculate/templates/template_engine.py | 18 +++--- tests/templates/format/test_raw.py | 58 ++++++++++++++++--- tests/templates/test_directory_processor.py | 17 ++---- .../templates_45/install/dir_62/file_0 | 2 +- 8 files changed, 106 insertions(+), 97 deletions(-) diff --git a/calculate/templates/format/base_format.py b/calculate/templates/format/base_format.py index 67fd635..430c8db 100644 --- a/calculate/templates/format/base_format.py +++ b/calculate/templates/format/base_format.py @@ -354,14 +354,9 @@ class Format(): already_changed=False): '''Метод для создания заголовка измененного файла и удаления его из текста исходного файла.''' - header_pattern = (r'^{0}' + r'-' * 79 + r'\n' + - r'{0} Modified by Calculate Utilities [\d\w\.]*\n' + - r'{0} Processed template files:\n' + - r'(?P({0}\s*[/\w\d\-_\.]*\n)+)' + - r'{0}' + r'-' * 79 + r'\n?').format( - self.comment_symbol) + header_pattern = self._get_header_pattern() template_paths = [] - if already_changed: + if already_changed and self.comment_symbol: header_regex = re.compile(header_pattern) parsing_result = header_regex.search(input_text) for template in parsing_result.\ @@ -370,14 +365,41 @@ class Format(): template = template[len(self.comment_symbol):] template_paths.append(template.strip()) template_paths.append(template_path) - header = ('{0}' + '-' * 79 + '\n' + - '{0} Modified by Calculate Utilities {1}\n' + - '{0} Processed template files:\n' + - '{0} ' + '\n{0} '.join(template_paths) + '\n' + - '{0}' + '-' * 79 + '\n').format(self.comment_symbol, - self.CALCULATE_VERSION) + header = self._make_header(template_paths) document_text = re.sub(header_pattern, '', input_text) return header, document_text + def _make_header(self, template_paths: list): + if not self.comment_symbol: + return "" + elif self.comment_symbol in ("xml", "XML"): + return ("\n" + + '\n').format(self.CALCULATE_VERSION) + else: + return ('{0}' + '-' * 79 + '\n' + + '{0} Modified by Calculate Utilities {1}\n' + + '{0} Processed template files:\n' + + '{0} ' + '\n{0} '.join(template_paths) + '\n' + + '{0}' + '-' * 79 + '\n').format(self.comment_symbol, + self.CALCULATE_VERSION) + + def _get_header_pattern(self): + if self.comment_symbol in {"xml", "XML"}: + return (r'\n?') + else: + return (r'^{0}' + r'-' * 79 + r'\n' + + r'{0} Modified by Calculate Utilities [\d\w\.]*\n' + + r'{0} Processed template files:\n' + + r'(?P({0}\s*[/\w\d\-_\.]*\n)+)' + + r'{0}' + r'-' * 79 + r'\n?').format(self.comment_symbol) + def __bool__(self): return bool(self._document_dictionary) diff --git a/calculate/templates/format/raw_format.py b/calculate/templates/format/raw_format.py index 375669b..9753438 100644 --- a/calculate/templates/format/raw_format.py +++ b/calculate/templates/format/raw_format.py @@ -17,7 +17,7 @@ class RawFormat(Format): add_header=False, already_changed=False, parameters=ParametersContainer()): - self.comment_symbol = parameters.comment or "#" + self.comment_symbol = parameters.comment or "" self._before = join_before if add_header and not ignore_comments: diff --git a/calculate/templates/format/xml_gconf_format.py b/calculate/templates/format/xml_gconf_format.py index cb22cd8..ea654c0 100644 --- a/calculate/templates/format/xml_gconf_format.py +++ b/calculate/templates/format/xml_gconf_format.py @@ -10,7 +10,6 @@ try: except ImportError: from xml.etree.ElementTree import Element, SubElement, ElementTree, \ tostring -import re class XMLGConfFormat(Format): @@ -19,6 +18,8 @@ class XMLGConfFormat(Format): _initialized = False + comment_symbol = 'xml' + def __new__(cls, *args, **kwargs): if not cls._initialized: cls._initialize_parser() @@ -267,29 +268,3 @@ class XMLGConfFormat(Format): include_element = SubElement(current_element, element_tag) include_element.text = dictionary[dict_element] - - def _get_header_and_document_text(self, input_text, - template_path, - already_changed=False): - '''Метод для создания заголовка измененного файла и удаления его из - текста исходного файла.''' - header_pattern = (r'\n?') - template_paths = [] - if already_changed: - header_regex = re.compile(header_pattern) - parsing_result = header_regex.search(input_text) - template_paths.extend(parsing_result.groupdict()[ - 'template_paths'].strip().split('\n')) - template_paths.append(template_path) - header = ("\n" + - '\n').format(self.CALCULATE_VERSION) - document_text = re.sub(header_pattern, '', input_text) - return header, document_text diff --git a/calculate/templates/format/xml_xfce_format.py b/calculate/templates/format/xml_xfce_format.py index 48bc5c9..ab43f7d 100644 --- a/calculate/templates/format/xml_xfce_format.py +++ b/calculate/templates/format/xml_xfce_format.py @@ -10,7 +10,6 @@ try: except ImportError: from xml.etree.ElementTree import Element, SubElement, ElementTree, \ tostring -import re class XMLXfceFormat(Format): @@ -19,6 +18,8 @@ class XMLXfceFormat(Format): _initialized = False + comment_symbol = 'xml' + def __new__(cls, *args, **kwargs): if not cls._initialized: cls._initialize_parser() @@ -202,29 +203,3 @@ class XMLXfceFormat(Format): element_head.pop('tag'), **element_head, value=dictionary[dict_element]) - - def _get_header_and_document_text(self, input_text, - template_path, - already_changed=False): - '''Метод для создания заголовка измененного файла и удаления его из - текста исходного файла.''' - header_pattern = (r'\n?') - template_paths = [] - if already_changed: - header_regex = re.compile(header_pattern) - parsing_result = header_regex.search(input_text) - template_paths.extend(parsing_result.groupdict()[ - 'template_paths'].strip().split('\n')) - template_paths.append(template_path) - header = ("\n" + - '\n').format(self.CALCULATE_VERSION) - document_text = re.sub(header_pattern, '', input_text) - return header, document_text diff --git a/calculate/templates/template_engine.py b/calculate/templates/template_engine.py index 9a2fe5c..fd75a33 100644 --- a/calculate/templates/template_engine.py +++ b/calculate/templates/template_engine.py @@ -1263,8 +1263,9 @@ class CalculateExtension(Extension): for section in variable_path: if section in current_container._namespaces: current_container = current_container._namespaces[section] - elif (section in current_container._variables and current_container. - _variables[section].variable_type is HashType): + elif (section in current_container._variables + and current_container._variables[section].variable_type + is HashType): current_container = current_container._variables[section] if section != variable_path[-1]: # Если обнаружен хэш, но в пути к переменной кроме ключа @@ -1465,7 +1466,7 @@ class CalculateExtension(Extension): atom_name = self.package_atom_parser.parse_package_parameter( package_atom) return atom_name.version - except PackageAtomError as error: + except PackageAtomError: return Version() else: # package = context.parent['__parameters__'].package @@ -1476,10 +1477,10 @@ class CalculateExtension(Extension): def get_full_filepath(self, fname): # TODO: добавить получение домашней директории пользователя - #if fname[0] == "~": - # # Получаем директорию пользователя - # fname = os.path.join(self.homeDir, - # fname.partition("/")[2], "")[:-1] + # if fname[0] == "~": + # # Получаем директорию пользователя + # fname = os.path.join(self.homeDir, + # fname.partition("/")[2], "")[:-1] # TODO: учитывать также root_path fname = os.path.join( self.parameters_processor.chroot_path, @@ -1495,7 +1496,7 @@ class CalculateExtension(Extension): try: reg = re.compile(regpattern, re.MULTILINE) except re.error: - raise TemplateSyntaxError(_("Wrong regular expression")) + raise TemplateSyntaxError("Wrong regular expression") fileContent = readFile(fname) if not fileContent: return "" @@ -1534,6 +1535,7 @@ class CalculateExtension(Extension): except OSError: return "" + class TemplateEngine: def __init__(self, directory_path=None, datavars_module=Variables(), diff --git a/tests/templates/format/test_raw.py b/tests/templates/format/test_raw.py index 0480c9c..3a5eb8e 100644 --- a/tests/templates/format/test_raw.py +++ b/tests/templates/format/test_raw.py @@ -1,6 +1,7 @@ import pytest from collections import OrderedDict from calculate.templates.format.raw_format import RawFormat +from calculate.templates.template_engine import ParametersContainer @pytest.mark.raw @@ -47,6 +48,7 @@ parameter-3 = c;''' assert raw_document_1.document_text == output def test_first_with_comment(self): + parameters = ParametersContainer({'comment': '#'}) document_1 = '''parameter-1 = a; parameter-2 = b; parameter-3 = c;''' @@ -66,13 +68,14 @@ parameter-4 = d; parameter-5 = e;''' raw_document_1 = RawFormat(document_1, 'path/to/template', - add_header=True) + add_header=True, parameters=parameters) raw_document_2 = RawFormat(document_2, 'path/to/template') raw_document_1.join_template(raw_document_2) assert raw_document_1.document_text == output def test_second_with_comment(self): + parameters = ParametersContainer({'comment': 'xml'}) document_1 = '''parameter-1 = a; parameter-2 = b; parameter-3 = c;''' @@ -80,11 +83,12 @@ parameter-3 = c;''' document_2 = '''parameter-4 = d; parameter-5 = e;''' - output = '''#------------------------------------------------------------------------------- -# Modified by Calculate Utilities 4.0 -# Processed template files: -# path/to/template -#------------------------------------------------------------------------------- + output = ''' + parameter-4 = d; parameter-5 = e; parameter-1 = a; @@ -92,13 +96,15 @@ parameter-2 = b; parameter-3 = c;''' raw_document_1 = RawFormat(document_1, 'path/to/template', - join_before=True, add_header=True) + join_before=True, add_header=True, + parameters=parameters) raw_document_2 = RawFormat(document_2, 'path/to/template') raw_document_1.join_template(raw_document_2) assert raw_document_1.document_text == output def test_third_with_comment(self): + parameters = ParametersContainer({'comment': '#'}) original_text = '''parameter-1 = a; parameter-2 = b; parameter-3 = c;''' @@ -123,7 +129,43 @@ parameter-6 = f;''' original_1 = RawFormat(original_text, 'path/to/template_1', add_header=True, - already_changed=False) + already_changed=False, + parameters=parameters) + template_1 = RawFormat(template_text_1, 'path/to/template_1') + template_2 = RawFormat(template_text_2, 'path/to/template_2') + + original_1.join_template(template_1) + original_2 = RawFormat(original_1.document_text, 'path/to/template_2', + add_header=True, + already_changed=True, + parameters=parameters) + + original_2.join_template(template_2) + + assert original_2.document_text == output + + def test_fourth_with_empty_comment_symbol(self): + parameters = ParametersContainer({'comment': ''}) + original_text = '''parameter-1 = a; +parameter-2 = b; +parameter-3 = c;''' + + template_text_1 = '''parameter-4 = d; +parameter-5 = e;''' + + template_text_2 = '''parameter-6 = f;''' + + output = '''parameter-1 = a; +parameter-2 = b; +parameter-3 = c; +parameter-4 = d; +parameter-5 = e; +parameter-6 = f;''' + + original_1 = RawFormat(original_text, 'path/to/template_1', + add_header=True, + already_changed=False, + parameters=parameters) template_1 = RawFormat(template_text_1, 'path/to/template_1') template_2 = RawFormat(template_text_2, 'path/to/template_2') diff --git a/tests/templates/test_directory_processor.py b/tests/templates/test_directory_processor.py index cf692de..32a6313 100644 --- a/tests/templates/test_directory_processor.py +++ b/tests/templates/test_directory_processor.py @@ -1408,13 +1408,6 @@ class TestDirectoryProcessor: '/etc/dir_60/._cfg0000_file_0')) def test_default_raw_and_append_replace_contents_not_matching(self): - template_path = join_paths(CHROOT_PATH, - "templates_44/install/dir_61/file_0") - expected_new_text = ('#' + '-' * 79 + '\n' + - '# Modified by Calculate Utilities 4.0\n' + - '# Processed template files:\n' + - f'# { template_path }\n' + - '#' + '-' * 79 + '\n\nnew content') datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, 'templates_44') directory_processor = DirectoryProcessor( @@ -1423,7 +1416,7 @@ class TestDirectoryProcessor: package='test-category/test-package' ) directory_processor.process_template_directories() - assert os.path.exists(join_paths(CHROOT_PATH,'/etc/dir_61/file_0')) + assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_61/file_0')) with open(join_paths(CHROOT_PATH, '/etc/dir_61/file_0'), 'r') as original_file: original_text = original_file.read() @@ -1434,7 +1427,7 @@ class TestDirectoryProcessor: '/etc/dir_61/._cfg0000_file_0'), 'r') as new_file: new_text = new_file.read() - assert new_text == expected_new_text + assert new_text == '\nnew content' def test_current_template_variable(self): datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, @@ -1453,7 +1446,7 @@ class TestDirectoryProcessor: package='test-category/test-package' ) directory_processor.process_template_directories() - assert os.path.exists(join_paths(CHROOT_PATH,'/etc/dir_62/file_0')) + assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_62/file_0')) with open(join_paths(CHROOT_PATH, '/etc/dir_62/file_0'), 'r') as output_file: output_text = output_file.read() @@ -1468,7 +1461,7 @@ class TestDirectoryProcessor: package='test-category/test-package' ) directory_processor.process_template_directories() - assert os.path.exists(join_paths(CHROOT_PATH,'/etc/dir_63/file_0')) + assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_63/file_0')) def test_using_excepting_action_values(self): datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, @@ -1479,7 +1472,7 @@ class TestDirectoryProcessor: package='test-category/test-package' ) directory_processor.process_template_directories() - assert os.path.exists(join_paths(CHROOT_PATH,'/etc/dir_65/file_0')) + assert os.path.exists(join_paths(CHROOT_PATH, '/etc/dir_65/file_0')) def test_view_tree(self): list_path = join_paths(CHROOT_PATH, '/etc') diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_45/install/dir_62/file_0 b/tests/templates/testfiles/test_dir_processor_root/templates_45/install/dir_62/file_0 index 159adf6..05c4d0a 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_45/install/dir_62/file_0 +++ b/tests/templates/testfiles/test_dir_processor_root/templates_45/install/dir_62/file_0 @@ -1,2 +1,2 @@ -{% calculate append = 'join', format = 'raw' %} +{% calculate append = 'join', format = 'raw', comment = '#' %} template = {{ main.cl.current_template }}