Mercurial > hg > TagInTheMiddle
annotate 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 |
rev | line source |
---|---|
0
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 import os |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 class Tags(object): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 """file (e.g. .ini) based tagging scheme""" |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 def __init__(self, path): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 """ |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 - path: path to file |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 """ |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 self.path = path |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 if not os.path.exists(path): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 f = file(path, 'w') |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 f.close() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 self.tags = {} |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 self.urls = {} |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 self.read(path) |
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 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 def read(self): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 for line in file(path).readlines(): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 line = line.strip() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 if not line: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 continue |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 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
|
25 tag = tag.strip() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 urls = urls.strip() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 self.tags[tag] = set([urls]) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 for url in urls: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 self.urls.setdefault(url, set()).add(tag) |
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 def write(self): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 f = file(self.path, 'w') |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 for tag in sorted(self.tags.keys()): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 print >> f, '%s = %s' % (tag, ' '.join(self.tags[tag])) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 f.close() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 def add(self, tag, url): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
38 if url in self.tags[tag]: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 return |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 self.tags[tag].add(url) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 self.urls[url].add(tag) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 self.write() |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 def remove(self, tag, url): |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 if url not in self.tags[tag]: |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 return |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 self.tags[tag].remove(url) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 self.urls[url].remove(tag) |
1c5cbbde4299
initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 self.write() |