def main():
    parser = argparse.ArgumentParser(prog='python add_organizations.py')
    parser.add_argument('-u', '--user', help='Cloudant user email')
    parser.add_argument('-p', '--password', help='Cloudant password')
    parser.add_argument('-a', '--account', help='Cloudant account')
    parser.add_argument('database', help='Cloudant database')

    args = parser.parse_args()

    if args.user is None:
        args.user = input('Email: ')

    if args.password is None:
        args.password = getpass('Password: '******'Cloudant account: ')

    client = Cloudant(args.user, args.password,
                      account=args.account,
                      connect=True,
                      auto_renew=True,
                      adapter=Replay429Adapter(retries=10, initialBackoff=0.01))

    try:
        migrate(client, args.database)
    finally:
        client.disconnect()
 def setUp(self):
     """
     Set up test attributes for unit tests targeting a database
     """
     if os.environ.get('RUN_CLOUDANT_TESTS') is None:
         admin_party = False
         if (os.environ.get('ADMIN_PARTY') and
             os.environ.get('ADMIN_PARTY') == 'true'):
             admin_party = True
         self.user = os.environ.get('DB_USER', None)
         self.pwd = os.environ.get('DB_PASSWORD', None)
         self.url = os.environ['DB_URL']
         self.client = CouchDB(self.user, self.pwd, admin_party, url=self.url)
     else:
         self.account = os.environ.get('CLOUDANT_ACCOUNT')
         self.user = os.environ.get('DB_USER')
         self.pwd = os.environ.get('DB_PASSWORD')
         self.url = os.environ.get(
             'DB_URL',
             'https://{0}.cloudant.com'.format(self.account))
         self.client = Cloudant(
             self.user,
             self.pwd,
             url=self.url,
             x_cloudant_user=self.account)
    def insertDbUrl(self, action, link, intent):
        if action == 'url':
            data = {
                'url': link,
                'bot': 'None',
                'intent': intent
            }
        elif action == 'bot':
            data = {
                'url': 'None',
                'bot': link,
                'intent': intent
            }
        client = Cloudant(COUCHDB_USER, COUCHDB_PASSWORD, url=COUCHDB_URL)

        # Connect to the server
        client.connect()

        # Perform client tasks...
        session = client.session()

        actionsDB = client['actions']
        my_document = actionsDB.create_document(data)

        # Check that the document exists in the database
        if my_document.exists():
            print 'SUCCESS!!'
 def set_up_client(self, auto_connect=False, auto_renew=False, encoder=None):
     if os.environ.get('RUN_CLOUDANT_TESTS') is None:
         admin_party = False
         if (os.environ.get('ADMIN_PARTY') and
             os.environ.get('ADMIN_PARTY') == 'true'):
             admin_party = True
         self.user = os.environ.get('DB_USER', None)
         self.pwd = os.environ.get('DB_PASSWORD', None)
         self.url = os.environ['DB_URL']
         self.client = CouchDB(
             self.user,
             self.pwd,
             admin_party,
             url=self.url,
             connect=auto_connect,
             auto_renew=auto_renew,
             encoder=encoder
         )
     else:
         self.account = os.environ.get('CLOUDANT_ACCOUNT')
         self.user = os.environ.get('DB_USER')
         self.pwd = os.environ.get('DB_PASSWORD')
         self.url = os.environ.get(
             'DB_URL',
             'https://{0}.cloudant.com'.format(self.account))
         self.client = Cloudant(
             self.user,
             self.pwd,
             url=self.url,
             x_cloudant_user=self.account,
             connect=auto_connect,
             auto_renew=auto_renew,
             encoder=encoder
         )
class CloudantHelper:
    COUCHDB_USER = ""
    COUCHDB_PASSWORD = ""
    COUCHDB_URL = ""

    dbclient = None

    def __init__(self, userid, password, url):
        self.init(userid, password, url)
        pass

    def init(self, userid, password, url):
        self.COUCHDB_URL = url
        self.COUCHDB_USER = userid
        self.COUCHDB_PASSWORD = password

        print("Connecting to Cloudant..")
        self.dbclient = Cloudant(self.COUCHDB_USER, self.COUCHDB_PASSWORD, url=self.COUCHDB_URL)

        # Connect to the server
        self.dbclient.connect()
        print("Connected to Cloudant!")

    def query(self, database=None, selectorField=None, value=None):

        if self.dbclient is None:
            self.dbclient.connect()

        db = self.dbclient[database]
        query = Query(db)
        if query is not None:
            with query.custom_result(selector={selectorField: value}) as res:
                if res is not None:
                    return res[0]
                else:
                    return None
        else:
            return None

    def queryAll(self, database=None, field=None, value=None):
        resultsArray = []
        if self.dbclient is None:
            self.dbclient.connect()

        db = self.dbclient[database]
        result_collection = Result(db.all_docs, include_docs=True)

        count = 0
        for result in result_collection:
            if result['doc'] is not None:
                if field in result['doc'] and result['doc'][field] == value:
                    resultsArray.append(result['doc'])
                    count += 1
        return resultsArray

    def disconnect(self):
        # Disconnect from the server
        if self.dbclient is not None:
            self.dbclient.disconnect()
            print("Disconnected from Cloudant.")
    def getClassifierId(self):
        client = Cloudant(COUCHDB_USER, COUCHDB_PASSWORD, url=COUCHDB_URL)

        # Connect to the server
        client.connect()

        classifierDB = client['classifierdb']
        result_collection = Result(classifierDB.all_docs, include_docs=True)

        for result in result_collection:
            print result['doc']
            if result['doc'] is not None:
                if 'status' in result['doc'] and result['doc']['status'] == 'A':
                    classifier = result['doc']['classifierId']
        print "Classifier: " + str(classifier)

        # Disconnect from the server
        client.disconnect()
        return classifier
示例#7
0
 def __init__(self):
     threading.Thread.__init__(self)
     self.setDaemon(True)
     self._homeDir        = os.path.expanduser("~/.sensomatic")
     self._configFileName = self._homeDir + '/config.ini'
     self._config         = ConfigParser.ConfigParser()
     self._readConfig()
     self._redis          = redis.StrictRedis(host=self._config.get("REDIS", "ServerAddress"), port=self._config.get("REDIS", "ServerPort"), db=0)
     self._cloudant       = Cloudant(self._config.get("CLOUDANT", "Username"), self._config.get("CLOUDANT", "Password"), url=self._config.get("CLOUDANT", "ServerAddress"))
     self.checkDB()
 def test_constructor_with_account(self):
     """
     Test instantiating a client object using an account name
     """
     # Ensure that the client is new
     del self.client
     self.client = Cloudant(self.user, self.pwd, account=self.account)
     self.assertEqual(
         self.client.server_url,
         'https://{0}.cloudant.com'.format(self.account)
         )
示例#9
0
 def delete_target_record(self, doc_id):
     client = Cloudant(self.readuser,
                       self.readpass,
                       url=self.url,
                       connect=True, auto_renew=True)
     my_database = client['stock-targets']
     # First retrieve the document
     my_document = my_database[doc_id]
     # Delete the document
     my_document.delete()
     return "Document deleted"
示例#10
0
def WelcomeToMyapp():

    client = Cloudant(
        "39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix",
        "48e26645f504209f85b4c44d74a4cb14bc0d059a22b361534b78f406a513f8ff",
        url=
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com"
    )
    client.connect()

    end_point = '{0}/{1}'.format(
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com",
        "coolstuff" + "/_all_docs?")
    params = {'include_docs': 'true'}

    response = client.r_session.get(end_point, params=params)
    table22 = response.json()['rows'][0]['doc']['current'], 5, 6
    # table22 = 4,5,6

    return render_template('tester.html', table=table22)
示例#11
0
def saveTickers(tickers):
    client = Cloudant.iam("d02a0070-4a25-4e81-b712-a8e6c38a8863-bluemix",
                          "0CpvlhxnS58tIZMsdu4QuUqw4bai6t1EYcJAv4Mo4lnI")
    client.connect()
    database_name = "user_db"
    # print(client.all_dbs())
    db = client[database_name]  #open database
    print(db)
    userDoc = db["user1"]
    userDoc['tickers'] = tickers
    userDoc.save()
    def init(self, userid, password, url):
        self.COUCHDB_URL = url
        self.COUCHDB_USER = userid
        self.COUCHDB_PASSWORD = password

        print("Connecting to Cloudant..")
        self.dbclient = Cloudant(self.COUCHDB_USER, self.COUCHDB_PASSWORD, url=self.COUCHDB_URL)

        # Connect to the server
        self.dbclient.connect()
        print("Connected to Cloudant!")
