Mercurial > hg > TagInTheMiddle
view taginthemiddle/model.py @ 14:86118cd8912e
display what tags we are talking about
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 05 May 2010 22:45:25 -0700 |
parents | 12344c001831 |
children | c85d42296c06 |
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 Ideally, the form of tags would be something like a table Tag | URL | Author | Date [seconds since epoch] """ 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() def read(self): for line in file(self.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()