Commits

Konstantine Rybnikov committed 3cebe86

looks like fixed

  • Participants
  • Parent commits cbb6276

Comments (0)

Files changed (7)

 syntax: glob
 config.py
 *.pyc
-
+.project
+.pydevproject
+.settings/**
 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
+from openju import OpenJu
+
 # configuration
 DEBUG = True
 JID = 'openju@example.com'
 PASSWORD = 'FasengEntoabApJo'
 
-from openju import OpenJu
 app = OpenJu(__name__)
 app.config.from_object(__name__)
 
         'JID': None,
         'PWD': None,
         'MONGO_DBNAME': 'openju',
+        'MONGO_HOST': None,
+        'MONGO_PORT': None,
         'LOGGER_NAME': None,
     }
     
         default configuration is to log to stderr if the application is
         in debug mode.  This logger can be used to (surprise) log messages.
         Here some examples::
-
+        
             app.logger.debug('A value for debugging')
             app.logger.warning('A warning ocurred (%d apples)', 42)
             app.logger.error('An error occoured')
-
+            
         .. versionadded:: 0.3
         """
         if self._logger and self._logger.name == self.logger_name:
         if not authres:
             raise Exception("Unable to authorize on %s - check login/password." % server)
         
-        from openju.messagecb import message_cb
-        conn.RegisterHandler('message', message_cb)
+        from openju.message_bus import MessageBus
+        message_bus = MessageBus()
+        conn.RegisterHandler('message', message_bus)
         conn.sendInitPresence()
         self.logger.info('Bot started')
         
+        class MetaObject(object):
+            pass
+        
+        from openju.globals import _request_ctx_stack
+        
+        meta = MetaObject()
+        meta.current_app = self
+        meta.g = MetaObject()
+        _request_ctx_stack.push(meta)
+        
         def step_on(conn):
             try:
                 conn.Process(1)

openju/globals.py

 
 _request_ctx_stack = LocalStack()
 g = LocalProxy(lambda: _request_ctx_stack.top.g)
+current_app = LocalProxy(lambda: _request_ctx_stack.top.current_app)

openju/message_bus.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import xmpp
+from openju import g
+import re
+
+from openju.util import none_or_int
+from openju.validators import UserAlertError
+import openju.controllers as controllers
+
+def error_handler(f):
+    def new_f(conn, *args, **kw):
+        try:
+            f(conn, *args, **kw)
+        except UserAlertError, e:
+            conn.send(xmpp.Message(g.user, unicode(e)))
+    return new_f
+
+class MessageBus(object):
+    @error_handler
+    def __call__(self, conn, mess):
+        """ kind a routing thing """
+        
+        def cmd_last_messages(user_jid, text):
+            pattern = re.compile(ur'\s*#\s*$')
+            m = pattern.match(text)
+            if m is not None:
+                return controllers.message.last_messages()
+        
+        def cmd_read_message(user_jid, text):
+            # pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<more>\+)?\s*')
+            pattern = re.compile(ur'^#(?P<message_hrid>[0-9]+)\s*$')
+            m = pattern.match(text)
+            if m is not None:
+                d = m.groupdict()
+                # d['r_no'] = none_or_int(d['r_no'])
+                return controllers.message.read(int(d['message_hrid']))
+        
+        def cmd_new_nick(user_jid, text):
+            cmd_text = u'NICK'
+            if text.startswith(cmd_text):
+                nick = text[len(cmd_text) + 1:].strip()
+                return controllers.user.new_nick(nick)
+        
+        def cmd_post_reply(user_jid, text):
+            pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<reply>\s.*)')
+            m = pattern.match(text)
+            if m is not None:
+                d = m.groupdict()
+                d['r_no'] = none_or_int(d['r_no'])
+                if d['r_no'] is not None: d['r_no'] = int(d['r_no'])
+                return controllers.message.post_reply(
+                    int(d['message_hrid']),
+                    d['reply'],
+                    r_no=d['r_no'])
+        
+        def cmd_post_message(user_jid, text):
+            return controllers.message.post(text)
+        
+        text = mess.getBody()
+        user = mess.getFrom()
+        user_jid = u'%(node)s@%(domain)s' % dict(
+            node=user.getNode(),
+            domain=user.getDomain())
+        
+        if text is not None: # don't know why, but sometimes it's None. TODO: debug it
+            def call_list(*f_list):
+                for f in f_list:
+                    res = f(user_jid, text)
+                    if isinstance(res, unicode):
+                        conn.send(xmpp.Message(user, res))
+                    if res is not None:
+                        return
+            g.user = user
+            g.text = text
+            g.user_jid = user_jid
+
+            call_list(
+                cmd_last_messages,
+                cmd_read_message,
+                cmd_new_nick,
+                cmd_post_reply,
+                cmd_post_message,
+                )

openju/messagecb.py

-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-
-import xmpp
-from openju import g
-import re
-
-from openju.util import none_or_int
-from openju.validators import UserAlertError
-import openju.controllers as controllers
-
-def error_handler(f):
-    def new_f(conn, *args, **kw):
-        try:
-            f(conn, *args, **kw)
-        except UserAlertError, e:
-            conn.send(xmpp.Message(g.user, unicode(e)))
-    return new_f
-
-@error_handler
-def message_cb(conn, mess):
-    """ kind a routing thing """
-    
-    def cmd_last_messages(user_jid, text):
-        pattern = re.compile(ur'\s*#\s*$')
-        m = pattern.match(text)
-        if m is not None:
-            return controllers.message.last_messages()
-    
-    def cmd_read_message(user_jid, text):
-        # pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<more>\+)?\s*')
-        pattern = re.compile(ur'^#(?P<message_hrid>[0-9]+)\s*$')
-        m = pattern.match(text)
-        if m is not None:
-            d = m.groupdict()
-            # d['r_no'] = none_or_int(d['r_no'])
-            return controllers.message.read(int(d['message_hrid']))
-    
-    def cmd_new_nick(user_jid, text):
-        cmd_text = u'NICK'
-        if text.startswith(cmd_text):
-            nick = text[len(cmd_text) + 1:].strip()
-            return controllers.user.new_nick(nick)
-    
-    def cmd_post_reply(user_jid, text):
-        pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<reply>\s.*)')
-        m = pattern.match(text)
-        if m is not None:
-            d = m.groupdict()
-            d['r_no'] = none_or_int(d['r_no'])
-            if d['r_no'] is not None: d['r_no'] = int(d['r_no'])
-            return controllers.message.post_reply(
-                int(d['message_hrid']),
-                d['reply'],
-                r_no=d['r_no'])
-    
-    def cmd_post_message(user_jid, text):
-        return controllers.message.post(text)
-    
-    text = mess.getBody()
-    user = mess.getFrom()
-    user_jid = u'%(node)s@%(domain)s' % dict(
-        node=user.getNode(),
-        domain=user.getDomain())
-    
-    if text is not None: # don't know why, but sometimes it's None. TODO: debug it
-        def call_list(*f_list):
-            for f in f_list:
-                res = f(user_jid, text)
-                if isinstance(res, unicode):
-                    conn.send(xmpp.Message(user, res))
-                if res is not None:
-                    return
-        g.user = user
-        g.text = text
-        g.user_jid = user_jid
-        
-        call_list(
-            cmd_last_messages,
-            cmd_read_message,
-            cmd_new_nick,
-            cmd_post_reply,
-            cmd_post_message,
-            )
 
 from pymongo import Connection
 
+from openju.globals import current_app
+
 class mongo_connection_wrapper(object):
     def __init__(self):
         self.mongo = Connection()
 
 class mongo_db_wrapper(object):
     def __init__(self):
-        params = [x for x in [
-            get_conf_val('mongo_host'),
-            get_conf_val('mongo_port')
-            ] if x is not None]
+        params = [
+            x for x in [
+                current_app.config.get('MONGO_HOST'),
+                current_app.config.get('MONGO_PORT')
+                ]
+            if x is not None]
         self.mongo = Connection(*params)
     
     def __enter__(self):
-        return self.mongo[get_conf_val('mongo_dbname')]
+        return self.mongo[current_app.config.get('MONGO_DBNAME')]
     
     def __exit__(self, type, value, traceback):
         self.mongo.disconnect()