From fbb21545ecfd2dbcd4d033b88c112bffb35ec2bb Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Fri, 20 Sep 2019 15:28:33 +0200 Subject: [PATCH] [ADD]XML DSL addon --- xml_dsl/__init__.py | 18 +++++++++ xml_dsl/__manifest__.py | 29 +++++++++++++++ xml_dsl/base.py | 46 +++++++++++++++++++++++ xml_dsl/odoo.py | 81 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 xml_dsl/__init__.py create mode 100644 xml_dsl/__manifest__.py create mode 100644 xml_dsl/base.py create mode 100644 xml_dsl/odoo.py diff --git a/xml_dsl/__init__.py b/xml_dsl/__init__.py new file mode 100644 index 0000000..99581b9 --- /dev/null +++ b/xml_dsl/__init__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- + +# Copyright 2019 Fabien Bourgeois +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from . import odoo diff --git a/xml_dsl/__manifest__.py b/xml_dsl/__manifest__.py new file mode 100644 index 0000000..586f206 --- /dev/null +++ b/xml_dsl/__manifest__.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- + +# Copyright 2019 Fabien Bourgeois +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +{ + 'name': 'Odoo XML DSL base module and fns', + 'summary': 'Odoo XML Domain Specific Language base module and functions', + 'description': """ Odoo XML Domain Specific Language base module and functions """, + 'version': '10.0.0.1.0', + 'category': 'Yaltik', + 'author': 'Fabien Bourgeois', + 'license': 'AGPL-3', + 'application': False, + 'installable': True, + 'depends': ['base'] +} diff --git a/xml_dsl/base.py b/xml_dsl/base.py new file mode 100644 index 0000000..d806771 --- /dev/null +++ b/xml_dsl/base.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2019 Fabien Bourgeois +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +""" XML helpers and macros """ + +from xml.etree import ElementTree as ET + +def xmlroot(tree): + """ Special process for root XML Node """ + rootel = ET.Element(tree['tag'], tree['attrs']) + children = tree['children'] + if children: + xmlchild(rootel, children) + return rootel + +def xmlchild(parent, children): + """ Handling of children (ie non root) XML Nodes with/o text and + subchildren (recursive) """ + for child in children: + if isinstance(child, str): + parent.text = child + else: + attrs = {unicode(k): unicode(v) for k, v in child['attrs'].items()} + new_parent = ET.SubElement(parent, child['tag'], attrs) + subchildren = child['children'] + if subchildren: + xmlchild(new_parent, subchildren) + +def xmln(tag='', attrs=None, children=None, text=False): + """ XMLNode with default children, not attributes """ + children = ([text] if text else children) or [] + return {'tag': tag, 'attrs': attrs or {}, 'children': children} diff --git a/xml_dsl/odoo.py b/xml_dsl/odoo.py new file mode 100644 index 0000000..b4a3be7 --- /dev/null +++ b/xml_dsl/odoo.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2019 Fabien Bourgeois +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +""" Odoo XML DSL """ + +from . import base as x + +def odoo(*args): + """ odoo tag shortcut """ + return x.xmlroot(x.xmln('odoo', {}, *args)) +def data(*args): + """ data tag shortcut """ + return x.xmln('data', {}, *args) +def record(*args): + """ record tag shortcut """ + return x.xmln('record', *args) + +def view(xmlid, children): + """ view tag shortcut """ + return record({'id': xmlid, 'model': 'ir.ui.view'}, children) + +def view_def(xmlid, name, model, arch): + """ View and first fields simplification with record xmlid, name, targeted model """ + return view(xmlid, [field_name(name), field_model(model), field_arch(arch)]) + +def view_inherit(name, model, inherit, arch): + """ Inherited View simplification with name of the record, xmlid for model + and inherited view """ + module = __name__.split('.')[2] + inherited = inherit.split('.')[1] + xmlid = '%s_inherit_%s' % (inherited, module) + return view(xmlid, [field_name(name), field_model(model), + field_inherit(inherit), field_arch(arch)]) + +def actions_server_code(xmlid, name, modelref, code): + """ Server actions of type code """ + return record({'id': xmlid, 'model': 'ir.actions.server'}, + [field_name(name), + field({'name': 'model_id', 'ref': modelref}), + field({'name': 'state'}, ['code']), + field({'name': 'code'}, [code])]) + +def field(*args): + """ field tag shortcut """ + return x.xmln('field', *args) +def field_name(name): + """ field name tag shortcut """ + return field({'name': 'name'}, [name]) +def field_model(model): + """ field model tag shortcut """ + return field({'name': 'model'}, [model]) +def field_inherit(xmlid): + """ field inherit tag shortcut """ + return field({'name': 'inherit_id', 'ref': xmlid}) +def field_arch(*args): + """ field arch tag shortcut """ + return field({'name': 'arch', 'type': 'xml'}, *args) + +def xml_write(mpath, filename, tree): + """ Write XML file according to filename and given tree """ + import os.path + from xml.etree import ElementTree as ET + output_xml = ET.tostring(tree) + output_path = os.path.dirname(os.path.abspath(mpath)) + fpath = u'%s/%s' % (output_path, filename) + with open(fpath, 'w') as xml_file: + xml_file.write(output_xml)