[REF]Hy Odoo repackging

This commit is contained in:
Fabien BOURGEOIS 2020-03-27 00:28:28 +01:00
parent 48a14d8dd9
commit b260bf1f8d
3 changed files with 24 additions and 23 deletions

View File

@ -16,4 +16,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import hy import hy
from . import odoo from . import odoo, xml

View File

@ -18,16 +18,7 @@
" Odoo macros " " Odoo macros "
(import [os [path]]) (import [os [path]])
(import logging)
(import [hy-odoo.xml [xmlroot xmln]]) (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 ; XML helpers functions and macros
@ -104,19 +95,6 @@
(ox-field {"name" "model" "eval" (+ "'" model "'")} []) (ox-field {"name" "model" "eval" (+ "'" model "'")} [])
(ox-field {"name" "value" "eval" action})])) (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 ; Odoo Backend macros
(defmacro/g! compute-fn [field dependencies body] (defmacro/g! compute-fn [field dependencies body]

View File

@ -19,6 +19,8 @@
(import [xml.etree.ElementTree :as ET]) (import [xml.etree.ElementTree :as ET])
;; Helpers
(defn xmlroot [tree] (defn xmlroot [tree]
"Special process for root XML Node" "Special process for root XML Node"
(setv rootel (.Element ET (get tree "tag") (get tree "attrs")) (setv rootel (.Element ET (get tree "tag") (get tree "attrs"))
@ -45,3 +47,24 @@
(if (instance? list attrs) (if (instance? list attrs)
(do (setv children attrs) (setv attrs {})))) (do (setv children attrs) (setv attrs {}))))
{"tag" tag "attrs" (or attrs {}) "children" (or children [])}) {"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))))