Commit abbeea61 authored by Julien Cristau's avatar Julien Cristau
Browse files

Port mboximport and its tests to cubicweb 3.19

Require cubicweb 3.20 for cubicweb.utils.admincnx.
This means mboximport no longer works with a dbapi connection, so the
ccplugin is broken.
parent 7262f08ca328
......@@ -20,7 +20,7 @@ classifiers = [
]
# used packages
__depends__ = {'cubicweb': '>= 3.19.0',
__depends__ = {'cubicweb': '>= 3.20.0',
'cubicweb-file': '>= 1.9.0',
'logilab-common': '>= 0.58.3',
}
......
......@@ -10,7 +10,8 @@ import sys
from os.path import isdir
from cStringIO import StringIO
from cubicweb.toolsutils import CONNECT_OPTIONS, Command, config_connect
from cubicweb.toolsutils import CONNECT_OPTIONS, Command
from cubicweb.utils import admincnx
from cubicweb.cwctl import CWCTL
from cubes.email.mboximport import MBOXImporter
......@@ -62,8 +63,7 @@ class MBOXImportCommand(Command):
def connect(self, appid):
"""create a connection to `appid`"""
cnx = config_connect(appid, self.config)
cnx.load_appobjects(cubes=None, subpath=('entities',))
cnx = admincnx(appid)
return cnx
def create_importer(self, cnx):
......
......@@ -20,7 +20,7 @@ BuildArch: noarch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
BuildRequires: %{python} %{python}-setuptools
Requires: cubicweb >= 3.19.0
Requires: cubicweb >= 3.20.0
Requires: cubicweb-file >= 1.9.0
Requires: logilab-common >= 0.58.3
......
......@@ -9,7 +9,7 @@ Homepage: http://www.cubicweb.org/project/cubicweb-email
Package: cubicweb-email
Architecture: all
Depends: cubicweb-common (>= 3.19.0), cubicweb-file (>= 1.9.0)
Depends: cubicweb-common (>= 3.20.0), cubicweb-file (>= 1.9.0)
Description: email component for the CubicWeb framework
This CubicWeb component models email messages.
.
......
......@@ -43,8 +43,7 @@ class MBOXImporter(object):
skipsign=False, autocommit=False):
self.cnx = cnx
self.schema = cnx.get_schema()
self.req = cnx.request()
self.execute = self.req.execute
self.execute = cnx.execute
self._verbose = verbose
self._interactive = interactive
self._skipsign = skipsign
......@@ -106,7 +105,7 @@ class MBOXImporter(object):
subject = message.get('subject') or u'(no subject)'
sender = message.multi_addrs('from')[0]
sendereid = self.address_eid(sender[1], sender[0])
email = self.req.create_entity('Email', messageid=msgid,
email = self.cnx.create_entity('Email', messageid=msgid,
subject=subject, date=message.date(),
headers=message.headers(),
sender=sendereid)
......@@ -182,7 +181,7 @@ class MBOXImporter(object):
encoding = u'ascii'
if isinstance(data, str):
data = unicode(data, encoding)
self.req.set_shared_data('raw_content_%s_%s' %
self.cnx.set_shared_data('raw_content_%s_%s' %
(emaileid, self._part_index + 1),
str(part.message))
else:
......@@ -194,7 +193,7 @@ class MBOXImporter(object):
name = name or u'no name'
if isinstance(data, unicode):
data = data.encode('utf8')
epart = self.req.create_entity('File', data=Binary(data),
epart = self.cnx.create_entity('File', data=Binary(data),
data_name=name,
data_format=contenttype,
data_encoding=encoding)
......@@ -203,7 +202,7 @@ class MBOXImporter(object):
{'x': emaileid, 'y': epart.eid})
else:
self._part_index += 1
epart = self.req.create_entity('EmailPart',
epart = self.cnx.create_entity('EmailPart',
content=data,
content_format=contenttype,
ordernum=self._part_index)
......@@ -223,7 +222,7 @@ class MBOXImporter(object):
# create a new email address to link to
alias = alias or None
# XXX could try to link created address to a person
eaddress = self.req.create_entity('EmailAddress', address=address,
eaddress = self.cnx.create_entity('EmailAddress', address=address,
alias=alias)
self._notify_created('emailaddress', eaddress.eid)
return eaddress.eid
......@@ -241,7 +240,7 @@ class MBOXImporter(object):
{'title': subject})
if rset:
return rset[0][0]
thread = self.req.create_entity('EmailThread', title=subject)
thread = self.cnx.create_entity('EmailThread', title=subject)
self._notify_created('emailthread', thread.eid)
return thread.eid
......@@ -14,25 +14,27 @@ from cubes.email.mboximport import MBOXImporter
class MBOXImporterTC(CubicWebTC):
def test_mbox_format(self):
mi = MBOXImporter(self.cnx)
mi.import_mbox(self.datapath('mbox'))
self._test_base(mi)
with self.admin_access.client_cnx() as cnx:
mi = MBOXImporter(cnx)
mi.import_mbox(self.datapath('mbox'))
self._test_base(mi)
def test_maildir_format(self):
mi = MBOXImporter(self.cnx)
maildirpath = self.datapath('maildir')
newpath = os.path.join(maildirpath, 'new')
if not os.path.exists(newpath):
os.mkdir(newpath)
mi.import_maildir(maildirpath)
self._test_base(mi)
with self.admin_access.client_cnx() as cnx:
mi = MBOXImporter(cnx)
maildirpath = self.datapath('maildir')
newpath = os.path.join(maildirpath, 'new')
if not os.path.exists(newpath):
os.mkdir(newpath)
mi.import_maildir(maildirpath)
self._test_base(mi)
def _test_base(self, mi):
self.assertEqual(sorted([(x, len(y)) for x, y in mi.created.items()]),
[('email', 2), ('emailaddress', 4),
('emailpart', 5), ('emailthread', 2), ('file', 2)])
self.assertEqual(mi.skipped, [])
rset = self.execute('Any X ORDERBY S WHERE X is Email, X subject S')
rset = mi.cnx.execute('Any X ORDERBY S WHERE X is Email, X subject S')
self.assertEqual(len(rset), 2)
email = rset.get_entity(0, 0)
self.assertEqual(email.subject, 'Re: [Python-projects] Pylint: Disable-msg for a block or statement?')
......@@ -44,7 +46,7 @@ class MBOXImporterTC(CubicWebTC):
self.assertEqual(sorted([(f.data_name, f.data_format) for f in email.attachment]),
[(u'astng.patch', u'text/x-diff'), (u'pylint.patch', u'text/x-diff')])
assert not self.vreg.schema['parts'].inlined
self.assertEqual(len(self.execute('Any P WHERE E parts P, E eid %s'%email.eid)), 2)
self.assertEqual(len(mi.cnx.execute('Any P WHERE E parts P, E eid %s'%email.eid)), 2)
self.assertEqual(len(email.parts), 2)
part1, part2 = email.parts_in_order()
self.assertEqual(part1.content_format,'text/plain')
......@@ -188,41 +190,44 @@ http://lists.logilab.org/mailman/listinfo/python-projects
"""
def test_no_subject(self):
mi = MBOXImporter(self.cnx)
mi.import_mbox_stream(StringIO(self.NOSUBJECT))
rset = self.execute('Any X ORDERBY S WHERE X is Email, X subject S')
self.assertEqual(len(rset), 1)
email = rset.get_entity(0, 0)
self.assertEqual(email.subject, '(no subject)')
self.assertEqual(email.references(), set(('<xxx@blabla>',)))
with self.admin_access.client_cnx() as cnx:
mi = MBOXImporter(cnx)
mi.import_mbox_stream(StringIO(self.NOSUBJECT))
rset = cnx.execute('Any X ORDERBY S WHERE X is Email, X subject S')
self.assertEqual(len(rset), 1)
email = rset.get_entity(0, 0)
self.assertEqual(email.subject, '(no subject)')
self.assertEqual(email.references(), set(('<xxx@blabla>',)))
def test_double_import(self):
mi = MBOXImporter(self.cnx)
mi.import_mbox_stream(StringIO(self.NOSUBJECT))
mi.import_mbox_stream(StringIO(self.NOSUBJECT))
rset = self.execute('Any X ORDERBY S WHERE X is Email, X subject S')
self.assertEqual(len(rset), 1)
with self.admin_access.client_cnx() as cnx:
mi = MBOXImporter(cnx)
mi.import_mbox_stream(StringIO(self.NOSUBJECT))
mi.import_mbox_stream(StringIO(self.NOSUBJECT))
rset = cnx.execute('Any X ORDERBY S WHERE X is Email, X subject S')
self.assertEqual(len(rset), 1)
def test_address_detection(self):
# test both case normalization and canonicalization of email address
eid1 = self.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"')[0][0]
eid2 = self.execute('INSERT EmailAddress X: X address "maarten@philips.com"')[0][0]
self.execute('SET X prefered_form Y WHERE X eid %s, Y eid %s' % (eid1, eid2))
#self.assertEqual(env.vreg.schema['identical_to'].symetric, True)
#ea1 = cursor.execute('Any X WHERE X eid %s' % eid2).get_entity(0, None)
#self.assertEqual(ea1.canonical, True)
#testrset = cursor.execute('Any X WHERE X canonical FALSE')
#self.assertEqual(testrset[0][0], eid1)
#testrset = cursor.execute('Any X WHERE X canonical TRUE')
#self.assertEqual(testrset[0][0], eid2)
#testrset = cursor.execute('Any X WHERE X identical_to Y, X canonical TRUE, Y eid %(y)s', {'y': eid1})
#self.assertEqual(testrset[0][0], eid2)
mi = MBOXImporter(self.cnx)
mi.import_mbox_stream(StringIO(self.NOSUBJECT))
rset = self.execute('Any X ORDERBY S WHERE X is Email, X subject S')
self.assertEqual(len(rset), 1)
email = rset.get_entity(0, 0)
self.assertEqual(email.sender[0].address, 'maarten@philips.com')
with self.admin_access.client_cnx() as cnx:
# test both case normalization and canonicalization of email address
eid1 = cnx.execute('INSERT EmailAddress X: X address "maarten.ter.huurne@philips.com"')[0][0]
eid2 = cnx.execute('INSERT EmailAddress X: X address "maarten@philips.com"')[0][0]
cnx.execute('SET X prefered_form Y WHERE X eid %s, Y eid %s' % (eid1, eid2))
#self.assertEqual(env.vreg.schema['identical_to'].symetric, True)
#ea1 = cursor.execute('Any X WHERE X eid %s' % eid2).get_entity(0, None)
#self.assertEqual(ea1.canonical, True)
#testrset = cursor.execute('Any X WHERE X canonical FALSE')
#self.assertEqual(testrset[0][0], eid1)
#testrset = cursor.execute('Any X WHERE X canonical TRUE')
#self.assertEqual(testrset[0][0], eid2)
#testrset = cursor.execute('Any X WHERE X identical_to Y, X canonical TRUE, Y eid %(y)s', {'y': eid1})
#self.assertEqual(testrset[0][0], eid2)
mi = MBOXImporter(cnx)
mi.import_mbox_stream(StringIO(self.NOSUBJECT))
rset = cnx.execute('Any X ORDERBY S WHERE X is Email, X subject S')
self.assertEqual(len(rset), 1)
email = rset.get_entity(0, 0)
self.assertEqual(email.sender[0].address, 'maarten@philips.com')
if __name__ == '__main__':
......
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