[PATCH 12 of 13] compat: modernize the email imports

Alejandro Santos alejolp at alejolp.com
Wed Aug 5 15:55:46 CDT 2009


# HG changeset patch
# User Alejandro Santos <alejolp at alejolp.com>
# Date 1249505636 10800
# Node ID 3352f2de360abad11763c242e9835d1fb195c3c5
# Parent  c7ba5bcbffb4aacd3b46c7f6d66f5c85b5d0d9d7
compat: modernize the email imports

diff -r c7ba5bcbffb4 -r 3352f2de360a hgext/convert/gnuarch.py
--- a/hgext/convert/gnuarch.py	Wed Aug 05 17:53:56 2009 -0300
+++ b/hgext/convert/gnuarch.py	Wed Aug 05 17:53:56 2009 -0300
@@ -9,8 +9,12 @@
 from common import NoRepo, commandline, commit, converter_source
 from mercurial.i18n import _
 from mercurial import util
-import os, shutil, tempfile, stat, locale
-from email.Parser import Parser
+import os, sys, shutil, tempfile, stat, locale
+
+if sys.version_info[:2] >= (2, 5): # Py 2.5, Py3k
+    from email.parser import Parser
+else:
+    from email.Parser import Parser
 
 class gnuarch_source(converter_source, commandline):
 
diff -r c7ba5bcbffb4 -r 3352f2de360a hgext/hgcia.py
--- a/hgext/hgcia.py	Wed Aug 05 17:53:56 2009 -0300
+++ b/hgext/hgcia.py	Wed Aug 05 17:53:56 2009 -0300
@@ -40,7 +40,13 @@
 from mercurial.i18n import _
 from mercurial.node import *
 from mercurial import cmdutil, patch, templater, util, mail
-import email.Parser
+
+import sys
+
+if sys.version_info[:2] >= (2, 5): # Py 2.5+, Py3k
+    import email.parser as emailparser
+else: # Py 2.4
+    import email.Parser as emailparser
 
 import xmlrpclib
 from xml.sax import saxutils
@@ -198,7 +204,7 @@
         srv.hub.deliver(msg)
 
     def sendemail(self, address, data):
-        p = email.Parser.Parser()
+        p = emailparser.Parser()
         msg = p.parsestr(data)
         msg['Date'] = util.datestr(format="%a, %d %b %Y %H:%M:%S %1%2")
         msg['To'] = address
diff -r c7ba5bcbffb4 -r 3352f2de360a hgext/notify.py
--- a/hgext/notify.py	Wed Aug 05 17:53:56 2009 -0300
+++ b/hgext/notify.py	Wed Aug 05 17:53:56 2009 -0300
@@ -70,7 +70,13 @@
 
 from mercurial.i18n import _
 from mercurial import patch, cmdutil, templater, util, mail
-import email.Parser, fnmatch, socket, time
+import fnmatch, socket, time, sys
+
+if sys.version_info[:2] >= (2, 5): # Py 2.5+, Py3k
+    import email.parser as emailparser
+else: # Py 2.4
+    import email.Parser as emailparser
+
 
 # template for single changeset can include email headers.
 single_template = '''
@@ -179,7 +185,7 @@
     def send(self, ctx, count, data):
         '''send message.'''
 
-        p = email.Parser.Parser()
+        p = emailparser.Parser()
         msg = p.parsestr(data)
 
         # store sender and subject
diff -r c7ba5bcbffb4 -r 3352f2de360a hgext/patchbomb.py
--- a/hgext/patchbomb.py	Wed Aug 05 17:53:56 2009 -0300
+++ b/hgext/patchbomb.py	Wed Aug 05 17:53:56 2009 -0300
@@ -71,9 +71,21 @@
 hgrc(5) for details.
 '''
 
-import os, errno, socket, tempfile, cStringIO, time
-import email.MIMEMultipart, email.MIMEBase
-import email.Utils, email.Encoders, email.Generator
+import os, errno, socket, tempfile, cStringIO, time, sys
+
+if sys.version_info[:2] >= (2, 5): # Py 2.5+, Py3k
+    import email.mime.multipart as emailmimemultipart
+    import email.mime.base as emailmimebase
+    import email.utils as emailutils
+    import email.encoders as emailenconders
+    import email.generator as emailgenerator
+else: # Py 2.4
+    import email.MIMEMultipart as emailmimemultipart
+    import email.MIMEBase as emailmimebase
+    import email.Utils as emailutils
+    import email.Encoders as emailenconders
+    import email.Generator as emailgenerator
+
 from mercurial import cmdutil, commands, hg, mail, patch, util
 from mercurial.i18n import _
 from mercurial.node import bin
