From ba839855bdfc75e5430d94b64eaee72ec310265b Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Tue, 17 Sep 2019 16:39:34 +0200 Subject: [PATCH] [ADD]Hy base v12 --- __init__.py | 19 +++++++++++++++ __manifest__.py | 31 +++++++++++++++++++++++ odoo.hy | 60 +++++++++++++++++++++++++++++++++++++++++++++ xml.hy | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 __init__.py create mode 100644 __manifest__.py create mode 100644 odoo.hy create mode 100644 xml.hy diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..f19445b --- /dev/null +++ b/__init__.py @@ -0,0 +1,19 @@ +# -*- 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 . + +import hy +from . import odoo diff --git a/__manifest__.py b/__manifest__.py new file mode 100644 index 0000000..30cccbf --- /dev/null +++ b/__manifest__.py @@ -0,0 +1,31 @@ +# -*- 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': 'HY base module and macros', + 'summary': 'Hylang base module and macros', + 'description': """ Hylang base module and macros """, + 'version': '12.0.0.1.0', + 'category': 'HY', + 'author': 'Fabien Bourgeois', + 'license': 'AGPL-3', + 'application': False, + 'installable': True, + 'depends': ['base'], + 'data': [], + "external_dependencies": {'python' : ['hy']} +} diff --git a/odoo.hy b/odoo.hy new file mode 100644 index 0000000..02ab982 --- /dev/null +++ b/odoo.hy @@ -0,0 +1,60 @@ +;; -*- 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 macros " + +(require [odoo.addons.hy_base.xml [*]]) + +(defmacro ox-odoo [&rest args] `(xmlr "odoo" ~@args)) +(defmacro ox-data [&rest args] `(xmlnc "data" ~@args)) +(defmacro ox-record [&rest args] `(xmlnc "record" ~@args)) + +(defmacro ox-view [xmlid children] `(ox-record {"id" ~xmlid "model" "ir.ui.view"} ~children)) +(defmacro ox-view-def [xmlid name model &rest body] + "View and first fields simplification with record xmlid, name, targeted model" + `(do + (ox-view ~xmlid + [(ox-field-name ~name) + (ox-field-model ~model) + (ox-field-arch ~@body)]))) +(defmacro ox-view-inherit [name model inherit &rest body] + "Inherited View simplification with name of the record, xmlid for model and + inherited view" + (setv module (get (.split __name__ ".") 2) + inherited (get (.split inherit ".") 1) + xmlid f"{inherited}_inherit_{module}") + `(do + (ox-view ~xmlid + [(ox-field-name ~name) + (ox-field-model ~model) + (ox-field-inherit ~inherit) + (ox-field-arch ~@body)]))) + +(defmacro ox-field [&rest args] `(xmlna "field" ~@args)) +(defmacro ox-field-name [name] `(ox-field {"name" "name"} [~name])) +(defmacro ox-field-model [model] `(ox-field {"name" "model"} [~model])) +(defmacro ox-field-inherit [xmlid] `(ox-field {"name" "inherit_id" "ref" ~xmlid} [])) +(defmacro ox-field-arch [&rest args] `(ox-field {"name" "arch" "type" "xml"} ~@args)) + +(defmacro/g! xml-write [filename tree] + "Write XML file according to filename and given tree" + `(do + (import [os [path]]) + (setv ~g!output-xml (.tostring ET ~tree) + ~g!output-path (.dirname path (.abspath path __file__)) + ~g!fpath (+ ~g!output-path "/" ~filename)) + (with [f (open ~g!fpath "w")] (.write f ~g!output-xml)))) diff --git a/xml.hy b/xml.hy new file mode 100644 index 0000000..f3eb736 --- /dev/null +++ b/xml.hy @@ -0,0 +1,65 @@ +;; -*- 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 " + +(import [xml.etree.ElementTree :as ET]) + +(defn xmlroot [tree] + "Special process for root XML Node" + (setv root (first tree)) + (setv rootel (.Element ET (get root "tag") (get root "attrs"))) + (setv children (get root "children")) + (if children (xmlchild rootel children)) + (return rootel)) + +(defn xmlchild [parent children] + "Handling of children (ie non root) XML Nodes with/o text and subchildren + (recursive)" + (for [c children] + (if (string? c) + (setv (. parent text) c) + (do + (setv attrs (dfor [k v] (.items (get c "attrs")) [k (str v)])) + (setv new_parent (.SubElement ET parent (get c "tag") attrs)) + (setv subchildren (get c "children")) + (if subchildren (xmlchild new_parent subchildren)))))) + +(defmacro xmlnc [&rest args] + "XMLNode with default children, not attributes" + (cond [(= (len args) 1) (setv tag (first args) attrs {} children [])] + [(= (len args) 2) (setv tag (first args) attrs {} children (last args))] + [(= (len args) 3) (setv tag (first args) attrs (get args 1) children (last args))]) + `{"tag" ~tag "attrs" ~attrs "children" ~children}) + +(defmacro xmlna [&rest args] + "XMLNode with default attributes, not children" + (cond [(= (len args) 1) (setv tag (first args) attrs {} children [])] + [(= (len args) 2) (setv tag (first args) attrs (last args) children [])] + [(= (len args) 3) (setv tag (first args) attrs (get args 1) children (last args))]) + `{"tag" ~tag "attrs" ~attrs "children" ~children}) + +(defmacro xmlnt [&rest args] + "XMLNode with no child but maybe some text" + (cond [(= (len args) 1) (setv tag (first args) attrs {} text "")] + [(= (len args) 2) (setv tag (first args) attrs {} text (last args))] + [(= (len args) 3) (setv tag (first args) attrs (get args 1) text (last args))]) + `(xmln ~tag ~attrs [~text])) + +(defmacro xmlr [&rest args] + "XML Root node" + (setv expr `(xmlnc ~@args)) `(xmlroot [~expr]))