Mercurial > hg > commentator
annotate commentator/model.py @ 2:689b9d928dc8
add date, reflect api change of lxmlmiddleware
author | k0s <k0scist@gmail.com> |
---|---|
date | Tue, 26 Jan 2010 23:00:51 -0500 |
parents | 1c95a3fa76c1 |
children | d0d8524d9495 |
rev | line source |
---|---|
0 | 1 import os |
2 import pickle | |
3 | |
4 from datetime import datetime | |
5 | |
6 class PickleComments(object): | |
7 # TODO: locking | |
8 def __init__(self, database): | |
9 self.database = database | |
10 if not os.path.exists(database): | |
11 f = file(database, 'w') | |
12 pickle.dump({}, f) | |
13 f.close() | |
14 | |
15 def comment(self, uri, **kw): | |
2
689b9d928dc8
add date, reflect api change of lxmlmiddleware
k0s <k0scist@gmail.com>
parents:
0
diff
changeset
|
16 kw['date'] = datetime.now() |
0 | 17 f = file(self.database) |
18 comments = pickle.load(f) | |
19 f.close() | |
20 comments.setdefault(uri, []).append(kw) | |
21 f = file(self.database, 'w') | |
22 comments = pickle.dump(comments, f) | |
23 f.close() | |
24 | |
25 def comments(self, uri): | |
26 f = file(self.database) | |
27 comments = pickle.load(f) | |
28 f.close() | |
29 return comments.get(uri, []) | |
30 | |
31 | |
32 try: | |
33 import couchdb | |
34 | |
35 class CouchComments(object): | |
36 def __init__(self, db): | |
37 self.couch = couchdb.Server() | |
38 if db not in self.couch: | |
39 self.db = self.couch.create(db) | |
40 else: | |
41 self.db = self.couch[db] | |
42 | |
43 def comment(self, uri, **kw): | |
44 if uri in self.db: | |
45 comments = self.db[uri]['comments'] | |
46 comments.append(kw) | |
47 self.db[uri] = { 'comments': comments } | |
48 else: | |
49 self.db[uri] = { 'comments': [ kw ] } | |
50 | |
51 def comments(self, uri): | |
52 if uri in self.db: | |
53 doc = self.db[uri] | |
54 return doc ['comments'] | |
55 return [] | |
56 | |
57 except ImportError: | |
58 pass |