Commit ff63d6cafb1ef878bb474e09536d7943119036a9
1 parent
75a7b4e0
Exists in
master
and in
39 other branches
PEP8 conformity
Showing
1 changed file
with
31 additions
and
24 deletions
Show diff stats
colab/super_archives/management/commands/import_emails.py
... | ... | @@ -17,10 +17,9 @@ from django.template.loader import render_to_string |
17 | 17 | from django.utils.translation import ugettext as _ |
18 | 18 | |
19 | 19 | from colab.super_archives.utils.email import colab_send_email |
20 | -from colab.super_archives.models import MailingList, Message, \ | |
21 | - Thread, EmailAddress | |
22 | -from colab.super_archives.management.commands.message import Message as \ | |
23 | - CustomMessage | |
20 | +from colab.super_archives.models import (MailingList, Message, | |
21 | + Thread, EmailAddress) | |
22 | +from message import Message as CustomMessage | |
24 | 23 | |
25 | 24 | |
26 | 25 | class Command(BaseCommand, object): |
... | ... | @@ -30,7 +29,7 @@ class Command(BaseCommand, object): |
30 | 29 | |
31 | 30 | default_archives_path = '/var/lib/mailman/archives/private' |
32 | 31 | RE_SUBJECT_CLEAN = re.compile('((re|res|fw|fwd|en|enc):)|\[.*?\]', |
33 | - re.IGNORECASE) | |
32 | + re.IGNORECASE) | |
34 | 33 | THREAD_CACHE = {} |
35 | 34 | EMAIL_ADDR_CACHE = {} |
36 | 35 | |
... | ... | @@ -38,20 +37,23 @@ class Command(BaseCommand, object): |
38 | 37 | |
39 | 38 | # A new command line option to get the dump file to parse. |
40 | 39 | option_list = BaseCommand.option_list + ( |
41 | - make_option('--archives_path', | |
40 | + make_option( | |
41 | + '--archives_path', | |
42 | 42 | dest='archives_path', |
43 | - help='Path of email archives to be imported. (default: %s)' % | |
44 | - default_archives_path, | |
43 | + help=('Path of email archives to be imported. ' | |
44 | + '(default: {})').format(default_archives_path), | |
45 | 45 | default=default_archives_path), |
46 | 46 | |
47 | - make_option('--exclude-list', | |
47 | + make_option( | |
48 | + '--exclude-list', | |
48 | 49 | dest='exclude_lists', |
49 | 50 | help=("Mailing list that won't be imported. It can be used many" |
50 | 51 | "times for more than one list."), |
51 | 52 | action='append', |
52 | 53 | default=None), |
53 | 54 | |
54 | - make_option('--all', | |
55 | + make_option( | |
56 | + '--all', | |
55 | 57 | dest='all', |
56 | 58 | help='Import all messages (default: False)', |
57 | 59 | action="store_true", |
... | ... | @@ -88,11 +90,11 @@ class Command(BaseCommand, object): |
88 | 90 | # tuple (as represented in the code down here) was a valid |
89 | 91 | # option but its performance was too poor. |
90 | 92 | # |
91 | - #for message in tuple(mbox)[index:]: | |
92 | - # yield message | |
93 | + # for message in tuple(mbox)[index:]: | |
94 | + # yield message | |
93 | 95 | # |
94 | 96 | key = index |
95 | - while mbox.has_key(key): | |
97 | + while key in mbox: | |
96 | 98 | key += 1 |
97 | 99 | yield key-1, mbox[key-1] |
98 | 100 | |
... | ... | @@ -127,7 +129,8 @@ class Command(BaseCommand, object): |
127 | 129 | else: |
128 | 130 | try: |
129 | 131 | mailinglist = MailingList.objects.get( |
130 | - name=mailinglist_name) | |
132 | + name=mailinglist_name | |
133 | + ) | |
131 | 134 | n_msgs = mailinglist.last_imported_index |
132 | 135 | except MailingList.DoesNotExist: |
133 | 136 | n_msgs = 0 |
... | ... | @@ -163,7 +166,9 @@ class Command(BaseCommand, object): |
163 | 166 | return |
164 | 167 | |
165 | 168 | # Update last imported message into the DB |
166 | - mailinglist, created = MailingList.objects.get_or_create(name=list_name) | |
169 | + mailinglist, created = MailingList.objects.get_or_create( | |
170 | + name=list_name | |
171 | + ) | |
167 | 172 | mailinglist.last_imported_index = index |
168 | 173 | |
169 | 174 | if created: |
... | ... | @@ -174,7 +179,7 @@ class Command(BaseCommand, object): |
174 | 179 | else: |
175 | 180 | # If the message is already at the database don't do anything |
176 | 181 | try: |
177 | - messages = Message.all_objects.get( | |
182 | + Message.all_objects.get( | |
178 | 183 | message_id=msg_id, |
179 | 184 | thread__mailinglist=mailinglist |
180 | 185 | ) |
... | ... | @@ -260,11 +265,11 @@ class Command(BaseCommand, object): |
260 | 265 | def handle(self, *args, **options): |
261 | 266 | """Main command method.""" |
262 | 267 | |
263 | - | |
264 | 268 | # Already running, so quit |
265 | 269 | if os.path.exists(self.lock_file): |
266 | - self.log(("This script is already running. (If your are sure it's " | |
267 | - "not please delete the lock file in %s')") % self.lock_file) | |
270 | + self.log(("This script is already running. " | |
271 | + "(If your are sure it's not please " | |
272 | + "delete the lock file in {}')").format(self.lock_file)) | |
268 | 273 | sys.exit(0) |
269 | 274 | |
270 | 275 | if not os.path.exists(os.path.dirname(self.lock_file)): |
... | ... | @@ -274,17 +279,19 @@ class Command(BaseCommand, object): |
274 | 279 | self.log('Using archives_path `%s`' % self.default_archives_path) |
275 | 280 | |
276 | 281 | if not os.path.exists(archives_path): |
277 | - raise CommandError('archives_path (%s) does not exist' % | |
278 | - archives_path) | |
282 | + msg = 'archives_path ({}) does not exist'.format(archives_path) | |
283 | + raise CommandError(msg) | |
279 | 284 | run_lock = file(self.lock_file, 'w') |
280 | 285 | run_lock.close() |
281 | 286 | |
282 | 287 | try: |
283 | - self.import_emails(archives_path, | |
284 | - options.get('all'), options.get('exclude_lists')) | |
288 | + self.import_emails( | |
289 | + archives_path, | |
290 | + options.get('all'), | |
291 | + options.get('exclude_lists'), | |
292 | + ) | |
285 | 293 | except Exception as e: |
286 | 294 | logging.exception(e) |
287 | 295 | raise |
288 | 296 | finally: |
289 | 297 | os.remove(self.lock_file) |
290 | - | ... | ... |