From 5e56a16aa3ce9b578fa1a257d53cd777a5aae2fd Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Tue, 1 Nov 2022 17:49:50 +0100 Subject: [PATCH] [REF][UPD]Hy Odoo to Hy 0.24.0 --- Makefile | 3 +- hy_odoo/__init__.py | 4 +-- hy_odoo/macros/general.hy | 14 ++-------- hy_odoo/macros/odoo.hy | 19 ++++++------- hy_odoo/macros/test.hy | 9 ++---- hy_odoo/odoo.hy | 25 ++++++++--------- hy_odoo/tests/test_xml_base.hy | 43 +++++++++++++++------------- hy_odoo/xml_base.hy | 51 ++++++++++++++++------------------ setup.py | 2 +- 9 files changed, 75 insertions(+), 95 deletions(-) diff --git a/Makefile b/Makefile index e8f49dc..6bd84b8 100644 --- a/Makefile +++ b/Makefile @@ -5,5 +5,4 @@ clean: .PHONY: test test: - hy2 hy_odoo/tests/test_xml_base.hy - hy2 hy_odoo/tests/test_odoo.hy + hy hy_odoo/tests/test_xml_base.hy diff --git a/hy_odoo/__init__.py b/hy_odoo/__init__.py index ead10b5..1f8d69c 100644 --- a/hy_odoo/__init__.py +++ b/hy_odoo/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2020-2021 Fabien Bourgeois +# Copyright 2020-2022 Fabien Bourgeois # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -16,4 +16,4 @@ # along with this program. If not, see . import hy -import odoo, xml_base, utils +from . import odoo, xml_base, utils diff --git a/hy_odoo/macros/general.hy b/hy_odoo/macros/general.hy index f26a543..cb3ada9 100644 --- a/hy_odoo/macros/general.hy +++ b/hy_odoo/macros/general.hy @@ -1,6 +1,6 @@ ;; -*- coding: utf-8 -*- ;; -;; Copyright 2021 Fabien Bourgeois +;; Copyright 2021-2022 Fabien Bourgeois ;; ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU Affero General Public License as @@ -17,17 +17,7 @@ " Hy General 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 ustr-cast [s] - "If python2, return unicode casting, else str casting" - (if-python2 `(unicode ~s) `(str ~s))) +(defmacro instance? [type record] `(isinstance ~record ~type)) (defmacro hydict [dic] "Generate dict with mangled keys, from HyDict list" diff --git a/hy_odoo/macros/odoo.hy b/hy_odoo/macros/odoo.hy index f1a84f4..2236214 100644 --- a/hy_odoo/macros/odoo.hy +++ b/hy_odoo/macros/odoo.hy @@ -27,19 +27,18 @@ (setv op (second hy-domain) field (mangle (nth hy-domain 2)) value (nth hy-domain 3)) - `(, ~field ~op ~value)) + `#(~field ~op ~value)) ; 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/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] + `(defn [(.depends api ~@dependencies)] ~(HySymbol fname) [self] + ~descr + ~body))) (defmacro compute-field [fname body] "Takes fname Symbol and body to create computed field" diff --git a/hy_odoo/macros/test.hy b/hy_odoo/macros/test.hy index 9396b28..1094629 100644 --- a/hy_odoo/macros/test.hy +++ b/hy_odoo/macros/test.hy @@ -1,6 +1,6 @@ ;; -*- coding: utf-8 -*- ;; -;; Copyright 2021 Fabien Bourgeois +;; Copyright 2021-2022 Fabien Bourgeois ;; ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU Affero General Public License as @@ -17,8 +17,6 @@ " 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-list-equal [left right] `(.assertListEqual 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-not-is-instance [left right] `(.assertNotIsInstance self ~left ~right)) (defmacro o-assert-raises [Error] `(.assertRaises self ~Error)) -(defmacro o-assert-raises-regex [Error regexp] - (if-python2 - `(.assertRaisesRegexp self ~Error ~regexp) - `(.assertRaisesRegex self ~Error ~regexp))) +(defmacro o-assert-raises-regex [Error regexp] `(.assertRaisesRegex self ~Error ~regexp)) (defmacro odo-assert-raises [Error body] "Macro to test Error with self.assertRaises and do block" diff --git a/hy_odoo/odoo.hy b/hy_odoo/odoo.hy index e6551b4..5d3a869 100644 --- a/hy_odoo/odoo.hy +++ b/hy_odoo/odoo.hy @@ -1,6 +1,6 @@ ;; -*- coding: utf-8 -*- ;; -;; Copyright 2019-2021 Fabien Bourgeois +;; Copyright 2019-2022 Fabien Bourgeois ;; ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU Affero General Public License as @@ -17,18 +17,17 @@ " Odoo macros and helpers " -(require [hy-odoo.macros.general [if-python2]]) -(import [os [path]] - [hy-odoo.xml_base [xmlroot xmln]]) +(import os [path] + hy-odoo.xml_base [xmlroot xmln]) ; Global helpers (defn strdm [hy-domain] "Generate Odoo domain from Hy like tuple domain" (do - (setv (, op field value) hy-domain + (setv #(op field value) hy-domain field (mangle field) - value (if (string? value) f"'{value}'" value)) + value (when (string? value) f"'{value}'" value)) (return f"('{field}', '{op}', {value})"))) ; XML helpers functions and macros @@ -152,11 +151,11 @@ (defn modulehytopy [module-path hy-files] "Transforms hy to py for translation purpose" (import astor) - (import [os.path [dirname]] - [io [open :as iopen]] - [hy.lex [hy-parse]] - [hy.compiler [hy-compile]] - [hy.errors [filtered-hy-exceptions]]) + (import os.path [dirname] + io [open :as iopen] + hy.lex [hy-parse] + hy.compiler [hy-compile] + hy.errors [filtered-hy-exceptions]) (defn hytopy [source path] "Hy source to Py source" @@ -165,14 +164,12 @@ (.to-source (. astor code-gen) -ast)) (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)) output-path (.replace hy-path ".hy" ".py") content ["# Generate from Hy AST, for Babel translation purpose only." "# For real source code, please see and use HY source." (hytopy hy-source hy-path)]) - (if-python2 - (.insert content 0 "# -*- coding: utf-8 -*-") (continue)) (setv output-py (.join "\n" content)) (with [f (iopen output-path "w")] (.write f output-py)))) diff --git a/hy_odoo/tests/test_xml_base.hy b/hy_odoo/tests/test_xml_base.hy index 411f36d..2c8b08f 100644 --- a/hy_odoo/tests/test_xml_base.hy +++ b/hy_odoo/tests/test_xml_base.hy @@ -1,6 +1,6 @@ ;; -*- coding: utf-8 -*- ;; -;; Copyright 2019-2021 Fabien Bourgeois +;; Copyright 2019-2022 Fabien Bourgeois ;; ;; This program is free software: you can redistribute it and/or modify ;; 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 ;; along with this program. If not, see . -b" XML Helpers tests " +" XML Helpers tests " + +(require + hyrule.control [defmain] + hy-odoo.macros.test *) -(require [hy-odoo.macros.test [*]]) (import unittest - [functools [partial]] - [xml.etree.ElementTree :as ET] - [os [unlink]] - [hy-odoo.xml-base [XMLDictElement xmln xmlroot xmlchild xml-write]]) + functools [partial] + xml.etree.ElementTree :as ET + os [unlink] + hy-odoo.xml-base [XMLDictElement xmln xmlroot xmlchild xml-write]) (defclass TextXMLBase [(. unittest TestCase)] - b"XML Helpers tests" + "XML Helpers tests" (defn test-xmln [self] - b"Text xmln" + "Text xmln" ; XMLDictElement (o-assert-is-instance (xmln) XMLDictElement) ; Tags @@ -54,7 +57,7 @@ b" XML Helpers tests " (o-assert-is-instance element XMLDictElement)) (defn test-xmlchild [self] - b"Test xmlchild" + "Test xmlchild" (setv parent (xmlroot {"tag" "root" "attrs" {} "children" []}) xmlc-par (partial xmlchild parent)) @@ -68,33 +71,33 @@ b" XML Helpers tests " (o-assert-equal (. parent text) "some text") (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-dict-equal (. child attrib) {"a" "b"}) (o-assert-list-equal (list child) []) (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"}) (xmlc-par [(xmln "tchildren" {} [(xmln "subchild" {} [])])]) - (setv child (.next (.iter parent "tchildren")) + (setv child (next (.iter parent "tchildren")) subchildren (list child)) (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] - b"Test xmlroot" + "Test xmlroot" (setv root (xmlroot {"tag" "root" "attrs" {} "children" []})) (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 "attrs")] (xmlroot {"tag" "root"}))) (defn test-xml-write [self] - b"Test xml-write" + "Test xml-write" (setv children [(xmln "child1" {"attr" "value"} []) (xmln "child2" {} "Some text")] @@ -103,13 +106,13 @@ b" XML Helpers tests " (o-assert-is-none (xmlw "/badpath")) (o-assert-is-none (xmlw "/bad.ext")) - (xmlw --file--) - (setv filepath (.replace --file-- ".hy" "_views.xml")) + (xmlw __file__) + (setv filepath (.replace __file__ ".hy" "_views.xml")) (with [output-file (open filepath "r")] (setv output-xml (.read output-file)) (o-assert-in "" output-xml) - (o-assert-in "" output-xml) + (o-assert-in "" output-xml) (o-assert-in "Some text" output-xml) (unlink filepath)))) diff --git a/hy_odoo/xml_base.hy b/hy_odoo/xml_base.hy index 84cf7f1..629ae92 100644 --- a/hy_odoo/xml_base.hy +++ b/hy_odoo/xml_base.hy @@ -1,6 +1,6 @@ ;; -*- coding: utf-8 -*- ;; -;; Copyright 2019-2021 Fabien Bourgeois +;; Copyright 2019-2022 Fabien Bourgeois ;; ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU Affero General Public License as @@ -17,12 +17,12 @@ " XML helpers and macros " -(require [hy-odoo.macros.general [if-python2 ustr-cast]]) -(require [hy-odoo.macros.odoo [pdb]]) -(import [collections [namedtuple]] - [functools [partial]] - [os [path]] - [xml.etree.ElementTree :as ET]) +(require hy-odoo.macros.general [instance?] + hy-odoo.macros.odoo [pdb]) +(import collections [namedtuple] + functools [partial] + os [path] + xml.etree.ElementTree :as ET) ;; Types @@ -40,21 +40,21 @@ (defn xmlchild [parent children] "Handling of children (ie non root) XML Nodes with/o text and subchildren (recursive)" - (cond [(string? children) (setv (. parent text) children)] - [(instance? XMLDictElement children) - (do - (setv attrs (dfor [k v] (.items (. children attrs)) [(ustr-cast k) (ustr-cast v)])) - (setv new-parent (.SubElement ET parent (. children tag) attrs) - subchildren (. children children)) - (when subchildren) (xmlchild new-parent subchildren))] - [(instance? list children) (list( map (partial xmlchild parent) children))] - [True (raise (TypeError "Invalid arguments for xmlchild"))])) + (cond (instance? str children) (setv (. parent text) children) + (instance? XMLDictElement children) + (do + (setv attrs (dfor [k v] (.items (. children attrs)) [(str k) (str v)])) + (setv new-parent (.SubElement ET parent (. children tag) attrs) + subchildren (. children children)) + (when subchildren) (xmlchild new-parent subchildren)) + (instance? list children) (list( map (partial xmlchild parent) children)) + 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" (when (instance? list attrs) (setv children attrs 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 (instance? list children) (return (xmldictel children))) (raise (TypeError "Invalid arguments for xmln"))) @@ -62,14 +62,11 @@ (defn xml-write [filepath tree] "Write XML file according to filepath and given tree" (when (.endswith filepath ".hy") - (if-python2 - (do - (import [xml.etree.ElementTree :as ET] - [xml.dom [minidom]]) - (setv output-xml (.toprettyxml :indent " " - (.parseString minidom (.tostring ET tree))))) - (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")] + (setv output-xml (.decode (.tostring ET + tree + :xml_declaration True + :encoding "utf-8") "utf-8") + output-path (.split (.abspath path filepath) "/") + (get output-path -1) (.replace (get output-path -1) ".hy" "_views.xml") output-path (.join "/" output-path)) (with [output-file (open output-path "w")] (.write output-file output-xml)))) diff --git a/setup.py b/setup.py index 8a580ad..4bdb734 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup( package_data={ 'hy_odoo': ['*.hy'], }, - install_requires=["hy==0.24.*"], + install_requires=["hy==0.24.*", "hyrule==0.2.*"], author="Fabien Bourgeois", author_email="fabien@yaltik.com", description="HY functions and macros for Odoo",