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