comparison 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
comparison
equal deleted inserted replaced
0:1c5cbbde4299 1:837cfc05d4d9
1 """
2 tags have:
3 - a list of resources they're applied to
4 - a time they're applied
5 for now just assume auth is on and that anyone authenticated may tag
6 e.g. a tags file
7
8 foo = /bar:1273023556 /fleem/baz:1273023556 etc
9 cats = /cats:1273023556 /blog/mycat:1273023556
10 """
11
1 import os 12 import os
13 import time
2 14
3 class Tags(object): 15 class Tags(object):
4 """file (e.g. .ini) based tagging scheme""" 16 """file (e.g. .ini) based tagging scheme"""
5 17
6 def __init__(self, path): 18 def __init__(self, path):
11 if not os.path.exists(path): 23 if not os.path.exists(path):
12 f = file(path, 'w') 24 f = file(path, 'w')
13 f.close() 25 f.close()
14 self.tags = {} 26 self.tags = {}
15 self.urls = {} 27 self.urls = {}
28 self.times = {}
16 self.read(path) 29 self.read(path)
17 30
18 31
19 def read(self): 32 def read(self):
20 for line in file(path).readlines(): 33 for line in file(path).readlines():
24 tag, urls = line.split('=', 1) # asserts '=' in line 37 tag, urls = line.split('=', 1) # asserts '=' in line
25 tag = tag.strip() 38 tag = tag.strip()
26 urls = urls.strip() 39 urls = urls.strip()
27 self.tags[tag] = set([urls]) 40 self.tags[tag] = set([urls])
28 for url in urls: 41 for url in urls:
42 url, time = url.rsplit(':', 1)
29 self.urls.setdefault(url, set()).add(tag) 43 self.urls.setdefault(url, set()).add(tag)
44 self.times[(tag, url)] = int(time)
30 45
31 def write(self): 46 def write(self):
32 f = file(self.path, 'w') 47 f = file(self.path, 'w')
33 for tag in sorted(self.tags.keys()): 48 for tag in sorted(self.tags.keys()):
34 print >> f, '%s = %s' % (tag, ' '.join(self.tags[tag])) 49 print >> f, '%s = %s' % (tag, ' '.join(['%s:%s' % (url, self.times(tag, url))
50 for url in self.tags[tag]]))
35 f.close() 51 f.close()
36 52
37 def add(self, tag, url): 53 def add(self, tag, url):
38 if url in self.tags[tag]: 54 if url in self.tags[tag]:
39 return 55 return
40 self.tags[tag].add(url) 56 self.tags[tag].add(url)
41 self.urls[url].add(tag) 57 self.urls[url].add(tag)
58 self.times[(tag, url)] = int(time.time())
42 self.write() 59 self.write()
43 60
44 def remove(self, tag, url): 61 def remove(self, tag, url):
45 if url not in self.tags[tag]: 62 if url not in self.tags[tag]:
46 return 63 return