import pony
import SCons.Builder
from bag import MongoConnectionInfo

def __build_connection_info(env):
    connection_info = MongoConnectionInfo() 
    if 'mongo_db' in env:    
        connection_info.host = env['mongo_db']
    if 'mongo_port'in env:     
        connection_info.port = env['mongo_port']
    if 'mongo_user' in env:
        connection_info.user = env['mongo_user']
    if 'mongo_port' in env:
        connection_info.pwd = env['mongo_pwd']
        connection_info.controlled_access  = True

    return connection_info

def __charge(target, source, env):
     
    pony.charge_json_file(str(source[0]), __build_connection_info(env))
    return None

def __deliver(target, source, env):
    
    fname=str(source[0])
    with open(fname, 'r') as f:
        pony.deliver_json(f.read(), __build_connection_info(env))

    return None

charge_artifact = SCons.Builder.Builder( action = __charge,
                                        src_suffix = '.json')

deliver_dependencies = SCons.Builder.Builder(  action = __deliver,
                                             src_suffix = '.json')                                        

def establish_contact (scons_environment):
    scons_environment['BUILDERS']['charge'] = charge_artifact
    scons_environment['BUILDERS']['deliver'] = deliver_dependencies
    return scons_environment