summaryrefslogtreecommitdiff
path: root/service/pixelated/adapter
diff options
context:
space:
mode:
authorPatrick Maia and Victor Shyba <pixelated-team+pmaia+vshyba@thoughtworks.com>2014-08-26 22:06:43 +0000
committerPatrick Maia <pmaia@thoughtworks.com>2014-08-26 22:27:48 +0000
commit6592bfad8cb90d7256de949b181ed9d0b684afec (patch)
tree0541e5a280ea168d4b8603344449916c5c007593 /service/pixelated/adapter
parentcf3ea5411adb12dd1bdc12ec01a17b37d974c7c3 (diff)
extracts tag and status conversion logic
Diffstat (limited to 'service/pixelated/adapter')
-rw-r--r--service/pixelated/adapter/mail_service.py9
-rw-r--r--service/pixelated/adapter/pixelated_mail.py43
-rw-r--r--service/pixelated/adapter/status.py43
-rw-r--r--service/pixelated/adapter/tag.py68
4 files changed, 121 insertions, 42 deletions
diff --git a/service/pixelated/adapter/mail_service.py b/service/pixelated/adapter/mail_service.py
index 82925870..f2db8d16 100644
--- a/service/pixelated/adapter/mail_service.py
+++ b/service/pixelated/adapter/mail_service.py
@@ -22,7 +22,6 @@ from pixelated.bitmask_libraries.provider import LeapProvider
from pixelated.bitmask_libraries.session import LeapSessionFactory
from pixelated.bitmask_libraries.auth import LeapCredentials
from pixelated.adapter.pixelated_mail import PixelatedMail
-from pixelated.tags import Tags
class MailService:
@@ -34,7 +33,6 @@ class MailService:
self.server_name = server_name
self.mailbox_name = 'INBOX'
self.certs_home = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "certificates"))
- self.tags = Tags()
self._open_leap_session()
except:
traceback.print_exc(file=sys.stdout)
@@ -64,10 +62,11 @@ class MailService:
def _update_tag_list(self, tags):
for tag in tags:
- self.tags.add(tag)
+ pass
+ # self.tags.add(tag)
def _update_flags(self, new_tags, mail_id):
- new_tags_flag_name = ['tag_' + tag.name for tag in new_tags if tag.name not in Tags.SPECIAL_TAGS]
+ new_tags_flag_name = ['tag_' + tag.name for tag in new_tags if tag.name not in ['inbox', 'drafts', 'sent', 'trash']]
self.set_flags(mail_id, new_tags_flag_name)
def set_flags(self, mail_id, new_tags_flag_name):
@@ -80,7 +79,7 @@ class MailService:
return PixelatedMail.from_leap_mail(message)
def all_tags(self):
- return self.tags
+ return []
def thread(self, thread_id):
raise NotImplementedError()
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py
index 4169bb23..3e4e415b 100644
--- a/service/pixelated/adapter/pixelated_mail.py
+++ b/service/pixelated/adapter/pixelated_mail.py
@@ -13,32 +13,13 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
-from pixelated.tags import Tag
-from pixelated.tags import Tags
+from pixelated.adapter.tag import Tag
+from pixelated.adapter.status import Status
import dateutil.parser as dateparser
class PixelatedMail:
- LEAP_FLAGS = ['\\Seen',
- '\\Answered',
- '\\Flagged',
- '\\Deleted',
- '\\Draft',
- '\\Recent',
- 'List']
-
- LEAP_FLAGS_STATUSES = {
- '\\Seen': 'read',
- '\\Answered': 'replied'
- }
-
- LEAP_FLAGS_TAGS = {
- '\\Deleted': 'trash',
- '\\Draft': 'drafts',
- '\\Recent': 'inbox'
- }
-
def __init__(self):
pass
@@ -56,8 +37,7 @@ class PixelatedMail:
return mail
def _extract_status(self):
- flags = self.leap_mail.getFlags()
- return [converted for flag, converted in self.LEAP_FLAGS_STATUSES.items() if flag in flags]
+ return Status.from_flags(self.leap_mail.getFlags())
def _extract_headers(self):
temporary_headers = {}
@@ -69,21 +49,9 @@ class PixelatedMail:
def _extract_tags(self):
flags = self.leap_mail.getFlags()
- tag_names = self._converted_tags(flags) + self._custom_tags(flags)
- tags = []
- for tag in tag_names:
- tags.append(Tag(tag))
+ tags = set(Tag.from_flag(flag) for flag in flags)
return tags
- def _converted_tags(self, flags):
- return [converted for flag, converted in self.LEAP_FLAGS_TAGS.items() if flag in flags]
-
- def _custom_tags(self, flags):
- return [self._remove_prefix(flag) for flag in self.leap_mail.getFlags() if flag.startswith('tag_')]
-
- def _remove_prefix(self, flag_name):
- return flag_name.replace('tag_', '', 1)
-
def update_tags(self, tags):
self.tags = [Tag(tag) for tag in tags]
return self.tags
@@ -93,11 +61,12 @@ class PixelatedMail:
def as_dict(self):
tags = [tag.name for tag in self.tags]
+ statuses = [status.name for status in self.status]
return {
'header': self.headers,
'ident': self.ident,
'tags': tags,
- 'status': self.status,
+ 'status': statuses,
'security_casing': self.security_casing,
'body': self.body
}
diff --git a/service/pixelated/adapter/status.py b/service/pixelated/adapter/status.py
new file mode 100644
index 00000000..2002b649
--- /dev/null
+++ b/service/pixelated/adapter/status.py
@@ -0,0 +1,43 @@
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+
+class Status:
+
+ LEAP_FLAGS_STATUSES = {
+ '\\Seen': 'read',
+ '\\Answered': 'replied'
+ }
+
+ @classmethod
+ def from_flag(cls, flag):
+ return Status(cls.LEAP_FLAGS_STATUSES[flag])
+
+ @classmethod
+ def from_flags(cls, flags):
+ return set(cls.from_flag(flag) for flag in flags if flag in cls.LEAP_FLAGS_STATUSES.keys())
+
+ def __init__(self, name):
+ self.name = name
+ self.ident = name.__hash__()
+
+ def __eq__(self, other):
+ return self.name == other.name
+
+ def __hash__(self):
+ return self.name.__hash__()
+
+ def __repr__(self):
+ return self.name
diff --git a/service/pixelated/adapter/tag.py b/service/pixelated/adapter/tag.py
new file mode 100644
index 00000000..daef30fa
--- /dev/null
+++ b/service/pixelated/adapter/tag.py
@@ -0,0 +1,68 @@
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+import json
+
+
+class Tag:
+
+ LEAP_FLAGS_TAGS = {
+ '\\Deleted': 'trash',
+ '\\Draft': 'drafts',
+ '\\Recent': 'inbox'
+ }
+
+ @classmethod
+ def from_flags(cls, flags):
+ return set(filter(None, (cls.from_flag(flag) for flag in flags)))
+
+ @classmethod
+ def from_flag(cls, flag):
+ if flag in cls.LEAP_FLAGS_TAGS.keys():
+ return Tag(cls.LEAP_FLAGS_TAGS[flag])
+ if flag.startswith('tag_'):
+ return Tag(cls._remove_prefix(flag))
+ return None
+
+ @classmethod
+ def _remove_prefix(cls, flag_name):
+ return flag_name.replace('tag_', '', 1)
+
+ def __init__(self, name, default=False):
+ self.name = name
+ self.default = default
+ self.ident = name.__hash__()
+
+ def __eq__(self, other):
+ return self.name == other.name
+
+ def __hash__(self):
+ return self.name.__hash__()
+
+ def as_dict(self):
+ return {
+ 'name': self.name,
+ 'default': self.default,
+ 'ident': self.ident,
+ 'counts': {
+ 'total': 0,
+ 'read': 0,
+ 'starred': 0,
+ 'replied': 0
+ }
+ }
+
+ def __repr__(self):
+ return self.name