Example #1
0
def set_led():
    global wiringpi
    global DB_IP
    # 9
    content = request.form
    print(content['led_index'])

    # 10
    if int(content['led_index']) == 1:
        wiringpi.digitalWrite(LED_PIN_1, int(content['status']))  # 12
    elif int(content['led_index']) == 2:
        wiringpi.digitalWrite(LED_PIN_2, int(content['status']))  # 12

    # 13 -- DATABSE WRITE DOCUEMTENT --
    couch_client = CouchDB('admin',
                           'admin',
                           url='http://' + DB_IP + ':5984',
                           connect=True)
    log_db = couch_client['logs']
    doc = log_db.create_document({
        'action':
        "SET LED " + str(content['led_index']) + " BY USER TO " +
        str(content['status'])
    })
    couch_client.disconnect()

    return jsonify({'status': 'ok'})
Example #2
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
def upload_clarifications(clarifications_list):
    user = os.getenv("USER")
    password = os.getenv("PASSWORD")
    url = os.getenv("URL")
    db_name = os.getenv("CLARIFICATION_DB")
    client = Cloudant(user, password, url=url, connect=True, auto_renew=True)

    # Open clarifications DB
    clarifications_db = client[db_name]
    print(clarifications_db)
    print(clarifications_list)
    try:
        for i, clarification in enumerate(clarifications_list):
            clarifications_db.create_document(
                json.loads(
                    json.dumps(vars(clarification),
                               indent=4,
                               sort_keys=True,
                               default=str)))
            print('subido')
            if i % 10 == 0:
                time.sleep(3)
    except Exception as e:
        print(e)

    # Disconnect from the server
    client.disconnect()
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
Example #5
0
def getLastSavedDbState():
    from cloudant.client import Cloudant
    from cloudant.result import Result
    from cloudant.adapters import Replay429Adapter
    from credentials import getUserName, getPassword, getUrl

    #Establish a connection to the cloudant database by providing the credentials necessary
    client = Cloudant(getUserName(),
                      getPassword(),
                      url=getUrl(),
                      adapter=Replay429Adapter(retries=10,
                                               initialBackoff=0.01))
    client.connect()

    #Connect to the "news_articles" database remotely
    db = client.get("news_articles", remote=True)
    #Get all records from the database
    resultset = Result(db.all_docs, include_docs=True)

    news_articles_list = list()
    #Loop through the database records
    for result in resultset:
        #Add all article titles to a list.
        #The news article titles are unique and will be used to check if an article already exists in the database
        news_articles_list.append(result["doc"]["article_title"])
    client.disconnect()

    return news_articles_list
class CouchDB():
    def __init__(self, cfg):
        self._cfg = cfg
        self._client = Cloudant(self._cfg['user'],
                                self._cfg['password'],
                                url=self._cfg['host'])
        self._client.connect()
        self._db = self._get_db()

    def _get_db(self):
        databases = self._client.all_dbs()
        db_name = self._cfg[self._name]
        if not db_name in databases:
            self._client.create_database(db_name)
        return self._client[db_name]

    def update_document(self, document_id, attributes_dict):
        if attributes_dict is None:
            return False

        document = self._db[document_id]
        for key in attributes_dict.keys():
            document[key] = attributes_dict[key]
        document.save()
        return True

    def reconnect(self):
        try:
            self._client.disconnect()
            self._client.connect()
        except Exceptions as e:
            print('Eror: {}'.format(e))
    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
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()
Example #9
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)
Example #10
0
def get_user(name):
    if name not in user_dictionary.keys(
    ):  # we need to handle the creation of a new user
        user = User(my_name=name)
        user_dictionary[name] = user
        user.save_me_to_db()
    else:
        myclient = Cloudant(serviceUsername,
                            servicePassword,
                            url=serviceURL,
                            adapter=httpAdapter)
        myclient.connect()
        users_db = myclient.get(user_database_name, remote=True)
        #if users_db is not None:
        users_returned = QueryResult(
            Query(users_db, selector={'_id': {
                '$eq': name
            }}))
        if users_returned is not None:
            list = users_returned.all()
            for u in list:
                t1 = u['_id']
                t2 = str(u['value'])
                # print(t2)
                dec = jsonpickle.decode(t2)
                user_dictionary[t1] = dec
        myclient.disconnect()
    return user_dictionary[name]
