Author: dmeyer
Date: Sat Feb 18 17:50:18 2006
New Revision: 1194
Modified:
trunk/WIP/vfs/src/__init__.py
trunk/WIP/vfs/src/db.py
trunk/WIP/vfs/src/item.py
trunk/WIP/vfs/test/cache.py
trunk/WIP/vfs/test/client.py
Log:
more updates, it starts to look good
Modified: trunk/WIP/vfs/src/__init__.py
==============================================================================
--- trunk/WIP/vfs/src/__init__.py (original)
+++ trunk/WIP/vfs/src/__init__.py Sat Feb 18 17:50:18 2006
@@ -44,6 +44,9 @@
# get logging object
log = logging.getLogger('vfs')
+# connected client object
+_client = None
+
def connect(vfsdb, logfile=None, loglevel=logging.INFO):
"""
Connect to the vfs database dir given by 'vfsdb'. A server will be started
@@ -55,9 +58,12 @@
the are started by the same user. It will shutdown if no client is
connected
for over 5 seconds.
"""
+ global _client
+
try:
# try to connect to an already running server
- return Client(vfsdb)
+ _client = Client(vfsdb)
+ return _client
except socket.error:
pass
@@ -76,13 +82,13 @@
while time.time() < stop:
step()
try:
- c = Client(vfsdb)
+ _client = Client(vfsdb)
# client ready, close fd to server
for fd in server_fd:
fd.close()
# stop temp timer
t.stop()
- return c
+ return _client
except socket.error:
pass
@@ -99,3 +105,21 @@
# raise error
raise OSError('Unable to start vfs server')
+
+
+def get(filename):
+ """
+ Get object for the given filename.
+ """
+ if not _client:
+ raise RuntimeError('vfs not connected')
+ return _client.get(filename)
+
+
+def query(**args):
+ """
+ Query the database.
+ """
+ if not _client:
+ raise RuntimeError('vfs not connected')
+ return _client.query(**args)
Modified: trunk/WIP/vfs/src/db.py
==============================================================================
--- trunk/WIP/vfs/src/db.py (original)
+++ trunk/WIP/vfs/src/db.py Sat Feb 18 17:50:18 2006
@@ -134,7 +134,7 @@
# return None
- def __str__(self):
+ def __repr__(self):
"""
Convert object to string (usefull for debugging)
"""
@@ -308,7 +308,8 @@
def _query_dir(self, parent):
"""
- A query to get all files in a directory.
+ A query to get all files in a directory. The parameter parent is a
+ directort object.
"""
dirname = parent.filename[:-1]
items = []
@@ -373,96 +374,119 @@
return items
-# def _query_attr(self, *args, **kwargs):
-# """
-# A query to get a list of possible values of one attribute. Special
-# keyword 'attr' the query is used for that. This query will not return
-# a list of items.
-# """
-# kwargs['distinct'] = True
-# kwargs['attrs'] = [ kwargs['attr'] ]
-# del kwargs['attr']
-# return [ x[1] for x in self._db.query_raw(**kwargs)[1] if x[1] ]
+ def _query_filename(self, filename):
+ """
+ Return item for filename, can't be in overlay
+ """
+ dirname = os.path.dirname(filename)
+ basename = os.path.basename(filename)
+ # find correct mountpoint
+ for m in self._mountpoints:
+ if dirname.startswith(m.directory):
+ break
+ parent = self._get_dir(dirname, m)
+ if parent._vfs_id:
+ # parent is a valid db item, query
+ e = self._db.query(parent=parent._vfs_id, name=basename)
+ if e:
+ # entry is in the db
+ basename = e[0]
+ if os.path.isdir(filename):
+ return create_dir(basename, parent)
+ return create_file(basename, parent)
+
+
+ def _query_id(self, (type, id), cache=None):
+ """
+ Return item based on (type,id). Use given cache if provided.
+ """
+ i = self._db.query(type=type, id=id)[0]
+ # now we need a parent
+ if i['name'] == '':
+ # root node found, find correct mountpoint
+ for m in self._mountpoints:
+ if m.id == i['parent']:
+ break
+ else:
+ raise AttributeError('bad media %s' % i['parent'])
+ return create_dir(i, m)
+ # query for parent
+ pid = i['parent']
+ if cache is not None and pid in cache:
+ parent = cache[pid]
+ else:
+ parent = self._query_id(pid)
+ if cache is not None:
+ cache[pid] = parent
+
+ if i['type'] == 'dir':
+ # it is a directory, make a dir item
+ return create_dir(i, parent)
+ if parent._vfs_isdir:
+ # parent is dir, this item is not
+ return create_file(i, parent)
+ # neither dir nor file, something else
+ return create_item(i, parent)
+
+
+ def _query_parent(self, parent):
+ """
+ Return all items for the given parent object.
+ """
+ if parent._vfs_isdir:
+ return self._query_dir(parent)
+ raise AttributeError('oops, fix me')
+
+
+ def _query_attr(self, query):
+ """
+ A query to get a list of possible values of one attribute. Special
+ keyword 'attr' the query is used for that. This query will not return
+ a list of items.
+ """
+ attr = query['attr']
+ del query['attr']
+
+ result = self._db.query_raw(attrs=[attr], distinct=True, **query)[1]
+ result = [ x[1] for x in result if x[1] ]
+ # sort results and return
+ result.sort()
+ return result
+
+
def query(self, **query):
"""
- Internal query function inside the thread. This function will use the
- corrent internal query function based on special keywords.
+ Main query function.
"""
-# print 'QUERY', query
# make sure db is ok
self.commit()
-
- if 'filename' in query and len(query) == 1:
- # return item for filename, can't be in overlay
- filename = query['filename']
- dirname = os.path.dirname(filename)
- basename = os.path.basename(filename)
- # find correct mountpoint
- for m in self._mountpoints:
- if dirname.startswith(m.directory):
- break
- parent = self._get_dir(dirname, m)
- if parent._vfs_id:
- # parent is a valid db item, query
- e = self._db.query(parent=parent._vfs_id, name=basename)
- if e:
- # entry is in the db
- basename = e[0]
- if os.path.isdir(filename):
- return create_dir(basename, parent)
- return create_file(basename, parent)
-
+ # do query based on type
+ if 'filename' in query and len(query) == 1:
+ return self._query_filename(query['filename'])
if 'id' in query and len(query) == 1:
- # return item based on id (id is type,id)
-
- i = self._db.query(type=query['id'][0], id=query['id'][1])[0]
- # now we need a parent
- if i['name'] == '':
- # root node found, find correct mountpoint
- for m in self._mountpoints:
- if m.id == i['parent']:
- break
- else:
- raise AttributeError('bad media %s' % i['parent'])
- return create_dir(i, m)
- # query for parent
- parent = self.query(id=i['parent'])
- if i['type'] == 'dir':
- # it is a directory, make a dir item
- return create_dir(i, parent)
- if parent._vfs_isdir:
- # parent is dir, this item is not
- return create_file(i, parent)
- # neither dir nor file, something else
- return create_item(i, parent)
-
-
+ return self._query_id(query['id'])
if 'parent' in query and len(query) == 1:
- # return all items for the given parent (parent is an object)
- parent = query['parent']
- if parent._vfs_isdir:
- return self._query_dir(parent)
- raise AttributeError('oops, fix me')
+ return self._query_parent(query['parent'])
+ if 'attr' in query:
+ return self._query_attr(query)
-# if 'attr' in kwargs:
-# return self._query_attr(*args, **kwargs)
-# if 'device' in kwargs:
-# # A query to monitor a media (mountpoint). Special keyword
'media' in
-# # the query is used for that.
-# for m in self._mountpoints:
-# if m.device == kwargs['device']:
-# return m
-# raise AttributeError('Unknown device' % kwargs['device'])
-# return self._db.query(*args, **kwargs)
-
+ # 'raw' query
result = []
+ cache = {}
for r in self._db.query(**query):
- # get the parent of the item
- # FIXME: cache results here
- parent = self.query(id=r['parent'])
+
+ # get parent
+ pid = r['parent']
+ if pid in cache:
+ parent = cache[pid]
+ else:
+ parent = self._query_id(pid, cache)
+ cache[pid] = parent
+
+ # create item
if r['type'] == 'dir':
# it is a directory, make a dir item
result.append(create_dir(r, parent))
@@ -472,8 +496,20 @@
else:
# neither dir nor file, something else
result.append(create_item(r, parent))
+
+ # sort results and return
+ result.sort(lambda x,y: cmp(x.url, y.url))
return result
+# if 'device' in kwargs:
+# # A query to monitor a media (mountpoint). Special keyword
'media' in
+# # the query is used for that.
+# for m in self._mountpoints:
+# if m.device == kwargs['device']:
+# return m
+# raise AttributeError('Unknown device' % kwargs['device'])
+# return self._db.query(*args, **kwargs)
+
def add_object(self, type, *args, **kwargs):
Modified: trunk/WIP/vfs/src/item.py
==============================================================================
--- trunk/WIP/vfs/src/item.py (original)
+++ trunk/WIP/vfs/src/item.py Sat Feb 18 17:50:18 2006
@@ -208,26 +208,6 @@
return os.stat(self.filename)[stat.ST_MTIME]
-# def listdir(self):
-# """
-# List the directory. This returns a database object.
-# """
-# if self.db:
-# return self.db.query(dirname=self.filename)
-# raise AttributeError('item has no db object')
-
-
-# def os_listdir(self):
-# """
-# Return (cached) os.listdir information including the overlay dir.
-# The result is a list of basename, url.
-# """
-# if self._os_listdir == None:
-# listing = util.listdir(self.filename[:-1], self.media)
-# self._os_listdir = [ (x[x.rfind('/')+1:], x) for x in listing ]
-# return self._os_listdir
-
-
def __repr__(self):
"""
Convert object to string (usefull for debugging)
Modified: trunk/WIP/vfs/test/cache.py
==============================================================================
--- trunk/WIP/vfs/test/cache.py (original)
+++ trunk/WIP/vfs/test/cache.py Sat Feb 18 17:50:18 2006
@@ -4,10 +4,10 @@
import logging
# full parameter set for connect
-# c = kaa.vfs.connect('vfsdb', 'logfile', logging.INFO)
+# kaa.vfs.connect('vfsdb', 'logfile', logging.INFO)
# simple connect
-c = kaa.vfs.connect('vfsdb')
+kaa.vfs.connect('vfsdb')
checked = []
to_check = []
@@ -45,8 +45,7 @@
check()
-d = c.get(sys.argv[1])
-to_check.append(d)
+to_check.append(kaa.vfs.get(sys.argv[1]))
check()
Modified: trunk/WIP/vfs/test/client.py
==============================================================================
--- trunk/WIP/vfs/test/client.py (original)
+++ trunk/WIP/vfs/test/client.py Sat Feb 18 17:50:18 2006
@@ -1,29 +1,33 @@
import kaa.vfs
-from kaa.notifier import Timer, OneShotTimer
import sys
import kaa
-import kaa.notifier
-from kaa.base.weakref import weakref
import time
-import logging
-
def msg(*args):
print '>>>>>>>>>', args
-c = kaa.vfs.connect('vfsdb')
-video = c.get(sys.argv[1])
-print video
-x = video.listdir()
-
-x.signals['changed'].connect(msg, 'changed')
-x.signals['progress'].connect(msg, 'progress')
-x.signals['up-to-date'].connect(msg, 'up-to-date')
-
-x.monitor()
-for f in x:
- print repr(f)
+kaa.vfs.connect('vfsdb')
+
+a = u'Inkubus Sukkubus'
+#a = u'Bif Naked'
+
+t1 = time.time()
+result = kaa.vfs.get(sys.argv[1]).listdir()
+# result = kaa.vfs.query(artist=a)
+# result = kaa.vfs.query(attr='album', type='audio')
+t2 = time.time()
+
+# x.signals['changed'].connect(msg, 'changed')
+# x.signals['progress'].connect(msg, 'progress')
+# x.signals['up-to-date'].connect(msg, 'up-to-date')
+
+# x.monitor()
+
+print 'query took', (t2 - t1)
+
+if 1:
+ for r in result:
+ print r
-# OneShotTimer(x.monitor, False).start(1)
print 'loop'
kaa.main()
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog