annotate taginthemiddle/model.py @ 1:837cfc05d4d9

add times to tags; cant things ever be easy?
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 04 May 2010 18:46:05 -0700
parents 1c5cbbde4299
children 1182315b18ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
1 """
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
2 tags have:
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
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
5 for now just assume auth is on and that anyone authenticated may tag
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
6 e.g. a tags file
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
7
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
8 foo = /bar:1273023556 /fleem/baz:1273023556 etc
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
9 cats = /cats:1273023556 /blog/mycat:1273023556
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
10 """
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
11
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 import os
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
13 import time
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 class Tags(object):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 """file (e.g. .ini) based tagging scheme"""
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 def __init__(self, path):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 """
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 - path: path to file
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 """
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 self.path = path
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 if not os.path.exists(path):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 f = file(path, 'w')
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 f.close()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 self.tags = {}
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 self.urls = {}
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
28 self.times = {}
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 self.read(path)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 def read(self):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 for line in file(path).readlines():
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 line = line.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 if not line:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 continue
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 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
38 tag = tag.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 urls = urls.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 self.tags[tag] = set([urls])
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 for url in urls:
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
42 url, time = url.rsplit(':', 1)
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 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
44 self.times[(tag, url)] = int(time)
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 def write(self):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 f = file(self.path, 'w')
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 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
49 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
50 for url in self.tags[tag]]))
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 f.close()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 def add(self, tag, url):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 if url in self.tags[tag]:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 return
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 self.tags[tag].add(url)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 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
58 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
59 self.write()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 def remove(self, tag, url):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 if url not in self.tags[tag]:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 return
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 self.tags[tag].remove(url)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 self.urls[url].remove(tag)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 self.write()