Example #11
0
def main(dict):

    # Checking if new deck has right values in it
    if not "deckname" in dict:
        return {"error_message": "A deck must have a name value ('deckname')"}
    if not "cardurls" in dict:
        return {
            "error_message":
            "A deck must have atleast one card (in 'cardurls'). A card must have values 'url' and 'amount'."
        }

    # 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")

    # Checking if deckname is already in use
    if not deck_name_valid(dict["deckname"], deckfile):
        client.disconnect()
        return {"error_message": "Deck name is already in use."}

    # Getting value for the new deck
    currentval = 0.0
    amount_of_cards = 0
    for url in dict["cardurls"]:
        currentval += get_price(url["url"]) * url["amount"]
        amount_of_cards += url["amount"]
    dict["value"] = round(currentval, 2)
    dict["card_amount"] = amount_of_cards

    # Removing unnecassary stuff from dict
    if "__ow_headers" in dict:
        dict.pop("__ow_headers")
    if "__ow_method" in dict:
        dict.pop("__ow_method")
    if "__ow_path" in dict:
        dict.pop("__ow_path")

    # Adding new deck to the collection
    deckfile["decks"].append(dict)
    deckfile.save()

    # Disconnecting from client
    client.disconnect()

    # Informing user that adding succeeded
    return {
        'message': "A new deck was added succesfully!",
        'deckname': dict["deckname"],
        'amount': amount_of_cards,
        'value': dict["value"],
        'tag': "add"
    }
Example #12
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()
Example #13
0
def update_all_to_db():
    adapter = Replay429Adapter(200, 0.25)
    client = Cloudant(api_user, api_pass, url=api_url, adapter=adapter)
    # or using url
    # client = Cloudant(USERNAME, PASSWORD, url='https://acct.cloudant.com')

    # Connect to the server
    client.connect()

    # Perform client tasks...
    session = client.session()
    print('Username: {0}'.format(session['userCtx']['name']))
    print('Databases: {0}'.format(client.all_dbs()))

    courses_db = client['purdue_courses']

    file_path = "CourseInfo.json"
    f = open(file_path, 'r')
    text = f.read()
    f.close()
    deep_caches = json.loads(text)

    cache = dict()
    for cache_type, cache in deep_caches.items():
        cache.update(cache)

    bulk_update(cache, courses_db)

    # Disconnect from the server
    client.disconnect()
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.")
Example #15
0
def getItems(dictUse):
    from cloudant.client import Cloudant
    from cloudant.error import CloudantException
    from cloudant.result import Result, ResultByKey
    import json
    returningList = []
    numberList = []

    serviceUsername = "******"
    servicePassword = "******"
    serviceURL = "https://c2c7ebdf-134b-4f53-9805-d37f520c6933-bluemix.cloudant.com"

    client = Cloudant(serviceUsername, servicePassword, url=serviceURL)
    client.connect()

    #name storage_unit
    databaseName = "storage_unit"
    myDatabaseDemo = client.create_database(databaseName)
    result_collection = Result(myDatabaseDemo.all_docs, include_docs=True)
    for doc in result_collection:
        if(doc["doc"]["Blood Type"] == dictUse["Blood Type"] and (doc["doc"]["Location"]).lower() == dictUse["Location"].lower()):
            returningList.append(doc["doc"]["Name"])
            numberList.append(doc["doc"]["Number"])
                

    client.disconnect()
    returnString = ""
    for i in range (len(returningList)):
        if(i == len(returningList) - 1):
            returnString += returningList[i]
        else:
            returnString += returningList[i] + ', '
    return returnString, numberList
Example #16
0
def main(dict):
    
    # Checking if user has typed a deck that this function is trying to find
    if not "deckname" in dict:
        return { "error_message": "Type value to 'deckname' to fetch specific deck's information." }
    
    # 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")
    
    # Trying to find deck from database
    name_to_find = dict["deckname"]
    result = {}
    for deck in deckfile["decks"]:
        if name_to_find == deck["deckname"]:
            result = deck
            break
    else:
        return { "error_message": f"Couldn't find a deck named {name_to_find}." }
    
    client.disconnect()
    result["tag"] = "get_deck"
    result["message"] = "Deck's information is presented in Slack by MTG Deck Bot!"
    return result
Example #17
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()
Example #18
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
Example #19
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")

    # Creating data that will be sent back to client
    data = {}
    data["decks"] = []
    for deck in deckfile["decks"]:
        deckdata = {}
        deckdata["name"] = deck["deckname"]
        deckdata["value"] = deck["value"]
        deckdata["card_amount"] = deck["card_amount"]
        data["decks"].append(deckdata)
    data["tag"] = "get_decks"
    data["message"] = "Decks are presented in Slack by MTG Deck Bot."

    client.disconnect()

    return data
