Author: dmeyer
Date: Sat Sep 15 15:05:30 2007
New Revision: 2811

Log:
Make it possible for the server to call the item methods for
list, scan, etc.


Added:
   trunk/beacon/src/server/controller.py
Modified:
   trunk/beacon/src/client.py
   trunk/beacon/src/file.py
   trunk/beacon/src/server/hwmon/__init__.py
   trunk/beacon/src/server/hwmon/client.py
   trunk/beacon/src/server/server.py

Modified: trunk/beacon/src/client.py
==============================================================================
--- trunk/beacon/src/client.py  (original)
+++ trunk/beacon/src/client.py  Sat Sep 15 15:05:30 2007
@@ -317,6 +317,15 @@
         yield result
 
 
+    def _beacon_parse(self, item):
+        """
+        Parse the item, returns InProgress.
+        """
+        if not self.is_connected():
+            return False
+        return self.rpc('item.request', item.filename)
+
+
     # -------------------------------------------------------------------------
     # Server callbacks
     # -------------------------------------------------------------------------

Modified: trunk/beacon/src/file.py
==============================================================================
--- trunk/beacon/src/file.py    (original)
+++ trunk/beacon/src/file.py    Sat Sep 15 15:05:30 2007
@@ -127,11 +127,10 @@
         Request the item to be scanned. (Client API only)
         Returns either False if not connected or an InProgress object.
         """
-        if not self._beacon_controller().is_connected():
-            return False
-        rpc = self._beacon_controller().rpc('item.request', self.filename)
-        rpc.connect_once(self._beacon_database_update)
-        return rpc
+        result = self._beacon_controller()._beacon_parse(self)
+        if isinstance(result, kaa.notifier.InProgress):
+            result.connect_once(self._beacon_database_update)
+        return result
 
 
     # -------------------------------------------------------------------------

Added: trunk/beacon/src/server/controller.py
==============================================================================
--- (empty file)
+++ trunk/beacon/src/server/controller.py       Sat Sep 15 15:05:30 2007
@@ -0,0 +1,119 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# controller.py - Server controller interface for Media and Item
+# -----------------------------------------------------------------------------
+# $Id$
+#
+# -----------------------------------------------------------------------------
+# kaa.beacon.server - A virtual filesystem with metadata
+# Copyright (C) 2007 Dirk Meyer
+#
+# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
+#
+# Please see the file AUTHORS for a complete list of authors.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
+# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+# Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+# -----------------------------------------------------------------------------
+
+__all__ = [ 'Controller' ]
+
+# kaa imports
+import kaa.notifier
+
+# kaa.beacon imports
+import hwmon
+from kaa.beacon.media import medialist
+from parser import parse
+
+
+class Controller(object):
+    """
+    The controller defines the callbacks Item and Media need. On client
+    side this is all implemented in the Client class.
+    """
+    def __init__(self, handler, db, rootfs=None):
+        self._db = db
+        self._changed = []
+        hwmon.connect()
+        medialist.connect(self)
+        hwmon.set_database(handler, db, rootfs)
+
+    # Item callbacks
+
+    def _beacon_parse(self, item):
+        """
+        Parse an item
+        """
+        return parse(self._db, item)
+
+
+    @kaa.notifier.yield_execution()
+    def _beacon_update_all(self):
+        """
+        Timed callback to write all changes to the db.
+        """
+        while self._db.read_lock.is_locked():
+            yield self._db.read_lock.yield_unlock()
+        changes = self._changed
+        self._changed = []
+        for item in changes:
+            self._db.update_object(item._beacon_id, item._beacon_changes)
+            item._beacon_changes = {}
+        # commit to update monitors
+        self._db.commit()
+
+
+    def _beacon_update(self, item):
+        """
+        Mark item as changed to be updated in the db.
+        """
+        if not self._changed:
+            # register timer to do the changes
+            kaa.notifier.OneShotTimer(self._beacon_update_all).start(0.1)
+        self._changed.append(item)
+
+
+    def query(self, **query):
+        """
+        Database query.
+        """
+        return self._db.query(**query)
+
+
+    @kaa.notifier.yield_execution()
+    def delete_item(self, item):
+        """
+        Delete an item.
+        """
+        while self._db.read_lock.is_locked():
+            yield self._db.read_lock.yield_unlock()
+        self._db.delete_object(item._beacon_id)
+
+    # Media callbacks
+
+    def eject(self, media):
+        """
+        Eject media
+        """
+        hwmon.get_client().eject(media)
+
+
+    def _beacon_media_information(self, media):
+        """
+        Get media information from te database.
+        """
+        return self._db.query_media(media)

Modified: trunk/beacon/src/server/hwmon/__init__.py
==============================================================================
--- trunk/beacon/src/server/hwmon/__init__.py   (original)
+++ trunk/beacon/src/server/hwmon/__init__.py   Sat Sep 15 15:05:30 2007
@@ -65,6 +65,8 @@
 
 
 def set_database(handler, db, rootfs=None):
-    if not _client:
-        connect()
     _client.set_database(handler, db, rootfs)
+
+
+def get_client():
+    return _client

Modified: trunk/beacon/src/server/hwmon/client.py
==============================================================================
--- trunk/beacon/src/server/hwmon/client.py     (original)
+++ trunk/beacon/src/server/hwmon/client.py     Sat Sep 15 15:05:30 2007
@@ -60,7 +60,6 @@
         self._db = db
         # handler == beacon.Server
         self.handler = handler
-        medialist.connect(self)
         self.rpc('connect')
         self._device_add(rootfs)
 
@@ -79,9 +78,7 @@
         return self.rpc('device.eject', dev.prop.get('beacon.id'))
 
 
-    def _beacon_media_information(self, media):
-        return self._db.query_media(media)
-
+    # rpc callbacks
 
     @kaa.rpc.expose('device.add')
     def _device_add(self, dev):

Modified: trunk/beacon/src/server/server.py
==============================================================================
--- trunk/beacon/src/server/server.py   (original)
+++ trunk/beacon/src/server/server.py   Sat Sep 15 15:05:30 2007
@@ -45,7 +45,7 @@
 
 # kaa.beacon server imports
 import parser
-import hwmon
+from controller import Controller
 from db import *
 from monitor import Monitor
 from crawl import Crawler
@@ -149,20 +149,21 @@
         # set up.
         self._db.commit()
 
-        # give database to hwmon
+        # give database to controller / hardware monitor
         rootfs = {
             'beacon.id': 'kaa.beacon.root',
             'block.device': '',
             'volume.mount_point': '/'
         }
 
-        hwmon.set_database(self, self._db, rootfs)
+        self.item_controller = Controller(self, self._db, rootfs)
         self._db.commit()
 
         for dir in config.monitors:
             self.monitor_dir(os.path.expandvars(os.path.expanduser(dir)))
 
 
+
     # -------------------------------------------------------------
     # client handling
     # -------------------------------------------------------------

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to