Mercurial > hg > TagInTheMiddle
view 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 |
line wrap: on
line source
import os class Tags(object): """file (e.g. .ini) based tagging scheme""" def __init__(self, path): """ - path: path to file """ self.path = path if not os.path.exists(path): f = file(path, 'w') f.close() self.tags = {} self.urls = {} self.read(path) def read(self): for line in file(path).readlines(): line = line.strip() if not line: continue tag, urls = line.split('=', 1) # asserts '=' in line tag = tag.strip() urls = urls.strip() self.tags[tag] = set([urls]) for url in urls: self.urls.setdefault(url, set()).add(tag) def write(self): f = file(self.path, 'w') for tag in sorted(self.tags.keys()): print >> f, '%s = %s' % (tag, ' '.join(self.tags[tag])) f.close() def add(self, tag, url): if url in self.tags[tag]: return self.tags[tag].add(url) self.urls[url].add(tag) self.write() def remove(self, tag, url): if url not in self.tags[tag]: return self.tags[tag].remove(url) self.urls[url].remove(tag) self.write()