示例#13
0
def save_loc_to_db(user, location):
    client = Cloudant.iam("", "")
    client.connect()
    db = client["test"]

    loc = json.loads(location)

    data = {"user": user, "lastLat": loc['lat'], "lastLng": loc['lng']}

    doc = db.create_document(data)

    client.disconnect()
def getall(param, db):
    db_username = param['username']
    api = param['api']
    databaseName = db
    client = Cloudant.iam(db_username, api, connect=True)
    myDatabaseDemo = client[databaseName]
    dic = {}
    i = 0
    for document in myDatabaseDemo:
        dic[i] = document["_id"]
        i = i + 1
    return dic
示例#15
0
def enter_room():
    # 接続
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    myDatabase = client[databaseName]

    #書き換え
    search_id = int(request.args.get('room_id'))
    guest_name = request.args.get('guest_name')
    guest_station = request.args.get('guest_station')
    result_collection = Result(myDatabase.all_docs, include_docs=True)
    for item in result_collection:
        if item["doc"]["room_id"] == search_id:
            target = myDatabase[item["doc"]["_id"]]
            target['guest_name'] = guest_name
            target['guest_station'] = guest_station
            target.save()

    output = {"success": True}

    return jsonify(output)
示例#16
0
def write_to_Error_log(text):
    user_data = get_user_data()
    msg = "ERROR - Date:" + str(RecDate()) + " AT TIME: " + str(
        RecTime()) + ": " + str(text) + str('\n')
    print("Writting to ERROR LOG file:", msg)
    # log_txt_file="/home/pi/hms/" + str(RecDate())+"-HMS-log.txt"
    # f=open(log_txt_file, 'a')
    # f.write(msg)
    # f.close()

    DATABASE_NAME = "hms_log"
    client = Cloudant(user_data['cloud_acct_username'],
                      user_data['cloud_acct_pword'],
                      url=user_data['cloud_act_url'])

    client.connect()

    my_database = client[DATABASE_NAME]

    json_document = {
        "d": dt.datetime.now().strftime("%m-%d-%Y"),
        "t": dt.datetime.now().strftime("%H:%M:%S"),
        "m": msg
    }
    try:
        new_document = my_database.create_document(json_document)
    except:
        time.sleep(30)
        try:
            new_document = my_database.create_document(json_document)
        except:
            return
    client.disconnect()
示例#17
0
def getDataByMin2():
    client = Cloudant(
        "39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix",
        "48e26645f504209f85b4c44d74a4cb14bc0d059a22b361534b78f406a513f8ff",
        url=
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com"
    )
    client.connect()

    end_point = '{0}/{1}'.format(
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com",
        "load2" + "/_all_docs?")

    params = {'include_docs': 'true'}
    response = client.r_session.get(end_point, params=params)

    i = 1
    table = []
    while (i < 7):
        #make a function that adds all the data for past min
        #each db entry is an average of 3 seconds
        # there are 60/3 entries in a min
        if i == 1:
            x = 1
        else:
            x = (20 * (i - 1))
        total_power_for_one_min = 0
        while (x < 20 * i):
            total_power_for_one_min += response.json(
            )['rows'][-x]['doc']['Power']
            x += 1

            #since we are getting the average for 3 seconds we are only getting
            #20 seconds worth of total power so multiply by 3 and you get one minute

        table.append(total_power_for_one_min * 3)
        # table[i] = (response.json
        # table.insert(i,response.json()['rows'][i]['doc']['current'])
        i = i + 1
    return table
def save_data(Results):
    #check if we run on bluemix
    if 'VCAP_SERVICES' in os.environ:
        vcap_servicesData = json.loads(os.environ['VCAP_SERVICES'])
    else:
        print("On Local PC")
        json_file = open("static/vcap-local.json")
        s = json_file.read()
        vcap_servicesData = json.loads(s)
        vcap_servicesData = vcap_servicesData[u'services']

    # Connect To Cloudant DB
    cloudantNoSQLDBData = vcap_servicesData[u'cloudantNoSQLDB']
    credentials = cloudantNoSQLDBData[0]
    credentialsData = credentials[u'credentials']
    serviceUsername = credentialsData[u'username']
    servicePassword = credentialsData[u'password']
    serviceURL = credentialsData[u'url']

    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    database = client['results']
    database.create_document({"data": Results, "Time-in": time.time()})
    client.disconnect()
    return 0
示例#19
0
def Retrieve_Result():
    #check if we run on bluemix
    if 'VCAP_SERVICES' in os.environ:
        vcap_servicesData = json.loads(os.environ['VCAP_SERVICES'])
    else:
        print("On Local PC")
        json_file = open("vcap-local.json")
        s = json_file.read()
        vcap_servicesData = json.loads(s)
        vcap_servicesData = vcap_servicesData[u'services']

    # Connect To Cloudant DB
    cloudantNoSQLDBData = vcap_servicesData[u'cloudantNoSQLDB']
    credentials = cloudantNoSQLDBData[0]
    credentialsData = credentials[u'credentials']
    serviceUsername = credentialsData[u'username']
    servicePassword = credentialsData[u'password']
    serviceURL = credentialsData[u'url']

    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    database_json = client['results']

    result = {u"data": [], u"Time": []}

    for dat in database_json:
        result[u"data"].append(dat[u'data'])
        result[u"Time"].append(dat[u'Time-in'])

    client.disconnect()
    return json.dumps(result)
    def getUrl(self, intent):
        action = {}
        client = Cloudant(COUCHDB_USER, COUCHDB_PASSWORD, url=COUCHDB_URL)

        # Connect to the server
        client.connect()

        actionsDB = client['actions']
        result_collection = Result(actionsDB.all_docs, include_docs=True)

        for result in result_collection:
            print result
            if result['doc'] is not None:
                if result['doc']['intent'] == intent:
                    if 'url' in result['doc'] and 'bot' in result['doc']:
                        if result['doc']['url'] is None:
                            action['bot'] = result['doc']['bot']
                        else:
                            action['url'] = result['doc']['url']
                    break
        print "Action: " + str(action)

        # Disconnect from the server
        client.disconnect()

        return action
示例#21
0
def getTimeLast():
    client = Cloudant(
        "39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix",
        "48e26645f504209f85b4c44d74a4cb14bc0d059a22b361534b78f406a513f8ff",
        url=
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com"
    )
    client.connect()

    end_point = '{0}/{1}'.format(
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com",
        "coolstuff" + "/_all_docs?")
    end_point_status = '{0}/{1}'.format(
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com",
        "status" + "/_all_docs?")

    params = {'include_docs': 'true'}
    response = client.r_session.get(end_point, params=params)
    response_status = client.r_session.get(end_point_status, params=params)
    i = 1
    table = []
    while (i < 7):

        table.append(response.json()['rows'][-i]['doc']['current'])
        # table[i] = (response.json
        # table.insert(i,response.json()['rows'][i]['doc']['current'])
        i = i + 1
    client.disconnect()

    return table
示例#22
0
def req_my_data():
    USERNAME = "******"
    PASSWORD = "******"
    cloudant_url = "https://*****:*****@ba62d85b-fc08-4706-8a9e-ba91bfbf6174-bluemix.cloudant.com"
    my_cl_db_name = 'my_cl_db'

    client = Cloudant(USERNAME, PASSWORD, url=cloudant_url)
    client.connect()  # Connect to the server
    my_cl_db_handler = cloudant.database.CloudantDatabase(
        client, my_cl_db_name)

    # Assignment Q1: Find all reviews from member A1004AX2J2HXGL.
    selector = {'member_id': {'$eq': 'A1004AX2J2HXGL'}}
    docs_collection = my_cl_db_handler.get_query_result(selector)
    for each_doc in docs_collection:
        print(each_doc)

    # Assignment Q2: Find the number of reviews by each member.
    resp = my_cl_db_handler.get_search_result(ddoc_id='py_ddoc',
                                              index_name='py_index',
                                              query='*:*',
                                              counts=["member_id"])
    print('Number of reviews by each member: ', resp['counts'])

    # Assignment Q3: Find the product ids of all products with at least one review rating less than 3.
    selector = {'rating': {'$lt': 3}}
    fields = ['product id']
    docs_collection = my_cl_db_handler.get_query_result(selector, fields)
    for each_doc in docs_collection:
        print(each_doc)

    client.disconnect()
