Marcin Kasperski avatar Marcin Kasperski committed 520a344

on_logout added

interactive_shell properly shuts down

clock_statistician is more or less done.

Comments (0)

Files changed (4)

sample/clock_statistician.py

 from mekk.fics import fics_connector
 from mekk.fics.fics_client import FicsClient
 from mekk.fics.fics_client_mixin_event_methods import EventMethodsMixin
+from mekk.fics.tell_commands import tell_errors
 from mekk.fics.tell_commands.fics_client_mixin_tell_commands import TellCommandsMixin, TellCommand
 #from mekk.fics.support.tell_status import TellLoopPrevention
 
         return self.dbpool.runInteraction(stat)
 
     def stat_clock(self, clock_base, clock_inc):
-        return self.dbpool.runQuery("""
-            SELECT COUNT(*) AS clock_count
-            FROM games
-            WHERE variant IN ('standard', 'blitz', 'lightning')
-              AND clock_base = ? AND clock_inc = ?
-        """, [clock_base, clock_inc])
+        def stat(tx):
+            r = tx.execute("""
+                SELECT COUNT(*) AS clock_count
+                FROM games
+                WHERE variant IN ('standard', 'blitz', 'lightning')
+                AND clock_base = ? AND clock_inc = ?
+                """, [clock_base, clock_inc])
+            return r.fetchall()[0][0]
+        return self.dbpool.runInteraction(stat)
 
 #################################################################################
 # Commands (handling tells to the bot)
         return 2,2
     @defer.inlineCallbacks
     def run(self, fics_client, player, *args, **kwargs):
-        base, inc = args
+        try:
+            base, inc = int(args[0]), int(args[1])
+        except ValueError:
+            raise tell_errors.InvalidCommandParameters()
         stat = yield self.clock_statistician.stat_clock(base, inc)
         # Alternative reply method
         yield fics_client.tell_to(player, "I spotted %s games played with %s %s clock" % (

sample/interactive_shell.py

         yield self.set_finger(FINGER_TEXT)
         self.console.register_fics_client(self, user)
 
+    def on_logout(self):
+        if reactor.running:
+            reactor.stop()
+
     def on_fics_information(self, what, args):
         return self.console.show_information(what, args)
 
 
     def connectionLost(self, reason=None):
         #noinspection PyUnresolvedReferences
-        reactor.stop()
+        if reactor.running:
+            reactor.stop()
 
     #################################################################
     # APIs used from our client and from functions above

src/mekk/fics/fics_client.py

         """
         return defer.succeed(None)
 
+    def on_logout(self):
+        """
+        Called after connection to FICS is closed. May cleanup.
+
+        Note: in some cases (really fast connection drop) on_logout may be called
+        without on_login or after only part on_login executed.
+        """
+        return defer.succeed(None)
+
     def on_command_timeout(self):
         """
         Called whenever we detect that some issued command timed out.

src/mekk/fics/fics_connector.py

         self._line_sender = None
         self._keep_alive = None  # This also stops
         self._list_cache = None
+        d = self.client.on_logout()
+        if isinstance(d, defer.Deferred):
+            d.addErrback(self.on_unhandled_processing_failure)
         return basic.LineReceiver.connectionLost(self, reason)
 
     def rawDataReceived(self, data):
             self._fics_handle = name
             logger_lib.info("%s: Logged in as %s" % (self.label, name))
             self.setLineMode()
-            self.post_login()
+            d = self.post_login()
+            if isinstance(d, defer.Deferred):
+                d.addErrback(self.on_unhandled_processing_failure)
+            else:
+                logger_lib.warn("%s: Ugly object returned from post_login method (expected Deferred): %s" % (
+                    self.label, str(d)))
 
     def lineReceived(self, line):
         # Note: we split most of lineReceived into doLineReceived to make it possible
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.