From c18e598dfb1da22ab4e155688b7b22050dc45740 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Mon, 22 Feb 2021 01:16:16 +0100 Subject: [PATCH] [ADD][IMP]ustr casting macro --- hy_odoo/macros.hy | 4 ++++ hy_odoo/xml_base.hy | 6 ++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/hy_odoo/macros.hy b/hy_odoo/macros.hy index 9913cc4..9916485 100644 --- a/hy_odoo/macros.hy +++ b/hy_odoo/macros.hy @@ -24,3 +24,7 @@ (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))) diff --git a/hy_odoo/xml_base.hy b/hy_odoo/xml_base.hy index ce52eaf..b44415a 100644 --- a/hy_odoo/xml_base.hy +++ b/hy_odoo/xml_base.hy @@ -17,7 +17,7 @@ " XML helpers and macros " -(require [hy-odoo.macros [if-python2]]) +(require [hy-odoo.macros [if-python2 ustr-cast]]) (import [collections [namedtuple]] [functools [partial]] [os [path]] @@ -42,9 +42,7 @@ (cond [(string? children) (setv (. parent text) children)] [(instance? XMLDictElement children (do - (if-python2 - (setv attrs (dfor [k v] (.items (. children attrs)) [(unicode k) (unicode v)])) - (setv attrs (dfor [k v] (.items (. children attrs)) [(str k) (str v)]))) + (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)))]