Package lastfm :: Module filecache

[hide private]
[frames] | no frames]

# Source Code for Module lastfm.filecache

      1  #!/usr/bin/env python
      2
      3  __author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>"
      4  __version__ = "0.2"
      5  __license__ = "GNU Lesser General Public License"
      6
      7  import sys
      8  if sys.version < '2.6':
      9      import md5
     10 -    def md5hash(string):
     11          return md5.new(string).hexdigest()
     12  else:
     13      from hashlib import md5
     14 -    def md5hash(string):
     15          return md5(string).hexdigest()
     16
     17  import os
     18  import tempfile
     19
     20 -class _FileCacheError(Exception):
     21          '''Base exception class for FileCache related errors'''
     22
     23 -class FileCache(object):
     24
     25          DEPTH = 3
     26
     27 -        def __init__(self,root_directory=None):
     28                  self._InitializeRootDirectory(root_directory)
     29
     30 -        def Get(self,key):
     31                  path = self._GetPath(key)
     32                  if os.path.exists(path):
     33                          return open(path).read()
     34                  else:
     35                          return None
     36
     37 -        def Set(self,key,data):
     38                  path = self._GetPath(key)
     39                  directory = os.path.dirname(path)
     40                  if not os.path.exists(directory):
     41                          os.makedirs(directory)
     42                  if not os.path.isdir(directory):
     43                          raise _FileCacheError('%s exists but is not a directory' % directory)
     44                  temp_fd, temp_path = tempfile.mkstemp()
     45                  temp_fp = os.fdopen(temp_fd, 'w')
     46                  temp_fp.write(data)
     47                  temp_fp.close()
     48                  if not path.startswith(self._root_directory):
     49                          raise _FileCacheError('%s does not appear to live under %s' %
     50                                                                    (path, self._root_directory))
     51                  if os.path.exists(path):
     52                          os.remove(path)
     53                  os.rename(temp_path, path)
     54
     55 -        def Remove(self,key):
     56                  path = self._GetPath(key)
     57                  if not path.startswith(self._root_directory):
     58                          raise _FileCacheError('%s does not appear to live under %s' %
     59                                                                    (path, self._root_directory ))
     60                  if os.path.exists(path):
     61                          os.remove(path)
     62
     63 -        def GetCachedTime(self,key):
     64                  path = self._GetPath(key)
     65                  if os.path.exists(path):
     66                          return os.path.getmtime(path)
     67                  else:
     68                          return None
     69
     70 -        def _GetUsername(self):
     71                  '''Attempt to find the username in a cross-platform fashion.'''
     72                  return os.getenv('USER') or \
     73                          os.getenv('LOGNAME') or \
     74                          os.getenv('USERNAME') or \
     75                          os.getlogin() or \
     76                          'nobody'
     77
     78 -        def _GetTmpCachePath(self):
     79                  username = self._GetUsername()
     80                  cache_directory = 'python.cache_' + username
     81                  return os.path.join(tempfile.gettempdir(), cache_directory)
     82
     83 -        def _InitializeRootDirectory(self, root_directory):
     84                  if not root_directory:
     85                          root_directory = self._GetTmpCachePath()
     86                  root_directory = os.path.abspath(root_directory)
     87                  if not os.path.exists(root_directory):
     88                          os.mkdir(root_directory)
     89                  if not os.path.isdir(root_directory):
     90                          raise _FileCacheError('%s exists but is not a directory' %
     91                                                                    root_directory)
     92                  self._root_directory = root_directory
     93
     94 -        def _GetPath(self,key):
     95                  hashed_key = md5hash(key)
     96                  return os.path.join(self._root_directory,
     97                                                          self._GetPrefix(hashed_key),
     98                                                          hashed_key)
     99
    100 -        def _GetPrefix(self,hashed_key):
    101                  return os.path.sep.join(hashed_key[0:FileCache.DEPTH])
    102