changeset 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
files taginthemiddle/middleware.py taginthemiddle/model.py
diffstat 2 files changed, 18 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/taginthemiddle/middleware.py
+++ b/taginthemiddle/middleware.py
@@ -9,24 +9,16 @@ from handlers import Index
 from model import Tags
 
 from genshi.template import TemplateLoader
 from lxmlmiddleware import LXMLMiddleware
 from paste.fileapp import FileApp
 from pkg_resources import resource_filename
 from webob import Request, Response, exc
 
-# tags have:
-# - a list of resources they're applied to
-# for now just assume  auth is on and that anyone authenticated may tag
-
-# e.g. a tags file
-# foo = /bar /fleem/baz etc
-# cats = /cats /blog/mycat
-
 class Tagger(LXMLMiddleware):
 
     # instance defaults
     defaults = { 'auto_reload': 'False',
                  'template_dirs': '',
                  'patterns': '*'
                  'tags_file': None,
                  'url': 'tags'
--- a/taginthemiddle/model.py
+++ b/taginthemiddle/model.py
@@ -1,49 +1,66 @@
+"""
+ 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(self.tags[tag]))
+      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()