diff options
| author | toshio <toshio@mm3test.fedoraproject.org> | 2012-03-12 23:27:56 +0000 |
|---|---|---|
| committer | toshio <toshio@mm3test.fedoraproject.org> | 2012-03-12 23:27:56 +0000 |
| commit | 1c4f1cb5a0332a1fcc4d2fa8cf908f029cf23594 (patch) | |
| tree | 53779390ff498842d2b919da62182fe582d4d0ca /lib | |
| download | hyperkitty-1c4f1cb5a0332a1fcc4d2fa8cf908f029cf23594.tar.gz hyperkitty-1c4f1cb5a0332a1fcc4d2fa8cf908f029cf23594.tar.xz hyperkitty-1c4f1cb5a0332a1fcc4d2fa8cf908f029cf23594.zip | |
Import the initial version of the hyperkitty archiver
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/__init__.py | 14 | ||||
| -rw-r--r-- | lib/mockup.py | 167 | ||||
| -rw-r--r-- | lib/notmuch.py | 105 |
3 files changed, 286 insertions, 0 deletions
diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..aa5a3d9 --- /dev/null +++ b/lib/__init__.py @@ -0,0 +1,14 @@ +#-*- coding: utf-8 -*- + +from hashlib import md5 +import urllib + + +def gravatar_url(email): + '''Return a gravatar url for an email address''' + size = 64 + default = "http://fedoraproject.org/static/images/" + \ + "fedora_infinity_%ix%i.png" % (size, size) + query_string = urllib.urlencode({'s': size, 'd': default}) + identifier = md5(email).hexdigest() + return 'http://www.gravatar.com/avatar/%s?%s' % (identifier, query_string) diff --git a/lib/mockup.py b/lib/mockup.py new file mode 100644 index 0000000..6dfe298 --- /dev/null +++ b/lib/mockup.py @@ -0,0 +1,167 @@ +#-*- coding: utf-8 -*- + + +class Email(object): + """ Email class containing the information needed to store and + display email threads. + """ + + def __init__(self): + """ Constructor. + Instanciate the default attributes of the object. + """ + self.email_id = '' + self.title = '' + self.body = '' + self.tags = [] + self.category = 'question' + self.category_tag = None + self.participants = set(['Pierre-Yves Chibon']) + self.answers = [] + self.liked = 0 + self.author = '' + self.avatar = None + +class Author(object): + """ Author class containing the information needed to get the top + author of the month! + """ + + def __init__(self): + """ Constructor. + Instanciate the default attributes of the object. + """ + self.name = None + self.kudos = 0 + self.avatar = None + + +def get_email_tag(tag): + threads = generate_random_thread() + output = [] + for email in threads: + if tag in email.tags or tag in email.category: + output.append(email) + elif email.category_tag and tag in email.category_tag: + output.append(email) + return output + + +def generate_thread_per_category(): + threads = generate_random_thread() + categories = {} + for thread in threads: + category = thread.category + if thread.category_tag: + category = thread.category_tag + if category in categories.keys(): + categories[category].append(thread) + else: + categories[category] = [thread] + return categories + +def generate_top_author(): + authors = [] + + author = Author() + author.name = 'Pierre-Yves Chibon' + author.avatar = 'https://secure.gravatar.com/avatar/072b4416fbfad867a44bc7a5be5eddb9' + author.kudos = 3 + authors.append(author) + + author = Author() + author.name = 'Stanislav Ochotnický' + author.avatar = 'http://sochotni.fedorapeople.org/sochotni.jpg' + author.kudos = 4 + authors.append(author) + + author = Author() + author.name = 'Toshio Kuratomi' + author.avatar = 'https://secure.gravatar.com/avatar/7a9c1d88f484c9806bceca0d6d91e948' + author.kudos = 5 + authors.append(author) + + return authors + +def generate_random_thread(): + threads = [] + + ## 1 + email = Email() + email.email_id = 1 + email.title = 'Headsup! krb5 ccache defaults are changing in Rawhide' + email.age = '6 days' + email.body = '''Dear fellow developers, +with the upcoming Fedora 18 release (currently Rawhide) we are going to change the place where krb5 credential cache files are saved by default. + +The new default for credential caches will be the /run/user/username directory. +''' + email.tags.extend(['rawhide', 'krb5']) + email.participants = set(['Stephen Gallagher', 'Toshio Kuratomi', 'Kevin Fenzi', 'Seth Vidal']) + email.answers.extend([1,2,3,4,5,6,7,8,9,10,11,12]) + email.liked = 1 + email.author = 'Stephen Gallagher' + email.avatar = 'http://fedorapeople.org/~sgallagh/karrde712.png' + threads.append(email) + + ## 2 + email = Email() + email.email_id = 2 + email.title = 'Problem in packaging kicad' + email.age = '6 days' + email.body = '''Paragraph 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ''' + email.tags.extend(['packaging', 'kicad']) + email.participants = set(['Pierre-Yves Chibon', 'Tom "spot" Callaway', 'Toshio Kuratomi', 'Kevin Fenzi']) + email.answers.extend([1,2,3,4,5,6,7,8,9,10,11,12]) + email.liked = 0 + email.author = 'Pierre-Yves Chibon' + email.avatar = 'https://secure.gravatar.com/avatar/072b4416fbfad867a44bc7a5be5eddb9' + threads.append(email) + + ## 3 + email = Email() + email.email_id = 3 + email.title = 'Update Java Guideline' + email.age = '6 days' + email.body = '''Paragraph 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ''' + email.tags.extend(['rawhide', 'krb5']) + email.participants = set(['Stanislav Ochotnický', 'Tom "spot" Callaway', 'Stephen Gallagher', 'Jason Tibbitts', 'Rex Dieter', 'Toshio Kuratomi']) + email.answers.extend([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]) + email.liked = 5 + email.category = 'todo' + email.author = 'Stanislav Ochotnický' + email.avatar = 'http://sochotni.fedorapeople.org/sochotni.jpg' + threads.append(email) + + ## 4 + email = Email() + email.email_id = 4 + email.title = 'Agenda for the next Board Meeting' + email.age = '6 days' + email.body = '''Paragraph 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ''' + email.tags.extend(['agenda', 'board']) + email.participants = set(['Toshio Kuratomi', 'Tom "spot" Callaway', 'Robyn Bergeron', 'Max Spevack']) + email.answers.extend([1,2,3,4,5,6,7,8,9,10,11,12]) + email.liked = 20 + email.category = 'agenda' + email.author = 'Toshio Kuratomi' + email.avatar = 'https://secure.gravatar.com/avatar/7a9c1d88f484c9806bceca0d6d91e948' + threads.append(email) + + ## 5 + email = Email() + email.email_id = 5 + email.title = 'I told you so! ' + email.age = '6 days' + email.body = '''Paragraph 1: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ''' + email.tags.extend(['systemd', 'mp3', 'pulseaudio']) + email.participants = set(['Pierre-Yves Chibon']) + email.answers.extend([1,2,3,4,5,6,7,8,9,10,11,12]) + email.liked = 0 + email.author = 'Pierre-Yves Chibon' + email.avatar = 'https://secure.gravatar.com/avatar/072b4416fbfad867a44bc7a5be5eddb9' + email.category = 'shut down' + email.category_tag = 'dead' + threads.append(email) + + return threads diff --git a/lib/notmuch.py b/lib/notmuch.py new file mode 100644 index 0000000..848adea --- /dev/null +++ b/lib/notmuch.py @@ -0,0 +1,105 @@ +#-*- coding: utf-8 -*- + +from __future__ import absolute_import + +from calendar import timegm +from datetime import datetime, timedelta +import json +import os + +import bunch +import notmuch + +from mm_app.lib import gravatar_url + +# Used to remove tags that notmuch added automatically that we don't want +IGNORED_TAGS = (u'inbox', u'unread', u'signed') + +def get_ro_db(path): + # Instead of throwing an exception, notmuch bindings tend to segfault if + # the database path doesn't exist. + + # Does the notmuch db exist? + actual_db_dir = os.path.join(path, '.notmuch') + if os.access(actual_db_dir, os.W_OK|os.X_OK) and os.path.isdir(actual_db_dir): + return notmuch.Database(path, + mode=notmuch.Database.MODE.READ_ONLY) + + raise IOError('Notmuch database not present in %(path)s' % + {'path': path}) + +def get_thread_info(thread): + thread_info = bunch.Bunch() + + # + # Get information about the first email of a thread + # + + first_email = tuple(thread.get_toplevel_messages())[0] + for tag in (tg for tg in first_email.get_tags() if tg.startswith('=msgid=')): + thread_info.email_id = tag.split('=msgid=', 1)[-1] + break + first_email_data = json.loads(first_email.format_message_as_json()) + + # Python-3.3 has ''.rsplit(maxsplit=1) (keyword arg). Until then, we need + # rsplit(None, 1) to get the desired behaviour + author = first_email_data['headers']['From'].rsplit(None, 1) + if author[-1].startswith('<'): + author[-1] = author[-1][1:] + if author[-1].endswith('>'): + author[-1] = author[-1][:-1] + # This accounts for From lines without a real name, just email address + name = author[0] + email = author[-1] + thread_info.author = name + thread_info.avatar = gravatar_url(email) + + for body_part in first_email_data['body']: + try: + # The body may have many parts. We only want the part that we + # can guess is the actual text of an email message. + # For this prototype, that is defined as + # has a content-type and content keys. and the content-type + # is text/plain. When this is not a prototype, the heuristic + # should be more advanced + if body_part['content-type'] == u'text/plain': + thread_info.body = body_part['content'] + break + except KeyError: + continue + + # + # Get meta info about the thread itself + # + + # Used for sorting threads + thread_info.most_recent = thread.get_newest_date() + date_as_offset = timegm(datetime.utcnow().timetuple()) - thread_info.most_recent + thread_info.age = str(timedelta(seconds=date_as_offset)) + thread_info.title = thread.get_subject() + # Because notmuch doesn't allow us to extend the schema, everything is + # in a tag. Extract those tags that have special meaning to us + thread_info.tags = [] + thread_info.answers = [] + thread_info.liked = 0 + for tag in thread.get_tags(): + if tag.startswith('=msgid='): + msgid = tag.split('=msgid=', 1)[-1] + # The first email doesn't count as a reply :-) + if msgid != thread_info.email_id: + thread_info.answers.append(tag.split('=msgid=', 1)[-1]) + elif tag.startswith('=threadlike='): + thread_info.liked = int(tag.split('=threadlike=', 1)[-1]) + elif tag.startswith('=topic='): + thread_info.category = tag.split('=topic=', 1)[-1] + print thread_info.category + elif tag in IGNORED_TAGS: + continue + else: + thread_info.tags.append(tag) + # notmuch has this nice method call to give us the info but it returns + # it as a string instead of a list + thread_info.participants = set(thread.get_authors().replace(u'| ', u', ', 1).split(u', ')) + + + return thread_info |
