From 76eb93223c6a88261494e8ba5dd51d39048aedaa Mon Sep 17 00:00:00 2001 From: "Stuart D. Gathman" Date: Tue, 26 Jul 2016 09:52:40 -0400 Subject: [PATCH] Use print function everywhere --- Milter/__init__.py | 9 +++++---- Milter/cache.py | 7 +++++-- Milter/dns.py | 5 +++-- Milter/dsn.py | 7 ++++--- Milter/dynip.py | 5 +++-- Milter/greylist.py | 3 ++- Milter/pyip6.py | 3 ++- Milter/test.py | 5 +++-- milter-template.py | 13 +++++++------ mime.py | 3 ++- sample.py | 15 +++++++-------- testutils.py | 6 +++--- 12 files changed, 46 insertions(+), 35 deletions(-) diff --git a/Milter/__init__.py b/Milter/__init__.py index 0dc03ce..7992b9e 100755 --- a/Milter/__init__.py +++ b/Milter/__init__.py @@ -8,6 +8,7 @@ # Copyright 2001,2009 Business Management Systems, Inc. # This code is under the GNU General Public License. See COPYING for details. +from __future__ import print_function __version__ = '0.9.8' import os @@ -365,7 +366,7 @@ class Base(object): for func,(nr,nc) in OPTIONAL_CALLBACKS.items(): func = getattr(klass,func) ca = getattr(func,'milter_protocol',0) - #print func,hex(nr),hex(nc),hex(ca) + #print(func,hex(nr),hex(nc),hex(ca)) p |= (nr|nc) & ~ca klass._protocol_mask = p return p @@ -579,9 +580,9 @@ class Milter(Base): ## Provide simple logging to sys.stdout def log(self,*msg): - print 'Milter:', - for i in msg: print i, - print + print('Milter:',end=None) + for i in msg: print(i,end=None) + print() @noreply def connect(self,hostname,family,hostaddr): diff --git a/Milter/cache.py b/Milter/cache.py index 6e50a6d..82bf046 100644 --- a/Milter/cache.py +++ b/Milter/cache.py @@ -46,6 +46,7 @@ # Copyright 2001,2002,2003,2004,2005 Business Management Systems, Inc. # This code is under the GNU General Public License. See COPYING for details. +from __future__ import print_function import time from plock import PLock @@ -147,7 +148,8 @@ class AddrCache(object): if not ts: return # already permanent self.cache[lsender] = (None,res) if not res: - print >>open(self.fname,'a'),sender + with open(self.fname,'a') as fp: + print(sender,file=fp) def __setitem__(self,sender,res): lsender = sender.lower() @@ -155,7 +157,8 @@ class AddrCache(object): self.cache[lsender] = (now,res) if not res and self.fname: s = time.strftime(AddrCache.time_format,time.localtime(now)) - print >>open(self.fname,'a'),sender,s # log refreshed senders + with open(self.fname,'a') as fp: + print(sender,s,file=fp) # log refreshed senders def __len__(self): return len(self.cache) diff --git a/Milter/dns.py b/Milter/dns.py index c626444..7106be8 100644 --- a/Milter/dns.py +++ b/Milter/dns.py @@ -1,6 +1,7 @@ ## @package Milter.dns # Provide a higher level interface to pydns. +from __future__ import print_function import DNS from DNS import DNSError @@ -120,5 +121,5 @@ if __name__ == '__main__': import sys s = Session() for n,t in zip(*[iter(sys.argv[1:])]*2): - print n,t - print s.dns(n,t) + print(n,t) + print(s.dns(n,t)) diff --git a/Milter/dsn.py b/Milter/dsn.py index 147305e..021d9d2 100644 --- a/Milter/dsn.py +++ b/Milter/dsn.py @@ -69,6 +69,7 @@ # a DSN or use a null MAIL FROM with an email address obtained from # anywhere else. # +from __future__ import print_function import smtplib import socket from email.Message import Message @@ -230,6 +231,6 @@ Subject: Test Test DSN template """ ) - print msg.as_string() - # print send_dsn(f,msg.as_string()) - # print send_dsn(q.s,'mail.example.com',msg.as_string()) + print(msg.as_string()) + # print(send_dsn(f,msg.as_string())) + # print(send_dsn(q.s,'mail.example.com',msg.as_string())) diff --git a/Milter/dynip.py b/Milter/dynip.py index 9b466a6..9fe8e1f 100644 --- a/Milter/dynip.py +++ b/Milter/dynip.py @@ -9,6 +9,7 @@ # wiley-268-8196.roadrunner.nf.net at ('205.251.174.46', 4810) # cbl-sd-02-79.aster.com.do at ('200.88.62.79', 4153) +from __future__ import print_function import re ip3 = re.compile('[0-9]{1,3}') @@ -91,6 +92,6 @@ if __name__ == '__main__': if ip in seen: continue seen.add(ip) if is_dynip(host,ip): - print '%s\t%s DYN' % (ip,host) + print('%s\t%s DYN' % (ip,host)) else: - print '%s\t%s' % (ip,host) + print('%s\t%s' % (ip,host)) diff --git a/Milter/greylist.py b/Milter/greylist.py index 1a7dcd1..5dd8431 100644 --- a/Milter/greylist.py +++ b/Milter/greylist.py @@ -1,3 +1,4 @@ +from __future__ import print_function import time import shelve import thread @@ -58,7 +59,7 @@ class Greylist(object): cnt = 0 dbp = self.dbp for key, r in dbp.iteritems(): - #print key,r,time.ctime(now) + #print(key,r,time.ctime(now)) if now > r.lastseen + self.greylist_retain: self.lock.acquire() try: diff --git a/Milter/pyip6.py b/Milter/pyip6.py index 6eea1ef..3921413 100644 --- a/Milter/pyip6.py +++ b/Milter/pyip6.py @@ -6,6 +6,7 @@ This module is free software, and you may redistribute it and/or modify it under the same terms as Python itself, so long as this copyright message and disclaimer are retained in their original form. """ +from __future__ import print_function import struct #from spf import RE_IP4 import re @@ -80,7 +81,7 @@ def inet_pton(p): (0, 0, 0, 0, 0, 65535, 258, 772) >>> try: inet_pton('::1.2.3.4.5') - ... except ValueError,x: print x + ... except ValueError,x: print(x) ::1.2.3.4.5 """ if p == '::': diff --git a/Milter/test.py b/Milter/test.py index cb2695e..4977cca 100644 --- a/Milter/test.py +++ b/Milter/test.py @@ -1,6 +1,7 @@ ## @package Milter.test # A test framework for milters +from __future__ import print_function import rfc822 import StringIO import Milter @@ -35,8 +36,8 @@ class TestBase(object): self._symlist = [ None, None, None, None, None, None, None ] def log(self,*msg): - for i in msg: print >>self.logfp, i, - print >>self.logfp + for i in msg: print(i,file=self.logfp,end=None) + print(file=self.logfp,flush=True) ## Set a macro value. # These are retrieved by the %milter with getsymval. diff --git a/milter-template.py b/milter-template.py index 3313dc0..076165c 100644 --- a/milter-template.py +++ b/milter-template.py @@ -7,6 +7,7 @@ ## Return REJECT,TEMPFAIL,ACCEPT to short circuit processing for a message. ## You can also add/del recipients, replacebody, add/del headers, etc. +from __future__ import print_function import Milter import StringIO import time @@ -131,10 +132,11 @@ def background(): t = logq.get() if not t: break msg,id,ts = t - print "%s [%d]" % (time.strftime('%Y%b%d %H:%M:%S',time.localtime(ts)),id), + print("%s [%d]" % (time.strftime('%Y%b%d %H:%M:%S',time.localtime(ts)),id), + end=None) # 2005Oct13 02:34:11 [1] msg1 msg2 msg3 ... - for i in msg: print i, - print + for i in msg: print(i,end=None) + print(flush=True) ## === @@ -149,12 +151,11 @@ def main(): flags += Milter.ADDRCPT flags += Milter.DELRCPT Milter.set_flags(flags) # tell Sendmail which features we use - print "%s milter startup" % time.strftime('%Y%b%d %H:%M:%S') - sys.stdout.flush() + print("%s milter startup" % time.strftime('%Y%b%d %H:%M:%S'),flush=True) Milter.runmilter("pythonfilter",socketname,timeout) logq.put(None) bt.join() - print "%s bms milter shutdown" % time.strftime('%Y%b%d %H:%M:%S') + print("%s bms milter shutdown" % time.strftime('%Y%b%d %H:%M:%S')) if __name__ == "__main__": main() diff --git a/mime.py b/mime.py index 6e4838c..6e075ec 100644 --- a/mime.py +++ b/mime.py @@ -93,6 +93,7 @@ # Copyright 2001,2002,2003,2004,2005 Business Management Systems, Inc. # This code is under the GNU General Public License. See COPYING for details. +from __future__ import print_function import StringIO import socket import Milter @@ -532,7 +533,7 @@ if __name__ == '__main__': def _list_attach(msg): t = msg.get_content_type() p = msg.get_payload(decode=True) - print msg.get_filename(),msg.get_content_type(),type(p) + print(msg.get_filename(),msg.get_content_type(),type(p)) msg = msg.get_submsg() if isinstance(msg,Message): return check_attachments(msg,_list_attach) diff --git a/sample.py b/sample.py index 40436bc..68bb17d 100644 --- a/sample.py +++ b/sample.py @@ -1,4 +1,4 @@ - +from __future__ import print_function # A simple milter. # Author: Stuart D. Gathman @@ -21,9 +21,9 @@ class sampleMilter(Milter.Milter): "Milter to replace attachments poisonous to Windows with a WARNING message." def log(self,*msg): - print "%s [%d]" % (strftime('%Y%b%d %H:%M:%S'),self.id), - for i in msg: print i, - print + print("%s [%d]" % (strftime('%Y%b%d %H:%M:%S'),self.id),end=None) + for i in msg: print(i,end=None) + print(flush=True) def __init__(self): self.tempname = None @@ -171,13 +171,12 @@ if __name__ == "__main__": socketname = os.getenv("HOME") + "/pythonsock" Milter.factory = sampleMilter Milter.set_flags(Milter.CHGBODY + Milter.CHGHDRS + Milter.ADDHDRS) - print """To use this with sendmail, add the following to sendmail.cf: + print("""To use this with sendmail, add the following to sendmail.cf: O InputMailFilters=pythonfilter Xpythonfilter, S=local:%s See the sendmail README for libmilter. -sample milter startup""" % socketname - sys.stdout.flush() +sample milter startup""" % socketname,flush=True) Milter.runmilter("pythonfilter",socketname,240) - print "sample milter shutdown" + print("sample milter shutdown") diff --git a/testutils.py b/testutils.py index d5a0b5f..c120ca1 100644 --- a/testutils.py +++ b/testutils.py @@ -1,3 +1,4 @@ +from __future__ import print_function import unittest import doctest import os @@ -32,9 +33,8 @@ class AddrCacheTestCase(unittest.TestCase): self.failUnless(not cache['temp@bar.com']) def testDomain(self): - fp = open(self.fname,'w') - print >>fp,'spammer.com' - fp.close() + with open(self.fname,'w') as fp: + print('spammer.com',file=fp) cache = AddrCache(fname=self.fname) cache.load(self.fname,30) self.failUnless('spammer.com' in cache)