comparison taginthemiddle/model.py @ 0:1c5cbbde4299

initial commit of middleware tagging; doesnt yet work
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 04 May 2010 08:37:15 -0700
parents
children 837cfc05d4d9
comparison
equal deleted inserted replaced
-1:000000000000 0:1c5cbbde4299
1 import os
2
3 class Tags(object):
4 """file (e.g. .ini) based tagging scheme"""
5
6 def __init__(self, path):
7 """
8 - path: path to file
9 """
10 self.path = path
11 if not os.path.exists(path):
12 f = file(path, 'w')
13 f.close()
14 self.tags = {}
15 self.urls = {}
16 self.read(path)
17
18
19 def read(self):
20 for line in file(path).readlines():
21 line = line.strip()
22 if not line:
23 continue
24 tag, urls = line.split('=', 1) # asserts '=' in line
25 tag = tag.strip()
26 urls = urls.strip()
27 self.tags[tag] = set([urls])
28 for url in urls:
29 self.urls.setdefault(url, set()).add(tag)
30
31 def write(self):
32 f = file(self.path, 'w')
33 for tag in sorted(self.tags.keys()):
34 print >> f, '%s = %s' % (tag, ' '.join(self.tags[tag]))
35 f.close()
36
37 def add(self, tag, url):
38 if url in self.tags[tag]:
39 return
40 self.tags[tag].add(url)
41 self.urls[url].add(tag)
42 self.write()
43
44 def remove(self, tag, url):
45 if url not in self.tags[tag]:
46 return
47 self.tags[tag].remove(url)
48 self.urls[url].remove(tag)
49 self.write()