[PATCH v2 3/9] utils: Move patch_to_mbox to utils module

Daniel Axtens dja at axtens.net
Tue Feb 14 07:48:34 AEDT 2017


Hi Stephen,

Thanks!

Reviewed-by: Daniel Axtens <dja at axtens.net>

Regards,
Daniel

Stephen Finucane <stephen at that.guru> writes:

> There's no reason to have this in '__init__.py'.
>
> Signed-off-by: Stephen Finucane <stephen at that.guru>
> ---
>  patchwork/views/__init__.py |  79 ---------------------------------
>  patchwork/views/bundle.py   |   3 +-
>  patchwork/views/patch.py    |   3 +-
>  patchwork/views/utils.py    | 106 ++++++++++++++++++++++++++++++++++++++++++++
>  patchwork/views/xmlrpc.py   |   2 +-
>  5 files changed, 111 insertions(+), 82 deletions(-)
>  create mode 100644 patchwork/views/utils.py
>
> diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py
> index db53cdf..c884515 100644
> --- a/patchwork/views/__init__.py
> +++ b/patchwork/views/__init__.py
> @@ -17,25 +17,13 @@
>  # along with Patchwork; if not, write to the Free Software
>  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
>  
> -from __future__ import absolute_import
> -
> -import datetime
> -from email.encoders import encode_7or8bit
> -from email.header import Header
> -from email.mime.nonmultipart import MIMENonMultipart
> -from email.parser import HeaderParser
> -import email.utils
> -import re
> -
>  from django.contrib import messages
>  from django.shortcuts import get_object_or_404
> -from django.utils import six
>  
>  from patchwork.filters import Filters
>  from patchwork.forms import MultiplePatchForm
>  from patchwork.models import Bundle
>  from patchwork.models import BundlePatch
> -from patchwork.models import Comment
>  from patchwork.models import Patch
>  from patchwork.models import Project
>  from patchwork.paginator import Paginator
> @@ -341,70 +329,3 @@ def process_multiplepatch_form(request, form, action, patches, context):
>          messages.warning(request, 'No patches updated')
>  
>      return errors
> -
> -
> -class PatchMbox(MIMENonMultipart):
> -    patch_charset = 'utf-8'
> -
> -    def __init__(self, _text):
> -        MIMENonMultipart.__init__(self, 'text', 'plain',
> -                                  **{'charset': self.patch_charset})
> -        self.set_payload(_text.encode(self.patch_charset))
> -        encode_7or8bit(self)
> -
> -
> -def patch_to_mbox(patch):
> -    postscript_re = re.compile('\n-{2,3} ?\n')
> -    body = ''
> -
> -    if patch.content:
> -        body = patch.content.strip() + "\n"
> -
> -    parts = postscript_re.split(body, 1)
> -    if len(parts) == 2:
> -        (body, postscript) = parts
> -        body = body.strip() + "\n"
> -        postscript = postscript.rstrip()
> -    else:
> -        postscript = ''
> -
> -    # TODO(stephenfin): Make this use the tags infrastructure
> -    for comment in Comment.objects.filter(submission=patch):
> -        body += comment.patch_responses
> -
> -    if postscript:
> -        body += '---\n' + postscript + '\n'
> -
> -    if patch.diff:
> -        body += '\n' + patch.diff
> -
> -    delta = patch.date - datetime.datetime.utcfromtimestamp(0)
> -    utc_timestamp = delta.seconds + delta.days * 24 * 3600
> -
> -    mail = PatchMbox(body)
> -    mail['Subject'] = patch.name
> -    mail['X-Patchwork-Submitter'] = email.utils.formataddr((
> -        str(Header(patch.submitter.name, mail.patch_charset)),
> -        patch.submitter.email))
> -    mail['X-Patchwork-Id'] = str(patch.id)
> -    if patch.delegate:
> -        mail['X-Patchwork-Delegate'] = str(patch.delegate.email)
> -    mail['Message-Id'] = patch.msgid
> -    mail.set_unixfrom('From patchwork ' + patch.date.ctime())
> -
> -    copied_headers = ['To', 'Cc', 'Date', 'From', 'List-Id']
> -    orig_headers = HeaderParser().parsestr(str(patch.headers))
> -    for header in copied_headers:
> -        if header in orig_headers:
> -            mail[header] = orig_headers[header]
> -
> -    if 'Date' not in mail:
> -        mail['Date'] = email.utils.formatdate(utc_timestamp)
> -
> -    # NOTE(stephenfin) http://stackoverflow.com/a/28584090/613428
> -    if six.PY3:
> -        mail = mail.as_bytes(True).decode()
> -    else:
> -        mail = mail.as_string(True)
> -
> -    return mail
> diff --git a/patchwork/views/bundle.py b/patchwork/views/bundle.py
> index bab431c..b6959e9 100644
> --- a/patchwork/views/bundle.py
> +++ b/patchwork/views/bundle.py
> @@ -28,7 +28,8 @@ from django.shortcuts import render, get_object_or_404
>  from patchwork.filters import DelegateFilter
>  from patchwork.forms import BundleForm, DeleteBundleForm
>  from patchwork.models import Bundle, BundlePatch, Project
> -from patchwork.views import generic_list, patch_to_mbox
> +from patchwork.views import generic_list
> +from patchwork.views.utils import patch_to_mbox
>  
>  
>  @login_required
> diff --git a/patchwork/views/patch.py b/patchwork/views/patch.py
> index 705359f..d1e81d1 100644
> --- a/patchwork/views/patch.py
> +++ b/patchwork/views/patch.py
> @@ -29,7 +29,8 @@ from django.shortcuts import render, get_object_or_404
>  
>  from patchwork.forms import PatchForm, CreateBundleForm
>  from patchwork.models import Patch, Project, Bundle, Submission
> -from patchwork.views import generic_list, patch_to_mbox
> +from patchwork.views import generic_list
> +from patchwork.views.utils import patch_to_mbox
>  
>  
>  def patch_list(request, project_id):
> diff --git a/patchwork/views/utils.py b/patchwork/views/utils.py
> new file mode 100644
> index 0000000..c998647
> --- /dev/null
> +++ b/patchwork/views/utils.py
> @@ -0,0 +1,106 @@
> +# Patchwork - automated patch tracking system
> +# Copyright (C) 2008 Jeremy Kerr <jk at ozlabs.org>
> +# Copyright (C) 2017 Stephen Finucane <stephen at that.guru>
> +#
> +# This file is part of the Patchwork package.
> +#
> +# Patchwork 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 2 of the License, or
> +# (at your option) any later version.
> +#
> +# Patchwork 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 Patchwork; if not, write to the Free Software
> +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
> +
> +import datetime
> +from email.encoders import encode_7or8bit
> +from email.header import Header
> +from email.mime.nonmultipart import MIMENonMultipart
> +from email.parser import HeaderParser
> +import email.utils
> +import re
> +
> +from django.utils import six
> +
> +from patchwork.models import Comment
> +
> +
> +class PatchMbox(MIMENonMultipart):
> +    patch_charset = 'utf-8'
> +
> +    def __init__(self, _text):
> +        MIMENonMultipart.__init__(self, 'text', 'plain',
> +                                  **{'charset': self.patch_charset})
> +        self.set_payload(_text.encode(self.patch_charset))
> +        encode_7or8bit(self)
> +
> +
> +def patch_to_mbox(patch):
> +    """Get an mbox representation of a single patch.
> +
> +    Arguments:
> +        patch: The Patch object to convert.
> +
> +    Returns:
> +        A string for the mbox file.
> +    """
> +    postscript_re = re.compile('\n-{2,3} ?\n')
> +    body = ''
> +
> +    if patch.content:
> +        body = patch.content.strip() + "\n"
> +
> +    parts = postscript_re.split(body, 1)
> +    if len(parts) == 2:
> +        (body, postscript) = parts
> +        body = body.strip() + "\n"
> +        postscript = postscript.rstrip()
> +    else:
> +        postscript = ''
> +
> +    # TODO(stephenfin): Make this use the tags infrastructure
> +    for comment in Comment.objects.filter(submission=patch):
> +        body += comment.patch_responses
> +
> +    if postscript:
> +        body += '---\n' + postscript + '\n'
> +
> +    if patch.diff:
> +        body += '\n' + patch.diff
> +
> +    delta = patch.date - datetime.datetime.utcfromtimestamp(0)
> +    utc_timestamp = delta.seconds + delta.days * 24 * 3600
> +
> +    mail = PatchMbox(body)
> +    mail['Subject'] = patch.name
> +    mail['X-Patchwork-Submitter'] = email.utils.formataddr((
> +        str(Header(patch.submitter.name, mail.patch_charset)),
> +        patch.submitter.email))
> +    mail['X-Patchwork-Id'] = str(patch.id)
> +    if patch.delegate:
> +        mail['X-Patchwork-Delegate'] = str(patch.delegate.email)
> +    mail['Message-Id'] = patch.msgid
> +    mail.set_unixfrom('From patchwork ' + patch.date.ctime())
> +
> +    copied_headers = ['To', 'Cc', 'Date', 'From', 'List-Id']
> +    orig_headers = HeaderParser().parsestr(str(patch.headers))
> +    for header in copied_headers:
> +        if header in orig_headers:
> +            mail[header] = orig_headers[header]
> +
> +    if 'Date' not in mail:
> +        mail['Date'] = email.utils.formatdate(utc_timestamp)
> +
> +    # NOTE(stephenfin) http://stackoverflow.com/a/28584090/613428
> +    if six.PY3:
> +        mail = mail.as_bytes(True).decode()
> +    else:
> +        mail = mail.as_string(True)
> +
> +    return mail
> diff --git a/patchwork/views/xmlrpc.py b/patchwork/views/xmlrpc.py
> index a8dc797..0a51d40 100644
> --- a/patchwork/views/xmlrpc.py
> +++ b/patchwork/views/xmlrpc.py
> @@ -45,7 +45,7 @@ from patchwork.models import Patch
>  from patchwork.models import Person
>  from patchwork.models import Project
>  from patchwork.models import State
> -from patchwork.views import patch_to_mbox
> +from patchwork.views.utils import patch_to_mbox
>  
>  
>  class PatchworkXMLRPCDispatcher(SimpleXMLRPCDispatcher,
> -- 
> 2.9.3
>
> _______________________________________________
> Patchwork mailing list
> Patchwork at lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork


More information about the Patchwork mailing list