From cda0282a336a3af6d3237ca89c684a81824f7ff8 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Mon, 22 Feb 2021 12:53:21 +0100 Subject: [PATCH] [MOV][REF]Move macros to their own package --- hy_odoo/macros/__init__.hy | 18 ++++++++ hy_odoo/{macros.hy => macros/general.hy} | 0 hy_odoo/macros/odoo.hy | 58 ++++++++++++++++++++++++ hy_odoo/{ => macros}/test.hy | 0 4 files changed, 76 insertions(+) create mode 100644 hy_odoo/macros/__init__.hy rename hy_odoo/{macros.hy => macros/general.hy} (100%) create mode 100644 hy_odoo/macros/odoo.hy rename hy_odoo/{ => macros}/test.hy (100%) diff --git a/hy_odoo/macros/__init__.hy b/hy_odoo/macros/__init__.hy new file mode 100644 index 0000000..cd924c2 --- /dev/null +++ b/hy_odoo/macros/__init__.hy @@ -0,0 +1,18 @@ +;; -*- coding: utf-8 -*- +;; +;; Copyright 2021 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 . + +; (require [hy-odoo.macros [general odoo test]]) diff --git a/hy_odoo/macros.hy b/hy_odoo/macros/general.hy similarity index 100% rename from hy_odoo/macros.hy rename to hy_odoo/macros/general.hy diff --git a/hy_odoo/macros/odoo.hy b/hy_odoo/macros/odoo.hy new file mode 100644 index 0000000..f1a84f4 --- /dev/null +++ b/hy_odoo/macros/odoo.hy @@ -0,0 +1,58 @@ +;; -*- coding: utf-8 -*- +;; +;; Copyright 2019-2021 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 related macros " + +(defmacro o-cmod [model-name] `(. cls env [~model-name])) +(defmacro o-mod [model-name] `(. self env [~model-name])) +(defmacro o-cref [ref-name] `((. cls env ref) ~ref-name)) +(defmacro o-ref [ref-name] `((. self env ref) ~ref-name)) + +(defmacro hydm [hy-domain] + "Generate Odoo domain from Hy like tuple domain" + (setv op (second hy-domain) + field (mangle (nth hy-domain 2)) + value (nth hy-domain 3)) + `(, ~field ~op ~value)) + +; Odoo ORM macros + +(defmacro/g! compute-fn [field dependencies body] + "Macro to make computed definition smoother" + (setv fname f"_compute_{(mangle field)}" descr f"Computes {field}" + dependencies (list (map mangle dependencies))) + (import [hy.models [HySymbol]]) + `(with-decorator (.depends api ~@dependencies) + (defn ~(HySymbol fname) [self] + ~descr + ~body))) + +(defmacro compute-field [fname body] + "Takes fname Symbol and body to create computed field" + (setv fn-name f"_compute_{(mangle fname)}") + `(setv ~fname (~@body :compute ~fn-name))) + +; Backend macros + +(defmacro __ [sentence] `((py "_") ~sentence)) + +(defmacro logger [] + `(do + (import logging) + (setv _logger (.getLogger logging --name--)))) + +(defmacro pdb [] `(do (import [pdb [set-trace]]) (set-trace))) diff --git a/hy_odoo/test.hy b/hy_odoo/macros/test.hy similarity index 100% rename from hy_odoo/test.hy rename to hy_odoo/macros/test.hy