Author: dmeyer
Date: Thu Sep 13 16:19:31 2007
New Revision: 2807

Log:
some refactoring

Modified:
   trunk/beacon/src/__init__.py
   trunk/beacon/src/client.py
   trunk/beacon/src/file.py
   trunk/beacon/src/item.py
   trunk/beacon/src/query.py

Modified: trunk/beacon/src/__init__.py
==============================================================================
--- trunk/beacon/src/__init__.py        (original)
+++ trunk/beacon/src/__init__.py        Thu Sep 13 16:19:31 2007
@@ -176,12 +176,13 @@
     """
     Gets statistics about the database. This function will block using
     kaa.notifier.step() until the client is connected.
+    (Only usefull for debugging)
     """
     if not _client:
         connect()
     while not _client.status == CONNECTED:
         kaa.notifier.step()
-    return _client._beacon_db_information()
+    return _client._db.get_db_info()
 
 
 def delete_media(id):

Modified: trunk/beacon/src/client.py
==============================================================================
--- trunk/beacon/src/client.py  (original)
+++ trunk/beacon/src/client.py  Thu Sep 13 16:19:31 2007
@@ -36,7 +36,6 @@
 
 # Python imports
 import os
-import copy
 import logging
 
 # kaa imports
@@ -229,7 +228,8 @@
         # Reset monitors to queries
         for query in self._queries:
             if query != None and query.monitoring:
-                self._beacon_monitor_add(query)
+                query.monitoring = False
+                query.monitor(True)
 
         return False
 
@@ -261,66 +261,14 @@
     # Internal API
     # -------------------------------------------------------------------------
 
-    def _beacon_monitor_add(self, query):
-        """
-        Monitor a query
-        """
-        if not self.status == CONNECTED:
-            return
-        q = copy.copy(query._query)
-        if 'parent' in q:
-            if not q['parent']._beacon_id:
-                # we need the get the id first
-                q['parent']._beacon_request(self._beacon_monitor_add, query)
-                return
-            q['parent'] = q['parent']._beacon_id
-        self.rpc('monitor.add', self.id, query.id, q)
-
-
-    def _beacon_monitor_remove(self, query):
-        """
-        Monitor a query
-        """
-        if not self.status == CONNECTED:
-            return
-        self.rpc('monitor.remove', self.id, query.id)
-
-
-    def _beacon_request(self, filename, callback, *args, **kwargs):
-        """
-        Request information about a filename.
-        """
-        if not self.status == CONNECTED:
-            return False
-        self.rpc('item.request', filename).connect(callback, *args, **kwargs)
-        return True
-
-
-    @kaa.notifier.yield_execution()
-    def _beacon_query(self, **kwargs):
-        """
-        Do a query from the db.
-        """
-        # we have to wait until we are sure that the db is free for
-        # read access or the sqlite client will find a lock and waits
-        # some time until it tries again. That time is too long, it
-        # can take up to two seconds.
-        yield self.rpc('db.lock')
-        result = self._db.query(**kwargs)
-        if isinstance(result, kaa.notifier.InProgress):
-            yield result
-            result = result.get_result()
-        self.rpc('db.unlock')
-        yield result
-
-
     def _beacon_update(self, item):
         """
         Update item in next main loop interation.
         """
         if not item._beacon_id:
-            # item has no beacon id, request the data before
-            # schedule the update
+            # Item has no beacon id, request the data before
+            # schedule the update. If we are not connected the
+            # update will be lost.
             item._beacon_request(self._beacon_update, item)
             return
         if not self._changed:
@@ -361,13 +309,6 @@
         yield result
 
 
-    def _beacon_db_information(self):
-        """
-        Get some basic db information for debugging.
-        """
-        return self._db.get_db_info()
-
-
     # -------------------------------------------------------------------------
     # Server callbacks
     # -------------------------------------------------------------------------
@@ -428,7 +369,8 @@
                 return query.signals['up-to-date'].emit()
             log.error('Error: unknown message from server: %s' % msg)
             return
-
+        log.error('query %s not found', id)
+        
 
     @kaa.rpc.expose('device.changed')
     def media_changed(self, id, prop):

Modified: trunk/beacon/src/file.py
==============================================================================
--- trunk/beacon/src/file.py    (original)
+++ trunk/beacon/src/file.py    Thu Sep 13 16:19:31 2007
@@ -43,6 +43,7 @@
 # get logging object
 log = logging.getLogger('beacon')
 
+CONNECTED = 'connected'
 
 class File(Item):
     """
@@ -228,12 +229,14 @@
 
     def _beacon_request(self, callback=None, *args, **kwargs):
         """
-        Request the item to be scanned.
+        Request the item to be scanned. (Client API only)
         """
-        f = self.get_controller()._beacon_request
-        f(self.filename, self._beacon_database_update, callback,
-          *args, **kwargs)
-        return None
+
+        if not self.get_controller().status == CONNECTED:
+            return False
+        rpc = self.get_controller().rpc('item.request', self.filename)
+        rpc.connect_once(self._beacon_database_update, callback, *args, 
**kwargs)
+        return True
 
 
     # -------------------------------------------------------------------------

Modified: trunk/beacon/src/item.py
==============================================================================
--- trunk/beacon/src/item.py    (original)
+++ trunk/beacon/src/item.py    Thu Sep 13 16:19:31 2007
@@ -134,7 +134,11 @@
 
         if request and not self._beacon_id:
             log.info('requesting data for %s', self)
-            self._beacon_request()
+            if not self._beacon_request():
+                # unable to do this right now
+                return None
+
+            # FIXME: remove notifier.step() here!
             while not self._beacon_id:
                 kaa.notifier.step()
 
@@ -253,7 +257,7 @@
         """
         Request the item to be scanned.
         """
-        return None
+        return False
 
 
     # -------------------------------------------------------------------------

Modified: trunk/beacon/src/query.py
==============================================================================
--- trunk/beacon/src/query.py   (original)
+++ trunk/beacon/src/query.py   Thu Sep 13 16:19:31 2007
@@ -31,6 +31,7 @@
 
 
 # Python imports
+import copy
 import logging
 
 # kaa imports
@@ -71,13 +72,19 @@
         }
         self.id = Query.NEXT_ID
         Query.NEXT_ID += 1
-        self._query = query
-        self._client = client
+
+        # public variables
         self.monitoring = False
-        # XXX: maybe 'completed' is better than 'valid'?
         self.valid = False
         self.result = []
-        self._beacon_start_query(query, False)
+        # internal variables
+        self._query = query
+        self._client = client
+        # some shortcuts from the client
+        self._rpc = self._client.rpc
+        self._db = self._client._db
+        # start inititial query
+        self._beacon_start_query(query)
 
 
     # -------------------------------------------------------------------------
@@ -89,12 +96,28 @@
         Turn on/off query monitoring
         """
         if self.monitoring == status:
+            # Nothing to do
+            return
+        if not self._client.status == CONNECTED:
+            # If the client is not connected yet, it will do this later.
+            # Rememeber that we wanted to connect
+            self.monitoring = status
             return
-        self.monitoring = status
-        # if the client is not connected yet, it will do this later.
         if status:
-            return self._client._beacon_monitor_add(self)
-        self._client._beacon_monitor_remove(self)
+            query = copy.copy(self._query)
+            if 'parent' in query:
+                parent = query['parent']
+                if not parent._beacon_id:
+                    # We need the get the id first. Call the function again
+                    # when there is an id.
+                    parent._beacon_request(self.monitor, status)
+                    return
+                query['parent'] = parent._beacon_id
+            self._rpc('monitor.add', self._client.id, self.id, query)
+        else:
+            self._rpc('monitor.remove', self._client.id, self.id)
+        # Store current status
+        self.monitoring = status
 
 
     def __iter__(self):
@@ -157,7 +180,7 @@
     # -------------------------------------------------------------------------
 
     @kaa.notifier.yield_execution()
-    def _beacon_start_query(self, query, emit_signal):
+    def _beacon_start_query(self, query):
         """
         Start the database query.
         """
@@ -166,9 +189,6 @@
             wait = kaa.notifier.YieldCallback()
             self._client.signals['connect'].connect_once(wait)
             yield wait
-            emit_signal = True
-            # Since query was async, we need to emit 'changed' signal, so
-            # override emit_signal argument.
 
         if 'parent' in query and isinstance(query['parent'], Item) and \
                not query['parent']._beacon_id:
@@ -177,20 +197,22 @@
             # request the real database id and do the query when done.
             parent = query['parent']
             log.info('force data for %s', parent)
-            parent._beacon_request(self._beacon_start_query, query, True)
+            parent._beacon_request(self._beacon_start_query, query)
             return
 
-        self.result = self._client._beacon_query(**query)
-        # _beacon_query always is async because it call a rpc
-        yield self.result
-        self.result = self.result()
-        # Since query was async, we need to emit 'changed' signal, so
-        # override emit_signal argument.
-        emit_signal = True
+        # we have to wait until we are sure that the db is free for
+        # read access or the sqlite client will find a lock and waits
+        # some time until it tries again. That time is too long, it
+        # can take up to two seconds.
+        yield self._rpc('db.lock')
+        self.result = self._db.query(**query)
+        if isinstance(self.result, kaa.notifier.InProgress):
+            yield self.result
+            self.result = self.result.get_result()
+        self._rpc('db.unlock')
 
         self.valid = True
-        if emit_signal:
-            self.signals['changed'].emit()
+        self.signals['changed'].emit()
         if self.signals['yield']:
             self.signals['yield'].emit(self)
             self.signals['yield'] = None
@@ -220,10 +242,16 @@
         """
         Changed message from server.
         """
-        result = self._client._beacon_query(**self._query)
-        # _beacon_query always is async because it call a rpc
-        yield result
-        result = result.get_result()
+        # we have to wait until we are sure that the db is free for
+        # read access or the sqlite client will find a lock and waits
+        # some time until it tries again. That time is too long, it
+        # can take up to two seconds.
+        yield self._rpc('db.lock')
+        result = self._db.query(**self._query)
+        if isinstance(result, kaa.notifier.InProgress):
+            yield result
+            result = result.get_result()
+        self._rpc('db.unlock')
         if send_signal or len(self.result) != len(result):
             # The query result length is different
             self.result = result

-------------------------------------------------------------------------
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