示例#23
0
def edit_cloudant_system_status_doc(s, v):
    # USERNAME = cloud_acct_username
    # PASSWORD = cloud_acct_pword
    # URL = cloud_act_url
    user_data = get_user_data()
    client = Cloudant(user_data['cloud_acct_username'],
                      user_data['cloud_acct_pword'],
                      url=user_data['cloud_act_url'])
    #client.connect()

    try:
        client.connect()
    except:
        write_to_Error_log(
            "error connecting to cloudant System Status database, will sleep for 30 seconds and then try again"
        )
        time.sleep(30)
        try:
            client.connect()
        except:
            write_to_Error_log(
                "2nd error in a row connecting to cloudant System Status database, will sleep for 5 minute and then skip"
            )
            time.sleep(300)
            return
    my_database = client["system_status"]
    my_document = my_database[s]
    my_document['v'] = v

    my_document['t'] = dt.datetime.now().strftime("%H:%M:%S")
    my_document['d'] = dt.datetime.now().strftime("%m-%d-%Y")
    my_document.save()
    print('\n\n************** system table updated************\n\n')
示例#24
0
def getCoursesByAttribute(attribute):
    client = Cloudant(user_name, password, url=url)
    client.connect()
    databaseName = "catalog"
    myDatabase = client[databaseName]
    if (myDatabase.exists()):
        print("Successfully created a database {}".format(databaseName))

    result_collection = list(Result(myDatabase.all_docs, include_docs=True))
    #print ("Retrieved full document:\n{0}\n".format(result_collection[0]))
    end_point = '{0}/{1}'.format(url, databaseName + "/_all_docs")
    params = {'include_docs': 'true'}
    response = client.r_session.get(end_point, params=params)
    resultz = []
    count = 0
    for i in range(0, len(result_collection)):
        tmp = result_collection[i]['doc']
        if ('keywords' in tmp):
            dct = tmp['keywords']
            for entry in dct:
                if (attribute.lower() in entry["text"].lower()):
                    resultz.append(tmp)
                    count += 1
    client.disconnect()
    return resultz
示例#25
0
def main(args):
    # Connect Cloudant
    db_client = Cloudant(args["USERNAME"],
                         args["PASSWORD"],
                         url=args["URL"],
                         adapter=Replay429Adapter(retries=10,
                                                  initialBackoff=0.01))
    db_client.connect()
    freee_tokens = db_client['freee_tokens']
    doc_id = args['TOKEN_DOC_ID']

    with Document(freee_tokens, doc_id) as document:
        refresh_token = document['refresh_token']
        payload = {
            'grant_type': 'refresh_token',
            'refresh_token': refresh_token,
            'client_id': args['CLIENT_ID'],
            'client_secret': args['CLIENT_SECRET'],
            'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob'
        }
        response = requests.post(
            'https://accounts.secure.freee.co.jp/public_api/token',
            data=payload).json()
        print(response)
        document['access_token'] = response['access_token']
        document['expires_in'] = response['expires_in']
        document['refresh_token'] = response['refresh_token']
        document['created_at'] = response['created_at']
    db_client.disconnect()
    return {'result': 'OK!'}
示例#26
0
 def test_constructor_with_creds_removed_from_url(self):
     """
     Test instantiating a client object using a URL
     """
     client = Cloudant(None, None, url='https://a9a9a9a9-a9a9-a9a9-a9a9-a9a9a9a9a9a9-bluemix'
                                       ':a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9'
                                       'a9a9a9a9a9a9@d8a01891-e4d2-4102-b5f8-751fb735ce31-'
                                       'bluemix.cloudant.com')
     self.assertEqual(client.server_url, 'https://d8a01891-e4d2-4102-b5f8-751fb735ce31-'
                                         'bluemix.cloudant.com')
     self.assertEqual(client._user, 'a9a9a9a9-a9a9-a9a9-a9a9-a9a9a9a9a9a9-bluemix')
     self.assertEqual(client._auth_token, 'a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a9a'
                                          '9a9a9a9a9a9a9a9a9a9a9a9a9')
示例#27
0
    def test_iam_client_session_login(self, m_set, m_login):
        # create IAM client
        client = Cloudant.iam('foo', MOCK_API_KEY)
        client.connect()

        # add a valid cookie to jar
        client.r_session.cookies.set_cookie(self._mock_cookie())

        client.session_login()

        m_set.assert_called_with(None, None)
        self.assertEqual(m_login.call_count, 2)
        self.assertEqual(m_set.call_count, 2)
示例#28
0
def salvarAnimes(request, path='/animes/A'):
    if request.method == 'POST':
        db = Cloudant(USUARIO_DB, SENHA_DB, url=URL, connect=True)['ahs']

        doc = db[request.POST.get('id')]
        doc['recomendado'] = mudarAtributoInt(
            request.POST.get('recomendar_anime'))
        doc['disponibilizar'] = mudarAtributoInt(
            request.POST.get('disponibilizar_anime'))
        doc['fansub'] = request.POST.get('fansub')
        doc.save()

    return redirect(path)
示例#29
0
    def test_iam_client_session_login_with_new_credentials(self, m_set, m_login):
        # create IAM client
        client = Cloudant.iam('foo', MOCK_API_KEY)
        client.connect()

        # add a valid cookie to jar
        client.r_session.cookies.set_cookie(self._mock_cookie())

        client.session_login('bar', 'baz')  # new creds

        m_set.assert_called_with('bar', 'baz')
        self.assertEqual(m_login.call_count, 2)
        self.assertEqual(m_set.call_count, 2)
示例#30
0
def room_info():
    # 接続
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()
    myDatabase = client[databaseName]

    # 読み込み
    search_id = int(request.args.get('room_id'))
    result_collection = Result(myDatabase.all_docs, include_docs=True)
    output = {}
    for item in result_collection:
        if item["doc"]["room_id"] == search_id:
            output = {
                "users": [{
                    "name": item["doc"]["host_name"],
                    "station": item["doc"]["host_station"]
                }, {
                    "name": item["doc"]["guest_name"],
                    "station": item["doc"]["guest_station"]
                }]
            }
    return jsonify(output)
示例#31
0
def worker(num, s3_client):

    print("Running worker %s" % num)

    season = 2017

    num_to_sport = {0: 'mlb', 1: 'nba', 2: 'nhl'}

    sport = num_to_sport[num]

    all_teams = teams[sport]

    size_data = []

    for team in all_teams:

        print("Collecting for %s" % team)
        size_data.append(get_s3_metadata(s3_client, sport, season, team))

    if 'VCAP_SERVICES' not in os.environ:
        from functions.credentials import *
        cloudant_client = Cloudant(CLOUDANT['username'],
                                   CLOUDANT['password'],
                                   url=CLOUDANT['url'],
                                   connect=True,
                                   auto_renew=True)

    else:

        vcap = json.loads(os.getenv('VCAP_SERVICES'))

        if 'cloudantNoSQLDB' in vcap:
            creds = vcap['cloudantNoSQLDB'][0]['credentials']
            user = creds['username']
            password = creds['password']
            url = 'https://' + creds['host']
            cloudant_client = Cloudant(user, password, url=url, connect=True)

    write_doc(cloudant_client, size_data, sport)
