[REF][UPD]Hy Odoo to Hy 0.24.0

This commit is contained in:
Fabien BOURGEOIS 2022-11-01 17:49:50 +01:00
parent a7f8c189fe
commit 5e56a16aa3
9 changed files with 75 additions and 95 deletions

View File

@ -5,5 +5,4 @@ clean:
.PHONY: test .PHONY: test
test: test:
hy2 hy_odoo/tests/test_xml_base.hy hy hy_odoo/tests/test_xml_base.hy
hy2 hy_odoo/tests/test_odoo.hy

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2020-2021 Fabien Bourgeois <fabien@yaltik.com> # Copyright 2020-2022 Fabien Bourgeois <fabien@yaltik.com>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
@ -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
import odoo, xml_base, utils from . import odoo, xml_base, utils

View File

@ -1,6 +1,6 @@
;; -*- coding: utf-8 -*- ;; -*- coding: utf-8 -*-
;; ;;
;; Copyright 2021 Fabien Bourgeois <fabien@yaltik.com> ;; Copyright 2021-2022 Fabien Bourgeois <fabien@yaltik.com>
;; ;;
;; This program is free software: you can redistribute it and/or modify ;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as ;; it under the terms of the GNU Affero General Public License as
@ -17,17 +17,7 @@
" Hy General Macros " " Hy General Macros "
(defmacro instance? [type record] `(isinstance ~record ~type))
(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 ustr-cast [s]
"If python2, return unicode casting, else str casting"
(if-python2 `(unicode ~s) `(str ~s)))
(defmacro hydict [dic] (defmacro hydict [dic]
"Generate dict with mangled keys, from HyDict list" "Generate dict with mangled keys, from HyDict list"

View File

@ -27,19 +27,18 @@
(setv op (second hy-domain) (setv op (second hy-domain)
field (mangle (nth hy-domain 2)) field (mangle (nth hy-domain 2))
value (nth hy-domain 3)) value (nth hy-domain 3))
`(, ~field ~op ~value)) `#(~field ~op ~value))
; Odoo ORM macros ; Odoo ORM macros
(defmacro/g! compute-fn [field dependencies body] #_(defmacro/g! compute-fn [field dependencies body]
"Macro to make computed definition smoother" "Macro to make computed definition smoother"
(setv fname f"_compute_{(mangle field)}" descr f"Computes {field}" (setv fname f"_compute_{(mangle field)}" descr f"Computes {field}"
dependencies (list (map mangle dependencies))) dependencies (list (map mangle dependencies)))
(import [hy.models [HySymbol]]) (import hy.models [HySymbol]
`(with-decorator (.depends api ~@dependencies) `(defn [(.depends api ~@dependencies)] ~(HySymbol fname) [self]
(defn ~(HySymbol fname) [self] ~descr
~descr ~body)))
~body)))
(defmacro compute-field [fname body] (defmacro compute-field [fname body]
"Takes fname Symbol and body to create computed field" "Takes fname Symbol and body to create computed field"

View File

@ -1,6 +1,6 @@
;; -*- coding: utf-8 -*- ;; -*- coding: utf-8 -*-
;; ;;
;; Copyright 2021 Fabien Bourgeois <fabien@yaltik.com> ;; Copyright 2021-2022 Fabien Bourgeois <fabien@yaltik.com>
;; ;;
;; This program is free software: you can redistribute it and/or modify ;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as ;; it under the terms of the GNU Affero General Public License as
@ -17,8 +17,6 @@
" Hy Odoo Tests Helpers and Macros " " Hy Odoo Tests Helpers and Macros "
(require [hy-odoo.macros.general [if-python2]])
(defmacro o-assert-equal [left right] `(.assertEqual self ~left ~right)) (defmacro o-assert-equal [left right] `(.assertEqual self ~left ~right))
(defmacro o-assert-list-equal [left right] `(.assertListEqual self ~left ~right)) (defmacro o-assert-list-equal [left right] `(.assertListEqual self ~left ~right))
(defmacro o-assert-dict-equal [left right] `(.assertDictEqual self ~left ~right)) (defmacro o-assert-dict-equal [left right] `(.assertDictEqual self ~left ~right))
@ -34,10 +32,7 @@
(defmacro o-assert-is-instance [left right] `(.assertIsInstance self ~left ~right)) (defmacro o-assert-is-instance [left right] `(.assertIsInstance self ~left ~right))
(defmacro o-assert-not-is-instance [left right] `(.assertNotIsInstance self ~left ~right)) (defmacro o-assert-not-is-instance [left right] `(.assertNotIsInstance self ~left ~right))
(defmacro o-assert-raises [Error] `(.assertRaises self ~Error)) (defmacro o-assert-raises [Error] `(.assertRaises self ~Error))
(defmacro o-assert-raises-regex [Error regexp] (defmacro o-assert-raises-regex [Error regexp] `(.assertRaisesRegex self ~Error ~regexp))
(if-python2
`(.assertRaisesRegexp self ~Error ~regexp)
`(.assertRaisesRegex self ~Error ~regexp)))
(defmacro odo-assert-raises [Error body] (defmacro odo-assert-raises [Error body]
"Macro to test Error with self.assertRaises and do block" "Macro to test Error with self.assertRaises and do block"

View File

@ -1,6 +1,6 @@
;; -*- coding: utf-8 -*- ;; -*- coding: utf-8 -*-
;; ;;
;; Copyright 2019-2021 Fabien Bourgeois <fabien@yaltik.com> ;; Copyright 2019-2022 Fabien Bourgeois <fabien@yaltik.com>
;; ;;
;; This program is free software: you can redistribute it and/or modify ;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as ;; it under the terms of the GNU Affero General Public License as
@ -17,18 +17,17 @@
" Odoo macros and helpers " " Odoo macros and helpers "
(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]])
; Global helpers ; Global helpers
(defn strdm [hy-domain] (defn strdm [hy-domain]
"Generate Odoo domain from Hy like tuple domain" "Generate Odoo domain from Hy like tuple domain"
(do (do
(setv (, op field value) hy-domain (setv #(op field value) hy-domain
field (mangle field) field (mangle field)
value (if (string? value) f"'{value}'" value)) value (when (string? value) f"'{value}'" value))
(return f"('{field}', '{op}', {value})"))) (return f"('{field}', '{op}', {value})")))
; XML helpers functions and macros ; XML helpers functions and macros
@ -152,11 +151,11 @@
(defn modulehytopy [module-path hy-files] (defn modulehytopy [module-path hy-files]
"Transforms hy to py for translation purpose" "Transforms hy to py for translation purpose"
(import astor) (import astor)
(import [os.path [dirname]] (import os.path [dirname]
[io [open :as iopen]] io [open :as iopen]
[hy.lex [hy-parse]] hy.lex [hy-parse]
[hy.compiler [hy-compile]] hy.compiler [hy-compile]
[hy.errors [filtered-hy-exceptions]]) hy.errors [filtered-hy-exceptions])
(defn hytopy [source path] (defn hytopy [source path]
"Hy source to Py source" "Hy source to Py source"
@ -165,14 +164,12 @@
(.to-source (. astor code-gen) -ast)) (.to-source (. astor code-gen) -ast))
(for [hy-file hy-files] (for [hy-file hy-files]
(setv hy-path (% "%s/%s.hy" (, (dirname module-path) hy-file)) (setv hy-path (% "%s/%s.hy" #((dirname module-path) hy-file))
hy-source (with [o (iopen hy-path "r" :encoding "utf-8")] (.read o)) hy-source (with [o (iopen hy-path "r" :encoding "utf-8")] (.read o))
output-path (.replace hy-path ".hy" ".py") output-path (.replace hy-path ".hy" ".py")
content ["# Generate from Hy AST, for Babel translation purpose only." content ["# Generate from Hy AST, for Babel translation purpose only."
"# For real source code, please see and use HY source." "# For real source code, please see and use HY source."
(hytopy hy-source hy-path)]) (hytopy hy-source hy-path)])
(if-python2
(.insert content 0 "# -*- coding: utf-8 -*-") (continue))
(setv output-py (.join "\n" content)) (setv output-py (.join "\n" content))
(with [f (iopen output-path "w")] (.write f output-py)))) (with [f (iopen output-path "w")] (.write f output-py))))

View File

@ -1,6 +1,6 @@
;; -*- coding: utf-8 -*- ;; -*- coding: utf-8 -*-
;; ;;
;; Copyright 2019-2021 Fabien Bourgeois <fabien@yaltik.com> ;; Copyright 2019-2022 Fabien Bourgeois <fabien@yaltik.com>
;; ;;
;; This program is free software: you can redistribute it and/or modify ;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as ;; it under the terms of the GNU Affero General Public License as
@ -15,20 +15,23 @@
;; You should have received a copy of the GNU Affero General Public License ;; 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/>. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
b" XML Helpers tests " " XML Helpers tests "
(require
hyrule.control [defmain]
hy-odoo.macros.test *)
(require [hy-odoo.macros.test [*]])
(import unittest (import unittest
[functools [partial]] functools [partial]
[xml.etree.ElementTree :as ET] xml.etree.ElementTree :as ET
[os [unlink]] os [unlink]
[hy-odoo.xml-base [XMLDictElement xmln xmlroot xmlchild xml-write]]) hy-odoo.xml-base [XMLDictElement xmln xmlroot xmlchild xml-write])
(defclass TextXMLBase [(. unittest TestCase)] (defclass TextXMLBase [(. unittest TestCase)]
b"XML Helpers tests" "XML Helpers tests"
(defn test-xmln [self] (defn test-xmln [self]
b"Text xmln" "Text xmln"
; XMLDictElement ; XMLDictElement
(o-assert-is-instance (xmln) XMLDictElement) (o-assert-is-instance (xmln) XMLDictElement)
; Tags ; Tags
@ -54,7 +57,7 @@ b" XML Helpers tests "
(o-assert-is-instance element XMLDictElement)) (o-assert-is-instance element XMLDictElement))
(defn test-xmlchild [self] (defn test-xmlchild [self]
b"Test xmlchild" "Test xmlchild"
(setv parent (xmlroot {"tag" "root" "attrs" {} "children" []}) (setv parent (xmlroot {"tag" "root" "attrs" {} "children" []})
xmlc-par (partial xmlchild parent)) xmlc-par (partial xmlchild parent))
@ -68,33 +71,33 @@ b" XML Helpers tests "
(o-assert-equal (. parent text) "some text") (o-assert-equal (. parent text) "some text")
(xmlc-par[(xmln "t" {"a" "b"} [])]) (xmlc-par[(xmln "t" {"a" "b"} [])])
(setv child (.next (.iter parent "t"))) (setv child (next (.iter parent "t")))
(o-assert-equal (. child tag) "t") (o-assert-equal (. child tag) "t")
(o-assert-dict-equal (. child attrib) {"a" "b"}) (o-assert-dict-equal (. child attrib) {"a" "b"})
(o-assert-list-equal (list child) []) (o-assert-list-equal (list child) [])
(xmlc-par [(xmln "t2" {1 2} [])]) (xmlc-par [(xmln "t2" {1 2} [])])
(setv child (.next (.iter parent "t2"))) (setv child (next (.iter parent "t2")))
(o-assert-dict-equal (. child attrib) {"1" "2"}) (o-assert-dict-equal (. child attrib) {"1" "2"})
(xmlc-par [(xmln "tchildren" {} [(xmln "subchild" {} [])])]) (xmlc-par [(xmln "tchildren" {} [(xmln "subchild" {} [])])])
(setv child (.next (.iter parent "tchildren")) (setv child (next (.iter parent "tchildren"))
subchildren (list child)) subchildren (list child))
(o-assert-equal (len subchildren) 1) (o-assert-equal (len subchildren) 1)
(o-assert-equal (. (first subchildren) tag) "subchild")) (o-assert-equal (. (get subchildren 0) tag) "subchild"))
(defn test-xmlroot [self] (defn test-xmlroot [self]
b"Test xmlroot" "Test xmlroot"
(setv root (xmlroot {"tag" "root" "attrs" {} "children" []})) (setv root (xmlroot {"tag" "root" "attrs" {} "children" []}))
(o-assert-is-instance root (. ET Element)) (o-assert-is-instance root (. ET Element))
(with [(o-assert-raises-regex TypeError "has no attribute")] (xmlroot False)) (with [(o-assert-raises-regex TypeError "is not subscriptable")] (xmlroot False))
(with [(o-assert-raises-regex KeyError "tag")] (xmlroot {})) (with [(o-assert-raises-regex KeyError "tag")] (xmlroot {}))
(with [(o-assert-raises-regex KeyError "attrs")] (xmlroot {"tag" "root"}))) (with [(o-assert-raises-regex KeyError "attrs")] (xmlroot {"tag" "root"})))
(defn test-xml-write [self] (defn test-xml-write [self]
b"Test xml-write" "Test xml-write"
(setv children [(xmln "child1" {"attr" "value"} []) (setv children [(xmln "child1" {"attr" "value"} [])
(xmln "child2" {} "Some text")] (xmln "child2" {} "Some text")]
@ -103,13 +106,13 @@ b" XML Helpers tests "
(o-assert-is-none (xmlw "/badpath")) (o-assert-is-none (xmlw "/badpath"))
(o-assert-is-none (xmlw "/bad.ext")) (o-assert-is-none (xmlw "/bad.ext"))
(xmlw --file--) (xmlw __file__)
(setv filepath (.replace --file-- ".hy" "_views.xml")) (setv filepath (.replace __file__ ".hy" "_views.xml"))
(with [output-file (open filepath "r")] (with [output-file (open filepath "r")]
(setv output-xml (.read output-file)) (setv output-xml (.read output-file))
(o-assert-in "<?xml version" output-xml) (o-assert-in "<?xml version" output-xml)
(o-assert-in "<root>" output-xml) (o-assert-in "<root>" output-xml)
(o-assert-in "<child1 attr=\"value\"/>" output-xml) (o-assert-in "<child1 attr=\"value\" />" output-xml)
(o-assert-in "<child2>Some text</child2>" output-xml) (o-assert-in "<child2>Some text</child2>" output-xml)
(unlink filepath)))) (unlink filepath))))

View File

@ -1,6 +1,6 @@
;; -*- coding: utf-8 -*- ;; -*- coding: utf-8 -*-
;; ;;
;; Copyright 2019-2021 Fabien Bourgeois <fabien@yaltik.com> ;; Copyright 2019-2022 Fabien Bourgeois <fabien@yaltik.com>
;; ;;
;; This program is free software: you can redistribute it and/or modify ;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as ;; it under the terms of the GNU Affero General Public License as
@ -17,12 +17,12 @@
" XML helpers and macros " " XML helpers and macros "
(require [hy-odoo.macros.general [if-python2 ustr-cast]]) (require hy-odoo.macros.general [instance?]
(require [hy-odoo.macros.odoo [pdb]]) hy-odoo.macros.odoo [pdb])
(import [collections [namedtuple]] (import collections [namedtuple]
[functools [partial]] functools [partial]
[os [path]] os [path]
[xml.etree.ElementTree :as ET]) xml.etree.ElementTree :as ET)
;; Types ;; Types
@ -40,21 +40,21 @@
(defn xmlchild [parent children] (defn xmlchild [parent children]
"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 (instance? str 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)) [(str k) (str 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"))))
(defn xmln [&optional [tag ""] [attrs {}] [children []]] (defn xmln [[tag ""] [attrs {}] [children []]]
"XMLDictElement building from dict object, with defaults" "XMLDictElement building from dict object, with defaults"
(when (instance? list attrs) (setv children attrs attrs {})) (when (instance? list attrs) (setv children attrs attrs {}))
(setv xmldictel (partial XMLDictElement tag attrs) (setv xmldictel (partial XMLDictElement tag attrs)
inst-str? (if-python2 (instance? unicode children) (instance? str children))) inst-str? (instance? str children))
(when inst-str? (return (xmldictel [children]))) (when inst-str? (return (xmldictel [children])))
(when (instance? list children) (return (xmldictel children))) (when (instance? list children) (return (xmldictel children)))
(raise (TypeError "Invalid arguments for xmln"))) (raise (TypeError "Invalid arguments for xmln")))
@ -62,14 +62,11 @@
(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 ".hy") (when (.endswith filepath ".hy")
(if-python2 (setv output-xml (.decode (.tostring ET
(do tree
(import [xml.etree.ElementTree :as ET] :xml_declaration True
[xml.dom [minidom]]) :encoding "utf-8") "utf-8")
(setv output-xml (.toprettyxml :indent " " output-path (.split (.abspath path filepath) "/")
(.parseString minidom (.tostring ET tree))))) (get output-path -1) (.replace (get output-path -1) ".hy" "_views.xml")
(setv output-xml (.decode (.tostring ET tree) "utf-8")))
(setv output-path (.split (.abspath path filepath) "/")
(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))))

View File

@ -7,7 +7,7 @@ setup(
package_data={ package_data={
'hy_odoo': ['*.hy'], 'hy_odoo': ['*.hy'],
}, },
install_requires=["hy==0.24.*"], install_requires=["hy==0.24.*", "hyrule==0.2.*"],
author="Fabien Bourgeois", author="Fabien Bourgeois",
author_email="fabien@yaltik.com", author_email="fabien@yaltik.com",
description="HY functions and macros for Odoo", description="HY functions and macros for Odoo",