Commits

Anonymous committed 3e92134

Split a file to two.

  • Participants
  • Parent commits dbabccf
  • Tags v_0.2.0

Comments (0)

Files changed (3)

kay/handlers.py

-# -*- coding: utf-8 -*-
-
-"""
-kay.handlers
-
-:Copyright: (c) 2009 Takashi Matsuo <tmatsuo@candit.jp> All rights reserved.
-:license: BSD, see LICENSE for more details.
-"""
-
-import logging
-
-from google.appengine.api import xmpp
-from google.appengine.ext.webapp import xmpp_handlers
-
-from werkzeug.exceptions import (
-  MethodNotAllowed, NotImplemented
-)
-
-METHODS = ['GET', 'POST', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']
-
-class BaseHandler(object):
-
-  def __init__(self):
-    pass
-
-  def __call__(self, request, **kwargs):
-    self.request = request
-    prepare_func = getattr(self, 'prepare', None)
-    if callable(prepare_func):
-      response = prepare_func()
-      if response:
-        return response
-    if request.method in METHODS:
-      func = getattr(self, request.method.lower(), None)
-      if callable(func):
-        try:
-          return func(**kwargs)
-        except Exception, e:
-          self.handle_exception(e)
-          raise
-      else:
-        return NotImplemented()
-    else:
-      return MethodNotAllowed()
-
-  def handle_exception(self, exception):
-    pass
-
-
-class XMPPBaseHandler(BaseHandler):
-  """A baseclass for XMPP handlers.
-
-  Implements a straightforward message delivery pattern. When a message is
-  received, message_received is called with a Message object that encapsulates
-  the relevant details. Users can reply using the standard XMPP API, or the
-  convenient .reply() method on the Message object.
-  """
-
-  def __init__(self):
-    import kay.sessions
-    super(XMPPBaseHandler, self).__init__()
-    setattr(self, kay.sessions.NO_SESSION, True)
-
-
-  def message_received(self, message):
-    """Called when a message is sent to the XMPP bot.
-
-    Args:
-      message: Message: The message that was sent by the user.
-    """
-    raise NotImplementedError()
-
-  def handle_exception(self, exception):
-    """Called if this handler throws an exception during execution.
-
-    Args:
-      exception: the exception that was thrown
-      debug_mode: True if the web application is running in debug mode
-    """
-    if self.xmpp_message:
-      self.xmpp_message.reply('Oops. Something went wrong.')
-    super(XMPPBaseHandler, self).handle_exception(exception)
-
-  def post(self):
-    try:
-      self.xmpp_message = xmpp.Message(self.request.form)
-    except xmpp.InvalidMessageError, e:
-      logging.error("Invalid XMPP request: Missing required field %s", e[0])
-      return
-    self.message_received(self.xmpp_message)
-
-
-class XMPPCommandHandler(xmpp_handlers.CommandHandlerMixin, XMPPBaseHandler):
-  pass

kay/handlers/__init__.py

+# -*- coding: utf-8 -*-
+
+"""
+kay.handlers
+
+:Copyright: (c) 2009 Takashi Matsuo <tmatsuo@candit.jp> All rights reserved.
+:license: BSD, see LICENSE for more details.
+"""
+
+import logging
+
+from google.appengine.api import xmpp
+from google.appengine.ext.webapp import xmpp_handlers
+
+from werkzeug.exceptions import (
+  MethodNotAllowed, NotImplemented
+)
+
+METHODS = ['GET', 'POST', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']
+
+class BaseHandler(object):
+
+  def __init__(self):
+    pass
+
+  def __call__(self, request, **kwargs):
+    self.request = request
+    prepare_func = getattr(self, 'prepare', None)
+    if callable(prepare_func):
+      response = prepare_func()
+      if response:
+        return response
+    if request.method in METHODS:
+      func = getattr(self, request.method.lower(), None)
+      if callable(func):
+        try:
+          return func(**kwargs)
+        except Exception, e:
+          self.handle_exception(e)
+          raise
+      else:
+        return NotImplemented()
+    else:
+      return MethodNotAllowed()
+
+  def handle_exception(self, exception):
+    pass

kay/handlers/xmpp.py

+# -*- coding: utf-8 -*-
+
+"""
+kay.handlers.xmpp
+
+:Copyright: (c) 2009 Takashi Matsuo <tmatsuo@candit.jp> All rights reserved.
+:license: BSD, see LICENSE for more details.
+"""
+
+import logging
+
+from google.appengine.api import xmpp
+from google.appengine.ext.webapp import xmpp_handlers
+
+from kay.handlers import BaseHandler
+
+class XMPPBaseHandler(BaseHandler):
+  """A baseclass for XMPP handlers.
+
+  Implements a straightforward message delivery pattern. When a message is
+  received, message_received is called with a Message object that encapsulates
+  the relevant details. Users can reply using the standard XMPP API, or the
+  convenient .reply() method on the Message object.
+  """
+
+  def __init__(self):
+    import kay.sessions
+    super(XMPPBaseHandler, self).__init__()
+    setattr(self, kay.sessions.NO_SESSION, True)
+
+
+  def message_received(self, message):
+    """Called when a message is sent to the XMPP bot.
+
+    Args:
+      message: Message: The message that was sent by the user.
+    """
+    raise NotImplementedError()
+
+  def handle_exception(self, exception):
+    """Called if this handler throws an exception during execution.
+
+    Args:
+      exception: the exception that was thrown
+      debug_mode: True if the web application is running in debug mode
+    """
+    if self.xmpp_message:
+      self.xmpp_message.reply('Oops. Something went wrong.')
+    super(XMPPBaseHandler, self).handle_exception(exception)
+
+  def post(self):
+    try:
+      self.xmpp_message = xmpp.Message(self.request.form)
+    except xmpp.InvalidMessageError, e:
+      logging.error("Invalid XMPP request: Missing required field %s", e[0])
+      return
+    self.message_received(self.xmpp_message)
+
+
+class XMPPCommandHandler(xmpp_handlers.CommandHandlerMixin, XMPPBaseHandler):
+  pass