Mercurial > hg > TagInTheMiddle
view 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 |
line wrap: on
line source
""" tags have: - a list of resources they're applied to - a time they're applied - for multi-user tags, the author should be passed (fudge for now) for now just assume auth is on and that anyone authenticated may tag e.g. a tags file foo = /bar:1273023556 /fleem/baz:1273023556 etc cats = /cats:1273023556 /blog/mycat:1273023556 """ import os import time 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.times = {} 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: url, time = url.rsplit(':', 1) self.urls.setdefault(url, set()).add(tag) self.times[(tag, url)] = int(time) def write(self): f = file(self.path, 'w') for tag in sorted(self.tags.keys()): print >> f, '%s = %s' % (tag, ' '.join(['%s:%s' % (url, self.times(tag, url)) for url in 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.times[(tag, url)] = int(time.time()) 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) del self.times[(tag, url)] self.write()