summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2014-07-25 21:38:29 +0300
committerLars Wirzenius <liw@liw.fi>2014-07-25 21:38:29 +0300
commit819f0cb22b5acce77ea8d4f9318da098e6097bee (patch)
treebd118ed304022d47330a4eab22bd706ae3f990ca
parent8432d6b54589070b1f8b481c4728659e4d119c84 (diff)
downloaddistix-819f0cb22b5acce77ea8d4f9318da098e6097bee.tar.gz
Add 'distix import-mail'
-rw-r--r--distixlib/plugins/import_mail_plugin.py86
-rw-r--r--without-tests1
-rw-r--r--yarns/080-import-mail.yarn26
3 files changed, 113 insertions, 0 deletions
diff --git a/distixlib/plugins/import_mail_plugin.py b/distixlib/plugins/import_mail_plugin.py
new file mode 100644
index 0000000..22034ef
--- /dev/null
+++ b/distixlib/plugins/import_mail_plugin.py
@@ -0,0 +1,86 @@
+# Copyright 2014 Lars Wirzenius
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# =*= License: GPL-3+ =*=
+
+
+import email
+import os
+import uuid
+
+import cliapp
+
+import distixlib
+
+
+class WrongArguments(distixlib.StructuredError):
+
+ msg = 'Wrong number of arguments: got {count}, wanted 2'
+
+
+class ImportMailPlugin(cliapp.Plugin):
+
+ def enable(self):
+ self.app.add_subcommand(
+ 'import-mail', self.import_mail, arg_synopsis='REPO FILE')
+
+ def import_mail(self, args):
+ repo_dirname, mail_filename = self._parse_command_line(args)
+
+ ticket = self._create_ticket_from_mail(mail_filename)
+ self._save_ticket_in_repository(repo_dirname, ticket)
+
+ if self.app.settings['save-ticket-id']:
+ self._save_ticket_id(
+ ticket_id, self.app.settings['save-ticket-id'])
+
+ def _parse_command_line(self, args):
+ if len(args) != 2:
+ raise WrongArguments(count=len(args))
+ return args
+
+ def _create_ticket_from_mail(self, mail_filename):
+ msg = self._read_mail_message(mail_filename)
+ ticket_id = self._invent_ticket_id()
+ ticket = self._create_ticket(ticket_id, msg['Subject'])
+ ticket.add_message(msg)
+ return ticket
+
+ def _read_mail_message(self, mail_filename):
+ with open(mail_filename) as f:
+ return email.message_from_file(f)
+
+ def _invent_ticket_id(self):
+ return uuid.uuid4().hex
+
+ def _create_ticket(self, ticket_id, title):
+ ticket = distixlib.Ticket()
+ ticket.set_ticket_id(ticket_id)
+ ticket.set_title(title)
+ return ticket
+
+ def _save_ticket_in_repository(self, repo_dirname, ticket):
+ os.chdir(repo_dirname)
+ ticket_store = distixlib.TicketStore(distixlib.tickets_dir_name)
+ filenames = ticket_store.add_ticket(ticket)
+ self._commit_changes(filenames)
+
+ def _commit_changes(self, filenames):
+ git = distixlib.Git('.')
+ git.add_and_commit(filenames, 'distix new ticket')
+
+ def _save_ticket_id(self, ticket_id, filename):
+ with open(filename, 'w') as f:
+ f.write(ticket_id)
diff --git a/without-tests b/without-tests
index 7311db7..e5f4e7e 100644
--- a/without-tests
+++ b/without-tests
@@ -2,6 +2,7 @@ setup.py
distixlib/app.py
distixlib/__init__.py
distixlib/plugins/global_settings_plugin.py
+distixlib/plugins/import_mail_plugin.py
distixlib/plugins/init_plugin.py
distixlib/plugins/list_plugin.py
distixlib/plugins/metadata_manipulation_plugin.py
diff --git a/yarns/080-import-mail.yarn b/yarns/080-import-mail.yarn
new file mode 100644
index 0000000..84c58f6
--- /dev/null
+++ b/yarns/080-import-mail.yarn
@@ -0,0 +1,26 @@
+Importing e-mails
+=================
+
+Distix needs to import e-mails. E-mails be fed to it one-by-one by a
+mail transport agent, or in mail folders by the user. In the latter,
+especially, distix needs to be careful about handling mails that have
+already been imported once.
+
+Let's start with the simple case of importing a single e-mail. Mail
+transport agents will feed the e-mail to distix via a pipe, so distix
+will read the e-mail from its standard input.
+
+ SCENARIO import one email
+
+ WHEN user attempts to run distix init REPO
+ THEN attempt succeeded
+
+ GIVEN file MAIL1 containing "From: foo@example.com\nSubject: bar\n\nyo\n"
+ WHEN user attempts to run distix import-mail REPO MAIL1
+ THEN attempt succeeded
+ AND everything in REPO is committed to git
+
+ WHEN user changes working directory to REPO
+ AND user attempts to run distix list
+ THEN attempt succeeded
+ AND output matches "^[0-9a-f]{32} bar$"