From 212b6dc3d35c69ccd17f1b40ca86bdd77c775791 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Sun, 3 May 2020 12:59:18 +0200 Subject: [PATCH 1/3] [IMP]Yaltik DSL : add pretty option on xml_write --- yaltik_dsl/__manifest__.py | 2 +- yaltik_dsl/test_xml_base.coco | 14 ++++++++++++-- yaltik_dsl/test_xml_base.py | 16 +++++++++++++--- yaltik_dsl/xml_base.coco | 12 +++++++----- yaltik_dsl/xml_base.py | 18 ++++++++++++------ 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/yaltik_dsl/__manifest__.py b/yaltik_dsl/__manifest__.py index e388fa8..e059173 100644 --- a/yaltik_dsl/__manifest__.py +++ b/yaltik_dsl/__manifest__.py @@ -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', diff --git a/yaltik_dsl/test_xml_base.coco b/yaltik_dsl/test_xml_base.coco index 648edcb..6442bed 100644 --- a/yaltik_dsl/test_xml_base.coco +++ b/yaltik_dsl/test_xml_base.coco @@ -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): '' `self.assertIn` output_xml '' `self.assertIn` output_xml 'Some text' `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() + '' `self.assertIn` output_xml + '' `self.assertIn` output_xml + 'Some text' `self.assertIn` output_xml + unlink(filepath) if __name__ == '__main__': unittest.main() diff --git a/yaltik_dsl/test_xml_base.py b/yaltik_dsl/test_xml_base.py index 572379d..c977586 100644 --- a/yaltik_dsl/test_xml_base.py +++ b/yaltik_dsl/test_xml_base.py @@ -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)('', output_xml) (self.assertIn)('', output_xml) (self.assertIn)('Some text', 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)('', output_xml) + (self.assertIn)('', output_xml) + (self.assertIn)('Some text', output_xml) + unlink(filepath) if __name__ == '__main__': unittest.main() diff --git a/yaltik_dsl/xml_base.coco b/yaltik_dsl/xml_base.coco index 801309b..0ba5652 100644 --- a/yaltik_dsl/xml_base.coco +++ b/yaltik_dsl/xml_base.coco @@ -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)) diff --git a/yaltik_dsl/xml_base.py b/yaltik_dsl/xml_base.py index d950a7f..4d7aa28 100644 --- a/yaltik_dsl/xml_base.py +++ b/yaltik_dsl/xml_base.py @@ -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)) From 32dff3cce7f26f66d39d803e854fddcc9b127908 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Sun, 3 May 2020 12:59:30 +0200 Subject: [PATCH 2/3] [ADD]Yaltik DSL Makefile --- yaltik_dsl/Makefile | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 yaltik_dsl/Makefile diff --git a/yaltik_dsl/Makefile b/yaltik_dsl/Makefile new file mode 100644 index 0000000..033b506 --- /dev/null +++ b/yaltik_dsl/Makefile @@ -0,0 +1,31 @@ +.PHONY: testprod +testprod: clean prod test + +.PHONY: btest +btest: build test + +.PHONY: setup +setup: + pip install -U typing coconut[watch,mypy,jobs] + +.PHONY: build +build: + coconut --notco --strict -t 2.7 -j sys . + +.PHONY: prod +prod: + coconut --notco --strict -t 2.7 -j sys -f . + +.PHONY: watch +watch: + coconut --notco --strict -t 2.7 -j sys -w . + +.PHONY: clean +clean: + find . -name '*.pyc' -delete + find . -name '__pycache__' -delete + +.PHONY: test +test: + python2 test_xml_base.py + python2 test_odoo.py From c48c086a0b48a30cedecc694df35c6e5eac6296b Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Sun, 3 May 2020 13:07:27 +0200 Subject: [PATCH 3/3] [REF]Yaltik DSL Test XML reindentation --- yaltik_dsl/test_xml_base.coco | 162 +++++++++++++++++----------------- yaltik_dsl/test_xml_base.py | 2 +- 2 files changed, 82 insertions(+), 82 deletions(-) diff --git a/yaltik_dsl/test_xml_base.coco b/yaltik_dsl/test_xml_base.coco index 6442bed..7f1ed42 100644 --- a/yaltik_dsl/test_xml_base.coco +++ b/yaltik_dsl/test_xml_base.coco @@ -24,109 +24,109 @@ from xml_base import xmln, xmlroot, xmlchild, xml_write class TestXMLBase(unittest.TestCase): - """ XML Helpers tests """ + """ XML Helpers tests """ - def test_xmln(self): - # Tags - (xmln()._asdict(), {'tag': '', 'attrs': {}, 'children': []}) |*> self.assertEquals - (xmln <| 'a tag' |> getattr$(?, 'tag'), 'a tag') |*> self.assertEquals + def test_xmln(self): + # Tags + (xmln()._asdict(), {'tag': '', 'attrs': {}, 'children': []}) |*> self.assertEquals + (xmln <| 'a tag' |> getattr$(?, 'tag'), 'a tag') |*> self.assertEquals - # Attrs - (xmln(attrs={'a good': 'one'}).attrs, {'a good': 'one'}) |*> self.assertEquals - (xmln <**| {'attrs': {'a good': 'one'}} |> getattr$(?, 'attrs'), {'a good': 'one'}) |*> self.assertEquals + # Attrs + (xmln(attrs={'a good': 'one'}).attrs, {'a good': 'one'}) |*> self.assertEquals + (xmln <**| {'attrs': {'a good': 'one'}} |> getattr$(?, 'attrs'), {'a good': 'one'}) |*> self.assertEquals - # Childrens - attrs ={'children': [1, 2, 3]} - (xmln <**| attrs |> getattr$(?, 'children') == [1, 2, 3]) |> self.assertTrue + # Childrens + attrs ={'children': [1, 2, 3]} + (xmln <**| attrs |> getattr$(?, 'children') == [1, 2, 3]) |> self.assertTrue - attrs = {'children': 'Some text'} - (xmln <**| attrs |> getattr$(?, 'children') == ['Some text']) |> self.assertTrue + attrs = {'children': 'Some text'} + (xmln <**| attrs |> getattr$(?, 'children') == ['Some text']) |> self.assertTrue - with self.assertRaisesRegexp(TypeError, 'Invalid arguments'): - xmln <**| {'children': False} + with self.assertRaisesRegexp(TypeError, 'Invalid arguments'): + xmln <**| {'children': False} - # Ensure that only children after tags is managed - element = xmln <*| ('tag', {'something': 'inside'}) - element.attrs `self.assertIsInstance` dict - element.children `self.assertIsInstance` list + # Ensure that only children after tags is managed + element = xmln <*| ('tag', {'something': 'inside'}) + element.attrs `self.assertIsInstance` dict + element.children `self.assertIsInstance` list - element = xmln <*| ('tag', ['something', 'inside']) - element.attrs `self.assertIsInstance` dict - element.children `self.assertIsInstance` list + element = xmln <*| ('tag', ['something', 'inside']) + element.attrs `self.assertIsInstance` dict + element.children `self.assertIsInstance` list - def test_xmlchild(self): - parent = {'tag': 'root', 'attrs': {}, 'children': []} |> xmlroot - xmlc_par = xmlchild$ <| parent + def test_xmlchild(self): + parent = {'tag': 'root', 'attrs': {}, 'children': []} |> xmlroot + xmlc_par = xmlchild$ <| parent - # Bad arguments - with self.assertRaisesRegexp(TypeError, 'Invalid arguments for xmlchild'): - xmlc_par <| False - # Need XMLDictElement, not dict - with self.assertRaisesRegexp(TypeError, 'Invalid arguments for xmlchild'): - xmlc_par <| [{'tag': 't', 'attrs': {'a': 'b'}, 'children': []}] + # Bad arguments + with self.assertRaisesRegexp(TypeError, 'Invalid arguments for xmlchild'): + xmlc_par <| False + # Need XMLDictElement, not dict + with self.assertRaisesRegexp(TypeError, 'Invalid arguments for xmlchild'): + xmlc_par <| [{'tag': 't', 'attrs': {'a': 'b'}, 'children': []}] - xmlc_par <| ['some text'] - parent.text `self.assertEquals` 'some text' + xmlc_par <| ['some text'] + parent.text `self.assertEquals` 'some text' - xmlc_par <| [xmln <**| {'tag': 't', 'attrs': {'a': 'b'}, 'children': []}] - child = parent.iter <| 't' |> next - child.tag `self.assertEquals` 't' - child.attrib `self.assertEquals` {'a': 'b'} - (child |> list) `self.assertEquals` [] + xmlc_par <| [xmln <**| {'tag': 't', 'attrs': {'a': 'b'}, 'children': []}] + child = parent.iter <| 't' |> next + child.tag `self.assertEquals` 't' + child.attrib `self.assertEquals` {'a': 'b'} + (child |> list) `self.assertEquals` [] - xmlc_par <| [xmln <**| {'tag': 't2', 'attrs': {1: 2}, 'children': []}] - child = parent.iter <| 't2' |> next - child.attrib `self.assertEquals` {'1': '2'} + xmlc_par <| [xmln <**| {'tag': 't2', 'attrs': {1: 2}, 'children': []}] + child = parent.iter <| 't2' |> next + child.attrib `self.assertEquals` {'1': '2'} - xmlc_par <| [xmln <**| {'tag': 'tchildren', 'attrs': {}, - 'children': [xmln <**| {'tag': 'subchild', 'attrs': {}, 'children': []}]}] - child = parent.iter <| 'tchildren' |> next - subchildren = (child |> list) - (subchildren |> len) `self.assertEquals` 1 - subchildren[0].tag `self.assertEquals` 'subchild' + xmlc_par <| [xmln <**| {'tag': 'tchildren', 'attrs': {}, + 'children': [xmln <**| {'tag': 'subchild', 'attrs': {}, 'children': []}]}] + child = parent.iter <| 'tchildren' |> next + subchildren = (child |> list) + (subchildren |> len) `self.assertEquals` 1 + subchildren[0].tag `self.assertEquals` 'subchild' - def test_xmlroot(self): - root = {'tag': 'root', 'attrs': {}, 'children': []} |> xmlroot - isinstance <*| (root, ET.Element) |> self.assertTrue + def test_xmlroot(self): + root = {'tag': 'root', 'attrs': {}, 'children': []} |> xmlroot + isinstance <*| (root, ET.Element) |> self.assertTrue - with self.assertRaisesRegexp(TypeError, 'has no attribute'): - False |> xmlroot - with self.assertRaisesRegexp(KeyError, 'tag'): - {} |> xmlroot - with self.assertRaisesRegexp(KeyError, 'attrs'): - {'tag': 'root'} |> xmlroot + with self.assertRaisesRegexp(TypeError, 'has no attribute'): + False |> xmlroot + with self.assertRaisesRegexp(KeyError, 'tag'): + {} |> xmlroot + with self.assertRaisesRegexp(KeyError, 'attrs'): + {'tag': 'root'} |> xmlroot - def test_xml_write(self): - children = [('child1', {'attr': 'value'}, []) |*> xmln, - ('child2', {}, "Some text") |*> xmln] - tree = xmlroot({'tag': 'root', 'attrs': {}, 'children': children}) - xmlw = xml_write$(?, tree) + def test_xml_write(self): + children = [('child1', {'attr': 'value'}, []) |*> xmln, + ('child2', {}, "Some text") |*> xmln] + tree = xmlroot({'tag': 'root', 'attrs': {}, 'children': children}) + xmlw = xml_write$(?, tree) - ('/badpath' |> xmlw) `self.assertEquals` None - ('/bad.ext' |> xmlw) `self.assertEquals` None + ('/badpath' |> xmlw) `self.assertEquals` None + ('/bad.ext' |> xmlw) `self.assertEquals` None - xmlw(__file__) # Default to pretty - filepath = __file__.replace('.py', '_views.xml') - with open(filepath, 'r') as output_file: - output_xml = output_file.read() - '' `self.assertIn` output_xml - '' `self.assertIn` output_xml - 'Some text' `self.assertIn` output_xml - unlink(filepath) + xmlw(__file__) # Default to pretty + filepath = __file__.replace('.py', '_views.xml') + with open(filepath, 'r') as output_file: + output_xml = output_file.read() + '' `self.assertIn` output_xml + '' `self.assertIn` output_xml + 'Some text' `self.assertIn` output_xml + 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.assertIn` output_xml - '' `self.assertIn` output_xml - 'Some text' `self.assertIn` output_xml - 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.assertIn` output_xml + '' `self.assertIn` output_xml + 'Some text' `self.assertIn` output_xml + unlink(filepath) if __name__ == '__main__': unittest.main() diff --git a/yaltik_dsl/test_xml_base.py b/yaltik_dsl/test_xml_base.py index c977586..6289d48 100644 --- a/yaltik_dsl/test_xml_base.py +++ b/yaltik_dsl/test_xml_base.py @@ -1,6 +1,6 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- -# __coconut_hash__ = 0x470897d2 +# __coconut_hash__ = 0x731dbc72 # Compiled with Coconut version 1.4.3 [Ernest Scribbler]