Example #20
0
def kriging_plot():
    #date =  str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
    data = dataset.data()
    data.sort_values(by=['component'])
    #data.set_index(keys=['component'], drop=False,inplace=True)
    names = data['component'].unique().tolist()

    for component in names:
        compdata = data.loc[data.component == component]
        compdata = compdata.reset_index(drop=True)
        if (4 > len(compdata.index)):
            continue

        krige_data = krige_task(compdata[[
            'latitude', 'longitude', 'value', 'unit', 'component', 'x', 'y'
        ]])

        #define documentDataFrame(compdata, columns=['latitude','longitude', 'value', 'unit', 'component'] )
        document = {
            'date': str(compdata['toTime'].iloc[0]),
            'component': str(compdata['component'].iloc[0]),
            'data':
            data.loc[data.component == component].to_json(orient='index'),
            'krige_data': simplejson.dumps(krige_data.tolist())
        }
        # 'data' : compdata.to_json(orient='index'),
        #connect to db
        client = Cloudant(user, password, url=url, connect=True)
        db = client.create_database(db_name, throw_on_exists=False)
        #store document
        document = db.create_document(document)
    #disconnect from db
    client.disconnect()
    return data.to_html()
Example #21
0
def get_from_db_by_params(user_name, region_name, place_name, db_name, object_example, asdocs=False, dates=None, word_list=None, logic='or_logic', exact=False):
    print("get_from_db_by_params")
    print(user_name + ";" + region_name + ";" + place_name + ";" + db_name + ";" + str(type(object_example)) + ";" +
                                                                                       str(asdocs))
    get_db_client = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=Replay429Adapter(retries=10, initialBackoff=0.01))
    try:
        our_list = []
        get_db_client.connect()
        db = get_db_client.get(db_name, remote=True)
        print("get_from_db_by_params logic is:" + logic)
        selector = generate_single_place_selector(user_name, region_name, place_name, dates, word_list, logic, exact)
        print("the selector is: " + str(selector))
        query_result = QueryResult(Query(db, selector=selector))
        if asdocs is True:
            for doc in query_result:
                our_list.append(doc)
        else:
            for doc in query_result:
                our_list.append(object_example.build_from_document(doc))
    except CloudantException as exc:
        print("CloudantException in get_from_db_by_params")
        print(exc)
        return exc
    except Exception as exc:
        print("non CloudantException exception in get_from_db_by_params")
        print(exc)
        return exc
    finally:
        get_db_client.disconnect()
        print("get_from_db_by_params id: " + str(db_name) + " len: " + str(len(our_list)))
    return our_list
Example #22
0
def save_list_to_db(inlist, db_name, object_example=None):
    print("save_list_to_db:" + db_name + "list len is: " + str(len(inlist)))
    documents = []
    for obj in inlist:
        if object_example is not None:
            document = obj.build_my_document()
        else:
            val = jsonpickle.encode(obj)
            document = {
                '_id': str(obj.id),
                'value': val
            }
        documents.append(document)
    client_save_to_db = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=httpAdapter2)
    try:
        client_save_to_db.connect()
        db = client_save_to_db.get(db_name, remote=True)
        db.bulk_docs(documents)
    except CloudantException as exc:
        print("CloudantException in save_list_to_db")
        print(exc)
        time.sleep(2)
        try:
            client_save_to_db.connect()
            db = client_save_to_db.get(db_name, remote=True)
            db.bulk_docs(documents)
        except Exception as exc:
            print("second exception after CloudantException exception in save_list_to_db")
            print(exc)
    except Exception as exc:
        print("non CloudantException exception in save_list_to_db")
        print(exc)
    finally:
        client_save_to_db.disconnect()
Example #23
0
def get_from_db(id_list, db_name, object_example=None):
        result_list = []
        client_get_from_db = Cloudant(serviceUsername, servicePassword, url=serviceURL, adapter=httpAdapter2)
        try:
            client_get_from_db.connect()
            db = client_get_from_db.get(db_name, remote=True)
            if db is None:
                print("error: no tweet db ready yet")
                return ""
            query_result = QueryResult(Query(db, selector={'_id': {'$in': id_list}}))
            index = 0
            for doc in query_result:
                if object_example is "asdocs":
                    result_list.append(doc)
                elif object_example is not None:
                    result_list.append(object_example.build_from_document(doc))
                    index = + 1
                else:
                    dec = jsonpickle.decode(str(doc['value']))
                    result_list.append(dec)
                    index =+ 1
            print("query_result len: " + str(index))
        except CloudantException as exc:
            print("CloudantException in get_from_db")
            print(exc)
            result_list = []
        except Exception as exc:
            print("non CloudantException exception in get_from_db")
            print(exc)
            result_list = []
        finally:
            client_get_from_db.disconnect()
            print("get_from_db id: " + str(db_name) + " len: " + str(len(result_list)))
        return result_list
