annotate taginthemiddle/model.py @ 4:61dd789330f7

stub out example .ini file for taginthemiddle
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 05 May 2010 22:07:56 -0700
parents 1182315b18ac
children fc3491dfe374
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
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
11 """
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
12
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 import os
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
14 import time
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 class Tags(object):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 """file (e.g. .ini) based tagging scheme"""
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 def __init__(self, path):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 """
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 - path: path to file
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 self.path = path
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 if not os.path.exists(path):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 f = file(path, 'w')
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 f.close()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 self.tags = {}
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 self.urls = {}
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
29 self.times = {}
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 self.read(path)
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
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 def read(self):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 for line in file(path).readlines():
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 line = line.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 if not line:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 continue
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 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
39 tag = tag.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 urls = urls.strip()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 self.tags[tag] = set([urls])
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 for url in urls:
1
837cfc05d4d9 add times to tags; cant things ever be easy?
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
43 url, time = url.rsplit(':', 1)
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 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
45 self.times[(tag, url)] = int(time)
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 def write(self):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 f = file(self.path, 'w')
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 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
50 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
51 for url in self.tags[tag]]))
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 f.close()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 def add(self, tag, url):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 if url in self.tags[tag]:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 return
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 self.tags[tag].add(url)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 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
59 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
60 self.write()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 def remove(self, tag, url):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63 if url not in self.tags[tag]:
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 return
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 self.tags[tag].remove(url)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 self.urls[url].remove(tag)
2
1182315b18ac add rudimentary code for handlers
Jeff Hammel <jhammel@mozilla.com>
parents: 1
diff changeset
67 del self.times[(tag, url)]
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 self.write()