From ff110f03e4bdf526e61d844e0c8296ad804d511f Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 18 Aug 2014 09:50:27 -0300 Subject: Apply PEP8 automatically --- py-fake-service/app/adapter/contacts.py | 11 ++++++++--- py-fake-service/app/adapter/mail.py | 18 ++++++++++++------ py-fake-service/app/adapter/mail_service.py | 17 ++++++++++------- py-fake-service/app/adapter/mailset.py | 8 ++++++-- py-fake-service/app/adapter/tag.py | 6 +++--- py-fake-service/app/adapter/tagsset.py | 8 ++++---- py-fake-service/app/pixelated_user_agent.py | 15 ++++++++++----- py-fake-service/app/search/search_query.py | 26 ++++++++++++-------------- 8 files changed, 65 insertions(+), 44 deletions(-) (limited to 'py-fake-service/app') diff --git a/py-fake-service/app/adapter/contacts.py b/py-fake-service/app/adapter/contacts.py index 45aa2177..aaf2d23e 100644 --- a/py-fake-service/app/adapter/contacts.py +++ b/py-fake-service/app/adapter/contacts.py @@ -1,6 +1,8 @@ -import re +import re + class Contacts: + def __init__(self): self.contacts = [] @@ -10,10 +12,13 @@ class Contacts: def search(self, query): contacts_query = re.compile(query) - return [contact.__dict__ for contact in self.contacts if contacts_query.match(contact.addresses[0])] + return [ + contact.__dict__ for contact in self.contacts if contacts_query.match( + contact.addresses[0])] + class Contact: + def __init__(self, contact): self.addresses = [contact] self.name = '' - diff --git a/py-fake-service/app/adapter/mail.py b/py-fake-service/app/adapter/mail.py index a6dee92e..30a53641 100644 --- a/py-fake-service/app/adapter/mail.py +++ b/py-fake-service/app/adapter/mail.py @@ -2,9 +2,14 @@ from datetime import datetime import random import calendar + class Mail: - NOW = calendar.timegm(datetime.strptime(datetime.now().isoformat(), "%Y-%m-%dT%H:%M:%S.%f").timetuple()) + NOW = calendar.timegm( + datetime.strptime( + datetime.now().isoformat(), + "%Y-%m-%dT%H:%M:%S.%f").timetuple()) + @staticmethod def from_json(mail_json): mail = Mail() @@ -18,7 +23,6 @@ class Mail: mail.draft_reply_for = mail_json.get('draft_reply_for', 0) return mail - def __init__(self, mbox_mail=None, ident=None): if mbox_mail: self.header = self._get_headers(mbox_mail) @@ -31,7 +35,8 @@ class Mail: def _get_body(self, message): if message.is_multipart(): - boundary = '--{boundary}'.format(boundary= message.get_boundary().strip()) + boundary = '--{boundary}'.format( + boundary=message.get_boundary().strip()) body_parts = [x.as_string() for x in message.get_payload()] body = boundary + '\n' @@ -42,7 +47,6 @@ class Mail: else: return message.get_payload() - def _get_status(self): status = [] if 'sent' in self.tags: @@ -55,7 +59,10 @@ class Mail: headers['from'] = mbox_mail.get_from() headers['to'] = [mbox_mail.get('To')] headers['subject'] = mbox_mail.get('Subject') - headers['date'] = datetime.fromtimestamp(random.randrange(1222222222, self.NOW)).isoformat() + headers['date'] = datetime.fromtimestamp( + random.randrange( + 1222222222, + self.NOW)).isoformat() headers['content_type'] = mbox_mail.get('Content-Type') return headers @@ -63,7 +70,6 @@ class Mail: def _get_tags(self, mbox_mail): return mbox_mail.get('X-TW-Pixelated-Tags').split(', ') - @property def subject(self): return self.header['subject'] diff --git a/py-fake-service/app/adapter/mail_service.py b/py-fake-service/app/adapter/mail_service.py index 41195c0a..3da7ce59 100644 --- a/py-fake-service/app/adapter/mail_service.py +++ b/py-fake-service/app/adapter/mail_service.py @@ -6,6 +6,7 @@ from mailset import MailSet from contacts import Contacts from mail import Mail + class MailService: MAILSET_PATH = os.path.join(os.environ['HOME'], 'mailsets', 'mediumtagged') @@ -15,8 +16,13 @@ class MailService: self.contacts = Contacts() def load_mailset(self): - mbox_filenames = [filename for filename in os.listdir(self.MAILSET_PATH) if filename.startswith('mbox')] - boxes = (mailbox.mbox(os.path.join(self.MAILSET_PATH, mbox)) for mbox in mbox_filenames) + mbox_filenames = [ + filename + for filename in os.listdir + (self.MAILSET_PATH) if filename.startswith('mbox')] + boxes = (mailbox.mbox + (os.path.join(self.MAILSET_PATH, mbox)) + for mbox in mbox_filenames) for box in boxes: message = box.popitem()[1] @@ -44,7 +50,6 @@ class MailService: if not purged: self.tagsset.increment_tag_total_count('trash') - def update_tags_for(self, mail_id, new_tags): mail = self.mail(mail_id) @@ -54,8 +59,8 @@ class MailService: increment_set = new_tags_set - old_tags_set decrement_set = old_tags_set - new_tags_set - map(lambda x : self.tagsset.increment_tag_total_count(x), increment_set) - map(lambda x : self.tagsset.decrement_tag_total_count(x), decrement_set) + map(lambda x: self.tagsset.increment_tag_total_count(x), increment_set) + map(lambda x: self.tagsset.decrement_tag_total_count(x), decrement_set) mail.tags = new_tags @@ -70,7 +75,6 @@ class MailService: mail = self.mailset.add_draft(Mail.from_json(mail)) return mail.ident - def update_draft(self, mail): mail = Mail.from_json(mail) self.mailset.update(mail) @@ -78,4 +82,3 @@ class MailService: def draft_reply_for(self, mail_id): return self.mailset.find(draft_reply_for=mail_id) - diff --git a/py-fake-service/app/adapter/mailset.py b/py-fake-service/app/adapter/mailset.py index 0b55bdc2..d08a58cb 100644 --- a/py-fake-service/app/adapter/mailset.py +++ b/py-fake-service/app/adapter/mailset.py @@ -1,5 +1,6 @@ from mail import Mail + class MailSet: def __init__(self): @@ -27,7 +28,7 @@ class MailSet: if 'trash' in mail.tags: del self.mails[mail_id] return True - mail.tags.append('trash') + mail.tags.append('trash') return False def update(self, mail): @@ -40,7 +41,10 @@ class MailSet: return mail def find(self, draft_reply_for): - match = [mail for mail in self.mails.values() if mail.draft_reply_for ==draft_reply_for] + match = [ + mail + for mail in self.mails.values + () if mail.draft_reply_for == draft_reply_for] if len(match) == 0: return None else: diff --git a/py-fake-service/app/adapter/tag.py b/py-fake-service/app/adapter/tag.py index bc5bee95..c982f524 100644 --- a/py-fake-service/app/adapter/tag.py +++ b/py-fake-service/app/adapter/tag.py @@ -1,5 +1,5 @@ class Tag: - DEFAULT_TAGS = ["inbox", "sent", "trash", "drafts"] + DEFAULT_TAGS = ["inbox", "sent", "trash", "drafts"] def __init__(self, name, ident): self.counts = { @@ -7,7 +7,7 @@ class Tag: 'read': 0, 'starred': 0, 'reply': 0 - } + } self.ident = ident self.name = name.lower() @@ -17,7 +17,7 @@ class Tag: self.counts['total'] += 1 def increment_read(self): - self.counts['read'] += 1 + self.counts['read'] += 1 def decrement_count(self): self.counts['total'] -= 1 diff --git a/py-fake-service/app/adapter/tagsset.py b/py-fake-service/app/adapter/tagsset.py index 67f4379d..41d50ead 100644 --- a/py-fake-service/app/adapter/tagsset.py +++ b/py-fake-service/app/adapter/tagsset.py @@ -1,6 +1,8 @@ from tag import Tag + class TagsSet: + def __init__(self): self.tags = {} self.ident = 0 @@ -10,7 +12,7 @@ class TagsSet: for tag in tags: tag = self._create_new_tag(tag) tag.increment_count() - + def all_tags(self): return self.tags.values() @@ -26,7 +28,7 @@ class TagsSet: tag.increment_count() else: self._create_new_tag(tagname) - + def decrement_tag_total_count(self, tag): self.tags.get(tag).decrement_count() @@ -35,5 +37,3 @@ class TagsSet: tag = self.tags.setdefault(tag.name, tag) self.ident += 1 return tag - - diff --git a/py-fake-service/app/pixelated_user_agent.py b/py-fake-service/app/pixelated_user_agent.py index bae95dee..7b163d7e 100644 --- a/py-fake-service/app/pixelated_user_agent.py +++ b/py-fake-service/app/pixelated_user_agent.py @@ -13,6 +13,7 @@ account = None loaded = False mail_service = MailService() + def respond_json(entity): response = json.dumps(entity) return Response(response=response, mimetype="application/json") @@ -20,7 +21,7 @@ def respond_json(entity): @app.route('/disabled_features') def disabled_features(): - return respond_json([]) + return respond_json([]) @app.route('/mails', methods=['POST']) @@ -44,7 +45,7 @@ def update_draft(): @app.route('/mails') def mails(): query = SearchQuery.compile(request.args.get('q', '')) - page = request.args.get('p', '') + page = request.args.get('p', '') window_size = request.args.get('w', '') fetched_mails = mail_service.mails(query, page, window_size) @@ -91,10 +92,12 @@ def mark_mail_as_read(mail_id): mail_service.mark_as_read(mail_id) return "" + @app.route('/contacts') def contacts(): contacts_query = request.args.get('q') - return respond_json({'contacts': mail_service.search_contacts(contacts_query)}) + return respond_json( + {'contacts': mail_service.search_contacts(contacts_query)}) @app.route('/draft_reply_for/') @@ -115,7 +118,9 @@ def load_mailset(mailset): os.mkdir(mbox_root) if len(os.listdir(mbox_root)) == 0: - response = requests.get('https://example.wazokazi.is:8154/go/static/mediumtagged.tar.gz', verify=False) + response = requests.get( + 'https://example.wazokazi.is:8154/go/static/mediumtagged.tar.gz', + verify=False) mbox_archive_path = os.path.join(mbox_root, 'mediumtagged.tar.gz') mbox_archive = open(mbox_archive_path, 'w') mbox_archive.write(response.content) @@ -134,7 +139,7 @@ def index(): if not loaded: load_mailset('mediumtagged') loaded = True - + return app.send_static_file('index.html') diff --git a/py-fake-service/app/search/search_query.py b/py-fake-service/app/search/search_query.py index 83c0bbca..765d1008 100644 --- a/py-fake-service/app/search/search_query.py +++ b/py-fake-service/app/search/search_query.py @@ -3,27 +3,27 @@ import re def _next_token(): - return StringRegexp('[^\s]+') + return StringRegexp('[^\s]+') -def _separators(): - return StringRegexp('[\s&]+') +def _separators(): + return StringRegexp('[\s&]+') def _compile_tag(compiled, token): - tag = token.split(":").pop() - if token[0] == "-": + tag = token.split(":").pop() + if token[0] == "-": compiled["not_tags"].append(tag) - else: - compiled["tags"].append(tag) - return compiled + else: + compiled["tags"].append(tag) + return compiled class SearchQuery: @staticmethod def compile(query): - compiled = {"tags": [], "not_tags": [], "general":[]} + compiled = {"tags": [], "not_tags": [], "general": []} scanner = StringScanner(query.encode('utf8').replace("\"", "")) first_token = True @@ -56,15 +56,13 @@ class SearchQuery: return True if self.compiled.get('general'): - search_terms = re.compile(self.compiled['general'], flags=re.IGNORECASE) + search_terms = re.compile( + self.compiled['general'], + flags=re.IGNORECASE) if search_terms.search(mail.body) or search_terms.search(mail.subject): return True if not [v for v in self.compiled.values() if v]: return True - return False - - - -- cgit v1.2.3