view commentator/model.py @ 5:451169e51935

work to make cache expiry work
author k0s <k0scist@gmail.com>
date Fri, 26 Feb 2010 12:27:25 -0500
parents f02a3672254e
children
line wrap: on
line source

import os
import pickle

from datetime import datetime

# need a timezone object, which python *should*
# have by default
try:
    from webob import _UTC # XXX
except ImportError:
    from webob.datetime_utils import UTC
        
class PickleComments(object):
    # TODO: locking
    def __init__(self, database):
        self.database = database
        if not os.path.exists(database):
            f = file(database, 'w')
            pickle.dump({}, f)
            f.close()

    def comment(self, uri, **kw):
        now = datetime.utcnow()
        now.replace(tzinfo=UTC)

        kw['date'] = now
        f = file(self.database)
        comments = pickle.load(f)
        f.close()
        comments.setdefault(uri, []).append(kw)
        f = file(self.database, 'w')
        comments = pickle.dump(comments, f)
        f.close()

    def comments(self, uri):
        f = file(self.database)
        comments = pickle.load(f)
        f.close()
        return comments.get(uri, [])

    
try:
    import couchdb

    class CouchComments(object):
        def __init__(self, db):
            self.couch = couchdb.Server()
            if db not in self.couch:
                self.db = self.couch.create(db)
            else:
                self.db = self.couch[db]

        def comment(self, uri, **kw):
            if uri in self.db:
                comments = self.db[uri]['comments']
                comments.append(kw)
                self.db[uri] = { 'comments': comments }
            else:
                self.db[uri] = { 'comments': [ kw ] }

        def comments(self, uri):
            if uri in self.db:
                doc = self.db[uri]
                return doc ['comments']
            return []

except ImportError:
    pass