[IMP]Yaltik DSL : add pretty option on xml_write

This commit is contained in:
Fabien BOURGEOIS 2020-05-03 12:59:18 +02:00
parent 0c037815b9
commit 212b6dc3d3
5 changed files with 45 additions and 17 deletions

View File

@ -19,7 +19,7 @@
'name': 'Yaltik Odoo DSL base module and fns',
'summary': 'Yaltik Odoo Domain Specific Language base module and functions',
'description': """ Yaltik Odoo Domain Specific Language base module and functions """,
'version': '10.0.0.3.1',
'version': '10.0.0.3.2',
'category': 'Yaltik',
'author': 'Fabien Bourgeois',
'license': 'AGPL-3',

View File

@ -108,7 +108,7 @@ class TestXMLBase(unittest.TestCase):
('/badpath' |> xmlw) `self.assertEquals` None
('/bad.ext' |> xmlw) `self.assertEquals` None
xmlw(__file__)
xmlw(__file__) # Default to pretty
filepath = __file__.replace('.py', '_views.xml')
with open(filepath, 'r') as output_file:
output_xml = output_file.read()
@ -116,7 +116,17 @@ class TestXMLBase(unittest.TestCase):
'<root>' `self.assertIn` output_xml
'<child1 attr="value"/>' `self.assertIn` output_xml
'<child2>Some text</child2>' `self.assertIn` output_xml
unlink(filepath)
unlink(filepath)
xmlw(__file__, pretty=False)
filepath = __file__.replace('.py', '_views.xml')
with open(filepath, 'r') as output_file:
output_xml = output_file.read()
'<?xml version' `self.assertNotIn` output_xml
'<root>' `self.assertIn` output_xml
'<child1 attr="value" />' `self.assertIn` output_xml
'<child2>Some text</child2>' `self.assertIn` output_xml
unlink(filepath)
if __name__ == '__main__':
unittest.main()

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# __coconut_hash__ = 0x84c101a5
# __coconut_hash__ = 0x470897d2
# Compiled with Coconut version 1.4.3 [Ernest Scribbler]
@ -132,7 +132,7 @@ class TestXMLBase(unittest.TestCase):
(self.assertEquals)(((xmlw)('/badpath')), None)
(self.assertEquals)(((xmlw)('/bad.ext')), None)
xmlw(__file__)
xmlw(__file__) # Default to pretty
filepath = __file__.replace('.py', '_views.xml')
with open(filepath, 'r') as output_file:
output_xml = output_file.read()
@ -140,7 +140,17 @@ class TestXMLBase(unittest.TestCase):
(self.assertIn)('<root>', output_xml)
(self.assertIn)('<child1 attr="value"/>', output_xml)
(self.assertIn)('<child2>Some text</child2>', output_xml)
unlink(filepath)
unlink(filepath)
xmlw(__file__, pretty=False)
filepath = __file__.replace('.py', '_views.xml')
with open(filepath, 'r') as output_file:
output_xml = output_file.read()
(self.assertNotIn)('<?xml version', output_xml)
(self.assertIn)('<root>', output_xml)
(self.assertIn)('<child1 attr="value" />', output_xml)
(self.assertIn)('<child2>Some text</child2>', output_xml)
unlink(filepath)
if __name__ == '__main__':
unittest.main()

View File

@ -73,11 +73,13 @@ def xmln(tag: Text = '',
raise TypeError('Invalid arguments for xmln')
def xml_write(filepath, tree):
def xml_write(filepath: Text, tree: ET.Element, pretty: bool = True) -> None:
""" Write XML file according to filename and given tree """
if '.py' |> filepath.endswith: # if .pyc, no need to generate XML
output_xml = tree |> ET.tostring |> minidom.parseString |> .toprettyxml(indent=' ')
output_xml = tree |> ET.tostring
if pretty:
output_xml = output_xml |> minidom.parseString |> .toprettyxml(indent=' ')
output_path = filepath |> path.abspath |> path.dirname
fpath = [output_path, filepath |> path.basename] |> '/'.join |> .replace('.py', '_views.xml')
with open(fpath, 'w') as output_file:
output_file.write(output_xml)
output_path = [output_path, filepath |> path.basename] |> '/'.join |> .replace('.py', '_views.xml')
with open(output_path, 'w') as output_file:
output_file.write(unicode(output_xml))

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# __coconut_hash__ = 0x178765c5
# __coconut_hash__ = 0x4c21227d
# Compiled with Coconut version 1.4.3 [Ernest Scribbler]
@ -134,11 +134,17 @@ def xmln(tag='', # type: Text
raise TypeError('Invalid arguments for xmln')
def xml_write(filepath, tree):
def xml_write(filepath, # type: Text
tree, # type: ET.Element
pretty=True # type: bool
):
# type: (...) -> None
""" Write XML file according to filename and given tree """
if (filepath.endswith)('.py'): # if .pyc, no need to generate XML
output_xml = ((minidom.parseString)((ET.tostring)(tree))).toprettyxml(indent=' ')
output_xml = (ET.tostring)(tree)
if pretty:
output_xml = ((minidom.parseString)(output_xml)).toprettyxml(indent=' ')
output_path = (path.dirname)((path.abspath)(filepath))
fpath = (('/'.join)([output_path, (path.basename)(filepath)])).replace('.py', '_views.xml')
with open(fpath, 'w') as output_file:
output_file.write(output_xml)
output_path = (('/'.join)([output_path, (path.basename)(filepath)])).replace('.py', '_views.xml')
with open(output_path, 'w') as output_file:
output_file.write(unicode(output_xml))