Commit 48e2e4fd authored by Jérome Perrin's avatar Jérome Perrin

OOoUtils: deprecate openFromString in favor of openFromBytes

Correct the naming because this takes a zip file content as argument, so
it expects bytes not string
parent 3f0a44d8
......@@ -3387,7 +3387,7 @@ class TestAccountingExport(AccountingTestCase):
form_id='AccountingTransaction_view')
from Products.ERP5OOo.OOoUtils import OOoParser
parser = OOoParser()
parser.openFromString(ods_data)
parser.openFromBytes(ods_data)
content_xml = parser.oo_files['content.xml']
# just make sure that we have the correct account name
self.assertEqual(
......
......@@ -20,11 +20,11 @@ def getSpreadsheet(file):
tmp_ooo.edit(data=file.read(), content_type=content_type)
tmp_ooo.convertToBaseFormat()
ignored, import_file_content = tmp_ooo.convert('ods')
ooo_parser.openFromString(str(import_file_content))
ooo_parser.openFromBytes(bytes(import_file_content))
else:
ooo_parser.openFile(file)
else:
ooo_parser.openFromString(file)
ooo_parser.openFromBytes(file)
return ooo_parser.getSpreadsheetsMapping()
......
......@@ -635,7 +635,7 @@ class TestInvoice(TestInvoiceMixin):
# the <draw:image> should not be present, because there's no logo
parser = OOoParser()
parser.openFromString(odt)
parser.openFromBytes(odt)
style_xml = parser.oo_files['styles.xml']
self.assertNotIn('<draw:image', style_xml)
......
......@@ -149,7 +149,7 @@ class TestOOoChart(TestOOoChartMixin):
from Products.ERP5OOo.OOoUtils import OOoParser
parser = OOoParser()
parser.openFromString(body)
parser.openFromBytes(body)
content_xml_view = parser.oo_files['content.xml']
doc_view = etree.fromstring(content_xml_view)
......@@ -243,7 +243,7 @@ class TestOOoChart(TestOOoChartMixin):
from Products.ERP5OOo.OOoUtils import OOoParser
parser = OOoParser()
parser.openFromString(body)
parser.openFromBytes(body)
content_xml_view = parser.oo_files['content.xml']
doc_view = etree.fromstring(content_xml_view)
......
......@@ -91,7 +91,7 @@ if not (content_type.startswith('application/vnd.sun.xml')
content_type=content_type)
tmp_ooo.convertToBaseFormat()
_, import_file_content = tmp_ooo.convert('ods')
parser.openFromString(str(import_file_content))
parser.openFromBytes(bytes(import_file_content))
else:
parser.openFile(import_file)
......
......@@ -49,6 +49,7 @@ from lxml import etree
from lxml.etree import Element, XMLSyntaxError
from copy import deepcopy
from warnings import warn
from Products.ERP5Type.Utils import deprecated
class CorruptedOOoFile(Exception): pass
......@@ -233,8 +234,9 @@ class OOoParser(Implicit):
self.pictures = {}
self.filename = None
def openFromString(self, text_content):
return self.openFile(BytesIO(text_content))
def openFromBytes(self, bytes_content):
return self.openFile(BytesIO(bytes_content))
openFromString = deprecated("openFromString is deprecated, use openFromBytes instead")(openFromBytes)
def openFile(self, file_descriptor):
"""
......
......@@ -52,16 +52,17 @@ class TestOOoParser(unittest.TestCase):
self.assertEqual(person_mapping[1],
['John Doe 0', 'John', 'Doe 0', 'john.doe0@foo.com'])
def test_openFromString(self):
def test_openFromBytes(self):
parser = OOoParser()
parser.openFromString(
open(makeFilePath('import_data_list.ods'), 'rb').read())
with open(makeFilePath('import_data_list.ods'), 'rb') as f:
parser.openFromBytes(f.read())
mapping = parser.getSpreadsheetsMapping()
self.assertEqual(['Person'], mapping.keys())
def test_getSpreadSheetMappingStyle(self):
parser = OOoParser()
parser.openFile(open(makeFilePath('import_data_list_with_style.ods'), 'rb'))
with open(makeFilePath('import_data_list_with_style.ods'), 'rb') as f:
parser.openFile(f)
mapping = parser.getSpreadsheetsMapping()
self.assertEqual(['Feuille1'], mapping.keys())
self.assertEqual(mapping['Feuille1'][1],
......@@ -75,7 +76,8 @@ class TestOOoParser(unittest.TestCase):
def test_getSpreadSheetMappingDataTypes(self):
parser = OOoParser()
parser.openFile(open(makeFilePath('import_data_list_data_type.ods'), 'rb'))
with open(makeFilePath('import_data_list_data_type.ods'), 'rb') as f:
parser.openFile(f)
mapping = parser.getSpreadsheetsMapping()
self.assertEqual(['Feuille1'], mapping.keys())
self.assertEqual(mapping['Feuille1'][0],
......
......@@ -598,9 +598,9 @@ class TestOOoStyle(ERP5TypeTestCase, ZopeTestCase.Functional):
# Is it good to do this only for ODT ?
from Products.ERP5OOo.OOoUtils import OOoParser
parser = OOoParser()
parser.openFromString(body)
parser.openFromBytes(body)
content_xml = parser.oo_files['content.xml']
self.assertIn('&lt;Escape&gt;&amp;<text:line-break/>newline', content_xml)
self.assertIn(b'&lt;Escape&gt;&amp;<text:line-break/>newline', content_xml)
def test_translation(self):
def gettext(message, **kw):
......@@ -632,7 +632,7 @@ class TestOOoStyle(ERP5TypeTestCase, ZopeTestCase.Functional):
from Products.ERP5OOo.OOoUtils import OOoParser
parser = OOoParser()
parser.openFromString(body)
parser.openFromBytes(body)
content_xml = parser.oo_files['content.xml']
self.assertIn(u'**àèüîó**', content_xml.decode('utf-8'))
......@@ -682,7 +682,7 @@ class TestOOoStyle(ERP5TypeTestCase, ZopeTestCase.Functional):
from Products.ERP5OOo.OOoUtils import OOoParser
parser = OOoParser()
parser.openFromString(body)
parser.openFromBytes(body)
content_xml = parser.oo_files['content.xml']
self.assertIn(u'**àèüîó**', content_xml.decode('utf-8'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment