Author: dmeyer
Date: Sun Mar 12 20:18:34 2006
New Revision: 1283

Modified:
   trunk/WIP/vfs/src/directory.py
   trunk/WIP/vfs/src/file.py
   trunk/WIP/vfs/src/item.py

Log:
add some doc

Modified: trunk/WIP/vfs/src/directory.py
==============================================================================
--- trunk/WIP/vfs/src/directory.py      (original)
+++ trunk/WIP/vfs/src/directory.py      Sun Mar 12 20:18:34 2006
@@ -2,10 +2,7 @@
 # -----------------------------------------------------------------------------
 # directory.py - Directory item of the VFS
 # -----------------------------------------------------------------------------
-# $Id: item.py 1273 2006-03-11 19:34:08Z dmeyer $
-#
-# TODO: handle all the FIXME and TODO comments inside this file and
-#       add docs for functions, variables and how to use this file
+# $Id$
 #
 # -----------------------------------------------------------------------------
 # kaa-vfs - A virtual filesystem with metadata
@@ -35,17 +32,27 @@
 # python imports
 import os
 import stat
+import logging
 
 # kaa.vfs imports
 from item import Item
 
+# get logging object
+log = logging.getLogger('vfs')
+
 UNKNOWN = -1
 
 class Directory(Item):
     """
-    A directory based item.
-    Object Attributes:
-    url, filename, getattr, setattr, keys, listdir
+    A directory based database item.
+
+    Attributes:
+    url:      unique url of the item
+    filename: filename of the directory on hd
+    listdir:  list all items in that directory
+    getattr:  function to get an attribute
+    setattr:  function to set an attribute
+    keys:     function to return all known attributes of the item
 
     Do not access attributes starting with _vfs outside kaa.vfs
     """
@@ -54,7 +61,7 @@
         if isinstance(data, str):
             # fake item, there is no database entry
             id = None
-            self.filename = parent.filename + data + '/' 
+            self.filename = parent.filename + data + '/'
             data = { 'name': data, 'mtime': UNKNOWN }
             if parent and parent._vfs_id:
                 data['parent_type'], data['parent_id'] = parent._vfs_id
@@ -74,29 +81,43 @@
         Item.__init__(self, id, 'file://' + self.filename, data, parent, media)
         self._vfs_overlay = False
         self._vfs_isdir = True
+        self._vfs_ovdir = media.overlay + '/' + 
self.filename[len(media.directory):]
+
+
+    def _vfs_mtime(self):
+        """
+        Return modification time of the item itself.
+
+        The modification time of a directory is the max value of the mtime from
+        the directory itself and the overlay directory (if that exists).
+        """
+        if os.path.isdir(self._vfs_ovdir):
+            return max(os.stat(self._vfs_ovdir)[stat.ST_MTIME],
+                       os.stat(self.filename)[stat.ST_MTIME])
+        return os.stat(self.filename)[stat.ST_MTIME]
 
 
     def _vfs_request(self):
+        """
+        Request the item to be scanned.
+        """
         
self._vfs_database_update(self._vfs_db()._vfs_request(self.filename[:-1]))
 
 
-    def listdir(self):
-        if not self._vfs_id:
-            # item is not in db, request information now
-            self._vfs_request()
-        return self._vfs_db().query(parent=self)
-        
-
     def _vfs_listdir(self):
+        """
+        Internal function to list all files in the directory and the overlay
+        directory. The result is a list of tuples. The first item is the
+        basename, the next is True when the file is in the overlay dir and
+        False if not.
+        """
         try:
             listdir = os.listdir(self.filename)
         except OSError:
             return []
 
-        media = self._vfs_media
-        overlay = media.overlay + '/' + self.filename[len(media.directory):]
         try:
