annotate taginthemiddle/model.py @ 14:86118cd8912e

display what tags we are talking about
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 05 May 2010 22:45:25 -0700
parents 12344c001831
children c85d42296c06
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
2
1182315b18ac add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
5 - for multi-user tags, the author should be passed (fudge for now)
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
6 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
7 e.g. a tags file
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
8
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
9 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
10 cats = /cats:1273023556 /blog/mycat:1273023556
9
12344c001831 note comment for future exploration
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
11
12344c001831 note comment for future exploration
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
12 Ideally, the form of tags would be something like a table
12344c001831 note comment for future exploration
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
13
12344c001831 note comment for future exploration
Jeff Hammel <jhammel@mozilla.com>
parents: 8
diff changeset
14 Tag | URL | Author | Date [seconds since epoch]
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
15 """
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
16
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 import os
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
18 import time
0
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 class Tags(object):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 """file (e.g. .ini) based tagging scheme"""
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 def __init__(self, path):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 """
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 - path: path to file
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 """
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 self.path = path
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 if not os.path.exists(path):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 f = file(path, 'w')
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 f.close()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 self.tags = {}
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 self.urls = {}
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
33 self.times = {}
8
fc3491dfe374 correct function signature for reading tags file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
34 self.read()
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 def read(self):
8
fc3491dfe374 correct function signature for reading tags file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
37 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
38 line = line.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 if not line:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 continue
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 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
42 tag = tag.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 urls = urls.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 self.tags[tag] = set([urls])
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 for url in urls:
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
46 url, time = url.rsplit(':', 1)
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 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
48 self.times[(tag, url)] = int(time)
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 def write(self):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 f = file(self.path, 'w')
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 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
53 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
54 for url in self.tags[tag]]))
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 f.close()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 def add(self, tag, url):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 if url in self.tags[tag]:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 return
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 self.tags[tag].add(url)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 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
62 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
63 self.write()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 def remove(self, tag, url):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 if url not in self.tags[tag]:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 return
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 self.tags[tag].remove(url)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 self.urls[url].remove(tag)
2
1182315b18ac add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
70 del self.times[(tag, url)]
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 self.write()