示例#32
0
def all_entries():
    #connect to the db
    client = Cloudant(user, password, url=url, connect=True)
    db = client.create_database(db_name, throw_on_exists=False)

    #get all docs
    docs = list(map(lambda doc: doc, db))
    #put them into a dataframe
    fdocs = json_normalize(docs)
    fdocs = DataFrame(fdocs, columns=['date', 'component', 'data', '_id'])
    fdocs['date'] = to_datetime(fdocs['date'])
    fdocs = fdocs.reset_index(drop=True)
    fdocs.sort_values(['date', 'component'])
    #get the components
    components = fdocs['component'].unique().tolist()

    data = [None] * len(fdocs)
    for i, row in fdocs.iterrows():
        tmp = read_json(fdocs.loc[i, 'data'], orient='index')
        tmp = tmp.reset_index()
        data[i] = tmp
        fdocs.loc[i, 'data'] = i

    #make a list of same size as components
    complist = [None] * len(components)
    for i in range(len(components)):
        #drop everything but relevant info
        tmp = fdocs.drop(fdocs[fdocs.component != components[i]].index)
        #drop duplicates
        tmp = tmp.drop_duplicates(subset=['date'], keep='first', inplace=False)
        #sort them
        tmp = tmp.sort_values(['date'], ascending=[False])
        #re index the dataframe
        tmp = tmp.reset_index(drop=True)
        #put the dataframe into the list
        complist[i] = tmp
    #disconnect from db
    client.disconnect()
    return render_template('entries.html', entries=complist, data=data)
示例#33
0
    def test_session_basic_with_no_credentials(self, m_req):
        """
        Test using basic access authentication with no credentials.
        """
        m_response_ok = mock.MagicMock()
        type(m_response_ok).status_code = mock.PropertyMock(return_value=200)
        m_req.return_value = m_response_ok

        client = Cloudant(None, None, url=self.url, use_basic_auth=True)
        client.connect()
        self.assertIsInstance(client.r_session, BasicSession)

        db = client['animaldb']

        m_req.assert_called_once_with(
            'HEAD',
            self.url + '/animaldb',
            allow_redirects=False,
            auth=None,  # ensure no authentication specified
            timeout=None)

        self.assertIsInstance(db, CloudantDatabase)
示例#34
0
def connect():
    """
    Creates a Cloudant DB client and establishes a connection.
    """

    global USERNAME_DB, PASSWORD_DB, URL_DB, client

    print("\n      " + Style.BRIGHT + Fore.BLACK + "- Generating the client of the Cloudant NoSQL DB service"
          + Style.RESET_ALL)

    # Asks the user for Cloudant credentials
    username = raw_input(Fore.BLUE + "        Enter the Cloudant username or empty to use the default value: "
                         + Fore.RESET) or USERNAME_DB
    password = getpass.getpass(Fore.BLUE + "        Enter the Cloudant password or empty to use the default value: "
                               + Fore.RESET) or PASSWORD_DB
    url = raw_input(Fore.BLUE + "        Enter the Cloudant URL or empty to use the default value: "
                    + Fore.RESET) or URL_DB

    # Checks if some Cloudant DB credential is empty
    if username.isspace() or password.isspace() or url.isspace():
        print(Fore.RED + "        ✖ The Cloudant DB credentials can not be empty." + Fore.RESET)
        sys.exit(0)

    try:
        # Creates the client using auto_renew to automatically renew expired cookie auth
        client = Cloudant(username.replace(" ", ""), password.replace(" ", ""), url=url.replace(" ", ""),
                          connect=True, auto_renew=True)

        # Establishes a connection with the service instance
        client.connect()

        print(Fore.GREEN + "        ✓ Cloudant DB client connected" + Fore.RESET)

    except Exception as cloudantError:
        print(Fore.RED + "        ✖ Error to initialize the Cloudant DB client. Exception: " + str(cloudantError) + "."
              + Fore.RESET)
        sys.exit(1)

    time.sleep(1)
def init():
    # This is the name of the database we are working with.
    databaseName = "persons_db"

    client = Cloudant.iam(this.__username__, this.__apiKey__)
    client.connect()

    myDatabase = client.create_database(databaseName)
    if not myDatabase.exists():
        #  IDK, raise some error or panic
        client.create_database(databaseName)
    this.__client__ = client
    this.__myDatabase__ = myDatabase
def download2():
    if request.form['submit_button6'] == 'download':
        client = Cloudant.iam("4a366964-f520-4ba2-afba-9a1b374f4277-bluemix",
                              "4FpjCCjAVftwY8ZLZDSUJa_8iG14u0c0VsXPB-YEhrU6",
                              connect=True)
        g = session.get("cust_db")
        c = session.get("customer_report")
        c = c.decode("utf-8")

        my_database = client[g]
        l = []
        for document in my_database:
            l.append(document)
        df = pd.DataFrame(l)
        del df['_id']
        del df['_rev']
        cols = [
            "tp_name", "VAN_or_AS2", "contact_person", "email_id", "status"
        ]
        df = df[cols]
        with PdfPages('report.pdf') as pp:
            fig, (ax, ax1,
                  ax2) = plt.subplots(3, 1, figsize=(10, 15))  #figsize=(12,4)
            ax.set_title("Project Reports of Trading Partners of " + c)
            ax.axis('tight')
            ax.axis('off')
            the_table = ax.table(cellText=df.values,
                                 colLabels=df.columns,
                                 loc='center',
                                 cellLoc='left',
                                 colLoc='left')
            ax1.set_title("Number of VAN and AS2")
            ax1.grid()
            bd = df['VAN_or_AS2'].value_counts()
            b = dict(bd)
            ax1.bar(b.keys(), b.values(), width=0.2, align='edge')
            ax2.set_title("Project status ")
            ax2.grid()
            labels = []
            sizes = []
            bd = df['status'].value_counts()
            b = dict(bd)
            for x, y in b.items():
                labels.append(x)
                sizes.append(y)
            patches, texts = ax2.pie(sizes, labels=labels)
            ax2.legend(patches, labels, loc="best")
            pp.savefig(fig, bbox_inches='tight')
            plt.close()
        path = "report.pdf"
        return send_file(path, as_attachment=True)
示例#37
0
def add_signal(status, load):
    client = Cloudant(
        "39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix",
        "48e26645f504209f85b4c44d74a4cb14bc0d059a22b361534b78f406a513f8ff",
        url=
        "https://*****:*****@39a4348e-3ce1-40cd-b016-1f85569d409e-bluemix.cloudant.com"
    )
    client.connect()

    databaseName = load
    myDatabase = client.create_database(databaseName)
    client.delete_database(databaseName)

    # if myDatabase.exists():
    #     print("'{0}' successfully created.\n".format(databaseName))
    #     client.delete_database(databaseName)
    myDatabase = client.create_database(databaseName)

    sampleData = [[status, "kitchen"]]

    # Create documents using the sample data.
    # Go through each row in the array
    for document in sampleData:
        # Retrieve the fields in each row.
        number = document[0]
        name = document[1]

        # Create a JSON document that represents
        # all the data in the row.
        jsonDocument = {"status": number, "circuit": name}

        # Create a document using the Database API.
        newDocument = myDatabase.create_document(jsonDocument)

        # Check that the document exists in the database.
        if newDocument.exists():
            print("Document '{0}' successfully created.".format(number))
        client.disconnect()
示例#38
0
def main(dict):

    # Connecting Cloudant client
    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://*****:*****@3e757df4-b583-42a5-9c83-67f2d1bc3d10-bluemix.cloudantnosqldb.appdomain.cloud"
    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    # Fetching JSON-file where decks are written
    databaseName = "deck-database"
    database = client.__getitem__(databaseName)
    deckfile = database.__getitem__("6ce8ea8c29e12ec8ec21e4a45787ad94")

    # Looping through decks and updating deck prices.
    try:
        for deck in deckfile["decks"]:
            currentval = 0.0
            for urls in deck["cardurls"]:
                currentval += get_price(urls["url"]) * urls["amount"]
            deck["value"] = round(currentval, 2)

        # Saving deckfile and closing client
        deckfile.save()
        client.disconnect()

        # Getting time when updating was done
        now = (datetime.now() + timedelta(hours=3)).strftime('%c')
        return {
            'message': 'Database updated!',
            'time': str(now),
            'tag': "update",
            'success': True
        }
    except HTTPError as err:
        client.disconnect()
        now = (datetime.now() + timedelta(hours=3)).strftime('%c')
        return {
            'message': 'Database updating failed!',
            'time': str(now),
            'tag': "update",
            'success': False
        }
