Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
added samples to README, exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Richter authored and Felix Richter committed Jan 24, 2011
1 parent 7de4b3f commit 36bda22
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
39 changes: 39 additions & 0 deletions README.md
Expand Up @@ -58,3 +58,42 @@ Dependencies
=========== ===========
* simplejson * simplejson
* pika * 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)
11 changes: 7 additions & 4 deletions genericore/utils.py
Expand Up @@ -39,7 +39,7 @@ def load_conf_parser(self,parser):
""" loads the configuration from a parser object """ """ loads the configuration from a parser object """


class Configurator(Configurable): 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 """ """ PROTO_VERSION is the protocol version of the module to configure """
Configurable.__init__(self,conf) Configurable.__init__(self,conf)
self.PROTO_VERSION = PROTO_VERSION self.PROTO_VERSION = PROTO_VERSION
Expand All @@ -59,14 +59,17 @@ def configure(self,conf_list):


self.populate_parser(parser) self.populate_parser(parser)
for configurable in conf_list: 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() args = parser.parse_args()


self.eval_parser(args) self.eval_parser(args)
for i in conf_list: for i in conf_list:
i.load_conf(self.config) try:
i.eval_parser(args) 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) self.blend(conf_list)
log.debug ('New Configuration:' + str(self.config)) log.debug ('New Configuration:' + str(self.config))
Expand Down

0 comments on commit 36bda22

Please sign in to comment.