@@ -139,7 +151,7 @@
         body += cdiffstat(ui, '\n'.join(desc), patch) + '\n\n'
 
     if opts.get('attach') or opts.get('inline'):
-        msg = email.MIMEMultipart.MIMEMultipart()
+        msg = emailmimemultipart.MIMEMultipart()
         if body:
             msg.attach(mail.mimeencode(ui, body, _charsets, opts.get('test')))
         p = mail.mimetextpatch('\n'.join(patch), 'x-patch', opts.get('test'))
@@ -349,15 +361,15 @@
                 or prompt(ui, 'Subject:', 'A bundle for your repository'))
 
         body = getdescription('', sender)
-        msg = email.MIMEMultipart.MIMEMultipart()
+        msg = emailmimemultipart.MIMEMultipart()
         if body:
             msg.attach(mail.mimeencode(ui, body, _charsets, opts.get('test')))
-        datapart = email.MIMEBase.MIMEBase('application', 'x-mercurial-bundle')
+        datapart = emailmimebase.MIMEBase('application', 'x-mercurial-bundle')
         datapart.set_payload(bundle)
         bundlename = '%s.hg' % opts.get('bundlename', 'bundle')
         datapart.add_header('Content-Disposition', 'attachment',
                             filename=bundlename)
-        email.Encoders.encode_base64(datapart)
+        emailenconders.encode_base64(datapart)
         msg.attach(datapart)
         msg['Subject'] = mail.headencode(ui, subj, _charsets, opts.get('test'))
         return [(msg, subj)]
@@ -402,7 +414,7 @@
 
     first = True
 
-    sender_addr = email.Utils.parseaddr(sender)[1]
+    sender_addr = emailutils.parseaddr(sender)[1]
     sender = mail.addressencode(ui, sender, _charsets, opts.get('test'))
     sendmail = None
     for m, subj in msgs:
@@ -418,7 +430,7 @@
             first = False
 
         m['User-Agent'] = 'Mercurial-patchbomb/%s' % util.version()
-        m['Date'] = email.Utils.formatdate(start_time[0], localtime=True)
+        m['Date'] = emailutils.formatdate(start_time[0], localtime=True)
 
         start_time = (start_time[0] + 1, start_time[1])
         m['From'] = sender
@@ -434,7 +446,7 @@
                 fp = util.popen(os.environ['PAGER'], 'w')
             else:
                 fp = ui
-            generator = email.Generator.Generator(fp, mangle_from_=False)
+            generator = emailgenerator.Generator(fp, mangle_from_=False)
             try:
                 generator.flatten(m, 0)
                 fp.write('\n')
@@ -446,7 +458,7 @@
         elif opts.get('mbox'):
             ui.status(_('Writing '), subj, ' ...\n')
             fp = open(opts.get('mbox'), 'In-Reply-To' in m and 'ab+' or 'wb+')
-            generator = email.Generator.Generator(fp, mangle_from_=True)
+            generator = emailgenerator.Generator(fp, mangle_from_=True)
             date = time.ctime(start_time[0])
             fp.write('From %s %s\n' % (sender_addr, date))
             generator.flatten(m, 0)
@@ -459,7 +471,7 @@
             # Exim does not remove the Bcc field
             del m['Bcc']
             fp = cStringIO.StringIO()
-            generator = email.Generator.Generator(fp, mangle_from_=False)
+            generator = emailgenerator.Generator(fp, mangle_from_=False)
             generator.flatten(m, 0)
             sendmail(sender, to + bcc + cc, fp.getvalue())
 
diff -r c7ba5bcbffb4 -r 3352f2de360a mercurial/byterange.py
--- a/mercurial/byterange.py	Wed Aug 05 17:53:56 2009 -0300
+++ b/mercurial/byterange.py	Wed Aug 05 17:53:56 2009 -0300
@@ -23,7 +23,11 @@
 import stat
 import urllib
 import urllib2
