Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added mongo connector class ( see mail_proc for usage)

  • Loading branch information...
commit 04afff9e8c1f7cb18171d922003462776785cf1c 1 parent 36bda22
Felix Richter authored
3  genericore/__init__.py
@@ -6,6 +6,7 @@
6 6 ]
7 7 #__license__
8 8 __contributors__ = ''
9   -import auto_amqp,utils
  9 +import auto_amqp,utils, mongo_connect
10 10 from auto_amqp import *
11 11 from utils import *
  12 +from mongo_connect import *
56 genericore/mongo_connect.py
... ... @@ -0,0 +1,56 @@
  1 +
  2 +import logging, sys
  3 +from utils import Configurable
  4 +from pymongo import Connection
  5 +log = logging.getLogger('MongoConnect')
  6 +
  7 +
  8 +# this is the "sub-configuration" of the given module name
  9 +GENERIC_CONFIG = {
  10 + "mongodb" : {
  11 + "host" : "localhost"
  12 + },
  13 + "collection" : {
  14 + "name" : "mail_user_stats",
  15 + "drop_collection" : False
  16 + }
  17 +}
  18 +class MongoConnect(Configurable):
  19 +
  20 + def create_connection(self):
  21 + conf = self.config[self.MODULE_NAME]
  22 + coll_conf = conf['collection']
  23 + try:
  24 + self.conn = Connection(**conf['mongodb'])
  25 + self.db = self.conn[coll_conf['name']]
  26 + except Exception as e:
  27 + log.error('Mongodb not running or unreachable ! Bailing out' + str(e))
  28 + sys.exit(0)
  29 +
  30 + if coll_conf ['drop_collection'] :
  31 + log.info('dropping collection due to public demand')
  32 + self.db.drop()
  33 +
  34 + def __init__(self,MODULE_NAME='mongo_connect',conf=None):
  35 + self.MODULE_NAME = MODULE_NAME
  36 + newConfig = {}
  37 + newConfig[MODULE_NAME] = GENERIC_CONFIG # extend our config and
  38 + #personalize it
  39 + Configurable.__init__(self,newConfig)
  40 + self.load_conf(conf)
  41 +
  42 + def close(self):
  43 + self.conn.close()
  44 +
  45 + def populate_parser(self,parser):
  46 + parser.add_argument('--mongohost',metavar='HOST',help='Mongodb Host')
  47 + parser.add_argument('--collection',metavar='PATH',help='Collection to save data in')
  48 + parser.add_argument('--drop-collection',action='store_true',help='drops the collection after successful connection, then continues')
  49 +
  50 + def eval_parser(self,parsed):
  51 + conf = self.config[self.MODULE_NAME]
  52 + mconf = conf['mongodb']
  53 + cconf = conf['collection']
  54 + mconf['host'] = parsed.mongohost if parsed.mongohost else mconf['host']
  55 + cconf['name'] = parsed.collection if parsed.collection else cconf['name']
  56 + cconf['drop_collection'] = parsed.drop_collection
6 genericore/utils.py
@@ -6,8 +6,8 @@
6 6 log = logging.getLogger('genericore-utils')
7 7
8 8 class Configurable(object):
9   - config = {}
10 9
  10 + config = {}
11 11 def __init__(self,config=None):
12 12 self.load_conf(config)
13 13
@@ -60,7 +60,7 @@ def configure(self,conf_list):
60 60 self.populate_parser(parser)
61 61 for configurable in conf_list:
62 62 try: configurable.populate_parser(parser)
63   - except : log.warning(str(configurable.__class__) + "does not have populate_parser")
  63 + except Exception as e: print (str(configurable.__class__) + "does not have populate_parser" + str(e))
64 64
65 65 args = parser.parse_args()
66 66
@@ -69,7 +69,7 @@ def configure(self,conf_list):
69 69 try:
70 70 i.load_conf(self.config)
71 71 i.eval_parser(args)
72   - except Exception as e: log.warning(str(i.__class__) + "does not have eval_parser or load_conf" + str(e))
  72 + except Exception as e: print (str(i.__class__) + "does not have eval_parser or load_conf" + str(e))
73 73
74 74 self.blend(conf_list)
75 75 log.debug ('New Configuration:' + str(self.config))

0 comments on commit 04afff9

Please sign in to comment.
Something went wrong with that request. Please try again.