diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-04-07 15:42:25 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-04-07 15:42:25 +0300 |
commit | a6c3c3e987b9762ec7dc0694526a6df6c88f8147 (patch) | |
tree | db09b69e700bf777a0f73cd4d3bd547c22286502 | |
parent | c81c3cf691057107b439ed61f1f0b0885055f061 (diff) | |
download | distix-a6c3c3e987b9762ec7dc0694526a6df6c88f8147.tar.gz |
refactor: add a context object
-rw-r--r-- | distixlib/plugins/import_mail_plugin.py | 58 |
1 files changed, 37 insertions, 21 deletions
diff --git a/distixlib/plugins/import_mail_plugin.py b/distixlib/plugins/import_mail_plugin.py index c6ba426..6e0a83a 100644 --- a/distixlib/plugins/import_mail_plugin.py +++ b/distixlib/plugins/import_mail_plugin.py @@ -58,15 +58,17 @@ class ImportMailPlugin(cliapp.Plugin): repo_dirname, mail_filename, keyvalue = self._parse_command_line(args) key, value = self._parse_keyvalue(keyvalue) msg = self._read_mail_message(mail_filename) - repo = distixlib.Repository(repo_dirname) - repo.require_clean_working_tree() - ticket_store = repo.open_ticket_store(distixlib.tickets_dir_name) - all_ticket_ids = ticket_store.get_ticket_ids() + + context = _ImportContext() + context.set_repo(distixlib.Repository(repo_dirname)) + context.repo.require_clean_working_tree() + + all_ticket_ids = context.store.get_ticket_ids() cache = _MessageIdCache() filenames = self._import_msg_to_ticket_store( - repo, ticket_store, all_ticket_ids, msg, cache, key, value) + context, all_ticket_ids, msg, cache, key, value) if filenames: - repo.commit_changes(filenames, self.commit_msg) + context.repo.commit_changes(filenames, self.commit_msg) def import_mbox(self, args): self._import_folder(args, mailbox.mbox) @@ -81,24 +83,27 @@ class ImportMailPlugin(cliapp.Plugin): args) key, value = self._parse_keyvalue(keyvalue) folder = folder_factory(folder_filename) - repo = distixlib.Repository(repo_dirname) - repo.require_clean_working_tree() - ticket_store = repo.open_ticket_store(distixlib.tickets_dir_name) - all_ticket_ids = ticket_store.get_ticket_ids() + + context = _ImportContext() + context.set_repo(distixlib.Repository(repo_dirname)) + context.repo.require_clean_working_tree() + + all_ticket_ids = context.store.get_ticket_ids() cache = _MessageIdCache() filenames = [] if self.app.settings['quiet']: progress = _QuietProgressReporter() else: progress = _MboxProgressReporter(len(folder)) + with contextlib.closing(folder), progress: for msg in folder: progress.next_msg() filenames += self._import_msg_to_ticket_store( - repo, ticket_store, all_ticket_ids, msg, cache, key, value) - filenames += ticket_store.save_changes() + context, all_ticket_ids, msg, cache, key, value) + filenames += context.store.save_changes() if filenames: - repo.commit_changes(filenames, self.commit_msg) + context.repo.commit_changes(filenames, self.commit_msg) def _parse_command_line(self, args): if len(args) == 2: @@ -120,32 +125,32 @@ class ImportMailPlugin(cliapp.Plugin): return email.message_from_file(f) def _import_msg_to_ticket_store( - self, repo, ticket_store, all_ticket_ids, msg, cache, key, value): + self, context, all_ticket_ids, msg, cache, key, value): referenced_ids = self._find_tickets_with_mails_referenced_by_msg( - ticket_store, all_ticket_ids, msg, cache) + context.store, all_ticket_ids, msg, cache) msg_ids = distixlib.get_ids_from_message(msg) filenames = [] if referenced_ids: for ticket_id in referenced_ids: - if not self._contains_message(ticket_store, ticket_id, msg): - ticket = ticket_store.get_ticket(ticket_id) + if not self._contains_message(context.store, ticket_id, msg): + ticket = context.store.get_ticket(ticket_id) ticket.add_message(msg) self._set_key_value(ticket, key, value) cache.add_msg_ids_for_ticket_id( ticket.get_ticket_id(), msg_ids) else: if not self._is_already_imported( - ticket_store, msg, all_ticket_ids): - new_ticket = self._create_ticket_from_msg(repo, msg) + context.store, msg, all_ticket_ids): + new_ticket = self._create_ticket_from_msg(context.repo, msg) self._set_key_value(new_ticket, key, value) cache.add_msg_ids_for_ticket_id( new_ticket.get_ticket_id(), msg_ids) all_ticket_ids.append(new_ticket.get_ticket_id()) - filenames = ticket_store.add_ticket(new_ticket) + filenames = context.store.add_ticket(new_ticket) - filenames += ticket_store.save_changes() + filenames += context.store.save_changes() return filenames def _set_key_value(self, ticket, key, value): @@ -226,6 +231,17 @@ class ImportMailPlugin(cliapp.Plugin): return ticket +class _ImportContext(object): + + def __init__(self): + self.repo = None + self.store = None + + def set_repo(self, repo): + self.repo = repo + self.store = self.repo.open_ticket_store(distixlib.tickets_dir_name) + + class _MessageIdCache(object): def __init__(self): |