Example #24
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!'}
Example #25
0
def write_to_cloudant(sensor_id, local, temp):
    # ACCOUNT_NAME= cloud_acct_org_name
    # USERNAME =  cloudant_username
    # PASSWORD =  cloud_acct_pword
    # URL= cloud_act_url
    # API_KEY= cloud_acct_API_key
    DATABASE_NAME = "temps"
    print("sub to write data to cloudant - SENSOR ID = ", sensor_id,
          "LOCATION = ", local, "TEMP = ", temp)
    user_data = get_user_data()
    client = Cloudant(user_data['cloud_acct_username'],
                      user_data['cloud_acct_pword'],
                      url=user_data['cloud_act_url'])

    try:
        client.connect()

    except:
        write_to_Error_log(
            "error connecting to cloudant TEMP 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 TEMP database, will sleep for 5 minute and then skip"
            )
            time.sleep(300)
            return

    my_database = client[DATABASE_NAME]

    json_document = {
        "s": sensor_id,
        "l": local,
        "temp": temp,
        "t": dt.datetime.now().strftime("%H:%M:%S"),
        "d": dt.datetime.now().strftime("%m-%d-%Y")
    }
    try:
        new_document = my_database.create_document(json_document)
    except:
        write_to_Error_log(
            "error writting new database document, will wait 30 seconds and try again"
        )
        time.sleep(30)
        try:
            new_document = my_database.create_document(json_document)
        except:
            write_to_Error_log(
                "2nd error in a row writting new database document, will wait 5 minutes and then skip"
            )
            return
    client.disconnect()
Example #26
0
def get_img(_id):
    #connect to the db
    client = Cloudant(user, password, url=url, connect=True)
    db = client.create_database(db_name, throw_on_exists=False)
    #get the document from the db
    doc = db[_id]

    #load the krige data
    H = simplejson.loads(doc['krige_data'])
    buffr = io.BytesIO()

    #min max values for the color
    maxvalue = 100
    minvalue = 0

    #set the min max values according to whats unhealthy
    #max will be unhealthy value, and in turn be red on the generated image
    #we set values based on https://uk-air.defra.gov.uk/air-pollution/daqi?view=more-info&pollutant=no2 , Accsessed 11.05.2018
    if (doc['component'] == 'PM2.5'):
        maxvalue = 60
        minvalue = 0
    if (doc['component'] == 'PM10'):
        maxvalue = 85
        minvalue = 0
    if (doc['component'] == 'NO2'):
        maxvalue = 420
        minvalue = 0
    #plot the figure
    fig, ax = subplots()
    ax.imshow(H,
              cmap=my_cmap,
              vmin=minvalue,
              vmax=maxvalue,
              origin='lower',
              interpolation='gaussian',
              alpha=0.7,
              extent=[X0, X1, Y0, Y1])
    ax.axis('off')

    #set proper image size and extent to plot
    fig.set_size_inches(5.95, 5)
    imgextent = ax.get_window_extent().transformed(
        fig.dpi_scale_trans.inverted())
    fig.savefig(buffr,
                format='svg',
                bbox_inches=imgextent,
                transparent=True,
                pad_inches=0,
                frameon=None)

    #go to start of file
    buffr.seek(0)

    #disconnect from db
    client.disconnect()

    return send_file(buffr, mimetype='image/svg+xml')
Example #27
0
class DBCloudant:
    def __init__(self):
        self.client = ""
        self.session = ""
        self.dataBase = ""
        self.document = ""
        self.query = ""

    def connect(self):
        self.client = Cloudant(conf.username,
                               conf.password,
                               url=conf.url,
                               connect=True)
        self.session = self.client.session()
        if self.session['userCtx']['name']:
            print("Coneccion realizada correctamente")
        else:
            print("Error al conectar con la BD")

    def disconnect(self):
        self.client.disconnect()

    def createDB(self, name):
        try:
            self.dataBase = self.client.create_database(name)
            if self.dataBase.exists():
                print("DB creada correctamente")
        except Exception as error:
            print("DB ya existente")

    def getDataBases(self):
        return self.client.all_dbs()

    def deleteDataBase(self, name):
        return self.client.delete_database(name)

    def openDB(self, name):
        self.dataBase = self.client[name]

    def createDocument(self, data):
        self.document = self.dataBase.create_document(data)
        if self.document.exists():
            print("Se creo el documento correctamente")

    def getDocument(self, name):
        self.document = self.dataBase[name]
        return self.document

    def getAllDocuments(self, name):
        return self.dataBase

    def getDocumentBySensor(self, sensor):
        self.query = cl.query.Query(self.dataBase)
        with self.query.custom_result(selector={"sensor": sensor}) as rst:
            return rst
