[REF][MOV]Macros on their own packages, small fixes

This commit is contained in:
Fabien BOURGEOIS 2021-02-22 12:54:51 +01:00
parent 2f5b50dcef
commit a66309faa1
2 changed files with 21 additions and 75 deletions

View File

@ -17,7 +17,7 @@
" Odoo macros and helpers " " Odoo macros and helpers "
(require [hy-odoo.macros [if-python2]]) (require [hy-odoo.macros.general [if-python2]])
(import [os [path]] (import [os [path]]
[hy-odoo.xml_base [xmlroot xmln]]) [hy-odoo.xml_base [xmlroot xmln]])
@ -31,30 +31,6 @@
value (if (string? value) f"'{value}'" value)) value (if (string? value) f"'{value}'" value))
(return f"('{field}', '{op}', {value})"))) (return f"('{field}', '{op}', {value})")))
(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))
(defmacro hydict [dic]
"Generate dict with mangled keys, from HyDict list"
(setv mangled-dic
(list
(map
(fn [pair]
(if (even? (nth pair 0))
(mangle (nth pair 1))
(nth pair 1)))
(enumerate dic))))
`{~@mangled-dic})
; XML helpers functions and macros ; XML helpers functions and macros
(defn odoo [children] (xmlroot (xmln "odoo" {} children))) (defn odoo [children] (xmlroot (xmln "odoo" {} children)))
@ -171,37 +147,6 @@
(field-inherit inherit) (field-inherit inherit)
(field-arch arch)])) (field-arch arch)]))
; 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)))
; Hy-related helpers ; Hy-related helpers
(defn modulehytopy [module-path hy-files] (defn modulehytopy [module-path hy-files]

View File

@ -17,7 +17,8 @@
" XML helpers and macros " " XML helpers and macros "
(require [hy-odoo.macros [if-python2 ustr-cast]]) (require [hy-odoo.macros.general [if-python2 ustr-cast]])
(require [hy-odoo.macros.odoo [pdb]])
(import [collections [namedtuple]] (import [collections [namedtuple]]
[functools [partial]] [functools [partial]]
[os [path]] [os [path]]
@ -40,12 +41,12 @@
"Handling of children (ie non root) XML Nodes with/o text and subchildren "Handling of children (ie non root) XML Nodes with/o text and subchildren
(recursive)" (recursive)"
(cond [(string? children) (setv (. parent text) children)] (cond [(string? children) (setv (. parent text) children)]
[(instance? XMLDictElement children [(instance? XMLDictElement children)
(do (do
(setv attrs (dfor [k v] (.items (. children attrs)) [(ustr-cast k) (ustr-cast v)])) (setv attrs (dfor [k v] (.items (. children attrs)) [(ustr-cast k) (ustr-cast v)]))
(setv new-parent (.SubElement ET parent (. children tag) attrs) (setv new-parent (.SubElement ET parent (. children tag) attrs)
subchildren (.children children)) subchildren (. children children))
(when subchildren) (xmlchild new-parent subchildren)))] (when subchildren) (xmlchild new-parent subchildren))]
[(instance? list children) (list( map (partial xmlchild parent) children))] [(instance? list children) (list( map (partial xmlchild parent) children))]
[True (raise (TypeError "Invalid arguments for xmlchild"))])) [True (raise (TypeError "Invalid arguments for xmlchild"))]))
@ -60,15 +61,15 @@
(defn xml-write [filepath tree] (defn xml-write [filepath tree]
"Write XML file according to filepath and given tree" "Write XML file according to filepath and given tree"
(when (.endswith filepath ".py") (when (.endswith filepath ".hy")
(if-python2 (if-python2
(do (do
(import [xml.etree.ElementTree :as ET] (import [xml.etree.ElementTree :as ET]
[xml.dom [minidom]]) [xml.dom [minidom]])
(setv output-xml (.toprettyxml :indent " " (setv output-xml (.toprettyxml :indent " "
(.parseString minidom (.tostring ET tree))))) (.parseString minidom (.tostring ET tree)))))
(setv output-xml (.decode (.tostring ET tree) "utf-8"))) (setv output-xml (.decode (.tostring ET tree) "utf-8")))
(setv output-path (.split (.abspath path filepath) "/") (setv output-path (.split (.abspath path filepath) "/")
(cut output-path -1) (.replace (last output-path) ".py" "_views.xml") (cut output-path -1) [(.replace (last output-path) ".hy" "_views.xml")]
output-path (.join "/" output-path)) output-path (.join "/" output-path))
(with [output-file (open output-path "w")] (.write output-file output-xml)))) (with [output-file (open output-path "w")] (.write output-file output-xml))))