From fd2da5bddfa226367f084a65dc4c776a6cc9a8cc Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Wed, 23 Oct 2019 05:29:17 +0200 Subject: [PATCH] [ADD]Hy Base : new macros * For computed fields ; * For migrations. --- odoo.hy | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/odoo.hy b/odoo.hy index 2103de9..64fa171 100644 --- a/odoo.hy +++ b/odoo.hy @@ -15,8 +15,9 @@ ;; You should have received a copy of the GNU Affero General Public License ;; along with this program. If not, see . -" Odoo XML macros " +" Odoo macros " +(import [os [path]]) (import logging [odoo.addons.hy_base.xml [*]]) (setv logger (.getLogger logging __name__)) @@ -27,6 +28,8 @@ python2-form python3-form)) +; XML helpers functions and macros + (defn ox-odoo [&rest args] (xmlroot (xmln "odoo" {} #*args))) (defn ox-data [&rest args] (xmln "data" {} #*args)) (defn ox-record [&rest args] (xmln "record" #*args)) @@ -95,3 +98,27 @@ ~g!fpath (+ ~g!output-path "/" ~filename)) (.debug logger (+ "Hy XML DSL : compiling " ~filename " to " ~g!output-path)) (with [f (open ~g!fpath "w")] (.write f ~g!output-xml)))) + +; Odoo Backend macros + +(defmacro/g! compute-fn [field dependencies body] + "Macro to make computed definition smoother" + (setv fname f"_compute_{(mangle field)}" descr f"Computes {field}") + `(do + (import [hy.models [HySymbol]]) + (with-decorator (.depends api ~@dependencies) + (defn ~(HySymbol fname) [self] + ~descr + ~body)))) + +(defmacro compute-field [fname body] + (setv fn-name f"_compute_{(mangle fname)}") + `(setv ~fname (~@body :compute ~fn-name))) + +; Migrations + +(defn generate-fn-name [filepath] + "Generate function name from filepath" + (setv version (.replace (get (.split (.dirname path filepath) "/") -1) "." "_") + pre-post (get (.split (.basename path filepath) "-") 0)) + f"{pre-post}_{version}")