Commits

Benjamin Smith committed 6126e96

Fixing a big mess.. Commit early, commit often? Naa..

Comments (0)

Files changed (7)

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import sys
+import argparse
+from configobj import ConfigObj
+from jabbercat.jabber_controller import JabberController, AuthenticationError
+
+
+def send_message(message, chat):
+    jc.send_message_to_chat(chat, message)
+
+if __name__ == '__main__':
+
+    parser = argparse.ArgumentParser(description='Cat something into a jabber chat')
+
+    parser.add_argument('-m', '--message', action='store', 
+        help='Message to send to chat')
+    parser.add_argument('-c', '--config', action='store', 
+        help='Config file to use (default /etc/jabbercat.conf)',
+        default='/etc/jabbercat/jabbercat.conf')
+
+    args = parser.parse_args()
+
+    config = ConfigObj(args.config)
+
+    jabber = config.get('jabber')
+
+    jc = JabberController(jabber.get('username'), jabber.get('password'),
+                          jabber.get('server'), jabber.get('resource'))
+
+    jc.connect()
+
+    try:
+        jc.authorize_with_server()
+    except AuthenticationError, exc_info:
+        print "%s" % exc_info
+        jc.connection.disconnect()
+        raise SystemExit
+
+    chat = jabber.get('chat')
+    jc.subscribe_to_chat(chat, 'jabbercat')
+
+    if args.message:
+        print args.message
+        send_message(args.message, chat)
+    else:
+        try:
+            message = sys.stdin.readline().strip()
+            if not message:
+                jc.connection.disconnect()
+                raise SystemExit
+
+            print "going to send message"
+            send_message(message, chat)
+
+        except KeyboardInterrupt:
+            jc.connection.disconnect()
+            raise SystemExit   

etc/jabbercat.conf

+[connection]
+debug = False
+[jabber]
+server = jabber.server.org
+username = myusername
+password = mypassword
+resource = myresourcename
+chat = chat@jabber.server.org
       license='',
       package_dir={"":"src"},
       packages=find_packages("src"),
-      scripts=['bin/jabbercat.py'],
+      scripts=['bin/jabbercat'],
       data_files = [('/etc/jabbercat/', ['etc/jabbercat.conf'])],
-      zip_safe = False      
+      zip_safe = False,
+      install_requires = ['web>=0.31', 'argparse>=0.9.1']
       )
 

src/jabbercat/jabber_controller.py

         self.password = password
         self.hostname = hostname
         self.resource = resource
-        self.config = self.setup_configuration()
-        self.connection = self.setup_connection()
+        self.config = None
+        self.connection = None
+        #self.connection = self.setup_connection()
 
     @property
     def user_id(self):
         user_id = "%s@%s/%s" % (self.username, self.hostname, self.resource)
         return user_id 
 
-    def setup_configuration(self):
-        config = ConfigObj('/etc/jabbercat/jabber.conf')
-        return config
+    def setup_configuration(self, config_file):
+        print config_file
+        self.config = ConfigObj(config_file)
+        return self.config
 
     def setup_connection(self):
         connection_config = self.config.get('connection')
         else:
             connection = jabber.Client(host = self.hostname)
 
+        self.connection = connection
         return connection
 
     def connect(self):

src/jabbercat/web_controller.py

 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-from jabbercat.jabber_controller import JabberController
-import cherrypy
+import web
+from web.contrib.template import render_jinja
+from jabbercat.jabber_controller import JabberController, AuthenticationError
+
+urls = []
+
+class MapUrls(type):
+    def __init__(this_class, name, bases, attrs):
+        urls.append(attrs["url"])
+        urls.append("%s.%s" % (this_class.__module__, name))
 
 class WebController(object):
 
-    @cherrypy.expose
-    def index(self):
-        return "Hello World"
+    def __init__(self, config):
+        self.app = web.application(urls, globals())
+        web_config = config.get('web')
+        self.render = render_jinja(web_config.get('templates'))
 
-    def serve_everything(self):
-        cherrypy.quickstart(WebController())
+    def run(self):
+        self.app.run()
+
+class Index(object):
+
+    __metaclass__ = MapUrls
+    url = '/'
+    def __init__(self, config):
+        self.controller = WebController(config)
+        self.render = self.controller.render
+
+    def GET(self):
+        return self.render.index()
+
+    def POST(self):
+        input = web.input()
+        jc.send_message_to_chat(chat, input.jabberinput)
+        return "%s sent" % (input.jabberinput)

tests/test_jabber_controller.py

                                                   self.hostname,
                                                   self.resource)
 
+        self.config = self.jabber_controller.setup_configuration('/etc/jabbercat/jabbercat.conf')
+        self.connection = self.jabber_controller.setup_connection()
+
     def should_get_user_id_property(self):
         assert self.jabber_controller.user_id == 'test@test.org/Home'
 
         assert self.jabber_controller.resource == 'Home'
 
     def should_setup_config_obj(self):
+        self.jabber_controller.setup_configuration('/etc/jabbercat/jabbercat.conf')
         assert jabber_controller.ConfigObj.calls('()', DontCare)
 
     def should_setup_connection_with_debugging(self):
         assert jabber_controller.jabber.calls('Presence', test_arg)
         assert self.jabber_controller.connection.calls('send', DontCare).once()
 
+    def should_unsubscribe_from_chat_room(self):
+        chat = 'test@conference.test.org'
+        nick = 'jabbercat'
+        test_arg = chat + '/' + nick
+        self.jabber_controller.unsubscribe_from_chat(chat, nick)
+        assert jabber_controller.jabber.calls('Presence', test_arg)
+        assert self.jabber_controller.connection.calls('send', DontCare).once()
+
     def should_send_message_to_chat_room(self):
         chat = 'test@conference.test.org'
         message = 'Testing'
                                                   self.hostname,
                                                   self.resource)
 
+        self.jabber_controller.setup_configuration('/etc/jabbercat/jabbercat.conf')
+        self.jabber_controller.setup_connection()
+
         nose_tools.assert_raises(SocketError, 
                 self.jabber_controller.connection.connect)

tests/test_web_controller.py

 import nose.tools as nose_tools
 from jabbercat import web_controller
 from jabbercat.web_controller import WebController
+from jabbercat.web_controller import Index
+from configobj import ConfigObj
+config = ConfigObj('/etc/jabbercat/jabbercat.conf')
 
 class WhenUsingWebController(DingusTestCase(WebController)):
     
     def setup(self):
         super(WhenUsingWebController, self).setup()
-        self.web_controller = WebController()
+        import web
+        self.web_controller = WebController(config)
 
-    def test_index_method(self):
-        assert self.web_controller.index() == 'Hello World'
+    def should_init_basic_app(self):
+        assert web_controller.web.calls('application', DontCare, DontCare).once()
 
-    def should_serve(self):
-        self.web_controller.serve_everything()
-        assert web_controller.cherrypy.calls('quickstart', DontCare)
+    def should_setup_renderer(self):
+        assert web_controller.render_jinja.calls('()', DontCare).once()
+
+    def should_call_app_run(self):
+        self.web_controller.run()
+        assert self.web_controller.app.calls('run').once()
+
+
+class WhenGettingOrPostingToIndex(object):
+
+    def should_render_html_of_index_via_get(self):
+        index = Index()
+        assert '<html>' in index.GET(), index.GET()
+
+    def should_render_html_of_index_via_post(self):
+        pass