Python language fix: replace C-style boolean values "1" and "0" with True/False

This commit is contained in:
Nikolaus Schulz 2010-07-16 19:13:59 +02:00
parent 7269c32734
commit e5ee9b98c1
2 changed files with 100 additions and 100 deletions

View File

@ -174,28 +174,28 @@ class Options:
archive_suffix = "_archive"
days_old_max = 180
date_old_max = None
delete_old_mail = 0
dry_run = 0
delete_old_mail = False
dry_run = False
filter_append = None
include_flagged = 0
include_flagged = False
locking_attempts = 5
lockfile_extension = ".lock"
lock_sleep = 1
no_compress = 0
only_archive_read = 0
lock_sleep = True
no_compress = False
only_archive_read = False
output_dir = None
pwfile = None
preserve_unread = 0
mangle_from = 1
quiet = 0
preserve_unread = False
mangle_from = True
quiet = False
read_buffer_size = 8192
script_name = os.path.basename(sys.argv[0])
min_size = None
verbose = 0
debug_imap = 0
warn_duplicates = 0
copy_old_mail = 0
archive_all = 0
verbose = False
debug_imap = False
warn_duplicates = False
copy_old_mail = False
archive_all = False
def parse_args(self, args, usage):
"""Set our runtime options from the command-line arguments.
@ -225,13 +225,13 @@ class Options:
if o == '--delete':
if self.copy_old_mail:
user_error("found conflicting options --copy and --delete")
self.delete_old_mail = 1
self.delete_old_mail = True
if o == '--include-flagged':
self.include_flagged = 1
self.include_flagged = True
if o == '--no-compress':
self.no_compress = 1
self.no_compress = True
if o == '--warn-duplicate':
self.warn_duplicates = 1
self.warn_duplicates = True
if o in ('-D', '--date'):
if archive_by:
user_error("you cannot specify both -d and -D options")
@ -252,27 +252,27 @@ class Options:
print usage
sys.exit(0)
if o in ('-n', '--dry-run'):
self.dry_run = 1
self.dry_run = True
if o in ('-q', '--quiet'):
self.quiet = 1
self.quiet = True
if o in ('-s', '--suffix'):
self.archive_suffix = a
if o in ('-S', '--size'):
self.min_size = string.atoi(a)
if o in ('-u', '--preserve-unread'):
self.preserve_unread = 1
self.preserve_unread = True
if o == '--dont-mangle':
self.mangle_from = 0
self.mangle_from = False
if o in ('-v', '--verbose'):
self.verbose = 1
self.verbose = True
if o == '--debug-imap':
self.debug_imap = int(a)
if o == '--copy':
if self.delete_old_mail:
user_error("found conflicting options --copy and --delete")
self.copy_old_mail = 1
self.copy_old_mail = True
if o == '--all':
self.archive_all = 1
self.archive_all = True
if o in ('-V', '--version'):
print __version__ + "\n\n" + __copyright__
sys.exit(0)
@ -301,7 +301,7 @@ class Options:
"%d %b %Y" , # Internet format
"%d %B %Y" , # Internet format with full month names
)
time.accept2dyear = 0 # I'm not going to support 2-digit years
time.accept2dyear = False # I'm not going to support 2-digit years
for format in date_formats:
try:
date = time.strptime(string, format)
@ -536,7 +536,7 @@ class TempMbox:
# practice to 'self.mbox_file.writelines(msg.fp.readlines())'
assert options.read_buffer_size > 0
linebuf = ""
while 1:
while True:
body = msg.fp.read(options.read_buffer_size)
if (not msg_has_mbox_format) and options.mangle_from:
# Be careful not to break pattern matching
@ -617,7 +617,7 @@ class IdentityCache:
if self.seen_ids.has_key(message_id):
user_warning("duplicate message id: '%s' in mailbox '%s'" %
(message_id, self.mailbox_name))
self.seen_ids[message_id] = 1
self.seen_ids[message_id] = True
# global class instances
@ -892,7 +892,7 @@ def is_flagged(message):
x_status = message.get('X-Status')
if x_status and re.search('F', x_status):
vprint("message is important (X-Status header='%s')" % x_status)
return 1
return True
file_name = None
try:
file_name = get_filename(message)
@ -901,9 +901,9 @@ def is_flagged(message):
# maildir mailboxes use the filename suffix to indicate flagged status
if file_name and re.search(":2,.*F.*$", file_name):
vprint("message is important (filename info has 'F')")
return 1
return True
vprint("message is not flagged important")
return 0
return False
def is_unread(message):
@ -912,7 +912,7 @@ def is_unread(message):
status = message.get('Status')
if status and re.search('R', status):
vprint("message has been read (status header='%s')" % status)
return 0
return False
file_name = None
try:
file_name = get_filename(message)
@ -921,9 +921,9 @@ def is_unread(message):
# maildir mailboxes use the filename suffix to indicate read status
if file_name and re.search(":2,.*S.*$", file_name):
vprint("message has been read (filename info has 'S')")
return 0
return False
vprint("message is unread")
return 1
return True
def sizeof_message(message):
@ -961,18 +961,18 @@ def is_smaller(message, size):
if message_size < size:
vprint("message is too small (%d bytes), minimum bytes : %d" % \
(message_size, size))
return 1
return True
else:
vprint("message is not too small (%d bytes), minimum bytes: %d" % \
(message_size, size))
return 0
return False
def should_archive(message):
"""Return true if we should archive the message, false otherwise"""
if options.archive_all:
return 1
old = 0
return True
old = False
time_message = guess_delivery_time(message)
if options.date_old_max == None:
old = is_older_than_days(time_message, options.days_old_max)
@ -982,14 +982,14 @@ def should_archive(message):
# I could probably do this in one if statement, but then I wouldn't
# understand it.
if not old:
return 0
return False
if not options.include_flagged and is_flagged(message):
return 0
return False
if options.min_size and is_smaller(message, options.min_size):
return 0
return False
if options.preserve_unread and is_unread(message):
return 0
return 1
return False
return True
def is_older_than_time(time_message, max_time):
@ -1005,10 +1005,10 @@ def is_older_than_time(time_message, max_time):
days_old = (max_time - time_message) / 24 / 60 / 60
if time_message < max_time:
vprint("message is %.2f days older than the specified date" % days_old)
return 1
return True
vprint("message is %.2f days younger than the specified date" % \
abs(days_old))
return 0
return False
def is_older_than_days(time_message, max_days):
@ -1023,13 +1023,13 @@ def is_older_than_days(time_message, max_days):
time_now = time.time()
if time_message > time_now:
vprint("warning: message has date in the future")
return 0
return False
secs_old_max = (max_days * 24 * 60 * 60)
days_old = (time_now - time_message) / 24 / 60 / 60
vprint("message is %.2f days old" % days_old)
if ((time_message + secs_old_max) < time_now):
return 1
return 0
return True
return False
def build_imap_filter():
"""Return an imap filter string"""

View File

@ -240,7 +240,7 @@ class TestTempMboxRemove(TestCaseInTempdir):
class TestOptionDefaults(unittest.TestCase):
def testVerbose(self):
"""verbose should be off by default"""
self.assertEqual(archivemail.options.verbose, 0)
self.assertEqual(archivemail.options.verbose, False)
def testDaysOldMax(self):
"""default archival time should be 180 days"""
@ -248,23 +248,23 @@ class TestOptionDefaults(unittest.TestCase):
def testQuiet(self):
"""quiet should be off by default"""
self.assertEqual(archivemail.options.quiet, 0)
self.assertEqual(archivemail.options.quiet, False)
def testDeleteOldMail(self):
"""we should not delete old mail by default"""
self.assertEqual(archivemail.options.delete_old_mail, 0)
self.assertEqual(archivemail.options.delete_old_mail, False)
def testNoCompress(self):
"""no-compression should be off by default"""
self.assertEqual(archivemail.options.no_compress, 0)
self.assertEqual(archivemail.options.no_compress, False)
def testIncludeFlagged(self):
"""we should not archive flagged messages by default"""
self.assertEqual(archivemail.options.include_flagged, 0)
self.assertEqual(archivemail.options.include_flagged, False)
def testPreserveUnread(self):
"""we should not preserve unread messages by default"""
self.assertEqual(archivemail.options.preserve_unread, 0)
self.assertEqual(archivemail.options.preserve_unread, False)
class TestOptionParser(unittest.TestCase):
def setUp(self):
@ -290,7 +290,7 @@ class TestOptionParser(unittest.TestCase):
"""--preserve-unread option is parsed correctly"""
archivemail.options.parse_args(["--preserve-unread"], "")
assert archivemail.options.preserve_unread
archivemail.options.preserve_unread = 0
archivemail.options.preserve_unread = False
archivemail.options.parse_args(["-u"], "")
assert archivemail.options.preserve_unread
@ -307,7 +307,7 @@ class TestOptionParser(unittest.TestCase):
"""--dry-run option is parsed correctly"""
archivemail.options.parse_args(["--dry-run"], "")
assert archivemail.options.dry_run
archivemail.options.preserve_unread = 0
archivemail.options.preserve_unread = False
archivemail.options.parse_args(["-n"], "")
assert archivemail.options.dry_run
@ -403,8 +403,8 @@ class TestIsTooOld(unittest.TestCase):
class TestParseIMAPUrl(unittest.TestCase):
def setUp(self):
archivemail.options.quiet = 1
archivemail.options.verbose = 0
archivemail.options.quiet = True
archivemail.options.verbose = False
archivemail.options.pwfile = None
urls_withoutpass = [
@ -473,8 +473,8 @@ class TestParseIMAPUrl(unittest.TestCase):
archivemail.parse_imap_url, url)
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.verbose = 0
archivemail.options.quiet = False
archivemail.options.verbose = False
archivemail.options.pwfile = None
########## acceptance testing ###########
@ -557,7 +557,7 @@ class TestArchiveMbox(TestArchive):
def setUp(self):
self.oldopts = copy.copy(archivemail.options)
archivemail.options.quiet = 1
archivemail.options.quiet = True
super(TestArchiveMbox, self).setUp()
def testOld(self):
@ -685,7 +685,7 @@ class TestArchiveMboxTimestamp(TestCaseInTempdir):
"""original mbox timestamps should always be preserved"""
def setUp(self):
super(TestArchiveMboxTimestamp, self).setUp()
archivemail.options.quiet = 1
archivemail.options.quiet = True
self.mbox_name = make_mbox(messages=3, hours_old=(24 * 180))
self.mtime = os.path.getmtime(self.mbox_name) - 66
self.atime = os.path.getatime(self.mbox_name) - 88
@ -711,7 +711,7 @@ class TestArchiveMboxTimestamp(TestCaseInTempdir):
self.assertAlmostEqual(self.atime, new_atime, utimes_precision)
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.quiet = False
archivemail.options.days_old_max = 180
os.remove(self.mbox_name)
super(TestArchiveMboxTimestamp, self).tearDown()
@ -719,8 +719,8 @@ class TestArchiveMboxTimestamp(TestCaseInTempdir):
class TestArchiveMboxAll(unittest.TestCase):
def setUp(self):
archivemail.options.quiet = 1
archivemail.options.archive_all = 1
archivemail.options.quiet = True
archivemail.options.archive_all = True
def testNew(self):
"""new messages should be archived with --all"""
@ -733,14 +733,14 @@ class TestArchiveMboxAll(unittest.TestCase):
assert archivemail.should_archive(self.msg)
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.archive_all = 0
archivemail.options.quiet = False
archivemail.options.archive_all = False
class TestArchiveMboxPreserveUnread(unittest.TestCase):
"""make sure the 'preserve_unread' option works"""
def setUp(self):
archivemail.options.quiet = 1
archivemail.options.preserve_unread = 1
archivemail.options.quiet = True
archivemail.options.preserve_unread = True
self.msg = make_message(hours_old=24*181, wantobj=True)
def testOldRead(self):
@ -754,15 +754,15 @@ class TestArchiveMboxPreserveUnread(unittest.TestCase):
assert not archivemail.should_archive(self.msg)
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.preserve_unread = 0
archivemail.options.quiet = False
archivemail.options.preserve_unread = False
class TestArchiveMboxSuffix(unittest.TestCase):
"""make sure the 'suffix' option works"""
def setUp(self):
self.old_suffix = archivemail.options.archive_suffix
archivemail.options.quiet = 1
archivemail.options.quiet = True
def testSuffix(self):
"""archiving with specified --suffix arguments"""
@ -778,7 +778,7 @@ class TestArchiveMboxSuffix(unittest.TestCase):
archivemail.make_archive_name(mbox_name))
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.quiet = False
archivemail.options.archive_suffix = self.old_suffix
@ -786,8 +786,8 @@ class TestArchiveDryRun(TestArchive):
"""make sure the 'dry-run' option works"""
def setUp(self):
super(TestArchiveDryRun, self).setUp()
archivemail.options.quiet = 1
archivemail.options.dry_run = 1
archivemail.options.quiet = True
archivemail.options.dry_run = True
def testOld(self):
"""archiving an old mailbox with the 'dry-run' option"""
@ -796,8 +796,8 @@ class TestArchiveDryRun(TestArchive):
self.verify()
def tearDown(self):
archivemail.options.dry_run = 0
archivemail.options.quiet = 0
archivemail.options.dry_run = False
archivemail.options.quiet = False
super(TestArchiveDryRun, self).tearDown()
@ -805,8 +805,8 @@ class TestArchiveDelete(TestArchive):
"""make sure the 'delete' option works"""
def setUp(self):
super(TestArchiveDelete, self).setUp()
archivemail.options.quiet = 1
archivemail.options.delete_old_mail = 1
archivemail.options.quiet = True
archivemail.options.delete_old_mail = True
def testNew(self):
"""archiving a new mailbox with the 'delete' option"""
@ -827,8 +827,8 @@ class TestArchiveDelete(TestArchive):
self.verify()
def tearDown(self):
archivemail.options.delete_old_mail = 0
archivemail.options.quiet = 0
archivemail.options.delete_old_mail = False
archivemail.options.quiet = False
super(TestArchiveDelete, self).tearDown()
@ -836,8 +836,8 @@ class TestArchiveCopy(TestArchive):
"""make sure the 'copy' option works"""
def setUp(self):
super(TestArchiveCopy, self).setUp()
archivemail.options.quiet = 1
archivemail.options.copy_old_mail = 1
archivemail.options.quiet = True
archivemail.options.copy_old_mail = True
def testNew(self):
"""archiving a new mailbox with the 'copy' option"""
@ -858,16 +858,16 @@ class TestArchiveCopy(TestArchive):
self.verify()
def tearDown(self):
archivemail.options.copy_old_mail = 0
archivemail.options.quiet = 0
archivemail.options.copy_old_mail = False
archivemail.options.quiet = False
super(TestArchiveCopy, self).tearDown()
class TestArchiveMboxFlagged(unittest.TestCase):
"""make sure the 'include_flagged' option works"""
def setUp(self):
archivemail.options.include_flagged = 0
archivemail.options.quiet = 1
archivemail.options.include_flagged = False
archivemail.options.quiet = True
def testOld(self):
"""by default, old flagged messages should not be archived"""
@ -883,20 +883,20 @@ class TestArchiveMboxFlagged(unittest.TestCase):
def testIncludeFlaggedOld(self):
"""old flagged messages should be archived with include_flagged"""
archivemail.options.include_flagged = 1
archivemail.options.include_flagged = True
msg = make_message(default_headers={"X-Status": "F"},
hours_old=24*181, wantobj=True)
assert archivemail.should_archive(msg)
def tearDown(self):
archivemail.options.include_flagged = 0
archivemail.options.quiet = 0
archivemail.options.include_flagged = False
archivemail.options.quiet = False
class TestArchiveMboxOutputDir(unittest.TestCase):
"""make sure that the 'output-dir' option works"""
def setUp(self):
archivemail.options.quiet = 1
archivemail.options.quiet = True
def testOld(self):
"""archiving an old mailbox with a sepecified output dir"""
@ -906,7 +906,7 @@ class TestArchiveMboxOutputDir(unittest.TestCase):
self.assertEqual(dir, os.path.dirname(archive_dir))
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.quiet = False
archivemail.options.output_dir = None
@ -918,8 +918,8 @@ class TestArchiveMboxUncompressed(TestArchive):
copy_name = None
def setUp(self):
archivemail.options.quiet = 1
archivemail.options.no_compress = 1
archivemail.options.quiet = True
archivemail.options.no_compress = True
super(TestArchiveMboxUncompressed, self).setUp()
def testOld(self):
@ -947,15 +947,15 @@ class TestArchiveMboxUncompressed(TestArchive):
self.verify()
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.no_compress = 0
archivemail.options.quiet = False
archivemail.options.no_compress = False
super(TestArchiveMboxUncompressed, self).tearDown()
class TestArchiveSize(unittest.TestCase):
"""check that the 'size' argument works"""
def setUp(self):
archivemail.options.quiet = 1
archivemail.options.quiet = True
msg_text = make_message(hours_old=24*181)
self.msg_size = len(msg_text)
fp = cStringIO.StringIO(msg_text)
@ -972,7 +972,7 @@ class TestArchiveSize(unittest.TestCase):
assert not archivemail.should_archive(self.msg)
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.quiet = False
archivemail.options.min_size = None
@ -980,7 +980,7 @@ class TestArchiveMboxMode(TestCaseInTempdir):
"""file mode (permissions) of the original mbox should be preserved"""
def setUp(self):
super(TestArchiveMboxMode, self).setUp()
archivemail.options.quiet = 1
archivemail.options.quiet = True
def testOld(self):
"""after archiving, the original mbox mode should be preserved"""
@ -1010,7 +1010,7 @@ class TestArchiveMboxMode(TestCaseInTempdir):
os.remove(self.mbox_name)
def tearDown(self):
archivemail.options.quiet = 0
archivemail.options.quiet = False
super(TestArchiveMboxMode, self).tearDown()