-            result = [ ( x, True ) for x in os.listdir(overlay) \
+            result = [ ( x, True ) for x in os.listdir(self._vfs_ovdir) \
                        if not x.startswith('.') and not x in listdir ]
         except OSError:
             result = []
@@ -106,30 +127,30 @@
 
 
     def _vfs_os_listdir(self):
+        """
+        Internal function to list all files in the directory and the overlay
+        directory. The result is a list of complete filenames. The function
+        will use an internal cache.
+        FIXME: This is an ugly solution.
+        """
         if hasattr(self, '_vfs_os_listdir_cache'):
             return self._vfs_os_listdir_cache
-        
+
         try:
             result = [ (x, self.filename + x) for x in 
os.listdir(self.filename)
                        if not x.startswith('.') ]
         except OSError:
             return []
 
-        media = self._vfs_media
-        overlay = media.overlay + '/' + self.filename[len(media.directory):]
         try:
-            result += [ ( x, overlay + x ) for x in os.listdir(overlay) \
+            result += [ ( x, self._vfs_ovdir + x ) for x in 
os.listdir(self._vfs_ovdir) \
                         if not x.startswith('.') and not x in listdir ]
         except OSError:
             pass
         self._vfs_os_listdir_cache = result
         return result
-        
-    def _vfs_mtime(self):
-        # TODO: add overlay dir to mtime
-        return os.stat(self.filename)[stat.ST_MTIME]
-    
-        
+
+
     def __repr__(self):
         """
         Convert object to string (usefull for debugging)
@@ -138,3 +159,14 @@
         if self._vfs_data['mtime'] == UNKNOWN:
             str += ' (new)'
         return str + '>'
+
+
+    def listdir(self):
+        """
+        Interface to kaa.vfs: List all files in the directory.
+        """
+        if not self._vfs_id:
+            log.info('requesting data for %s', self)
+            self._vfs_request()
+        return self._vfs_db().query(parent=self)
+

Modified: trunk/WIP/vfs/src/file.py
==============================================================================
--- trunk/WIP/vfs/src/file.py   (original)
+++ trunk/WIP/vfs/src/file.py   Sun Mar 12 20:18:34 2006
@@ -2,10 +2,7 @@
 # -----------------------------------------------------------------------------
 # file.py - File item of the VFS
 # -----------------------------------------------------------------------------
-# $Id: item.py 1273 2006-03-11 19:34:08Z dmeyer $
-#
-# TODO: handle all the FIXME and TODO comments inside this file and
-#       add docs for functions, variables and how to use this file
+# $Id$
 #
 # -----------------------------------------------------------------------------
 # kaa-vfs - A virtual filesystem with metadata
@@ -35,18 +32,28 @@
 # python imports
 import os
 import stat
+import logging
 
 # kaa.vfs imports
 from item import Item
 from directory import Directory
 
+# get logging object
+log = logging.getLogger('vfs')
+
+
 UNKNOWN = -1
 
 class File(Item):
     """
-    A file based item.
-    Object Attributes:
-    url, filename, getattr, setattr, keys
+    A file based database item.
+
+    Attributes:
+    url:      unique url of the item
+    filename: filename of the item on hd
+    getattr:  function to get an attribute
+    setattr:  function to set an attribute
+    keys:     function to return all known attributes of the item
 
     Do not access attributes starting with _vfs outside kaa.vfs
     """
@@ -70,10 +77,13 @@
 
 
     def _vfs_mtime(self):
-        # mtime is the the mtime for all files having the same
-        # base. E.g. the mtime of foo.jpg is the sum of the
-        # mtimeof foo.jpg and foo.jpg.xml or for foo.mp3 the
-        # mtime is the sum of foo.mp3 and foo.jpg.
+        """
+        Return modification time of the item itself.
+
+        mtime is the the mtime for all files having the same base. E.g. the
+        mtime of foo.jpg is the sum of the mtime of foo.jpg and foo.jpg.xml
+        or for foo.mp3 the mtime is the sum of foo.mp3 and foo.jpg.
+        """
         search = self._vfs_data['name']
         if search.rfind('.') > 0:
             search = search[:search.rfind('.')]
@@ -85,6 +95,9 @@
 
 
     def _vfs_request(self):
+        """
+        Request the item to be scanned.
+        """
         
self._vfs_database_update(self._vfs_db()._vfs_request(self.filename[:-1]))
 
 
@@ -96,7 +109,3 @@
         if self._vfs_data['mtime'] == UNKNOWN:
             str += ' (new)'
         return str + '>'
-
-
-# make it possible to override these
-

Modified: trunk/WIP/vfs/src/item.py
==============================================================================
--- trunk/WIP/vfs/src/item.py   (original)
+++ trunk/WIP/vfs/src/item.py   Sun Mar 12 20:18:34 2006
@@ -4,9 +4,6 @@
 # -----------------------------------------------------------------------------
 # $Id$
 #
-# TODO: handle all the FIXME and TODO comments inside this file and
-#       add docs for functions, variables and how to use this file
-#
 # -----------------------------------------------------------------------------
 # kaa-vfs - A virtual filesystem with metadata
 # Copyright (C) 2005 Dirk Meyer
@@ -32,18 +29,27 @@
 #
 # -----------------------------------------------------------------------------
 
+# python imports
+import logging
+
 # kaa imports
 from kaa.strutils import str_to_unicode
 
 # kaa.vfs imports
 from thumbnail import Thumbnail
 
-UNKNOWN = -1
+# get logging object
+log = logging.getLogger('vfs')
 
 class Item(object):
     """
-    Object Attributes:
-    url, getattr, setattr, keys
+    A database item.
+
+    Attributes:
+    url:      unique url of the item
+    getattr:  function to get an attribute
+    setattr:  function to set an attribute
+    keys:     function to return all known attributes of the item
 
     Do not access attributes starting with _vfs outside kaa.vfs
     """
@@ -62,44 +68,74 @@
         self._vfs_name = data['name']
 
 
-    def __repr__(self):
+    def _vfs_database_update(self, data):
         """
-        Convert object to string (usefull for debugging)
+        Callback from db with new data
         """
-        return '<vfs.Item %s>' % self.url
-
-
-    def _vfs_database_update(self, data):
-        # callback from db
         self._vfs_data = data
         self._vfs_id = (data['type'], data['id'])
         for key, value in self._vfs_changes.items():
             self._vfs_data[key] = value
-            
-        
+
+
     def _vfs_db(self):
-        # get db
+        """
+        Get the database connection (the client)
+        """
         return self._vfs_media.client
 
+
     def _vfs_mtime(self):
-        # return mtime
+        """
+        Return modification time of the item itself.
+        """
         return 0
 
+
     def _vfs_changed(self):
+        """
+        Return if the item is changed (based on modification time of
+        the data and in the database).
+        """
         return self._vfs_mtime() != self._vfs_data['mtime']
 
 
     def _vfs_request(self):
+        """
+        Request the item to be scanned.
+        """
         pass
-    
+
+
     def _vfs_tree(self):
+        """
+        Return an iterator to walk through the parents.
+        """
         return ParentIterator(self)
 
-    
-    def getattr(self, key):
+
+    def __repr__(self):
+        """
+        Convert object to string (usefull for debugging)
+        """
+        return '<vfs.Item %s>' % self.url
+
+
+    def getattr(self, key, request=True):
+        """
+        Interface to kaa.vfs. Return the value of a given attribute. If
+        the attribute is not in the db, return None. If the key starts with
+        'tmp:', the data will be fetched from a dict that is not stored in
+        the db. Loosing the item object will remove that attribute. If
+        request is True, scan the item if it is not in the db. If request is
+        False and the item is not in the db, results will be very limited.
+        """
         if key.startswith('tmp:'):
             return self._vfs_tmpdata[key[4:]]
 
+        if key == 'parent':
+            return self._vfs_parent
+
         if key == 'thumbnail' and hasattr(self, 'filename'):
             return Thumbnail(self.filename, url=self.url)
 
@@ -122,9 +158,9 @@
             if t.find('.') > 0:
                 t = t[:t.rfind('.')]
             return str_to_unicode(t)
-        
-        if not self._vfs_id:
-            # item is not in db, request information now
+
+        if request and not self._vfs_id:
+            log.info('requesting data for %s', self)
             self._vfs_request()
 
         if self._vfs_data.has_key(key):
@@ -132,23 +168,37 @@
         return None
 
 
-    def setattr(self, key, value):
+    def setattr(self, key, value, request=True):
+        """
+        Interface to kaa.vfs. Set the value of a given attribute. If the key
+        starts with 'tmp:', the data will only be valid in this item and not
+        stored in the db. Loosing the item object will remove that attribute. 
If
+        request is True, scan the item if it is not in the db. If request is
+        False and the item is not in the db, the value may be lost.
+        """
         if key.startswith('tmp:'):
             self._vfs_tmpdata[key[4:]] = value
             return
+        if not self._vfs_id:
+            log.info('requesting data for %s', self)
+            self._vfs_request()
         self._vfs_data[key] = value
         if not self._vfs_changes and self._vfs_id:
-            # FIXME: how to update an item not in the db yet?
             self._vfs_db().update(self)
         self._vfs_changes[key] = value
-        
-            
+
+
     def keys(self):
-        return self._vfs_data.keys()
+        """
+        Interface to kaa.vfs. Return all attributes of the item.
+        """
+        return self._vfs_data.keys() + self._vfs_tmpdata.keys()
 
 
 class ParentIterator(object):
-
+    """
+    Iterator to iterate thru the parent structure.
+    """
     def __init__(self, item):
         self.item = item
 


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to