From 305d17609f0a6ba1cca5092d9607f90633e006df Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Mon, 27 Apr 2020 11:32:10 +0200 Subject: [PATCH] [ADD]Add new global macros and helpers --- hy_odoo/odoo.hy | 37 ++++++++++++++++++++++++++++++++++++- setup.py | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/hy_odoo/odoo.hy b/hy_odoo/odoo.hy index 46c023c..fbcf0a6 100644 --- a/hy_odoo/odoo.hy +++ b/hy_odoo/odoo.hy @@ -137,7 +137,7 @@ (field {"name" "model" "eval" (+ "'" model "'")} []) (field {"name" "value" "eval" action})])) -; Odoo Backend macros +; Odoo ORM macros (defmacro/g! compute-fn [field dependencies body] "Macro to make computed definition smoother" @@ -154,8 +154,43 @@ (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))) + +; Hy-related helpers + +(defn modulehytopy [module-path hy-files] + "Transforms hy to py for translation purpose" + (import astor) + (import [os.path [dirname]] + [hy.lex [hy-parse]] + [hy.compiler [hy-compile]] + [hy.errors [filtered-hy-exceptions]]) + + (defn hytopy [source path] + "Hy source to Py source" + (setv hst (with [(filtered-hy-exceptions)] (hy-parse source :filename path)) + -ast (with [(filtered-hy-exceptions)] (hy-compile hst "__main__" :filename path :source source))) + (.to-source (. astor code-gen) -ast)) + + (for [hy-file hy-files] + (setv hy-path (% "%s/%s.hy" (, (dirname module-path) hy-file)) + hy-source (with [o (open hy-path "r")] (.read o)) + output-path (.replace hy-path ".hy" ".py") + output-py (% "# %s\n%s" (, "Generate from Hy AST, for Babel translation purpose only. For real source code, please see and use HY source" (hytopy hy-source hy-path)))) + (with [f (open output-path "w")] (.write f output-py)))) + ; Migrations (defn generate-fn-name [filepath] diff --git a/setup.py b/setup.py index 789c2e1..0844254 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages setup( name="hy_odoo", - version="0.4.0", + version="0.5.0", packages=find_packages(), package_data={ 'hy_odoo': ['*.hy'],