diff --git a/ChangeLog b/ChangeLog index 325e7929..88133581 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2025-01-13 Jan Kotanski + * add support for utf8 in xmla attribute/field values (#174) + * add support for utf8 in py2 (#176) + * tagged as 3.13.0 + 2024-09-27 Jan Kotanski * remove NXcollection attribute for nexus_logs (#168) * tagged as 3.12.0 diff --git a/man/nxswriter.1 b/man/nxswriter.1 index a8ae3d46..0e2e2974 100644 --- a/man/nxswriter.1 +++ b/man/nxswriter.1 @@ -27,7 +27,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. -.TH "NXSWRITER" "1" "Nov 07, 2024" "3.12" "NXSDataWriter" +.TH "NXSWRITER" "1" "Jan 13, 2025" "3.13" "NXSDataWriter" .SH NAME nxswriter \- nxswriter Documentation .sp diff --git a/nxswriter/FElement.py b/nxswriter/FElement.py index 409b3736..f9638aa4 100644 --- a/nxswriter/FElement.py +++ b/nxswriter/FElement.py @@ -195,7 +195,7 @@ def _findShape(self, rank, lengths=None, extraD=False, if sys.version_info > (3,): val = ("".join(self.content)).strip() else: - val = ("".join(self.content)).strip().encode() + val = ("".join(self.content)).strip().encode("utf8") found = False if checkData and self.source and self.source.isValid(): data = self.source.getData() @@ -497,9 +497,12 @@ def _setAttributes(self, excluded=None): ekey, "string", overwrite=True)[...] \ = self._tagAttrs[key].strip() else: + try: + vl = self._tagAttrs[key].strip().encode() + except Exception: + vl = self._tagAttrs[key].strip().encode("utf8") self.h5Object.attributes.create( - ekey, "string", overwrite=True)[...] \ - = self._tagAttrs[key].strip().encode() + ekey, "string", overwrite=True)[...] = vl def h5Attribute(self, name): """ provides attribute h5 object diff --git a/nxswriter/Release.py b/nxswriter/Release.py index 7a1f52d0..0d96fe35 100644 --- a/nxswriter/Release.py +++ b/nxswriter/Release.py @@ -21,4 +21,4 @@ #: package version -__version__ = "3.12.0" +__version__ = "3.13.0" diff --git a/nxswriter/TangoDataWriter.py b/nxswriter/TangoDataWriter.py index 3294ae11..364f4207 100644 --- a/nxswriter/TangoDataWriter.py +++ b/nxswriter/TangoDataWriter.py @@ -71,6 +71,7 @@ #: (:obj:`bool`) tango bug #213 flag related to EncodedAttributes in python3 PYTG_BUG_213 = False if sys.version_info > (3,): + unicode = str try: try: import tango @@ -315,9 +316,12 @@ def __setXML(self, xmlset): """ self.__fetcher = FetchNameHandler(streams=self._streams) if sys.version_info > (3,): - sax.parseString(bytes(xmlset, 'utf-8'), self.__fetcher) - else: sax.parseString(xmlset, self.__fetcher) + else: + if isinstance(xmlset, unicode): + sax.parseString(xmlset.encode('utf-8'), self.__fetcher) + else: + sax.parseString(xmlset, self.__fetcher) self.__xmlsettings = xmlset def __delXML(self): @@ -504,7 +508,15 @@ def openEntry(self): parser.setContentHandler(handler) parser.setErrorHandler(errorHandler) inpsrc = sax.InputSource() - inpsrc.setByteStream(StringIO(self.xmlsettings)) + if sys.version_info > (3,): + inpsrc.setByteStream(StringIO(self.xmlsettings)) + else: + if isinstance(self.xmlsettings, unicode): + inpsrc.setByteStream( + StringIO(self.xmlsettings.encode('utf-8'))) + else: + inpsrc.setByteStream(StringIO(self.xmlsettings)) + parser.parse(inpsrc) self.__initPool = handler.initPool diff --git a/test/TangoDataWriterH5Cpp_test.py b/test/TangoDataWriterH5Cpp_test.py index 41ad170a..ae0150b9 100644 --- a/test/TangoDataWriterH5Cpp_test.py +++ b/test/TangoDataWriterH5Cpp_test.py @@ -50,11 +50,11 @@ class TangoDataWriterH5CppTest(unittest.TestCase): def __init__(self, methodName): unittest.TestCase.__init__(self, methodName) - self._scanXmlpart = """ + self._scanXmlpart = u""" scan instrument - + @@ -1683,7 +1683,7 @@ def test_scanRecord_nexuspath(self): self.assertEqual(at.shape, ()) self.assertEqual(at.dtype, "string") self.assertEqual(at.name, "units") - self.assertEqual(at[...], "m") + self.assertEqual(at[...], u"\u03bcm") at = cnt.attributes["nexdatas_source"] self.assertTrue(at.is_valid) @@ -1749,7 +1749,7 @@ def test_scanRecord_nexuspath(self): if os.path.isfile(fname): os.remove(fname) -# pass + # pass # scanRecord test # \brief It tests recording of simple h5 file diff --git a/test/TangoDataWriterH5PY_test.py b/test/TangoDataWriterH5PY_test.py index 8fe5afdc..895a57c0 100644 --- a/test/TangoDataWriterH5PY_test.py +++ b/test/TangoDataWriterH5PY_test.py @@ -50,11 +50,11 @@ class TangoDataWriterH5PYTest(unittest.TestCase): def __init__(self, methodName): unittest.TestCase.__init__(self, methodName) - self._scanXmlpart = """ + self._scanXmlpart = u""" scan instrument - + @@ -1681,7 +1681,7 @@ def test_scanRecord_nexuspath(self): self.assertEqual(at.shape, ()) self.assertEqual(at.dtype, "string") self.assertEqual(at.name, "units") - self.assertEqual(at[...], "m") + self.assertEqual(at[...], u"\u03bcm") at = cnt.attributes["nexdatas_source"] self.assertTrue(at.is_valid) @@ -1747,7 +1747,7 @@ def test_scanRecord_nexuspath(self): if os.path.isfile(fname): os.remove(fname) -# pass + # pass # scanRecord test # \brief It tests recording of simple h5 file