Commit 3ca551bd authored by Alexandre Richardson's avatar Alexandre Richardson
Browse files

[mboximport] refactor mboximport to make it easier to subclass / customize (closes #2438204)


The following three main steps are now implemented in three separate
methods:

- database connection: self.connect(appid)
- importer creation: self.create_importer(cnx)
- mbox file import: self.import_mbox_files(importer, filenames)
parent d4f490ae5dbe
......@@ -40,34 +40,52 @@ class MBOXImportCommand(Command):
'help': 'skip email signature.',
}),
)
autocommit = True
def run(self, args):
"""run the command with its specific arguments"""
appid = args.pop(0)
def import_mbox_files(self, importer, filenames):
"""process `filenames` with `importer` and create corresponding
Email / EmailThread / etc. objects in the database.
"""
for fpath in filenames:
if fpath == '-':
stream = StringIO(sys.stdin.read())
else:
stream = open(fpath)
importer.import_mbox_stream(stream)
if importer.error:
print 'failed to import the following messages:'
print '\n'.join(importer.error)
sys.exit(1)
def connect(self, appid):
"""create a connection to `appid`"""
cnx = config_connect(appid, self.config)
cnx.load_appobjects(cubes=None, subpath=('entities',))
importer = MBOXImporter(cnx, verbose=True,
interactive=self.config.interactive,
skipsign=self.config.skip_sign)
# set autocommit, add an option to control that if needed
importer.autocommit_mode()
return cnx
def create_importer(self, cnx):
"""factory to instantiate the mbox importer"""
return MBOXImporter(cnx, verbose=True,
interactive=self.config.interactive,
skipsign=self.config.skip_sign,
autocommit=self.autocommit)
def run(self, args):
"""run the command with its specific arguments"""
cnx = self.connect(args.pop(0))
importer = self.create_importer(cnx)
try:
for fpath in args:
if fpath == '-':
stream = StringIO(sys.stdin.read())
else:
stream = open(fpath)
importer.import_mbox_stream(stream)
if importer.error:
print 'failed to import the following messages:'
print '\n'.join(importer.error)
sys.exit(1)
self.import_mbox_files(importer, args)
except:
# without a correct connection handling we exhaust repository's
# connections pool.
# the repository should be more resilient against bad clients !
cnx.rollback()
cnx.close()
raise
if not self.autocommit:
cnx.commit()
cnx.close()
CWCTL.register(MBOXImportCommand)
......@@ -36,7 +36,8 @@ def combinaison(list):
class MBOXImporter(object):
"""import content of a Unix mailbox into cubicweb as Email (and related) objects"""
def __init__(self, cnx, verbose=False, interactive=False, skipsign=False):
def __init__(self, cnx, verbose=False, interactive=False,
skipsign=False, autocommit=False):
self.cnx = cnx
self.schema = cnx.get_schema()
self.req = cnx.request()
......@@ -47,7 +48,7 @@ class MBOXImporter(object):
self.created = {}
self.skipped = []
self.error = []
self.autocommit = False
self.autocommit = autocommit
def autocommit_mode(self):
self.autocommit = True
......
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