Skip to content
Browse files

added mongo connector class ( see mail_proc for usage)

  • Loading branch information...
1 parent 36bda22 commit 04afff9e8c1f7cb18171d922003462776785cf1c Felix Richter committed Jan 26, 2011
Showing with 61 additions and 4 deletions.
  1. +2 −1 genericore/__init__.py
  2. +56 −0 genericore/mongo_connect.py
  3. +3 −3 genericore/utils.py
View
3 genericore/__init__.py
@@ -6,6 +6,7 @@
]
#__license__
__contributors__ = ''
-import auto_amqp,utils
+import auto_amqp,utils, mongo_connect
from auto_amqp import *
from utils import *
+from mongo_connect import *
View
56 genericore/mongo_connect.py
@@ -0,0 +1,56 @@
+
+import logging, sys
+from utils import Configurable
+from pymongo import Connection
+log = logging.getLogger('MongoConnect')
+
+
+# this is the "sub-configuration" of the given module name
+GENERIC_CONFIG = {
+ "mongodb" : {
+ "host" : "localhost"
+ },
+ "collection" : {
+ "name" : "mail_user_stats",
+ "drop_collection" : False
+ }
+}
+class MongoConnect(Configurable):
+
+ def create_connection(self):
+ conf = self.config[self.MODULE_NAME]
+ coll_conf = conf['collection']
+ try:
+ self.conn = Connection(**conf['mongodb'])
+ self.db = self.conn[coll_conf['name']]
+ except Exception as e:
+ log.error('Mongodb not running or unreachable ! Bailing out' + str(e))
+ sys.exit(0)
+
+ if coll_conf ['drop_collection'] :
+ log.info('dropping collection due to public demand')
+ self.db.drop()
+
+ def __init__(self,MODULE_NAME='mongo_connect',conf=None):
+ self.MODULE_NAME = MODULE_NAME
+ newConfig = {}
+ newConfig[MODULE_NAME] = GENERIC_CONFIG # extend our config and
+ #personalize it
+ Configurable.__init__(self,newConfig)
+ self.load_conf(conf)
+
+ def close(self):
+ self.conn.close()
+
+ def populate_parser(self,parser):
+ parser.add_argument('--mongohost',metavar='HOST',help='Mongodb Host')
+ parser.add_argument('--collection',metavar='PATH',help='Collection to save data in')
+ parser.add_argument('--drop-collection',action='store_true',help='drops the collection after successful connection, then continues')
+
+ def eval_parser(self,parsed):
+ conf = self.config[self.MODULE_NAME]
+ mconf = conf['mongodb']
+ cconf = conf['collection']
+ mconf['host'] = parsed.mongohost if parsed.mongohost else mconf['host']
+ cconf['name'] = parsed.collection if parsed.collection else cconf['name']
+ cconf['drop_collection'] = parsed.drop_collection
View
6 genericore/utils.py
@@ -6,8 +6,8 @@
log = logging.getLogger('genericore-utils')
class Configurable(object):
- config = {}
+ config = {}
def __init__(self,config=None):
self.load_conf(config)
@@ -60,7 +60,7 @@ def configure(self,conf_list):
self.populate_parser(parser)
for configurable in conf_list:
try: configurable.populate_parser(parser)
- except : log.warning(str(configurable.__class__) + "does not have populate_parser")
+ except Exception as e: print (str(configurable.__class__) + "does not have populate_parser" + str(e))
args = parser.parse_args()
@@ -69,7 +69,7 @@ def configure(self,conf_list):
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))
+ except Exception as e: print (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 04afff9

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