Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 1bd72c0c3f262157e9784295672d4978148e138a Felix Richter committed Jan 19, 2011
Showing with 108 additions and 0 deletions.
  1. +11 −0 genericore/__init__.py
  2. +76 −0 genericore/auto_amqp.py
  3. +12 −0 genericore/utils.py
  4. +9 −0 setup.py
@@ -0,0 +1,11 @@
+#!/usr/bin/env python2
+
+__version__ = '0.1'
+__author__ = [
+ "Felix Richter <github@syntax-fehler.de>"
+]
+#__license__
+__contributors__ = ''
+import auto_amqp,utils
+from auto_amqp import *
+from utils import *
@@ -0,0 +1,76 @@
+import pika
+import json, sys,time
+import logging
+log = logging.getLogger('genericore-amqp')
+
+DEFAULT_CONFIG = {
+ "connection" : {
+ "login" : "guest",
+ "password" : "guest",
+ "host" : "localhost",
+ "port" : 5672,
+ "heartbeat" : 0
+ },
+ "in" : {
+ "exchange" : False,
+ "type" : "fanout",
+ },
+ "out" : {
+ "exchange" : False,
+ "type" : "fanout",
+ }
+ }
+
+class auto_amqp:
+ conn = None
+ config = DEFAULT_CONFIG
+ def __init__(self,config=None):
+ if config:
+ self.load_conf(config)
+
+ def load_conf(self,config):
+ self.config.update(config)
+
+ def load_conf_file(self,config_file):
+ with open(config_file) as f:
+ new_conf = json.load(f)
+ self.load_conf(new_conf["genericore"])
+
+ def create_connection(self):
+ if self.connection:
+ raise Exception("Connection already open")
+ log.debug (self.config['connection'])
+ locals.update(self.config['connection'])
+ self.conn = pika.AsyncoreConnection(pika.ConnectionParameters(
+ credentials = pika.PlainCredentials(username,password),
+ heartbeat=heartbeat,
+ virtual_host=vhost,
+ port=port,
+ host=host))
+ self.channel = self.conn.channel()
+ self._setup_tubes()
+
+ def _setup_tubes(self):
+ chan = self.channel
+ inp = self.config['in']
+ out = self.config['out']
+ if inp['exchange']:
+ chan.exchange_declare(**inp)
+ self.qname = chan.queue_declare(exclusive=True).queue
+ chan.queue_bind(exchange=inp['exchange'],queue=self.qname)
+ self.consume = lambda cb : chan.basic_consume(cb,queue=self.qname,no_ack=True)
+ self.start_loop = lambda : chan.asyncore_loop()
+
+ if out['exchange']:
+ chan.exchange_declare(**out)
+ self.publish = lambda msg: chan.basic_publish(exchange=out['exchange'],routing_key='',body=msg)
+
+ def close_connection(self):
+ self.connection.close()
+ self.connection = None
+ #cleanup
+ if hasattr(self,'consume'):
+ delattr(self,'consume')
+ delattr(self,'start_loo')
+ if hasattr(self,'publish'):
+ delattr(self,'publish')
@@ -0,0 +1,12 @@
+#parses all "default" parser values with argparse
+
+import argparse
+
+def parse_default(parser):
+ parser.add_argument('-s','--host',default='141.31.8.11', help='AMQP host ip address')
+ parser.add_argument('--port',type=int,default=5672, help='AMQP host port')
+ parser.add_argument('-u','--username',default='shack', help='AMQP username')
+ parser.add_argument('-p','--password',default='guest', help='AMQP password')
+ parser.add_argument('-b','--heartbeat',type=int,default=0,help='AMQP Heartbeat value')
+ parser.add_argument('-v','--vhost',default='/',help='AMQP vhost definition')
+ parser.add_argument('-d','--debug_level',default='/',help='AMQP vhost definition')
@@ -0,0 +1,9 @@
+#!/usr/bin/env python2
+from distutils.core import setup
+setup(name='python-genericore',
+ description='connector class to genericore',
+ url='http://http://shackspace.de/wiki/doku.php?id=project:genericore#python-genericore',
+ author='Felix Richter<github@syntax-fehler.de>',
+ version='0.1',
+ packages=['genericore'],
+ )

0 comments on commit 1bd72c0

Please sign in to comment.