diff --git a/yaltik_dsl/__init__.py b/yaltik_dsl/__init__.py new file mode 100644 index 0000000..99581b9 --- /dev/null +++ b/yaltik_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/yaltik_dsl/__manifest__.py b/yaltik_dsl/__manifest__.py new file mode 100644 index 0000000..2512cf8 --- /dev/null +++ b/yaltik_dsl/__manifest__.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- + +# Copyright 2019-2020 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': '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.2.0', + 'category': 'Yaltik', + 'author': 'Fabien Bourgeois', + 'license': 'AGPL-3', + 'application': False, + 'installable': True, + 'depends': ['base'] +} diff --git a/yaltik_dsl/odoo.py b/yaltik_dsl/odoo.py new file mode 100644 index 0000000..253bfde --- /dev/null +++ b/yaltik_dsl/odoo.py @@ -0,0 +1,116 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2019-2020 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 .xml_base import xmlroot, xmln + +# XML helpers functions and macros + +def odoo(*args): return xmlroot(xmln('odoo', {}, *args)) + +def data(*args): + if len(args) == 1: + return xmln('data', {}, *args) + return xmln('data', *args) + +def function(*args): return xmln('function', *args) +def record(*args): return xmln('record', *args) +def form(*args): return xmln('form', *args) +def tree(*args): return xmln('tree', *args) +def search(*args): return xmln('search', *args) + +def act_window(*args): return xmln('act_window', *args) + +def act_window_model(model, attrs): + """ Build new act_window from model and args """ + model_und = model.replace('.', '_') + model_cap = ' '.join([w.capitalize() for w in model.split('.')]) + xmlid = '{:{}}'.format(model_und, '') + '_view_action' + name = '{:{}}'.format(model_cap, '') + ' Action' + attrs.update({'id': xmlid, 'name': name, 'res_model': model}) + return act_window(attrs) + +def menuitem(*args): return xmln('menuitem', *args) + +def menuitem_model(model, attrs): + """ Build new menuitem from model and attrs """ + model_und = model.replace('.', '_') + actionid = '{:{}}'.format(model_und, '') + '_view_action' + xmlid = '{:{}}'.format(model_und, '') + '_men' + attrs.update({'id': xmlid, 'action': actionid}) + return menuitem(attrs) + + +def group(*args): return xmln('group', *args) +def header(*args): return xmln('header', *args) +def footer(*args): return xmln('footer', *args) +def sheet(*args): return xmln('sheet', *args) +def button(*args): return xmln('button', *args) +def p(*args): return xmln('p', *args) +def xpath(*args): return xmln('xpath', *args) +def attribute(name, value): return xmln('attribute', {'name': name}, [value]) + +def field(*args): return xmln('field', *args) +def field_name(name): return field({'name': 'name'}, [name]) +def field_model(model): return field({'name': 'model'}, [model]) +def field_inherit(xmlid): return field({'name': 'inherit_id', 'ref': xmlid}, []) +def field_arch(*args): return field({'name': 'arch', 'type': 'xml'}, *args) + +def view(xmlid, children): 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_new(view_type, model, arch): + """ View : new view definition, based on type (form, tree, ...) and model ID """ + model_und = model.replace('.', '_') + model_cap = ' '.join([w.capitalize() for w in model.split('.')]) + xmlid = '{:{}}'.format(model_und, '') + '_view_' + '{:{}}'.format( + view_type, '') + name = '{:{}}'.format(model_cap, '') + ' ' + '{:{}}'.format(view_type. + capitalize(), '') + return view_def(xmlid=xmlid, name=name, model=model, arch=arch) + +def view_inherit(filename, model, inherit, arch): + """ Inherited View simplification with name of the record, xmlid for model + and inherited view """ + module = filename.split('.')[2] + inherited = inherit.split('.')[1] + xmlid = '{:{}}'.format(inherited, '') + '_inherit_' + '{:{}}'.format( + module, '') + model_cap = ' '.join([w.capitalize() for w in model.split('.')]) + name = '{:{}}'.format(model_cap, '') + ' Adaptations' + 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 client_action_multi(xmlid, name, model, action): + """ Client action multi (ir.values), with own xmlid, name, targeted model + and action """ + action = u"'ir.actions.server,%d'%" + '{:{}}'.format(action, '') + return record({'id': xmlid, 'model': 'ir.values'}, [field_name(name), + field({'name': 'key2', 'eval': u"'client_action_multi'"}, []), + field({'name': 'model', 'eval': u"'" + model + u"'"}, []), field + ({'name': 'value', 'eval': action})]) diff --git a/yaltik_dsl/xml_base.py b/yaltik_dsl/xml_base.py new file mode 100644 index 0000000..b92cd2a --- /dev/null +++ b/yaltik_dsl/xml_base.py @@ -0,0 +1,59 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2019-2020 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 os import path +import xml.etree.ElementTree as ET +from xml.dom import minidom + + +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} + +def xml_write(filepath, tree): + """ Write XML file according to filename and given tree """ + output_xml = minidom.parseString(ET.tostring(tree)).toprettyxml(indent=' ') + output_path = path.dirname(path.abspath(filepath)) + fpath = '%s/%s' % (output_path, path.basename(filepath).replace('.py', '_views.xml')) + with open(fpath, 'w') as output_file: + output_file.write(output_xml)