Mercurial > hg > TagInTheMiddle
annotate taginthemiddle/model.py @ 17:c85d42296c06
make the model almost be real
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 24 May 2010 07:44:39 -0700 |
parents | 12344c001831 |
children | 57ca873ed9bf |
rev | line source |
---|---|
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
1 """ |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
2 Tags models. Tags have: |
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
3 - a list of resources they're applied to |
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
4 - a time they're applied |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
5 - someone that tagged the resource |
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
6 """ |
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
7 |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 import os |
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
9 import time |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 class Tags(object): |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
12 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
13 abstract base class for tagging |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
14 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
15 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
16 def add(self, tag, url, author): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
17 """add a tag to a URL""" |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
18 raise NotImplementedError |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
19 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
20 def remove(self, tag, url): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
21 """remove a tag from a URL""" |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
22 raise NotImplementedError |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
23 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
24 def tags(self, url): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
25 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
26 get the tags for a URL |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
27 """ |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
29 def urls(self, *tags): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
30 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
31 get the URLs for a set of tags |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
32 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
33 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
34 class FileTags(Tags): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
35 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
36 file (e.g. .ini) based tagging scheme |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
37 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
38 File format: |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
39 foo = /bar:1273023556 /fleem/baz:1273023556 etc |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
40 cats = /cats:1273023556 /blog/mycat:1273023556 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
41 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
42 # XXX TODO: store user; currently this is not stored |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
43 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
44 def __init__(self, path, author): |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 """ |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 - path: path to file |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
47 - author: who tagged the resource (fudge for now) |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 """ |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 self.path = path |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 if not os.path.exists(path): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
51 f = file(path, 'w') |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
52 f.close() |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
53 self._tags = {} |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
54 self._urls = {} |
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
55 self.times = {} |
8
fc3491dfe374
correct function signature for reading tags file
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
56 self.read() |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
58 def read(self): |
8
fc3491dfe374
correct function signature for reading tags file
Jeff Hammel <jhammel@mozilla.com>
parents:
2
diff
changeset
|
59 for line in file(self.path).readlines(): |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
60 line = line.strip() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
61 if not line: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
62 continue |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
63 tag, urls = line.split('=', 1) # asserts '=' in line |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
64 tag = tag.strip() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
65 urls = urls.strip() |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
66 self._tags[tag] = set([urls]) |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 for url in urls: |
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
68 url, time = url.rsplit(':', 1) |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
69 self._urls.setdefault(url, set()).add(tag) |
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
70 self.times[(tag, url)] = int(time) |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
71 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
72 def write(self): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
73 f = file(self.path, 'w') |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
74 for tag in sorted(self.tags.keys()): |
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
75 print >> f, '%s = %s' % (tag, ' '.join(['%s:%s' % (url, self.times(tag, url)) |
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
76 for url in self.tags[tag]])) |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
77 f.close() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
78 |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
79 ### implementation of Tags interface |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
80 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
81 def add(self, tag, url, author): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
82 if url in self._tags[tag]: |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
83 return |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
84 self._tags[tag].add(url) |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
85 self._urls[url].add(tag) |
1
837cfc05d4d9
add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
86 self.times[(tag, url)] = int(time.time()) |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
87 self.write() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
88 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
89 def remove(self, tag, url): |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
90 if url not in self._tags[tag]: |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
91 return |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
92 self._tags[tag].remove(url) |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
93 self._urls[url].remove(tag) |
2
1182315b18ac
add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents:
1
diff
changeset
|
94 del self.times[(tag, url)] |
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
95 self.write() |
17
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
96 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
97 def tags(self, url): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
98 raise NotImplementedError |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
99 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
100 def urls(self, *tags): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
101 raise NotImplementedError |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
102 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
103 class SQLiteTags(Tags): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
104 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
105 Tags model using SQLite |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
106 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
107 Tags are relational data stored in a table |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
108 Tag | URL | Author | Date [seconds since epoch] |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
109 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
110 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
111 def __init__(self, path): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
112 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
113 - path: path to SQLite file |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
114 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
115 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
116 def add(self, tag, url, author): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
117 raise NotImplementedError |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
118 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
119 def remove(self, tag, url): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
120 raise NotImplementedError |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
121 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
122 def tags(self, url): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
123 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
124 select * from tags where url='url' |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
125 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
126 |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
127 def urls(self, *tags): |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
128 """ |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
129 select * from tags where tag='' |
c85d42296c06
make the model almost be real
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
130 """ |