Mercurial > hg > commentator
annotate commentator/model.py @ 3:d0d8524d9495
finish basic API...still doesnt expire results correctly
author | k0s <k0scist@gmail.com> |
---|---|
date | Fri, 26 Feb 2010 11:06:48 -0500 |
parents | 689b9d928dc8 |
children | f02a3672254e |
rev | line source |
---|---|
0 | 1 import os |
2 import pickle | |
3 | |
4 from datetime import datetime | |
3
d0d8524d9495
finish basic API...still doesnt expire results correctly
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
5 from webob import _UTC |
0 | 6 |
7 class PickleComments(object): | |
8 # TODO: locking | |
9 def __init__(self, database): | |
10 self.database = database | |
11 if not os.path.exists(database): | |
12 f = file(database, 'w') | |
13 pickle.dump({}, f) | |
14 f.close() | |
15 | |
16 def comment(self, uri, **kw): | |
3
d0d8524d9495
finish basic API...still doesnt expire results correctly
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
17 now = datetime.utcnow() |
d0d8524d9495
finish basic API...still doesnt expire results correctly
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
18 now.replace(tzinfo=_UTC()) |
d0d8524d9495
finish basic API...still doesnt expire results correctly
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
19 |
d0d8524d9495
finish basic API...still doesnt expire results correctly
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
20 # tzinfo crap |
d0d8524d9495
finish basic API...still doesnt expire results correctly
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
21 # now = datetime(*list(now.utctimetuple()[:-1]) + [_UTC()]) |
d0d8524d9495
finish basic API...still doesnt expire results correctly
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
22 |
d0d8524d9495
finish basic API...still doesnt expire results correctly
k0s <k0scist@gmail.com>
parents:
2
diff
changeset
|
23 kw['date'] = now |
0 | 24 f = file(self.database) |
25 comments = pickle.load(f) | |
26 f.close() | |
27 comments.setdefault(uri, []).append(kw) | |
28 f = file(self.database, 'w') | |
29 comments = pickle.dump(comments, f) | |
30 f.close() | |
31 | |
32 def comments(self, uri): | |
33 f = file(self.database) | |
34 comments = pickle.load(f) | |
35 f.close() | |
36 return comments.get(uri, []) | |
37 | |
38 | |
39 try: | |
40 import couchdb | |
41 | |
42 class CouchComments(object): | |
43 def __init__(self, db): | |
44 self.couch = couchdb.Server() | |
45 if db not in self.couch: | |
46 self.db = self.couch.create(db) | |
47 else: | |
48 self.db = self.couch[db] | |
49 | |
50 def comment(self, uri, **kw): | |
51 if uri in self.db: | |
52 comments = self.db[uri]['comments'] | |
53 comments.append(kw) | |
54 self.db[uri] = { 'comments': comments } | |
55 else: | |
56 self.db[uri] = { 'comments': [ kw ] } | |
57 | |
58 def comments(self, uri): | |
59 if uri in self.db: | |
60 doc = self.db[uri] | |
61 return doc ['comments'] | |
62 return [] | |
63 | |
64 except ImportError: | |
65 pass |