-import email.Utils
+
+if sys.version_info[:2] >= (2, 5): # Py 2,5+, Py3k
+    import email.utils as emailutils
+else: # Py 2.4
+    import email.Utils as emailutils
 
 if sys.version_info[0] == 2:
     _ftpwrapper = urllib.ftpwrapper
@@ -219,7 +223,7 @@
         localfile = urllib.url2pathname(file)
         stats = os.stat(localfile)
         size = stats[stat.ST_SIZE]
-        modified = email.Utils.formatdate(stats[stat.ST_MTIME])
+        modified = emailutils.formatdate(stats[stat.ST_MTIME])
         mtype = mimetypes.guess_type(file)[0]
         if host:
             host, port = urllib.splitport(host)
diff -r c7ba5bcbffb4 -r 3352f2de360a mercurial/mail.py
--- a/mercurial/mail.py	Wed Aug 05 17:53:56 2009 -0300
+++ b/mercurial/mail.py	Wed Aug 05 17:53:56 2009 -0300
@@ -7,8 +7,16 @@
 
 from i18n import _
 import util, encoding
-import os, smtplib, socket, quopri
-import email.Header, email.MIMEText, email.Utils
+import os, sys, smtplib, socket, quopri
+
+if sys.version_info[:2] >= (2, 5): # Py 2.5+, Py3k
+    import email.header as emailheader
+    import email.mime.text as emailmimetext
+    import email.utils as emailutils
+else: # Py 2.4
+    import email.Header as emailheader
+    import email.MIMEText as emailmimetext
+    import email.Utils as emailutils
 
 def _smtp(ui):
     '''build an smtp connection and return a function to send mail'''
@@ -111,7 +119,7 @@
                 # We'll go with us-ascii as a fallback.
                 pass
 
-    msg = email.MIMEText.MIMEText(s, subtype, cs)
+    msg = emailmimetext.MIMEText(s, subtype, cs)
     if enc:
         del msg['Content-Transfer-Encoding']
         msg['Content-Transfer-Encoding'] = enc
@@ -157,14 +165,14 @@
     if not display:
         # split into words?
         s, cs = _encode(ui, s, charsets)
-        return str(email.Header.Header(s, cs))
+        return str(emailheader.Header(s, cs))
     return s
 
 def addressencode(ui, address, charsets=None, display=False):
     '''Turns address into RFC-2047 compliant header.'''
     if display or not address:
         return address or ''
-    name, addr = email.Utils.parseaddr(address)
+    name, addr = emailutils.parseaddr(address)
     name = headencode(ui, name, charsets)
     try:
         acc, dom = addr.split('@')
@@ -179,7 +187,7 @@
             addr = addr.encode('ascii')
         except UnicodeDecodeError:
             raise util.Abort(_('invalid local address: %s') % addr)
-    return email.Utils.formataddr((name, addr))
+    return emailutils.formataddr((name, addr))
 
 def mimeencode(ui, s, charsets=None, display=False):
     '''creates mime text object, encodes it if needed, and sets
@@ -187,4 +195,4 @@
     cs = 'us-ascii'
     if not display:
         s, cs = _encode(ui, s, charsets)
-    return email.MIMEText.MIMEText(s, 'plain', cs)
+    return emailmimetext.MIMEText(s, 'plain', cs)
diff -r c7ba5bcbffb4 -r 3352f2de360a mercurial/patch.py
--- a/mercurial/patch.py	Wed Aug 05 17:53:56 2009 -0300
+++ b/mercurial/patch.py	Wed Aug 05 17:53:56 2009 -0300
@@ -9,9 +9,14 @@
 from i18n import _
 from node import hex, nullid, short
 import base85, mdiff, util, diffhelpers, copies
-import cStringIO, email.Parser, os, re, math
+import cStringIO, os, re, math
 import sys, tempfile, zlib
 
+if sys.version_info[:2] >= (2, 5): # Py 2.5+, Py3k
+    import email.parser as emailparser
+else: # Py 2.4
+    import email.Parser as emailparser
+
 gitre = re.compile('diff --git a/(.*) b/(.*)')
 
 class PatchError(Exception):
@@ -71,7 +76,7 @@
     fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
     tmpfp = os.fdopen(fd, 'w')
     try:
-        msg = email.Parser.Parser().parse(fileobj)
+        msg = emailparser.Parser().parse(fileobj)
 
         subject = msg['Subject']
         user = msg['From']


More information about the Mercurial-devel mailing list