# 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 . # # =*= License: GPL-3+ =*= import mailbox import os import distixlib class CannotLoadTicketError(distixlib.StructuredError): msg = "Can't load ticket from {directory}: {errno}: {strerror}" class TicketLoader(object): def load_from_directory(self, dirname): metadata_filename = os.path.join(dirname, 'ticket.yaml') try: metadata = self._load_metadata(metadata_filename) except distixlib.CannotLoadMetadataError as e: raise CannotLoadTicketError( directory=dirname, errno=e.kwargs['errno'], strerror=e.kwargs['strerror']) ticket = distixlib.Ticket() ticket.set_ticket_metadata(metadata) self._add_messages_to_ticket(ticket, self._load_messages(dirname)) ticket.make_clean() return ticket def _load_metadata(self, filename): # pragma: no cover metadata_loader = distixlib.MetadataLoader() return metadata_loader.load_from_file(filename) def _load_messages(self, dirname): # pragma: no cover maildir_name = os.path.join(dirname, 'Maildir') maildir = mailbox.Maildir(maildir_name, factory=None) return maildir.values() def _add_messages_to_ticket(self, ticket, messages): for message in messages: # pragma: no cover ticket.add_message(message)