[IMP]Yaltik DSL : allow other suffix for XML

This commit is contained in:
Fabien BOURGEOIS 2022-11-02 10:25:01 +01:00
parent 0cc980b7a9
commit c2ad72a3a6
3 changed files with 15 additions and 5 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': '16.0.0.4.1',
'version': '16.0.0.4.3',
'category': 'Yaltik',
'author': 'Fabien Bourgeois',
'license': 'AGPL-3',

View File

@ -62,14 +62,15 @@ def xmln(tag='', attrs={}, children=[]):
raise TypeError('Invalid arguments for xmln')
def xml_write(filepath, tree, pretty=True):
def xml_write(filepath, tree, pretty=True, xml_type='view'):
""" Write XML file according to filename and given tree """
if filepath.endswith('.py'): # if .pyc, no need to generate XML
output_xml = ET.tostring(tree)
if pretty:
output_xml = minidom.parseString(output_xml).toprettyxml(indent=' ')
output_path = path.abspath(filepath).split(u'/')
output_path[-1] = output_path[-1].replace(u'.py', u'_views.xml')
output_path = u'/'.join(output_path)
suffix = '_views.xml' if xml_type == 'view' else '_data.xml'
output_path = path.abspath(filepath).split('/')
output_path[-1] = output_path[-1].replace('.py', suffix)
output_path = '/'.join(output_path)
with open(output_path, 'w') as output_file:
output_file.write(output_xml)

View File

@ -115,6 +115,15 @@ class TestXMLBase(unittest.TestCase):
self.assertIn('<child1 attr="value"/>', output_xml)
self.assertIn('<child2>Some text</child2>', output_xml)
unlink(filepath)
xml_write(__file__, tree, xml_type='data')
filepath = __file__.replace('.py', '_data.xml')
with open(filepath, 'r') as output_file:
output_xml = output_file.read()
self.assertIn('<?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()