Permalink
Browse files

refactored configuration

new Configurator class which can be used to centralize configuration,
also provides basic functionality

introduced : populate_parser and eval_parser , populate_parser will populate an argparse parser with new parser arguments. eval_parser will evaluate the input via arguments for each class which implements Configurable
  • Loading branch information...
Felix Richter Felix Richter
Felix Richter authored and Felix Richter committed Jan 24, 2011
1 parent 6261e0b commit fc2960480991120206fa8ea1301b4188c4a76938
Showing with 56 additions and 21 deletions.
  1. +25 −3 genericore/auto_amqp.py
  2. +31 −18 genericore/utils.py
View
@@ -5,6 +5,7 @@
log = logging.getLogger('genericore-amqp')
DEFAULT_CONFIG = {
+ "amqp" : {
"connection" : {
"login" : "guest",
"password" : "guest",
@@ -22,6 +23,7 @@
"type" : "fanout",
}
}
+ }
class auto_amqp(Configurable):
conn = None
@@ -33,7 +35,7 @@ def create_connection(self):
""" starts the connection the the AMQP Serve """
if self.conn:
raise Exception("Connection already open")
- cfg = self.config['connection']
+ cfg = self.config['amqp']['connection']
log.debug (str(cfg))
locals().update()
self.conn = pika.AsyncoreConnection(pika.ConnectionParameters(
@@ -49,8 +51,8 @@ def create_connection(self):
def _setup_tubes(self):
""" creates the in 'config' configured input and output """
chan = self.channel
- inp = self.config['in']
- out = self.config['out']
+ inp = self.config['amqp']['in']
+ out = self.config['amqp']['out']
if inp['exchange']:
log.info('generating Input Queue'+ str(inp))
chan.exchange_declare(**inp)
@@ -73,3 +75,23 @@ def close_connection(self):
delattr(self,'start_loo')
if hasattr(self,'publish'):
delattr(self,'publish')
+
+ def populate_parser(self,parser):
+ """ populates an argparse parser """
+ parser.add_argument('-s','--host',dest='amqpHost', help='AMQP host ip address',metavar='HOST')
+ parser.add_argument('--port',type=int,dest='amqpPort',help='AMQP host port',metavar='PORT')
+ parser.add_argument('-u','--username',dest='amqpUsername', help='AMQP username',metavar='USER')
+ parser.add_argument('-p','--password',dest='amqpPassword',help='AMQP password',metavar='PASS')
+ parser.add_argument('-b','--heartbeat',dest='amqpHeartbeat',type=int,help='AMQP Heartbeat value',metavar='SECONDS')
+ parser.add_argument('-v','--vhost',dest='amqpVhost',help='AMQP vhost definition',metavar='VHOST')
+ def eval_parser(self,parsed):
+ """ loads its individual configuration from the parsed output """
+ conf = self.config['amqp']['connection']
+ conf['host'] = parsed.amqpHost if 'amqpHost' in dir(parsed) else conf['host']
+ conf['port'] = parsed.amqpHost if 'amqpPort' in dir(parsed) else conf['port']
+ conf['username'] = parsed.amqpHost if 'amqpUsername' in dir(parsed) else conf['username']
+ conf['password'] = parsed.amqpHost if 'amqpPassword' in dir(parsed) else conf['password']
+ conf['heartbeat'] = parsed.amqpHost if 'amqpHeartbeat' in dir(parsed) else conf['heartbeat']
+ conf['vhost'] = parsed.amqpHost if 'amqpVhost' in dir(parsed) else conf['vhost']
+
+
View
@@ -1,30 +1,15 @@
#parses all "default" parser values with argparse
-import argparse,hashlib
+import argparse,hashlib,sys
import logging
import simplejson as json #need to decode in ascii
log = logging.getLogger('genericore-utils')
-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('-c','--config',default='', help='configuration file')
- 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')
- parser.add_argument('--unique-key',action='store_true', help='Unique Key')
-
-def generate_unique(PROTO_VERSION,args):
- return hashlib.sha1(str(PROTO_VERSION) +
- json.dumps(args,sort_keys=True)).hexdigest()
class Configurable(object):
config = {}
def __init__(self,config=None):
- if config:
- self.load_conf(config)
+ self.load_conf(config)
def load_conf(self,new_config):
""" loads and merges configuration from the given dictionary """
@@ -49,4 +34,32 @@ def load_conf_file(self,config_file):
return
with open(config_file) as f:
new_conf = json.load(f,encoding='ascii')
- self.load_conf(new_conf["genericore"])
+ self.load_conf(new_conf)
+ def load_conf_parser(self,parser):
+ """ loads the configuration from a parser object """
+
+class Configurator(Configurable):
+ def __init__(self,PROTO_VERSION,conf=None):
+ """ PROTO_VERSION is the protocol version of the module to configure """
+ Configurable.__init__(self,conf)
+ self.PROTO_VERSION = PROTO_VERSION
+
+ def configure(self,conf_list):
+ for i in conf_list:
+ i.load_conf(self.config)
+ def populate_parser(self,parser):
+ parser.add_argument('-c','--config',dest='genConfig', help='configuration file',metavar='FILE')
+ #parser.add_argument('-d','--debug_level',help='Debug Level')
+ parser.add_argument('--unique-key',action='store_true', help='Unique Key')
+
+ def eval_parser(self,parsed):
+ if 'genConfig' in dir(parsed):
+ self.load_conf_file(parsed.genConfig)
+
+ if 'unique-key' in dir(parsed):
+ print self.generate_unique(parsed)
+ sys.exit(0)
+
+ def generate_unique(self,args):
+ print(hashlib.sha1(str(self.PROTO_VERSION) +
+ json.dumps(args,sort_keys=True)).hexdigest())

0 comments on commit fc29604

Please sign in to comment.