From b260bf1f8d3d3e16bebf8866ac7b581eeea71d52 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Fri, 27 Mar 2020 00:28:28 +0100 Subject: [PATCH] [REF]Hy Odoo repackging --- hy_odoo/__init__.py | 2 +- hy_odoo/odoo.hy | 22 ---------------------- hy_odoo/xml.hy | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/hy_odoo/__init__.py b/hy_odoo/__init__.py index ca6648c..87761c6 100644 --- a/hy_odoo/__init__.py +++ b/hy_odoo/__init__.py @@ -16,4 +16,4 @@ # along with this program. If not, see . import hy -from . import odoo +from . import odoo, xml diff --git a/hy_odoo/odoo.hy b/hy_odoo/odoo.hy index c6cc1a5..03424d4 100644 --- a/hy_odoo/odoo.hy +++ b/hy_odoo/odoo.hy @@ -18,16 +18,7 @@ " Odoo macros " (import [os [path]]) -(import logging) (import [hy-odoo.xml [xmlroot xmln]]) -(setv logger (.getLogger logging __name__)) - -(defmacro if-python2 [python2-form python3-form] - "If running on python2, execute python2-form, else, execute python3-form" - (import sys) - (if (< (get sys.version_info 0) 3) - python2-form - python3-form)) ; XML helpers functions and macros @@ -104,19 +95,6 @@ (ox-field {"name" "model" "eval" (+ "'" model "'")} []) (ox-field {"name" "value" "eval" action})])) -(defmacro/g! xml-write [filename tree] - "Write XML file according to filename and given tree" - `(do - (import [os [path]] - [xml.etree.ElementTree :as ET]) - (if-python2 - (setv ~g!output-xml (.tostring ET ~tree)) - (setv ~g!output-xml (.decode (.tostring ET ~tree) "utf-8"))) - (setv ~g!output-path (.dirname path (.abspath path __file__)) - ~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] diff --git a/hy_odoo/xml.hy b/hy_odoo/xml.hy index 98cff1b..04b3ef4 100644 --- a/hy_odoo/xml.hy +++ b/hy_odoo/xml.hy @@ -19,6 +19,8 @@ (import [xml.etree.ElementTree :as ET]) +;; Helpers + (defn xmlroot [tree] "Special process for root XML Node" (setv rootel (.Element ET (get tree "tag") (get tree "attrs")) @@ -45,3 +47,24 @@ (if (instance? list attrs) (do (setv children attrs) (setv attrs {})))) {"tag" tag "attrs" (or attrs {}) "children" (or children [])}) + +;; Macros + +(defmacro if-python2 [python2-form python3-form] + "If running on python2, execute python2-form, else, execute python3-form" + (import sys) + (if (< (get sys.version_info 0) 3) + python2-form + python3-form)) + +(defmacro/g! xml-write [filename tree] + "Write XML file according to filename and given tree" + `(do + (import [os [path]] + [xml.etree.ElementTree :as ET]) + (if-python2 + (setv ~g!output-xml (.tostring ET ~tree)) + (setv ~g!output-xml (.decode (.tostring ET ~tree) "utf-8"))) + (setv ~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))))