Example #28
0
def get_log():
    global DB_IP
    logs = []
    couch_client = CouchDB('admin',
                           'admin',
                           url='http://' + DB_IP + ':5984',
                           connect=True)
    log_db = couch_client['logs']
    for document in log_db:
        logs.append(document)
    couch_client.disconnect()
    logs.reverse()
    return jsonify(logs)
Example #29
0
def do_logs():
    tmp_log = []

    couch_client = CouchDB('admin',
                           'admin',
                           url='http://' + DB_IP + ':5984',
                           connect=True)
    log_db = couch_client['logs']

    for doc in log_db:
        tmp_log.append(doc['action'])
    couch_client.disconnect()
    return jsonify(tmp_log)
Example #30
0
def AntecedentesApiGet(request, cc):
    client = Cloudant(
        '0fe5f2c7-b763-4636-a768-6eb9ed79b1c6-bluemix',
        'd5fb4bdd9bdeb36d09224085e367317c4013b9ec5e65f8889efdc40621bbc9c8',
        url=
        'https://*****:*****@0fe5f2c7-b763-4636-a768-6eb9ed79b1c6-bluemix.cloudant.com',
        connect=True)
    client.connect()
    db = client['ctrata']
    document = db["perfiles"]
    r = str(cc) in document
    client.disconnect()
    return HttpResponse(r)
def start_historic(request):
    regex_result = re.search(
        "startStream\?csv=([a-z,A-Z,0-9,_-]+)\.csv&json=([a-z,A-Z,0-9,_-]+)\.json&question=([a-z,A-Z,0-9,\+_-]+)",
        request)
    if regex_result is None:
        print("Failed to find specified Files")
        return 0
    else:
        try:
            print("Question: " + regex_result.group(3))
        except:
            print("Failed to find Question asked")
            return 0

    #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['incoming_warning']

    t = str(int(time.time()))
    #jfile = t + ".json"
    #cfile = t + ".csv"
    json_db = {
        u"json-file": regex_result.group(2) + ".json",
        u"csv-file": regex_result.group(1) + ".csv",
        u"question": regex_result.group(3),
        u"Time-in": t
    }

    newDocument = database_json.create_document(json_db)

    client.disconnect()

    return 1
Example #32
0
def start():
    while (1):
        try:
            #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_json = client['incoming_warning']

            new_csv = []
            new_json = []
            new_question = []
            for doc in database_json:
                new_json.append(doc[u'json-file'])
                new_csv.append(doc[u'csv-file'])
                new_question.append(doc[u'question'])
                doc.delete()
            client.disconnect()

            #Run Processing
            if ((len(new_csv) == 1) and (len(new_json) == 1)
                    and (len(new_question) == 1)):
                print("Found new data")
                result = main(new_json[0], new_csv[0], new_question[0])
                save_data(result)
            elif ((len(new_csv) > 1) or (len(new_json) > 1)
                  or (len(new_question) > 1)):
                print("ERROR")
                # This is currently an issue we will fix this later
            else:
                time.sleep(1)
        except KeyboardInterrupt:
            print("done runing")
            return 0
    return 0
Example #33
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()
    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
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)
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()
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,
        "e_description": e_description,
        "j_description": j_description
        }

    print data
    rx = db.create_document(data)
    if rx.exists():
        print "SUCCESS!!"
        

# Disconnect from the server
client.disconnect()
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()
Example #39
0
#     time.sleep(1) #sleep for 10 secs

# while True:
#     result = result_collection[current_count]
#     if len(result) == 0:
#         time.sleep(1)#sleep for 10secs
#     else:
#         print "Fetching doc: ", current_count
#         rating = int(result[0]['doc']['Rating'])
#         print "Publishing rating value: ", rating
#         data = { 'Rating' : rating}
#         deviceCli.publishEvent("Rating", "json", data)
#         current_count += 1

while flag:
    docs_count = feedback_db.doc_count()
    if current_count == docs_count:
        time.sleep(10)
    # elif current_count == flag_count:
    #     print "Final rating"
    else:
        fetch()

# for x in range (0,10):
#   data = { 'Rating' : random.randrange(1,6)}
#   deviceCli.publishEvent("Rating", "json", data)
#   time.sleep(10)

deviceCli.disconnect()
cloudant_client.disconnect()