Logo Search packages:      
Sourcecode: webboard version File versions  Download package


# wbhistory.py
# Manage the histroy of previous posts

import gtk
import gconf
import time
import datetime
import gettext
from gettext import gettext as _
import logging

class WebBoardHistory:
        def __init__(self, config, notify_func=None):
            self.config = config
            self.notify_func = notify_func
            self.config.gconf.notify_add("/apps/webboard/history", self.notify)

        def add_paste(self, post, url):
            # set a nice title for the link
            text = post[:].strip(" \n\t")
            text.replace("\n", "")
            if len(text) > 26:
                text = "%s..." % text[0:20]
            stamp = time.time()
            # add to gconf
            self.add(stamp, text, url)

        def add(self, stamp_new, title_new, url_new):
            # do not keep more than five sites
            while len(self.sites) >= 5:
            # convert the nested history list to flat one
            # gconf doesn't only support flat lists
            _flat_list.extend((str(stamp_new), title_new, url_new))
            if len(self.sites) > 0:
                for id_old, stamp_old, title_old, url_old in self.sites:
                    _flat_list.extend((str(stamp_old),title_old, url_old))
            self.config.gconf.set_list("/apps/webboard/history", \
                                gconf.VALUE_STRING, _flat_list)

        def notify(self, client, cnxn_id, entry, user_data):
            # update the history
            # send the nofication signal to the childrens
            if self.notify_func is not None:

        def update(self):
            # read the history from gconf
            _flat_list = self.config.gconf.get_list("/apps/webboard/history", \
            # convert the flat list into a nested one
            # e.g. [(stamp, title, url), (stamp2, title2, url2)]
                if len(_flat_list) > 0:
                    while len(_flat_list) != 0:
                        stamp = float(_flat_list.pop(0))
                        title = _flat_list.pop(0)
                        url = _flat_list.pop(0)
                        id = len(self.sites)
                        self.sites.append((id, stamp, title, url))
                logging.warning("Failed to read the history from gconf - reseting")

        def age(self, id):
            now = datetime.datetime.now()
            sent = datetime.datetime.fromtimestamp(float(self.sites[int(id)][1]))

            diff = now -sent
            weeks, days = divmod(diff.days, 7)
            minutes, seconds = divmod(diff.seconds, 60)
            hours, minutes = divmod(minutes, 60)

            if weeks > 0:
                ret = gettext.ngettext("%s week ago", "%s weeks ago", \
                                       weeks) % weeks
            elif days > 0:
                ret = gettext.ngettext("%s day ago", "%s days ago", 
                                       days) % days
            elif hours > 0:
                ret = gettext.ngettext("%s hour ago", "%s hours ago", \
                                       hours) % hours
            elif minutes > 0:
                ret = gettext.ngettext("%s minute ago", \
                                       "%s minutes ago", minutes) % minutes
            elif seconds > 0:
                ret = gettext.ngettext("%s second ago", "%s seconds ago",
                                       seconds) % seconds
            return ret

Generated by  Doxygen 1.6.0   Back to index