From d2fd9703cb09d8ed80e53c7456f0b13dda318b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com> Date: Thu, 27 Sep 2007 18:15:03 +0000 Subject: [PATCH] Extend OOoParser so that it's possible to feed the parser with content as string, not only with a file object. git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@16678 20353a03-c40f-0410-a6d1-a30d3c3de9de --- product/ERP5OOo/OOoUtils.py | 4 ++++ product/ERP5OOo/tests/testOOoImport.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/product/ERP5OOo/OOoUtils.py b/product/ERP5OOo/OOoUtils.py index 2475028300..76514b2277 100644 --- a/product/ERP5OOo/OOoUtils.py +++ b/product/ERP5OOo/OOoUtils.py @@ -236,6 +236,10 @@ class OOoParser: self.ns = {} self.filename = None + security.declareProtected(Permissions.ImportExportObjects, 'openFromString') + def openFromString(self, text_content): + return self.openFile(StringIO(text_content)) + security.declareProtected(Permissions.ImportExportObjects, 'openFile') def openFile(self, file_descriptor): """ diff --git a/product/ERP5OOo/tests/testOOoImport.py b/product/ERP5OOo/tests/testOOoImport.py index 0c28de8c9a..036588b44e 100644 --- a/product/ERP5OOo/tests/testOOoImport.py +++ b/product/ERP5OOo/tests/testOOoImport.py @@ -36,6 +36,7 @@ from Testing import ZopeTestCase from AccessControl.SecurityManagement import newSecurityManager from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.Sequence import SequenceList +from Products.ERP5OOo.OOoUtils import OOoParser ooodoc_coordinates = ('127.0.0.1', 8008) @@ -198,6 +199,28 @@ class TestOOoImport(ERP5TypeTestCase): self.assertEquals(1, france.getIntIndex()) + # simple OOoParser tests + def test_getSpreadSheetMapping(self): + parser = OOoParser() + parser.openFile(open(makeFilePath('import_data_list.ods'), 'rb')) + mapping = parser.getSpreadsheetsMapping() + self.assertEquals(['Person'], mapping.keys()) + person_mapping = mapping['Person'] + self.assertTrue(isinstance(person_mapping, list)) + self.assertTrue(102, len(person_mapping)) + self.assertEquals(person_mapping[0], + ['Title', 'First Name', 'Last Name', 'Default Email Text']) + self.assertEquals(person_mapping[1], + ['John Doe 0', 'John', 'Doe 0', 'john.doe0@foo.com']) + + def test_openFromString(self): + parser = OOoParser() + parser.openFromString( + open(makeFilePath('import_data_list.ods'), 'rb').read()) + mapping = parser.getSpreadsheetsMapping() + self.assertEquals(['Person'], mapping.keys()) + + def test_suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestOOoImport)) -- 2.30.9