示例#39
0
def get_db_client(db_name: str = None):
    db_config = config.get('db')
    user = db_config.get('user')
    passwd = db_config.get('password')

    db_list = os.environ.get('dbip')
    if db_list:
        db_list = db_list.split(',')
        connected = False
        for ip in db_list:
            try:
                url = 'http://%s' % ip
                client = Cloudant(user,
                                  passwd,
                                  url=url,
                                  connect=True,
                                  auto_renew=True)
                connected = True
                break
            except:
                print("connect %s failed" % url)
        if not connected:
            print("cannot connect to db, exiting")
            sys.exit(1)
    else:
        url = db_config.get('url')
        try:
            client = Cloudant(user,
                              passwd,
                              url=url,
                              connect=True,
                              auto_renew=True)
        except:
            print("cannot connect to db, exiting")
            sys.exit(1)

    db_name = db_config.get('db') if db_name is None else db_name
    return client[db_name]
示例#40
0
def main():
    client = Cloudant(config.username, config.password, account=config.account)

    client.connect()

    dbs = client.all_dbs()

    output = []
    for db in dbs:
    	print 'Retrieving stats for {0}...'.format(db)
        db = CloudantDatabase(client, db)
        print "db: " + json.dumps(db)
        output.append(db.doc_count())

	print json.dumps(output, indent=4)
	print json.dumps(sort(output), indent=4)

    client.disconnect()
