Mercurial > hg > TagInTheMiddle
view 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 |
line wrap: on
line source
""" tags have: - a list of resources they're applied to - a time they're applied 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) self.write()