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

Commit

Permalink
added mongo connector class ( see mail_proc for usage)
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Richter authored and Felix Richter committed Jan 25, 2011
1 parent 36bda22 commit 04afff9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
3 changes: 2 additions & 1 deletion genericore/__init__.py
Expand Up @@ -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 *
56 changes: 56 additions & 0 deletions 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
6 changes: 3 additions & 3 deletions genericore/utils.py
Expand Up @@ -6,8 +6,8 @@
log = logging.getLogger('genericore-utils')

class Configurable(object):
config = {}

config = {}
def __init__(self,config=None):
self.load_conf(config)

Expand Down Expand Up @@ -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()

Expand All @@ -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))
Expand Down

0 comments on commit 04afff9

Please sign in to comment.