示例#41
0
class UnitTestDbBase(unittest.TestCase):
    """
    The base class for all unit tests targeting a database
    """

    @classmethod
    def setUpClass(cls):
        """
        If targeting CouchDB, Set up a CouchDB instance otherwise do nothing.
        """
        if os.environ.get('RUN_CLOUDANT_TESTS') is None:
            if os.environ.get('DB_URL') is None:
                os.environ['DB_URL'] = 'http://127.0.0.1:5984'

            if (os.environ.get('ADMIN_PARTY') and
                os.environ.get('ADMIN_PARTY') == 'true'):
                if os.environ.get('DB_USER'):
                    del os.environ['DB_USER']
                if os.environ.get('DB_PASSWORD'):
                    del os.environ['DB_PASSWORD']
                return

            if os.environ.get('DB_USER') is None:
                os.environ['DB_USER_CREATED'] = '1'
                os.environ['DB_USER'] = '******'.format(
                    unicode_(uuid.uuid4())
                    )
                os.environ['DB_PASSWORD'] = '******'
                resp = requests.put(
                    '{0}/_config/admins/{1}'.format(
                        os.environ['DB_URL'],
                        os.environ['DB_USER']
                        ),
                    data='"{0}"'.format(os.environ['DB_PASSWORD'])
                    )
                resp.raise_for_status()

    @classmethod
    def tearDownClass(cls):
        """
        If necessary, clean up CouchDB instance once all tests are complete.
        """
        if (os.environ.get('RUN_CLOUDANT_TESTS') is None and
            os.environ.get('DB_USER_CREATED') is not None):
            resp = requests.delete(
                '{0}://{1}:{2}@{3}/_config/admins/{4}'.format(
                    os.environ['DB_URL'].split('://', 1)[0],
                    os.environ['DB_USER'],
                    os.environ['DB_PASSWORD'],
                    os.environ['DB_URL'].split('://', 1)[1],
                    os.environ['DB_USER']
                    )
                )
            del os.environ['DB_USER_CREATED']
            del os.environ['DB_USER']
            resp.raise_for_status()

    def setUp(self):
        """
        Set up test attributes for unit tests targeting a database
        """
        self.set_up_client()

    def set_up_client(self, auto_connect=False, auto_renew=False, encoder=None,
                      timeout=(30,300)):
        if os.environ.get('RUN_CLOUDANT_TESTS') is None:
            admin_party = False
            if (os.environ.get('ADMIN_PARTY') and
                os.environ.get('ADMIN_PARTY') == 'true'):
                admin_party = True
            self.user = os.environ.get('DB_USER', None)
            self.pwd = os.environ.get('DB_PASSWORD', None)
            self.url = os.environ['DB_URL']
            self.client = CouchDB(
                self.user,
                self.pwd,
                admin_party,
                url=self.url,
                connect=auto_connect,
                auto_renew=auto_renew,
                encoder=encoder,
                timeout=timeout
            )
        else:
            self.account = os.environ.get('CLOUDANT_ACCOUNT')
            self.user = os.environ.get('DB_USER')
            self.pwd = os.environ.get('DB_PASSWORD')
            self.url = os.environ.get(
                'DB_URL',
                'https://{0}.cloudant.com'.format(self.account))
            self.client = Cloudant(
                self.user,
                self.pwd,
                url=self.url,
                x_cloudant_user=self.account,
                connect=auto_connect,
                auto_renew=auto_renew,
                encoder=encoder,
                timeout=timeout
            )


    def tearDown(self):
        """
        Ensure the client is new for each test
        """
        del self.client

    def db_set_up(self):
        """
        Set up test attributes for Database tests
        """
        self.client.connect()
        self.test_dbname = self.dbname()
        self.db = self.client._DATABASE_CLASS(self.client, self.test_dbname)
        self.db.create()

    def db_tear_down(self):
        """
        Reset test attributes for each test
        """
        self.db.delete()
        self.client.disconnect()
        del self.test_dbname
        del self.db

    def dbname(self, database_name='db'):
        return '{0}-{1}-{2}'.format(database_name, self._testMethodName, unicode_(uuid.uuid4()))

    def populate_db_with_documents(self, doc_count=100, **kwargs):
        off_set = kwargs.get('off_set', 0)
        docs = [
            {'_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i}
            for i in range(off_set, off_set + doc_count)
        ]
        return self.db.bulk_docs(docs)

    def create_views(self):
        """
        Create a design document with views for use with tests.
        """
        self.ddoc = DesignDocument(self.db, 'ddoc001')
        self.ddoc.add_view(
            'view001',
            'function (doc) {\n emit(doc._id, 1);\n}'
        )
        self.ddoc.add_view(
            'view002',
            'function (doc) {\n emit(doc._id, 1);\n}',
            '_count'
        )
        self.ddoc.add_view(
            'view003',
            'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}'
        )
        self.ddoc.add_view(
            'view004',
            'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}',
            '_count'
        )
        self.ddoc.add_view(
            'view005',
            'function (doc) {\n emit([doc.name, doc.age], 1);\n}'
        )
        self.ddoc.add_view(
            'view006',
            'function (doc) {\n emit([doc.name, doc.age], 1);\n}',
            '_count'
        )
        self.ddoc.save()
        self.view001 = self.ddoc.get_view('view001')
        self.view002 = self.ddoc.get_view('view002')
        self.view003 = self.ddoc.get_view('view003')
        self.view004 = self.ddoc.get_view('view004')
        self.view005 = self.ddoc.get_view('view005')
        self.view006 = self.ddoc.get_view('view006')

    def create_search_index(self):
        """
        Create a design document with search indexes for use
        with search query tests.
        """
        self.search_ddoc = DesignDocument(self.db, 'searchddoc001')
        self.search_ddoc['indexes'] = {'searchindex001': {
                'index': 'function (doc) {\n  index("default", doc._id); \n '
                'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} '
                'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} '
            }
        }
        self.search_ddoc.save()

    def load_security_document_data(self):
        """
        Create a security document in the specified database and assign
        attributes to be used during unit tests
        """
        self.sdoc = {
            'admins': {'names': ['foo'], 'roles': ['admins']},
            'members': {'names': ['foo1', 'foo2'], 'roles': ['developers']}
        }
        self.mod_sdoc = {
            'admins': {'names': ['bar'], 'roles': ['admins']},
            'members': {'names': ['bar1', 'bar2'], 'roles': ['developers']}
        }
        if os.environ.get('RUN_CLOUDANT_TESTS') is not None:
            self.sdoc = {
                'cloudant': {
                    'foo1': ['_reader', '_writer'],
                    'foo2': ['_reader']
                }
            }
            self.mod_sdoc = {
                'cloudant': {
                    'bar1': ['_reader', '_writer'],
                    'bar2': ['_reader']
                }
            }
        if os.environ.get('ADMIN_PARTY') == 'true':
            resp = requests.put(
                '/'.join([self.db.database_url, '_security']),
                data=json.dumps(self.sdoc),
                headers={'Content-Type': 'application/json'}
            )
        else:
            resp = requests.put(
                '/'.join([self.db.database_url, '_security']),
                auth=(self.user, self.pwd),
                data=json.dumps(self.sdoc),
                headers={'Content-Type': 'application/json'}
            )
        self.assertEqual(resp.status_code, 200)
# NLC コンフィグの取得
f = open('./nlc_config.json', 'r')
cnf = json.load(f)
f.close()

# Cloudant認証情報の取得
f = open('./cloudant_credentials_id.json', 'r')
cred = json.load(f)
f.close()

print "クラウダントへの接続"
dbname = cnf['database']
print dbname
client = Cloudant(cred['credentials']['username'], 
                  cred['credentials']['password'], 
                  url=cred['credentials']['url'])
client.connect()

# DBが存在していたら削除
print "既存データベースの削除"
try:
    db = client[dbname]
    if db.exists():
        client.delete_database(dbname)
except:
    print "新規データベースの作成"

# DBを新規作成
db = client.create_database(dbname)
    sendGridTo = config['sendGrid']['to'].split(",")
    sendGridFrom = config['sendGrid']['from']
    sendGridSubject = config['sendGrid']['subject']


###########################################################
# define cloudant database in bluemix to hold daily results
###########################################################

if config['cloudant']['username'] == None:
    createDB = False
    queryDB = False
else:
    createDB = True
    queryDB = True
    cloudant = Cloudant(config['cloudant']['username'], config['cloudant']['password'], url=config['cloudant']['url'])
    cloudant.connect()
    vsistatsDb = cloudant["vsistats"]

######################################
## Flags to control script logic
######################################
creatTickets=True

######################################
# Enable Logging
######################################

central = pytz.timezone("US/Central")
today = central.localize(datetime.now())
logging.basicConfig( filename='events.log', format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %I:%M:%S %p',level=logging.WARNING)
示例#44
0
class CloudantClientTests(UnitTestDbBase):
    """
    Cloudant specific client unit tests
    """

    def test_cloudant_context_helper(self):
        """
        Test that the cloudant context helper works as expected.
        """
        try:
            with cloudant(self.user, self.pwd, account=self.account) as c:
                self.assertIsInstance(c, Cloudant)
                self.assertIsInstance(c.r_session, requests.Session)
                self.assertEqual(c.r_session.auth, (self.user, self.pwd))
        except Exception as err:
            self.fail('Exception {0} was raised.'.format(str(err)))
    
    def test_constructor_with_account(self):
        """
        Test instantiating a client object using an account name
        """
        # Ensure that the client is new
        del self.client
        self.client = Cloudant(self.user, self.pwd, account=self.account)
        self.assertEqual(
            self.client.server_url,
            'https://{0}.cloudant.com'.format(self.account)
            )

    def test_connect_headers(self):
        """
        Test that the appropriate request headers are set
        """
        try:
            self.client.connect()
            self.assertEqual(
                self.client.r_session.headers['X-Cloudant-User'],
                self.account
                )
            agent = self.client.r_session.headers.get('User-Agent')
            ua_parts = agent.split('/')
            self.assertEqual(len(ua_parts), 6)
            self.assertEqual(ua_parts[0], 'python-cloudant')
            self.assertEqual(ua_parts[1], sys.modules['cloudant'].__version__)
            self.assertEqual(ua_parts[2], 'Python')
            self.assertEqual(ua_parts[3], '{0}.{1}.{2}'.format(
                sys.version_info[0], sys.version_info[1], sys.version_info[2])),
            self.assertEqual(ua_parts[4], os.uname()[0]),
            self.assertEqual(ua_parts[5], os.uname()[4])
        finally:
            self.client.disconnect()

    def test_db_updates_infinite_feed_call(self):
        """
        Test that infinite_db_updates() method call constructs and returns an
        InfiniteFeed object
        """
        try:
            self.client.connect()
            db_updates = self.client.infinite_db_updates()
            self.assertIsInstance(db_updates, InfiniteFeed)
            self.assertEqual(
                db_updates._url, '/'.join([self.client.server_url, '_db_updates']))
            self.assertIsInstance(db_updates._r_session, requests.Session)
            self.assertFalse(db_updates._raw_data)
            self.assertDictEqual(db_updates._options, {'feed': 'continuous'})
        finally:
            self.client.disconnect()

    def test_billing_data(self):
        """
        Test the retrieval of billing data
        """
        try:
            self.client.connect()
            expected = [
                'data_volume',
                'total',
                'start',
                'end',
                'http_heavy',
                'http_light'
                ]
            # Test using year and month
            year = 2016
            month = 1
            data = self.client.bill(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.bill()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_set_year_without_month_for_billing_data(self):
        """
        Test raising an exception when retrieving billing data with only
        year parameter
        """
        try:
            self.client.connect()
            year = 2016
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.bill(year)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - None')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_month_without_year_for_billing_data(self):
        """
        Test raising an exception when retrieving billing data with only
        month parameter
        """
        try:
            self.client.connect()
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.bill(None, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - None, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_invalid_type_year_for_billing_data(self):
        """
        Test raising an exception when retrieving billing data with a type
        string for the year parameter
        """
        try:
            self.client.connect()
            year = 'foo'
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.bill(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - foo, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_year_with_invalid_month_for_billing_data(self):
        """
        Test raising an exception when retrieving billing data with an
        invalid month parameter
        """
        try:
            self.client.connect()
            year = 2016
            month = 13
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.bill(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - 13')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_volume_usage_data(self):
        """
        Test the retrieval of volume usage data
        """
        try:
            self.client.connect()
            expected = [
                'data_vol',
                'granularity',
                'start',
                'end'
                ]
            # Test using year and month
            year = 2016
            month = 12
            data = self.client.volume_usage(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.volume_usage()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_set_year_without_month_for_volume_usage_data(self):
        """
        Test raising an exception when retrieving volume usage data with only
        year parameter
        """
        try:
            self.client.connect()
            year = 2016
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.volume_usage(year)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - None')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_month_without_year_for_volume_usage_data(self):
        """
        Test raising an exception when retrieving volume usage data with only
        month parameter
        """
        try:
            self.client.connect()
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.volume_usage(None, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - None, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_invalid_type_year_for_volume_usage_data(self):
        """
        Test raising an exception when retrieving volume usage data with a type
        string for the year parameter
        """
        try:
            self.client.connect()
            year = 'foo'
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.volume_usage(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - foo, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_year_with_invalid_month_for_volume_usage_data(self):
        """
        Test raising an exception when retrieving volume usage data with an
        invalid month parameter
        """
        try:
            self.client.connect()
            year = 2016
            month = 13
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.volume_usage(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - 13')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_requests_usage_data(self):
        """
        Test the retrieval of requests usage data
        """
        try:
            self.client.connect()
            expected = [
                'requests',
                'granularity',
                'start',
                'end'
                ]
            # Test using year and month
            year = 2016
            month = 1
            data = self.client.requests_usage(year, month)
            self.assertTrue(all(x in expected for x in data.keys()))
            #Test without year and month arguments
            del data
            data = self.client.requests_usage()
            self.assertTrue(all(x in expected for x in data.keys()))
        finally:
            self.client.disconnect()

    def test_set_year_without_month_for_requests_usage_data(self):
        """
        Test raising an exception when retrieving requests usage data with an
        invalid month parameter
        """
        try:
            self.client.connect()
            year = 2016
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.requests_usage(year)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - None')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_month_without_year_for_requests_usage_data(self):
        """
        Test raising an exception when retrieving requests usage data with only
        month parameter
        """
        try:
            self.client.connect()
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.requests_usage(None, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - None, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_invalid_type_year_for_requests_usage_data(self):
        """
        Test raising an exception when retrieving requests usage data with
        a type string for the year parameter
        """
        try:
            self.client.connect()
            year = 'foo'
            month = 1
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.requests_usage(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - foo, month - 1')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_set_year_with_invalid_month_for_requests_usage_data(self):
        """
        Test raising an exception when retrieving requests usage data with only
        year parameter
        """
        try:
            self.client.connect()
            year = 2016
            month = 13
            with self.assertRaises(CloudantArgumentError) as cm:
                self.client.requests_usage(year, month)
            expected = ('Invalid year and/or month supplied.  '
                        'Found: year - 2016, month - 13')
            self.assertEqual(str(cm.exception), expected)
        finally:
            self.client.disconnect()

    def test_shared_databases(self):
        """
        Test the retrieval of shared database list
        """
        try:
            self.client.connect()
            self.assertIsInstance(self.client.shared_databases(), list)
        finally:
            self.client.disconnect()

    def test_generate_api_key(self):
        """
        Test the generation of an API key for this client account
        """
        try:
            self.client.connect()
            expected = ['key', 'password', 'ok']
            api_key = self.client.generate_api_key()
            self.assertTrue(all(x in expected for x in api_key.keys()))
            self.assertTrue(api_key['ok'])
        finally:
            self.client.disconnect()

    def test_cors_configuration(self):
        """
        Test the retrieval of the current CORS configuration for this client
        account
        """
        try:
            self.client.connect()
            expected = ['allow_credentials', 'enable_cors', 'origins']
            cors = self.client.cors_configuration()
            self.assertTrue(all(x in expected for x in cors.keys()))
        finally:
            self.client.disconnect()

    def test_cors_origins(self):
        """
        Test the retrieval of the CORS origins list
        """
        try:
            self.client.connect()
            origins = self.client.cors_origins()
            self.assertIsInstance(origins, list)
        finally:
            self.client.disconnect()

    def test_disable_cors(self):
        """
        Test disabling CORS (assuming CORS is enabled)
        """
        try:
            self.client.connect()
            # Save original CORS settings
            save = self.client.cors_configuration()
            # Test CORS disable
            self.assertEqual(self.client.disable_cors(), {'ok': True})
            # Restore original CORS settings
            self.client.update_cors_configuration(
                save['enable_cors'],
                save['allow_credentials'],
                save['origins'],
                True
                )
        finally:
            self.client.disconnect()

    def test_update_cors_configuration(self):
        """
        Test updating CORS configuration
        """
        try:
            self.client.connect()
            # Save original CORS settings
            save = self.client.cors_configuration()
            # Test updating CORS settings, overwriting origins
            result = self.client.update_cors_configuration(
                True,
                True,
                ['https://ibm.com'],
                True)
            self.assertEqual(result, {'ok': True})
            updated_cors = self.client.cors_configuration()
            self.assertTrue(updated_cors['enable_cors'])
            self.assertTrue(updated_cors['allow_credentials'])
            expected = ['https://ibm.com']
            self.assertTrue(all(x in expected for x in updated_cors['origins']))
            # Test updating CORS settings, adding to origins
            result = self.client.update_cors_configuration(
                True,
                True,
                ['https://ibm.cloudant.com']
                )
            self.assertEqual(result, {'ok': True})
            del updated_cors
            updated_cors = self.client.cors_configuration()
            self.assertTrue(updated_cors['enable_cors'])
            self.assertTrue(updated_cors['allow_credentials'])
            expected.append('https://ibm.cloudant.com')
            self.assertTrue(all(x in expected for x in updated_cors['origins']))
            # Restore original CORS settings
            self.client.update_cors_configuration(
                save['enable_cors'],
                save['allow_credentials'],
                save['origins'],
                True
                )
        finally:
            self.client.disconnect()
示例#45
0
from cloudant.client import Cloudant

cloud   = Cloudant("3a4d4cf0-aed2-4916-8413-fa0177d2129f-bluemix", "24b4187fbd39510e84cc2cf10184cebf97ea56b836aab8ce4590ffe6477ae925", url = "https://3a4d4cf0-aed2-4916-8413-fa0177d2129f-bluemix.cloudant.com")
cloud.connect()

db   = cloud['usshorizon']
worf = db['_design/ansiroom.json']
data = worf.get_view("bedplant")

for i in data.result:
    try:
        print "%d -> %d" % (int(i['key']), int(i['value']))
    except:
        pass
        #print i
示例#46
0
  "id": "PC",
  "auth-method": "token",
  "auth-token": "qwertyuiop"
}

try:
  deviceCli = ibmiotf.device.Client(deviceOptions)
except Exception as e:
  print("Caught exception connecting device: %s" % str(e))
  sys.exit()

try:
    USERNAME = "******"
    PASSWORD = "******"
    cloudant_url = "https://*****:*****@ee2a9736-5f50-4046-8fb5-f57c210f05a7-bluemix.cloudant.com"
    cloudant_client = Cloudant(USERNAME, PASSWORD, url=cloudant_url)
    cloudant_client.connect()
    print "Successfully connected"
except Exception as e:
    print "Unable to connect to cloudant service!"
    sys.exit()

feedback_db = cloudant_client['ttpfeedback'] #opening ttpfeedback in cloudantdb
docs_count = feedback_db.doc_count()
current_count = docs_count
flag_count = input("How many participants?") #the count of the no. of participants in the event
pub_count = 1
flag = True
# result_collection  = Result(feedback_db.all_docs, include_docs = True) #collection
deviceCli.connect()
ratings = {} #ratings dict/hastable
示例#47
0
# Cloudant認証情報の取得
f = open('cloudant_credentials_id.json', 'r')
cred = json.load(f)
f.close()
print cred


# データベース名の取得
f = open('database_name.json', 'r')
dbn = json.load(f)
f.close()
print dbn['name']

client = Cloudant(cred['credentials']['username'], 
                  cred['credentials']['password'], 
                  url=cred['credentials']['url'])

# Connect to the server
client.connect()

# DB選択
db = client[dbn['name']]

# CSVを読んでループを回す
fn = 'weather_code.csv';
reader = csv.reader(codecs.open(fn),delimiter=',',quoting=csv.QUOTE_NONE)
for id, e_description,j_description  in reader:
    print id, e_description,j_description
    data = {
        "_id": id,
class UnitTestDbBase(unittest.TestCase):
    """
    The base class for all unit tests targeting a database
    """

    @classmethod
    def setUpClass(cls):
        """
        If targeting CouchDB, Set up a CouchDB instance otherwise do nothing.
          
        Note: Admin Party is currently unsupported so we must create a 
          CouchDB user for tests to function with a CouchDB instance if one is
          not provided.
        """
        if os.environ.get('RUN_CLOUDANT_TESTS') is None:
            if os.environ.get('DB_URL') is None:
                os.environ['DB_URL'] = 'http://127.0.0.1:5984'

            if (os.environ.get('ADMIN_PARTY') and
                os.environ.get('ADMIN_PARTY') == 'true'):
                if os.environ.get('DB_USER'):
                    del os.environ['DB_USER']
                if os.environ.get('DB_PASSWORD'):
                    del os.environ['DB_PASSWORD']
                return

            if os.environ.get('DB_USER') is None:
                os.environ['DB_USER_CREATED'] = '1'
                os.environ['DB_USER'] = '******'.format(
                    unicode_(uuid.uuid4())
                    )
                os.environ['DB_PASSWORD'] = '******'
                resp = requests.put(
                    '{0}/_config/admins/{1}'.format(
                        os.environ['DB_URL'],
                        os.environ['DB_USER']
                        ),
                    data='"{0}"'.format(os.environ['DB_PASSWORD'])
                    )
                resp.raise_for_status()

    @classmethod
    def tearDownClass(cls):
        """
        If necessary, clean up CouchDB instance once all tests are complete.
        """
        if (os.environ.get('RUN_CLOUDANT_TESTS') is None and
            os.environ.get('DB_USER_CREATED') is not None):
            resp = requests.delete(
                '{0}://{1}:{2}@{3}/_config/admins/{4}'.format(
                    os.environ['DB_URL'].split('://', 1)[0],
                    os.environ['DB_USER'],
                    os.environ['DB_PASSWORD'],
                    os.environ['DB_URL'].split('://', 1)[1],
                    os.environ['DB_USER']
                    )
                )
            del os.environ['DB_USER_CREATED']
            del os.environ['DB_USER']
            resp.raise_for_status()

    def setUp(self):
        """
        Set up test attributes for unit tests targeting a database
        """
        if os.environ.get('RUN_CLOUDANT_TESTS') is None:
            admin_party = False
            if (os.environ.get('ADMIN_PARTY') and
                os.environ.get('ADMIN_PARTY') == 'true'):
                admin_party = True
            self.user = os.environ.get('DB_USER', None)
            self.pwd = os.environ.get('DB_PASSWORD', None)
            self.url = os.environ['DB_URL']
            self.client = CouchDB(self.user, self.pwd, admin_party, url=self.url)
        else:
            self.account = os.environ.get('CLOUDANT_ACCOUNT')
            self.user = os.environ.get('DB_USER')
            self.pwd = os.environ.get('DB_PASSWORD')
            self.url = os.environ.get(
                'DB_URL',
                'https://{0}.cloudant.com'.format(self.account))
            self.client = Cloudant(
                self.user,
                self.pwd,
                url=self.url,
                x_cloudant_user=self.account)

    def tearDown(self):
        """
        Ensure the client is new for each test
        """
        del self.client

    def db_set_up(self):
        """
        Set up test attributes for Database tests
        """
        self.client.connect()
        self.test_dbname = self.dbname()
        self.db = self.client._DATABASE_CLASS(self.client, self.test_dbname)
        self.db.create()

    def db_tear_down(self):
        """
        Reset test attributes for each test
        """
        self.db.delete()
        self.client.disconnect()
        del self.test_dbname
        del self.db

    def dbname(self, database_name='db'):
        return '{0}-{1}'.format(database_name, unicode_(uuid.uuid4()))

    def populate_db_with_documents(self, doc_count=100, **kwargs):
        off_set = kwargs.get('off_set', 0)
        docs = [
            {'_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i}
            for i in range(off_set, off_set + doc_count)
        ]
        return self.db.bulk_docs(docs)

    def create_views(self):
        """
        Create a design document with views for use with tests.
        """
        self.ddoc = DesignDocument(self.db, 'ddoc001')
        self.ddoc.add_view(
            'view001',
            'function (doc) {\n emit(doc._id, 1);\n}'
        )
        self.ddoc.add_view(
            'view002',
            'function (doc) {\n emit(doc._id, 1);\n}',
            '_count'
        )
        self.ddoc.add_view(
            'view003',
            'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}'
        )
        self.ddoc.add_view(
            'view004',
            'function (doc) {\n emit(Math.floor(doc.age / 2), 1);\n}',
            '_count'
        )
        self.ddoc.add_view(
            'view005',
            'function (doc) {\n emit([doc.name, doc.age], 1);\n}'
        )
        self.ddoc.add_view(
            'view006',
            'function (doc) {\n emit([doc.name, doc.age], 1);\n}',
            '_count'
        )
        self.ddoc.save()
        self.view001 = self.ddoc.get_view('view001')
        self.view002 = self.ddoc.get_view('view002')
        self.view003 = self.ddoc.get_view('view003')
        self.view004 = self.ddoc.get_view('view004')
        self.view005 = self.ddoc.get_view('view005')
        self.view006 = self.ddoc.get_view('view006')

    def create_search_index(self):
        """
        Create a design document with search indexes for use
        with search query tests.
        """
        self.search_ddoc = DesignDocument(self.db, 'searchddoc001')
        self.search_ddoc['indexes'] = {'searchindex001': {
                'index': 'function (doc) {\n  index("default", doc._id); \n '
                'if (doc.name) {\n index("name", doc.name, {"store": true}); \n} '
                'if (doc.age) {\n index("age", doc.age, {"facet": true}); \n} \n} '
            }
        }
        self.search_ddoc.save()
示例#49
0
# Cloudant認証情報の取得
f = open('cloudant_credentials_id.json', 'r')
cred = json.load(f)
f.close()
print cred


# データベース名の取得
f = open('database_name.json', 'r')
dbn = json.load(f)
f.close()
print dbn['name']

client = Cloudant(cred['credentials']['username'], 
                  cred['credentials']['password'], 
                  url=cred['credentials']['url'])

# Connect to the server
client.connect()

# Create a database using an initialized client
# The result is a new CloudantDatabase or CouchDatabase based on the client
my_database = client.create_database(dbn['name'])

# You can check that the database exists
if my_database.exists():
    print 'SUCCESS!!'        

# Disconnect from the server
client.disconnect()
示例#50
0
# Cloudant認証情報の取得
f = open('cloudant_credentials_id.json', 'r')
cred = json.load(f)
f.close()
print cred


# データベース名の取得
f = open('database_name.json', 'r')
dbn = json.load(f)
f.close()
print dbn['name']

client = Cloudant(cred['credentials']['username'], 
                  cred['credentials']['password'], 
                  url=cred['credentials']['url'])

# Connect to the server
client.connect()

# DB選択
db = client[dbn['name']]

# Create a database using an initialized client
# The result is a new CloudantDatabase or CouchDatabase based on the client
client.delete_database(dbn['name'])


# You can check that the database exists
if db.exists():
示例#51
0
文件: db.py 项目: aqualove/api
def make_db_connection():
    services = current_app.config['cf_services']
    creds = services['cloudantNoSQLDB'][0]['credentials']
    client = Cloudant(creds['username'], creds['password'], url=creds['url'])
    client.connect()
    return client
示例#52
0
class CloudantDB(threading.Thread):

    def _readConfig(self):
        update = False

        if not os.path.isdir(self._homeDir):
            print "Creating homeDir"
            os.makedirs(self._homeDir)

        if os.path.isfile(self._configFileName):
            self._config.read(self._configFileName)
        else:
            print "Config file not found"
            update = True

        if not self._config.has_section('REDIS'):
            print "Adding Redis part"
            update = True
            self._config.add_section("REDIS")

        if not self._config.has_option("REDIS", "ServerAddress"):
            print "No Server Address"
            update = True
            self._config.set("REDIS", "ServerAddress", "<ServerAddress>")

        if not self._config.has_option("REDIS", "ServerPort"):
            print "No Server Port"
            update = True
            self._config.set("REDIS", "ServerPort", "6379")

        if not self._config.has_section('CLOUDANT'):
            print "Adding Cloudant part"
            update = True
            self._config.add_section("CLOUDANT")

        if not self._config.has_option("CLOUDANT", "ServerAddress"):
            print "No Server Address"
            update = True
            self._config.set("CLOUDANT", "ServerAddress", "<ServerAddress>")

        if not self._config.has_option("CLOUDANT", "Username"):
            print "No Username"
            update = True
            self._config.set("CLOUDANT", "Username", "Didditulle")

        if not self._config.has_option("CLOUDANT", "Password"):
            print "No Password"
            update = True
            self._config.set("CLOUDANT", "Password", "geheim")

        if update:
            with open(self._configFileName, 'w') as f:
                self._config.write(f)

    def __init__(self):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self._homeDir        = os.path.expanduser("~/.sensomatic")
        self._configFileName = self._homeDir + '/config.ini'
        self._config         = ConfigParser.ConfigParser()
        self._readConfig()
        self._redis          = redis.StrictRedis(host=self._config.get("REDIS", "ServerAddress"), port=self._config.get("REDIS", "ServerPort"), db=0)
        self._cloudant       = Cloudant(self._config.get("CLOUDANT", "Username"), self._config.get("CLOUDANT", "Password"), url=self._config.get("CLOUDANT", "ServerAddress"))
        self.checkDB()

    def checkDB(self):
        self._cloudant.connect()
        if "usshorizon" in self._cloudant.all_dbs():
            self._database = self._cloudant['usshorizon']
            return True
        else:
            print "Create DB"
            self._database = self._cloudant.create_database('usshorizon')
            if self._database.exists():
                print "Success"
                return True
            else:
                print "Error"
                return False

    def getData(self):
        data = {'timestamp':time.time()}
        for key in self._redis.keys():
            k = key.split('/')
            l = len(k)
            w = data
            for i in range(l):
                if k[i] not in w:
                    w[k[i]] = {}
                w = w[k[i]]
                if i == l - 1:
                    w['value'] = self._redis.get(key)
        return data

    def run(self):
        while True:
            d = self.getData()
            # print d
            try:
                if self.checkDB():
                    self._database.create_document(d)
            except Exception as e:
                print e
                time.sleep(60)
            time.sleep(10)