[ADD]Hy Base : new macros

* For computed fields ;
* For migrations.
This commit is contained in:
Fabien BOURGEOIS 2019-10-23 05:29:17 +02:00
parent f2ac8b6f09
commit fd2da5bddf
1 changed files with 28 additions and 1 deletions

29
odoo.hy
View File

@ -15,8 +15,9 @@
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
" 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}")