From d6c944890c7652bbebbdb74bb1bf3960ff05198e 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: Thu, 28 Jan 2021 15:53:52 +0300 Subject: [PATCH] Added ability to parse tag using xml_gconf format. fixed #76 --- .../templates/format/xml_gconf_format.py | 56 ++++++++++--------- tests/templates/test_directory_processor.py | 34 +++++++++-- .../etc.backup/file_28.xml | 42 ++++++++++++++ .../templates_64/install/.calculate_directory | 2 +- .../templates_65/install/.calculate_directory | 2 + .../templates_65/install/file_27.xml | 41 ++++++++++++++ 6 files changed, 147 insertions(+), 30 deletions(-) create mode 100644 tests/templates/testfiles/test_dir_processor_root/etc.backup/file_28.xml create mode 100644 tests/templates/testfiles/test_dir_processor_root/templates_65/install/.calculate_directory create mode 100644 tests/templates/testfiles/test_dir_processor_root/templates_65/install/file_27.xml diff --git a/calculate/templates/format/xml_gconf_format.py b/calculate/templates/format/xml_gconf_format.py index ea654c0..71c3b01 100644 --- a/calculate/templates/format/xml_gconf_format.py +++ b/calculate/templates/format/xml_gconf_format.py @@ -2,7 +2,7 @@ # from pyparsing import originalTextFor, Literal, Word, printables, OneOrMore,\ Optional -from .base_format import Format +from .base_format import Format, FormatError from ..template_engine import ParametersContainer from collections import OrderedDict try: @@ -10,6 +10,7 @@ try: except ImportError: from xml.etree.ElementTree import Element, SubElement, ElementTree, \ tostring +from pprint import pprint class XMLGConfFormat(Format): @@ -80,7 +81,6 @@ class XMLGConfFormat(Format): if 'value' in element_items: entry_value = element_items.pop('value') - elif 'ltype' in element_items: entry_value = [] for child in xml_element: @@ -100,9 +100,9 @@ class XMLGConfFormat(Format): ('name', parsing_result.name), *element_items.items()) return OrderedDict({element_name: entry_value}) - except Exception: - # Какая-то обработка ошибки. - return OrderedDict() + except Exception as error: + raise FormatError("Can not parse template tag 'entry':" + " {str(error)}.") def _gconf(self, xml_element): '''Метод для парсинга тега gconf.''' @@ -125,9 +125,9 @@ class XMLGConfFormat(Format): element_name = (parsing_result.action, xml_element.tag, ('name', parsing_result.name)) - except Exception: - # Какая-то обработка ошибки. - return OrderedDict() + except Exception as error: + raise FormatError("Can not parse template tag 'dir':" + f" {str(error)}.") for child in xml_element: item_to_add = self._processing_methods.get(child.tag, @@ -155,21 +155,20 @@ class XMLGConfFormat(Format): element_name = ('', xml_element.tag, *xml_element.items()) - except Exception: - # Какая-то обработка ошибки. - return OrderedDict() + except Exception as error: + raise FormatError("Can not parse template tag 'local_schema':" + " {str(error)}.") for child in xml_element: item_to_add = self._processing_methods.get(child.tag, self._unknown)(child) output_dictionary.update(item_to_add) - return OrderedDict({element_name: output_dictionary}) def _stringvalue(self, xml_element): '''Метод для парсинга тега stringvalue.''' - element_name = ('', 'stringvalue') value = xml_element.text + element_name = ('', xml_element.tag) if value is not None: return OrderedDict({element_name: value}) @@ -179,16 +178,24 @@ class XMLGConfFormat(Format): return OrderedDict({element_name: ''}) def _default(self, xml_element): - '''Уже не акутальный метод, вместо него теперь _unknown''' - output_dictionary = OrderedDict() element_name = ('', xml_element.tag, *xml_element.items()) - - for child in xml_element: - item_to_add = self._processing_methods.get(child.tag, - self._unknown)(child) - output_dictionary.update(item_to_add) - - return OrderedDict({element_name: output_dictionary}) + element_items = {name: value for name, value in xml_element.items()} + + if element_items['type'] == 'list': + default_value = [] + for child in xml_element: + item_to_add = self._processing_methods.get(child.tag, + self._unknown + )(child) + default_value.append(item_to_add) + else: + default_value = OrderedDict() + for child in xml_element: + item_to_add = self._processing_methods.get( + child.tag, + self._unknown)(child) + default_value.update(item_to_add) + return OrderedDict({element_name: default_value}) def _li(self, xml_element): '''Метод для разбора элементов с тегом li.''' @@ -230,15 +237,14 @@ class XMLGConfFormat(Format): element_attributes = OrderedDict({key: value for key, value in dict_element[2:]}) - if element_tag == 'dir' or element_tag == 'local_schema'\ - or element_tag == 'default': + if element_tag == 'dir' or element_tag == 'local_schema': include_element = SubElement(current_element, element_tag, **element_attributes) self._build_section(include_element, dictionary[dict_element]) - elif element_tag == 'entry': + elif element_tag == 'entry' or element_tag == 'default': if isinstance(dictionary[dict_element], OrderedDict): include_element = SubElement(current_element, element_tag, diff --git a/tests/templates/test_directory_processor.py b/tests/templates/test_directory_processor.py index 87e5bb0..08c856e 100644 --- a/tests/templates/test_directory_processor.py +++ b/tests/templates/test_directory_processor.py @@ -69,6 +69,9 @@ merge = Variables({'var_1': 674, 'calculate_domains': 'lists.calculate-linux.org', 'ip_value': '127.0.0.0/8'}) +vars_os = Variables({"var_0": "value_0", + "var_1": "value_1"}) + main = Variables({'cl_template_path': '{0},{1}'.format(os.path.join(CHROOT_PATH, 'templates'), @@ -79,9 +82,10 @@ main = Variables({'cl_template_path': 'cl_config_path': CONFIG_PATH, 'cl_config_archive': CONFIG_ARCHIVE_PATH, 'cl_exec_dir_path': EXECUTE_ARCHIVE_PATH, - 'cl_ignore_files': '*.swp'}) + 'cl_ignore_files': '*.swp', + 'os': vars_os}) -test = ({'test_root': CHROOT_PATH}) +test = Variables({'test_root': CHROOT_PATH}) datavars = Variables({'install': install, 'merge': merge, @@ -112,9 +116,13 @@ def show_tree(dir_path: str, indent: int = 0, node_name = node.name if node_name.startswith("._cfg") and check_cfg: node_name = node.name[len("._cfg0000_"):] + for name in test_names: if node_name.startswith(name): - number = int(node_name[len(name) + 1:]) + if '.' in node_name: + number = int(node_name.split('.')[0][len(name) + 1:]) + else: + number = int(node_name[len(name) + 1:]) if number > last_names[name]: last_names[name] = number break @@ -1861,7 +1869,7 @@ class TestDirectoryProcessor: atom_parser = PackageAtomParser(chroot_path=CHROOT_PATH) vim_pkg = Package( - atom_parser.parse_package_parameter(">app-editors/vim-8.0"), + atom_parser.parse_package_parameter("app-editors/vim"), chroot_path=CHROOT_PATH) vim_core_pkg = Package( atom_parser.parse_package_parameter("app-editors/vim-core"), @@ -1869,6 +1877,24 @@ class TestDirectoryProcessor: assert '/etc/file_26' in vim_pkg assert '/etc/file_26' not in vim_core_pkg + def test_to_solve_bug_with_xml_templates(self): + datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, + 'templates_65') + + directory_processor = DirectoryProcessor('install', + datavars_module=datavars) + directory_processor.process_template_directories() + assert os.path.exists(join_paths(CHROOT_PATH, '/etc/file_27.xml')) + + with open (join_paths(CHROOT_PATH, '/etc/file_27.xml'), 'r') as result: + result_text = result.read() + print("RESULT TEXT:") + print(result_text) + + with open (join_paths(CHROOT_PATH, '/etc/file_28.xml'), 'r') as result: + expected_text = result.read() + assert result_text == expected_text + def test_view_tree(self): list_path = join_paths(CHROOT_PATH, '/etc') test_names = ['dir', 'file', 'link'] diff --git a/tests/templates/testfiles/test_dir_processor_root/etc.backup/file_28.xml b/tests/templates/testfiles/test_dir_processor_root/etc.backup/file_28.xml new file mode 100644 index 0000000..74cb9d2 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/etc.backup/file_28.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + /usr/share/wallpapers/dm-value_1.jpg + + File to use for the background image. + + + + + + + + + + + + +
  • + +
  • +
    + A list of default mount options for volumes formatted with the ntfs file system using ntfs-3g. +
    +
    +
    +
    +
    +
    +
    +
    diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_64/install/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_64/install/.calculate_directory index 6b4ced9..6e7670e 100644 --- a/tests/templates/testfiles/test_dir_processor_root/templates_64/install/.calculate_directory +++ b/tests/templates/testfiles/test_dir_processor_root/templates_64/install/.calculate_directory @@ -1,2 +1,2 @@ {% calculate append = "skip", action = "install", -package = "app-editors/vim" %} +package = "test-category/test-package" %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_65/install/.calculate_directory b/tests/templates/testfiles/test_dir_processor_root/templates_65/install/.calculate_directory new file mode 100644 index 0000000..6e7670e --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_65/install/.calculate_directory @@ -0,0 +1,2 @@ +{% calculate append = "skip", action = "install", +package = "test-category/test-package" %} diff --git a/tests/templates/testfiles/test_dir_processor_root/templates_65/install/file_27.xml b/tests/templates/testfiles/test_dir_processor_root/templates_65/install/file_27.xml new file mode 100644 index 0000000..0e31821 --- /dev/null +++ b/tests/templates/testfiles/test_dir_processor_root/templates_65/install/file_27.xml @@ -0,0 +1,41 @@ +{% calculate format='xml_gconf', append='replace' %} + + + + + + + + + +{% if pkg('test-category/test-package') >= '1.1-r1' %} + /usr/share/wallpapers/Calculate-dm/contents/images/{{ main.os.var_0 }}.jpg +{% elif pkg('test-category/test-package') < '1.1-r1' %} + /usr/share/wallpapers/dm-{{ main.os.var_1 }}.jpg +{% endif %} + + File to use for the background image. + + + + + + + + + + + + +
  • + +
  • +
    + A list of default mount options for volumes formatted with the ntfs file system using ntfs-3g.
    +
    +
    +
    +
    +
    +
    +