Permalink
Browse files

added samples to README, exception handling

  • Loading branch information...
Felix Richter Felix Richter
Felix Richter authored and Felix Richter committed Jan 24, 2011
1 parent 7de4b3f commit 36bda227a75c81d87a861ae35a98d14a1df5c31d
Showing with 46 additions and 4 deletions.
  1. +39 −0 README.md
  2. +7 −4 genericore/utils.py
View
@@ -58,3 +58,42 @@ Dependencies
===========
* simplejson
* pika
+
+Usage
+=========
+
+This is what you normally want to do when writing a new genericore module:
+
+ conf = gen.Configurator(PROTO_VERSION,DESCRIPTION)
+ amqp = gen.auto_amqp()
+ s = YourMagic()
+
+ conf.configure([amqp,s]) #set up parser and eval parsed stuff
+
+ # start network connections, probably also for your Parser (e.g. backend)
+ amqp.create_connection()
+
+ def cb (ch,method,header,body):
+ entry = s.process(json.loads(body))
+ amqp.publish(json.dumps(entry))
+
+ amqp.consume(cb)
+ amqp.start_loop()
+
+Configurator
+===========
+python-genericore provides the Configurator class which can be instanciated
+in order to do all your nasty configuration and argument parsing stuff.
+
+In order to configure YOUR Object object you may want to do the following
+things:
+* derive your class from Configurable ( to have the load\_conf magic )
+ a "config" member variable is now available with the config dictionary
+* implement populate\_parser(parser)
+ in this function you can add custom arguments to the command line parser
+ (argparse is the weaopn of choice in Configurator)
+* implement eval\_parser(args)
+ do something with the then parsed arguments
+
+These functions will be called by the Configurator when doing a configure
+for a list of modules (including your's)
View
@@ -39,7 +39,7 @@ def load_conf_parser(self,parser):
""" loads the configuration from a parser object """
class Configurator(Configurable):
- def __init__(self,PROTO_VERSION,DESCRIPTION,conf=None):
+ def __init__(self,PROTO_VERSION=1,DESCRIPTION='description not set!',conf=None):
""" PROTO_VERSION is the protocol version of the module to configure """
Configurable.__init__(self,conf)
self.PROTO_VERSION = PROTO_VERSION
@@ -59,14 +59,17 @@ def configure(self,conf_list):
self.populate_parser(parser)
for configurable in conf_list:
- configurable.populate_parser(parser)
+ try: configurable.populate_parser(parser)
+ except : log.warning(str(configurable.__class__) + "does not have populate_parser")
args = parser.parse_args()
self.eval_parser(args)
for i in conf_list:
- i.load_conf(self.config)
- i.eval_parser(args)
+ try:
+ i.load_conf(self.config)
+ i.eval_parser(args)
+ except Exception as e: log.warning(str(i.__class__) + "does not have eval_parser or load_conf" + str(e))
self.blend(conf_list)
log.debug ('New Configuration:' + str(self.config))

0 comments on commit 36bda22

Please sign in to comment.