Browse Source

Added ability to parse <default> tag using xml_gconf format. fixed #76

master
parent
commit
d6c944890c
6 changed files with 147 additions and 30 deletions
  1. +31
    -25
      calculate/templates/format/xml_gconf_format.py
  2. +30
    -4
      tests/templates/test_directory_processor.py
  3. +42
    -0
      tests/templates/testfiles/test_dir_processor_root/etc.backup/file_28.xml
  4. +1
    -1
      tests/templates/testfiles/test_dir_processor_root/templates_64/install/.calculate_directory
  5. +2
    -0
      tests/templates/testfiles/test_dir_processor_root/templates_65/install/.calculate_directory
  6. +41
    -0
      tests/templates/testfiles/test_dir_processor_root/templates_65/install/file_27.xml

+ 31
- 25
calculate/templates/format/xml_gconf_format.py View File

@@ -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,


+ 30
- 4
tests/templates/test_directory_processor.py View File

@@ -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']


+ 42
- 0
tests/templates/testfiles/test_dir_processor_root/etc.backup/file_28.xml View File

@@ -0,0 +1,42 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
Modified by Calculate Utilities 4.0
Processed template files:
/var/calculate/development/calculate-utils-4-lib/tests/templates/testfiles/test_dir_processor_root/templates_65/install/file_27.xml
-->
<gconf>
<dir name="schemas">
<dir name="desktop">
<dir name="gnome">
<dir name="background">
<entry name="picture_filename" mtime="1267285026" type="schema" stype="string" owner="gnome">
<local_schema locale="C" short_desc="Picture Filename">
<default type="string">
<stringvalue>/usr/share/wallpapers/dm-value_1.jpg</stringvalue>
</default>
<longdesc>File to use for the background image.</longdesc>
</local_schema>
</entry>
</dir>
</dir>
</dir>
<dir name="system">
<dir name="storage">
<dir name="default_options">
<dir name="ntfs-3g">
<entry name="mount_options" mtime="1267283808" type="schema" stype="list" list_type="string">
<local_schema locale="C" short_desc="Default mount options for ntfs-3g fs">
<default type="list" ltype="string">
<li type="string">
<stringvalue> </stringvalue>
</li>
</default>
<longdesc>A list of default mount options for volumes formatted with the ntfs file system using ntfs-3g.</longdesc>
</local_schema>
</entry>
</dir>
</dir>
</dir>
</dir>
</dir>
</gconf>

+ 1
- 1
tests/templates/testfiles/test_dir_processor_root/templates_64/install/.calculate_directory View File

@@ -1,2 +1,2 @@
{% calculate append = "skip", action = "install",
package = "app-editors/vim" %}
package = "test-category/test-package" %}

+ 2
- 0
tests/templates/testfiles/test_dir_processor_root/templates_65/install/.calculate_directory View File

@@ -0,0 +1,2 @@
{% calculate append = "skip", action = "install",
package = "test-category/test-package" %}

+ 41
- 0
tests/templates/testfiles/test_dir_processor_root/templates_65/install/file_27.xml View File

@@ -0,0 +1,41 @@
{% calculate format='xml_gconf', append='replace' %}
<?xml version="1.0"?>
<gconf>
<dir name="schemas">
<dir name="desktop">
<dir name="gnome">
<dir name="background">
<entry name="picture_filename" mtime="1267285026" type="schema" stype="string" owner="gnome">
<local_schema locale="C" short_desc="Picture Filename">
<default type="string">
{% if pkg('test-category/test-package') >= '1.1-r1' %}
<stringvalue>/usr/share/wallpapers/Calculate-dm/contents/images/{{ main.os.var_0 }}.jpg</stringvalue>
{% elif pkg('test-category/test-package') < '1.1-r1' %}
<stringvalue>/usr/share/wallpapers/dm-{{ main.os.var_1 }}.jpg</stringvalue>
{% endif %}
</default>
<longdesc>File to use for the background image.</longdesc>
</local_schema>
</entry>
</dir>
</dir>
</dir>
<dir name="system">
<dir name="storage">
<dir name="default_options">
<dir name="ntfs-3g">
<entry name="mount_options" mtime="1267283808" type="schema" stype="list" list_type="string">
<local_schema locale="C" short_desc="Default mount options for ntfs-3g fs">
<default type="list" ltype="string">
<li type="string">
<stringvalue> </stringvalue>
</li>
</default>
<longdesc>A list of default mount options for volumes formatted with the ntfs file system using ntfs-3g.</longdesc> </local_schema>
</entry>
</dir>
</dir>
</dir>
</dir>
</dir>
</gconf>

Loading…
Cancel
Save