Mbox.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:PaStA 作者: lfd 项目源码 文件源码
def __init__(self, filename):
        mail = mailbox.mbox(filename, create=False)[0]

        # Simply name it commit_hash, otherwise we would have to refactor
        # tons of code.
        self.commit_hash = mail['Message-ID']
        self.mail_subject = mail['Subject']

        # we need timezone aware datetimes due to the fact, that most of all
        # emails contain timezone aware timestamps. There's an issue with
        # timezone unaware timestamps: they can't be compared to timezone aware
        # timestamps. To cope with that, we simple shift those mails to UTC
        # (which is also true in most cases).
        #
        # E.g. python converts this timestamp to an timezone unaware one,
        # while it is GMT:
        #   'Fri, 23 Feb 2007 13:35:50 -0000 (GMT)'
        try:
            date = email.utils.parsedate_to_datetime(mail['Date'])
        except:
            # assume epoch
            log.debug('  Message %s: unable to parse date %s' %
                      (self.commit_hash, mail['Date']))
            date = datetime.datetime.utcfromtimestamp(0)

        if date.tzinfo is None:
            date = date.replace(tzinfo=datetime.timezone.utc)

        payload = mail.get_payload()

        # Check encoding and decode
        cte = mail['Content-Transfer-Encoding']
        if cte == 'QUOTED-PRINTABLE':
            charset = mail.get_content_charset()
            if charset not in CHARSETS:
                charset = 'ascii'
            payload = quopri.decodestring(payload)
            payload = payload.decode(charset, errors='ignore')

        # MAY RAISE AN ERROR, FORBID RETURN NULL
        msg, diff = parse_payload(payload)

        # reconstruct commit message
        subject = self.mail_subject
        match = PATCH_SUBJECT_REGEX.match(self.mail_subject)
        if match:
            subject = match.group(1)
        msg = [subject, ''] + msg

        author_name = mail['From']
        author_email = ''
        match = MAIL_FROM_REGEX.match(author_name)
        if match:
            author_name = match.group(1)
            author_email = match.group(2)

        super(PatchMail, self).__init__(msg, diff, author_name, author_email,
                                        date, snip_header=True)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号