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):
|
|
16 f = file(self.database)
|
|
17 comments = pickle.load(f)
|
|
18 f.close()
|
|
19 comments.setdefault(uri, []).append(kw)
|
|
20 f = file(self.database, 'w')
|
|
21 comments = pickle.dump(comments, f)
|
|
22 f.close()
|
|
23
|
|
24 def comments(self, uri):
|
|
25 f = file(self.database)
|
|
26 comments = pickle.load(f)
|
|
27 f.close()
|
|
28 return comments.get(uri, [])
|
|
29
|
|
30
|
|
31 try:
|
|
32 import couchdb
|
|
33
|
|
34 class CouchComments(object):
|
|
35 def __init__(self, db):
|
|
36 self.couch = couchdb.Server()
|
|
37 if db not in self.couch:
|
|
38 self.db = self.couch.create(db)
|
|
39 else:
|
|
40 self.db = self.couch[db]
|
|
41
|
|
42 def comment(self, uri, **kw):
|
|
43 if uri in self.db:
|
|
44 comments = self.db[uri]['comments']
|
|
45 comments.append(kw)
|
|
46 self.db[uri] = { 'comments': comments }
|
|
47 else:
|
|
48 self.db[uri] = { 'comments': [ kw ] }
|
|
49
|
|
50 def comments(self, uri):
|
|
51 if uri in self.db:
|
|
52 doc = self.db[uri]
|
|
53 return doc ['comments']
|
|
54 return []
|
|
55
|
|
56 except ImportError:
|
|
57 pass
|