コード例 #1
0
ファイル: util.py プロジェクト: SRHerzog/ut
def get_db_and_collection_and_create_if_doesnt_exist(db, collection, mongo_client=None):
    if mongo_client is None:
        mongo_client = MongoClient()

    try:
        mongo_client.create_database(db)
    except Exception as e:
        print(e)

    try:
        mongo_client[db].create_collection(collection)
    except Exception as e:
        print(e)
    return mongo_client[db][collection]
コード例 #2
0
        elif cloudant_re.search(vcap[user_provided_result][0]['name']):
            cloudant_result = user_provided_result

    if mongo_result:
        creds = vcap[mongo_result][0]['credentials']
        uri = creds['uri']
        client = MongoClient(uri, ssl_cert_reqs=ssl.CERT_NONE)
        db = client[db_name][collection_name]
        vendor_name = 'mongo'
    elif cloudant_result:
        creds = vcap[cloudant_result][0]['credentials']
        user = creds['username']
        password = creds['password']
        url = 'https://' + creds['host']
        client = Cloudant(user, password, url=url, connect=True)
        db = client.create_database(db_name, throw_on_exists=False)
        vendor_name = 'cloudant'

elif "CLOUDANT_URL" in os.environ:
    client = Cloudant(os.environ['CLOUDANT_USERNAME'],
                      os.environ['CLOUDANT_PASSWORD'],
                      url=os.environ['CLOUDANT_URL'],
                      connect=True)
    db = client.create_database(db_name, throw_on_exists=False)
    vendor_name = 'cloudant'

elif os.path.isfile('vcap-local.json'):
    with open('vcap-local.json') as f:
        vcap = json.load(f)
        print('Found local VCAP_SERVICES')
コード例 #3
0
from pymongo import MongoClient
from pymongo import errors as pymongo_errors
import os
import json

settings = json.load(open('settings.json'))
client = MongoClient('mongodb://' + settings['mongo']['url'] + ':27017/')

imported_news = 'imported'

# if news doesn't exist create news
try:
    client['news']
except IndexError:
    client.create_database('news')

imported_db = client['news']

# if imported doesn't exist create news
try:
    imported_db[imported_news]
except IndexError:
    imported_db.create_collection(imported_news)

imported_collection = imported_db[imported_news]

for fn in os.listdir(imported_news):
    filename = os.path.join(imported_news, fn)
    if os.path.isfile(filename):
        d = None
        with open(filename, 'r') as f: