view taginthemiddle/model.py @ 17:c85d42296c06

make the model almost be real
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 24 May 2010 07:44:39 -0700
parents 12344c001831
children 57ca873ed9bf
line wrap: on
line source

"""
Tags models. Tags have:
 - a list of resources they're applied to
 - a time they're applied
 - someone that tagged the resource
"""

import os
import time

class Tags(object):
  """
  abstract base class for tagging
  """

  def add(self, tag, url, author):
    """add a tag to a URL"""
    raise NotImplementedError

  def remove(self, tag, url):
    """remove a tag from a URL"""
    raise NotImplementedError

  def tags(self, url):
    """
    get the tags for a URL
    """

  def urls(self, *tags):
    """
    get the URLs for a set of tags
    """

class FileTags(Tags):
  """
  file (e.g. .ini) based tagging scheme

  File format:
  foo = /bar:1273023556 /fleem/baz:1273023556 etc
  cats = /cats:1273023556 /blog/mycat:1273023556
  """
  # XXX TODO: store user;  currently this is not stored

  def __init__(self, path, author):
    """
    - path: path to file
    - author: who tagged the resource (fudge for now)
    """
    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()

  ### implementation of Tags interface

  def add(self, tag, url, author):
    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()

  def tags(self, url):
    raise NotImplementedError

  def urls(self, *tags):
    raise NotImplementedError

class SQLiteTags(Tags):
  """
  Tags model using SQLite

  Tags are relational data stored in a table
  Tag | URL | Author | Date [seconds since epoch]
  """

  def __init__(self, path):
    """
    - path: path to SQLite file
    """

  def add(self, tag, url, author):
    raise NotImplementedError

  def remove(self, tag, url):
    raise NotImplementedError

  def tags(self, url):
    """
    select * from tags where url='url'
    """

  def urls(self, *tags):
    """
    select * from tags where tag=''
    """