示例#1
0
 def db(self):
     conn = Connection(username=self.config.user,
                       password=self.config.password)
     name = self.config.database
     if not conn.hasDatabase(name):
         return conn.createDatabase(name)
     return conn[name]
    def __init__(
            self,
            user='******',
            password='******',
            url='http://127.0.0.1:8529',
            db_name='py2store',
            collection_name='test',
            key_fields=('key', )  # _id, _key and _rev are reserved by db
    ):
        self._connection = Connection(arangoURL=url,
                                      username=user,
                                      password=password)
        self._db_name = db_name
        self._collection_name = collection_name
        # if db not created
        if not self._connection.hasDatabase(self._db_name):
            self._connection.createDatabase(self._db_name)

        self._adb = self._connection[self._db_name]
        # if collection not created
        if not self._adb.hasCollection(self._collection_name):
            self._collection = self._adb.createCollection(
                name=self._collection_name)

        self._collection = self._adb[self._collection_name]

        if isinstance(key_fields, str):
            key_fields = (key_fields, )

        self._key_fields = key_fields
示例#3
0
    def __init__(self):
        conn = Connection(arangoURL=f"http://{DBConfig.host}:{DBConfig.port}",
                          username=DBConfig.User.name,
                          password=DBConfig.User.password)

        try:
            self.database = conn.createDatabase(name=DBConfig.database)
        except CreationError:
            self.database = conn[DBConfig.database]
示例#4
0
class pythonTests(unittest.TestCase):

    def setUp(self):
        ARANGODB_ROOT_USERNAME = os.getenv('ARANGODB_ROOT_USERNAME', 'root')
        ARANGODB_ROOT_PASSWORD = os.getenv('ARANGODB_ROOT_PASSWORD', 'root')
        self.conn = Connection(username=ARANGODB_ROOT_USERNAME, password=ARANGODB_ROOT_PASSWORD)

    def tearDown(self):
        # TODO: disconnect session and delete db
        pass

    def test_create_database(self):
        self.conn.createDatabase(name = "test_db")
        self.db = self.conn["test_db"]
示例#5
0
class pythonTests(unittest.TestCase):
    def setUp(self):
        ARANGODB_ROOT_USERNAME = os.getenv('ARANGODB_ROOT_USERNAME', 'root')
        ARANGODB_ROOT_PASSWORD = os.getenv('ARANGODB_ROOT_PASSWORD', 'root')
        self.conn = Connection(username=ARANGODB_ROOT_USERNAME,
                               password=ARANGODB_ROOT_PASSWORD)

    def tearDown(self):
        # TODO: disconnect session and delete db
        pass

    def test_create_database(self):
        self.conn.createDatabase(name="test_db")
        self.db = self.conn["test_db"]
示例#6
0
def init_db():
    """
    Initialize a connection instance to a running Arango database.
    Optional environment variables that are used here:
        DB_USERNAME
        DB_PASSWORD
        DB_NAME
        DB_URL
    All of the above have fallback values for a local test setup.
    """
    username = os.environ.get('DB_USERNAME', 'root')
    password = os.environ.get('DB_PASSWORD', 'password')
    db_name = os.environ.get('DB_NAME', '_system')
    db_url = os.environ.get('DB_URL', 'http://localhost:8529')
    # Connect to the database
    try:
        conn = Connection(username=username,
                          password=password,
                          arangoURL=db_url)
    except Exception as err:
        sys.stderr.write(str(err) + '\n')
        exit(1)
    finally:
        print('database connection established.')
    return conn[db_name]
示例#7
0
    def __init__(self):
        conn = Connection(username=USERNAME, password=PASSWORD)
        self.db = conn[DB_NAME]

        self.nodes = self.db[self.NODES_COLL]
        self.edges_nodes = self.db[self.EDGENODE_COLL]
        self.meta_relations = self.db[self.METAREL_COLL]
示例#8
0
def _init_db_connection():
    __arango_config = __CONFIG['ArangoDB']
    __arango_conn = Connection(arangoURL=__arango_config['url'],
                               username=__arango_config['user'],
                               password=__arango_config['password'])

    global arango_service
    arango_service = ArangoService(__arango_conn, __arango_config['db'])
示例#9
0
def main():
    """ main function - contains high level program logic"""
    print("[info] Connecting to Arango")
    arango_connection = Connection()

    _, onto_collection, relationships_collection = initialise_database(
        arango_connection)

    # Create documents
    if len(sys.argv) != 2:
        print("[error] You need to supply the name of the .owl file")
        sys.exit(-1)

    print("[info] Loading ontology from {0}".format(sys.argv[1]))
    ontology = pronto.Ontology(sys.argv[1])
    print("[info] Successfully loaded ontology")
    print("[info] Starting to index terms")

    last_percent = 0
    total_terms = len(ontology)

    for index, term in enumerate(ontology):
        # insert that term into arango's document collection
        # insert the term's relationships into arango's graph collection

        to_insert = make_term_entry(term)
        doc = onto_collection.createDocument(to_insert)
        doc['_key'] = term.id
        doc.save()
        make_graph_relations(term, relationships_collection)

        percent = int((index / total_terms) * 100)
        if (percent - last_percent) >= 5:
            print("[info] indexed {0}% of terms".format(percent))
            last_percent = percent

    # Now that we've done that, let's make actually make the graph
    print("[info] Finished indexing terms")
    print("[info] Creating graph: \"{0}\"".format(ONTOLOGY_GRAPH_NAME))

    payload = {
        "name":
        ONTOLOGY_GRAPH_NAME,
        "edgeDefinitions": [{
            "collection": ONTOLOGY_RELATIONSHIPS,
            "from": [ONTOLOGY_COLLECTION],
            "to": [ONTOLOGY_COLLECTION],
        }]
    }

    request_data = bytes(json.dumps(payload), 'UTF-8')
    request = urllib.request.Request(
        url="http://localhost:8529/_db/{0}/_api/gharial".format(DB_NAME),
        data=request_data,
    )
    urllib.request.urlopen(request)

    print("[info] All done!")
示例#10
0
def get_db() -> Database:
    """
    Returns database instance
    """
    return Connection(
        username=os.environ["ARANGO_USER"],
        password=os.environ["ARANGO_ROOT_PASSWORD"],
        arangoURL=f"http://{os.environ['ARANGO_HOST']}:{os.environ['ARANGO_PORT']}",
    )[DB_NAME]
示例#11
0
def main():
    assert ES_INDEX
    assert ES_HOST
    assert ARANGO_URL
    assert ARANGO_ROOT_PASSWORD
    assert ARANGO_COLLECTION
    assert ARANGO_USERNAME

    # ES connection
    es = Elasticsearch([ES_HOST])

    # Arango connection
    conn = Connection(arangoURL=ARANGO_URL, username=ARANGO_USERNAME, password=ARANGO_ROOT_PASSWORD)
    if ES_INDEX not in conn.databases:
        conn.createDatabase(name=ES_INDEX)
    db = conn[ES_INDEX]
    if not db.hasCollection(ARANGO_COLLECTION):
        db.createCollection(name=ARANGO_COLLECTION)

    # Build queries
    existed_patents_total = db.AQLQuery("RETURN LENGTH(Patents)").response['result'][0] or 1000
    existed_patents = db.AQLQuery(
        f"FOR doc IN {ARANGO_COLLECTION} RETURN doc._file",
        batchSize=existed_patents_total
    ).response['result']
    es_query_exclude_existed = {"query": {"bool": {"must_not": [{"ids": {"values": existed_patents}}]}}}
    aql_query_insert = f"INSERT @doc INTO {ARANGO_COLLECTION} LET newDoc = NEW RETURN newDoc"

    # Handle ES pagination
    patents = es.search(index=ES_INDEX, body=es_query_exclude_existed, scroll='1m', size=100)
    scroll_id = patents['_scroll_id']
    scroll_size = len(patents['hits']['hits'])
    while scroll_size > 0:

        # Add patents to Arango
        for hit in patents['hits']['hits']:
            hit['_file'] = hit['_id']
            db.AQLQuery(aql_query_insert, bindVars={'doc': hit})
            logging.info(f"Added: {hit['_file']}")

        # Scroll next batch
        patents = es.scroll(scroll_id=scroll_id, scroll='1m')
        scroll_id = patents['_scroll_id'],
        scroll_size = len(patents['hits']['hits'])
示例#12
0
 def get_connection(self, collection):
     try:
         conn = Connection(arangoURL=self.host,
                           username=self.username,
                           password=self.password)
         db = conn[self.databaseName]
         return db.collections[collection]
     except ConnectionError:
         print("Unable to create connection to the database")
         return None
示例#13
0
    def __init__(self,
                 db_name,
                 auth=False,
                 password='',
                 user='******',
                 arangoURL='http://127.0.0.1:8529'):
        """Init the database conection and set atributes db atribute."""
        if auth:
            self.conn = Connection(username=user,
                                   password=password,
                                   arangoURL=arangoURL)
        else:
            self.conn = Connection(username=user, arangoURL=arangoURL)

        if self.conn.hasDatabase(db_name):
            # conectartla
            self.db = pyArango.database.Database(self.conn, db_name)
            pass
        else:
            self.db = self.conn.createDatabase(name=db_name)
示例#14
0
def _get_db():
    """Return DB & collection
    
    Returns:
        (db, collection)
    """
    conn = Connection(arangoURL='http://localhost:8529',
                      username='******',
                      password='******')

    if not conn.hasDatabase(DB):
        db = conn.createDatabase(DB)
    else:
        db = conn[DB]

    if not db.hasCollection(COLLECTION):
        collection = db.createCollection(name=COLLECTION)
    else:
        collection = db.collections[COLLECTION]

    return db, collection
示例#15
0
 def __init__(self) -> None:
     self.conn = Connection(arangoURL=config.db_host,
                            username=config.db_username,
                            password=config.db_password)
     self.db = self._get_db(config.db_name)
     self.groups: Chats = self._get_collection('Chats')
     self.ab_bio_blacklist: AutobahnBioBlacklist = self._get_collection(
         'AutobahnBioBlacklist')
     self.ab_string_blacklist: AutobahnStringBlacklist = self._get_collection(
         'AutobahnStringBlacklist')
     self.ab_filename_blacklist: AutobahnFilenameBlacklist = self._get_collection(
         'AutobahnFilenameBlacklist')
     self.ab_channel_blacklist: AutobahnChannelBlacklist = self._get_collection(
         'AutobahnChannelBlacklist')
     self.ab_domain_blacklist: AutobahnDomainBlacklist = self._get_collection(
         'AutobahnDomainBlacklist')
     self.ab_collection_map = {
         '0x0': self.ab_bio_blacklist,
         '0x1': self.ab_string_blacklist,
         '0x2': self.ab_filename_blacklist,
         '0x3': self.ab_channel_blacklist,
         '0x4': self.ab_domain_blacklist
     }
     self.banlist: BanList = self._get_collection('BanList')
示例#16
0
def start(uname, passwd):
    conn = Connection(username=uname, password=passwd)
    db = conn["mydrives"]
    filesCollection = db["files"]
    students = [('Oscar', 'Wilde', 3.5), ('Thomas', 'Hobbes', 3.2),
                ('Mark', 'Twain', 3.0), ('Kate', 'Chopin', 3.8),
                ('Fyodor', 'Dostoevsky', 3.1), ('Jane', 'Austen', 3.4),
                ('Mary', 'Wollstonecraft', 3.7), ('Percy', 'Shelley', 3.5),
                ('William', 'Faulkner', 3.8), ('Charlotte', 'Bronte', 3.0)]
    for (first, last, gpa) in students:
        doc = filesCollection.createDocument()
        doc['name'] = "%s %s" % (first, last)
        doc['gpa'] = gpa
        doc['year'] = 2017
        doc._key = ''.join([first, last]).lower()
        try:
            doc.save()
        except CreationError:
            pass
示例#17
0
def benchmark(start_block, end_block, block_interval, compared_times):
    '''
    usage - benchmark for mysqldb and arangodb comparison
    @start_block - block height start
    @end_block - block height end
    @block_interval - block query range
    @compared_times - query times
    @return - {"mysql":[], "arango": [], "pyarango":[]}
    '''

    assert isinstance(start_block, int)
    assert isinstance(end_block, int)
    assert isinstance(block_interval, int)
    assert isinstance(compared_times, int)

    mysql_db = MySQLdb.connect(
        'localhost', dbuser, dbpass, dbname, charset='utf8')

    conn = Connection(username=dbuser, password=dbpass)
    pyarango_db = conn[dbname]

    ret = {"mysql": [], "arango": [], "pyarango": []}
    ret["param"] = {
        "start_block": start_block,
        "end_block": end_block,
        "block_interval": block_interval
    }

    while compared_times > 0:
        s = start_block + random.random() * (
            end_block - start_block - block_interval)
        e = s + block_interval

        s = int(s)
        e = int(e)

        ret["mysql"].append(select_mysql(mysql_db, s, e))
        ret["arango"].append(select_arango(database, s, e))
        ret["pyarango"].append(select_pyarango(pyarango_db, s, e))
        compared_times = compared_times - 1

    return ret
import requests
import os
from datetime import datetime
from datetime import timedelta
import re

import logging
from time import sleep
import multiprocessing
import threading
import time
import os
from pyArango.connection import Connection

_db = Connection(arangoURL='http://35.198.104.86:8529',
                 username='******',
                 password='******')
news_db = _db['news']
_COLLECTION_content = news_db['content']
GLOBAL_BASE_URL = 'https://www.dawn.com/archive/'
GLOBAL_HEADERS = [(
    'User-Agent',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36'
)]


class SleepTimer:
    def __init__(self):
        self.fail = 0
        self.success = 0
示例#19
0
 def setUp(self):
     ARANGODB_ROOT_USERNAME = os.getenv('ARANGODB_ROOT_USERNAME', 'root')
     ARANGODB_ROOT_PASSWORD = os.getenv('ARANGODB_ROOT_PASSWORD', 'root')
     self.conn = Connection(username=ARANGODB_ROOT_USERNAME,
                            password=ARANGODB_ROOT_PASSWORD)
示例#20
0
 def connect(self):
     self._connection: Connection = Connection(
         arangoURL=f'http://{self.config.host}:{self.config.port}',
         username=self.config.username,
         password=self.config.password)
     self.db: Database = self._connection[self.config.db]
示例#21
0
 def setUp(self):
     ARANGODB_ROOT_USERNAME = os.getenv('ARANGODB_ROOT_USERNAME', 'root')
     ARANGODB_ROOT_PASSWORD = os.getenv('ARANGODB_ROOT_PASSWORD', 'root')
     self.conn = Connection(username=ARANGODB_ROOT_USERNAME, password=ARANGODB_ROOT_PASSWORD)
示例#22
0
class ArangoDbPersister(Persister):
    """
    A basic ArangoDB persister.
    >>> from py2store.persisters.arangodb_w_pyarango import ArangoDbPersister
    >>> s = ArangoDbPersister()
    >>> k = {'key': '777'} # Each collection will happily accept user-defined _key values.
    >>> v = {'val': 'bar'}
    >>> for _key in s:
    ...     del s[_key]
    ...
    >>> k in s
    False
    >>> len(s)
    0
    >>> s[k] = v
    >>> len(s)
    1
    >>> s[k]
    {'val': 'bar'}
    >>> s.get(k)
    {'val': 'bar'}
    >>> s.get({'not': 'a key'}, {'default': 'val'})  # testing s.get with default
    {'default': 'val'}
    >>> list(s.values())
    [{'val': 'bar'}]
    >>> k in s  # testing __contains__ again
    True
    >>> del s[k]
    >>> len(s)
    0
    >>> s = ArangoDbPersister(db_name='py2store', key_fields=('name',))
    >>> for _key in s:
    ...     del s[_key]
    ...
    >>> s[{'name': 'guido'}] = {'yob': 1956, 'proj': 'python', 'bdfl': False}
    >>> s[{'name': 'guido'}]
    {'yob': 1956, 'proj': 'python', 'bdfl': False}
    >>> s[{'name': 'vitalik'}] = {'yob': 1994, 'proj': 'ethereum', 'bdfl': True}
    >>> s[{'name': 'vitalik'}]
    {'yob': 1994, 'proj': 'ethereum', 'bdfl': True}
    >>> for key, val in s.items():
    ...     print(f"{key}: {val}")
    {'name': 'guido'}: {'yob': 1956, 'proj': 'python', 'bdfl': False}
    {'name': 'vitalik'}: {'yob': 1994, 'proj': 'ethereum', 'bdfl': True}
    """

    # reserved by the database fields
    _reserved = {"_key", "_id", "_rev"}

    def __init__(
        self,
        user='******',
        password='******',
        url='http://127.0.0.1:8529',
        db_name='py2store',
        collection_name='test',
        key_fields=('key', ),  # _id, _key and _rev are reserved by db
        key_fields_separator='::',
    ):
        self._connection = Connection(
            arangoURL=url,
            username=user,
            password=password,
        )

        self._db_name = db_name
        self._collection_name = collection_name

        # If DB not created:
        if not self._connection.hasDatabase(self._db_name):
            self._connection.createDatabase(self._db_name)

        self._adb = self._connection[self._db_name]

        # If collection not created:
        if not self._adb.hasCollection(self._collection_name):
            self._collection = self._adb.createCollection(
                name=self._collection_name)

        self._collection = self._adb[self._collection_name]

        if isinstance(key_fields, str):
            key_fields = (key_fields, )

        self._key_fields = key_fields
        self._key_fields_separator = key_fields_separator

    def _make_key(self, keys_dict):
        """
        Convert a dict of keys into a real key-string by joining dict values in a predefined order.

        DB requirements for the key:
            The key must be a string value.
            Keys are case-sensitive.
            Numeric keys are not allowed.
            The key must be from 1 byte to 254 bytes long.
            It must consist of:
                - letters a-z (lower or upper case),
                - digits 0-9
                - any of the following characters: _ - : . @ ( ) + , = ; $ ! * ' %

            Any other characters cannot be used inside key values.
        """
        key_values = [keys_dict[key_label] for key_label in self._key_fields]
        key_str = self._key_fields_separator.join(key_values)
        return key_str

    def _split_key(self, joined_key_str):
        """
        Convert a key-string used by DB internally
        into a user-friendly dict of key labels and values.
        """
        key_values = joined_key_str.split(self._key_fields_separator)
        keys_dict = dict(zip(self._key_fields, key_values))
        return keys_dict

    def __fetchitem__(self, keys_dict):
        key = self._make_key(keys_dict)
        try:
            return self._collection[key]
        except DocumentNotFoundError:
            raise KeyError(f"No document found for query: {keys_dict}")

    def __getitem__(self, keys_dict):
        item = self.__fetchitem__(keys_dict)
        doc = item.getStore()

        # todo (Mike): maybe move this cleanup to a base Arango Store?
        # exclude reserved keys and corresponded values
        data = {
            key: doc[key]
            for key in doc
            if key not in self._reserved and key not in self._key_fields
        }
        return data

    def __setitem__(self, keys_dict, values_dict):
        try:
            doc = self.__fetchitem__(keys_dict)
        except KeyError:
            doc = self._collection.createDocument()
            doc._key = self._make_key(keys_dict)

        for k, v in values_dict.items():
            doc[k] = v

        doc.save()

    def __delitem__(self, keys_dict):
        doc = self.__fetchitem__(keys_dict)
        doc.delete()

    def __iter__(self):
        docs = self._collection.fetchAll()

        yield from ({key_name: doc[key_name]
                     for key_name in self._key_fields} for doc in docs)

    def __len__(self):
        return self._collection.count()
示例#23
0
with open(f'{DIR_PATH}/config.json', 'r') as f:
    CONFIG = json.load(f)

SCOPE = [
    'https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive'
]

CREDENTIALS = ServiceAccountCredentials.from_json_keyfile_name(
    f'{DIR_PATH}/credentials.json', SCOPE)
CLIENT = gspread.authorize(CREDENTIALS)
SHEET = CLIENT.open_by_key(CONFIG['google_sheet']['key'])

REVIEW_FIRST_DAY = date(2018, 5, 3)

CONN = Connection(username=CONFIG['db']['username'],
                  password=CONFIG['db']['password'])
if not CONN.hasDatabase('utopian'):
    CONN.createDatabase('utopian')

DB = CONN['utopian']

POSTS_COLLECTION = 'posts'


def connect_collection(db, col_name):
    if not db.hasCollection(name=col_name):
        db.createCollection(name=col_name)
    return db[col_name]


postCol = connect_collection(DB, POSTS_COLLECTION)
示例#24
0
class dbConnector:
    """Connects with ONLY ONE db and perform operation on it."""
    def __init__(self,
                 db_name,
                 auth=False,
                 password='',
                 user='******',
                 arangoURL='http://127.0.0.1:8529'):
        """Init the database conection and set atributes db atribute."""
        if auth:
            self.conn = Connection(username=user,
                                   password=password,
                                   arangoURL=arangoURL)
        else:
            self.conn = Connection(username=user, arangoURL=arangoURL)

        if self.conn.hasDatabase(db_name):
            # conectartla
            self.db = pyArango.database.Database(self.conn, db_name)
            pass
        else:
            self.db = self.conn.createDatabase(name=db_name)
            # Crearla

    def retrieve_collection(self, coll_name):  # doc, bd, coll?
        # Posiblemente innecesario
        """Return the collection in a list form."""
        self.db.reload()
        coll = self.db.collections[coll_name]
        document_list = []
        for document in coll.fetchAll():
            document_list.append(document._store)
        return document_list

    def save_document(self, doc, coll_name):  # doc, bd, coll?
        """
        Save the document in the database.

        It is saved in the collection with the name specified.
        Doc, is in a python dic form.
        """
        self.db.reload()
        if self.db.hasCollection(coll_name):
            coll = self.db.collections[coll_name]
            document = coll.createDocument()
            document._store = doc
            document.save()
        else:
            print('There is no collection with that name')

    def retrieve_document(self, coll_name, doc_name):  # doc, bd, coll?
        """Return the document in a python dic form."""
        self.db.reload()
        # FIXME: Modo cutre de encontrar documentos sin clave
        # Usar AQL quizas
        document_list = self.retrieve_collection(coll_name)
        for doc in document_list:
            if doc[doc_name]:
                return doc
        # doc._store
        pass

    def create_collection(self, coll_name):  # doc, bd, coll?
        """Create and return the collection."""
        self.db.reload()
        if self.db.hasCollection(coll_name):
            print('The database already has a collection with that name')
        else:
            self.db.createCollection(name=coll_name)
示例#25
0
#!/usr/bin/env python

from pyArango.connection import Connection

# connect to the db, the db has been setup via docker with no root password
conn = Connection(username='******')

db = conn['eegleio']

# try creating a 'Users' collection if it doesn't exist
try:
    userCollection = db.createCollection(name='Users')
except Exception as e:
    userCollection = db['Users']

# get name and key for the users that have [email protected] as an email
GET_USER_QUERY = """
    FOR u in Users
        FILTER u.email == "*****@*****.**"
        RETURN {name: u.name, key: u._key}
"""

# create document
document = userCollection.createDocument()
document['name'] = 'titouan'
document['email'] = '*****@*****.**'
document.save()

# execute query
result = db.AQLQuery(GET_USER_QUERY, rawResults=True)
示例#26
0
 def connection(self):  # pylint: disable=no-self-use
     ctx = _app_ctx_stack.top
     if ctx is not None:
         if not hasattr(ctx, 'arango'):
             ctx.arango = Connection(self.app.config['ARANGO_URL'])
         return ctx.arango
示例#27
0
from fuzzywuzzy import fuzz
from xpinyin import Pinyin
from pymongo import MongoClient
from datetime import datetime
from pyArango.connection import Connection
from collections import OrderedDict
import redis
from ConfigError import config, clients

config.dev_address = 102

pinyin = Pinyin()
conR = redis.Redis(host=config.redis_ip,
                   port=config.redis_port,
                   db=config.redis_db_error)
conn = Connection(config.arrangoPath, username='******', password=123456)
db_arango = conn[config.arrangoName]

conMongo = MongoClient(host=config.mongo_path, port=config.mongo_port)
db_mongo = conMongo[config.mongoDB]

correct_db = db_mongo.error_logging
wrong_db = db_mongo.error_fail


class ProofCheck:
    # def __init__(self,thresholds,confusingRedis,stopWord):
    #     self.thresholds=thresholds
    #     self.confusingRedis=confusingRedis
    #     self.stopWord=stopWord
示例#28
0
"""
Manage models in ArangoDB from a common interface.
"""

import os

from pyArango.connection import Connection
from pyArango.theExceptions import DocumentNotFoundError
from flask import current_app as app

AURL = os.getenv('ARANGO_URL', 'http://single-server-int:8529')
CONN = Connection(AURL, 'root', '', verify=False)

def get_database():
    """
    Get the database object
    :return: The database
    """
    if not CONN.hasDatabase('sce'):
        database = CONN.createDatabase('sce')
    else:
        database = CONN['sce']

    return database

def get_connection():
    """
    Get a connection and list of models.
    :return:
    """
    if not CONN.hasDatabase('sce'):
示例#29
0
from pyArango.connection import Connection, CreationError
import csv
import os
import logging
import sys

logging.basicConfig(format='%(asctime)s %(message)s')
logger = logging.getLogger('KurasutaArangoCreationEnrichment')
logger.setLevel(logging.DEBUG if 'DEBUG' in os.environ and os.environ['DEBUG'] else logging.INFO)

logger.debug('Connecting to arango database...')
arango_connection = Connection(
    arangoURL=os.environ['ARANGODB_URL'],
    username=os.environ['ARANGODB_USERNAME'],
    password=os.environ['ARANGODB_PASSWORD']
)
arango_database = arango_connection['kurasuta']

update_count = 0
tasking_count = 0
tasking_skipped = 0
logger.info('Starting...')
with open(sys.argv[1], 'r') as fp:
    for row in csv.reader(fp):
        sha256, completed_at, created_at = row
        if completed_at:
            completed_at = '%s UTC' % completed_at[:19]
        if created_at:
            created_at = '%s UTC' % created_at[:19]
        if completed_at:
            sample = arango_database['sample'][sha256]
示例#30
0
"""Create Connection to database."""
from pyArango.connection import Connection

from . import settings

CONNECTION = Connection(username=settings.USERNAME,
                        password=settings.PASSWORD,
                        arangoURL=settings.CONNECTION_URLS)
class ArangoDbPersister(Persister):
    """
    A basic ArangoDB persister.
    >>> from py2store.persisters._arangodb_in_progress import ArangoDbPersister
    >>> s = ArangoDbPersister()
    >>> k = {'key': '777'} # Each collection will happily accept user-defined _key values.
    >>> v = {'val': 'bar'}
    >>> for _key in s:
    ...     del s[_key]
    ...
    >>> k in s
    False
    >>> len(s)
    0
    >>> s[k] = v
    >>> len(s)
    1
    >>> s[k]
    {'val': 'bar'}
    >>> s.get(k)
    {'val': 'bar'}
    >>> s.get({'not': 'a key'}, {'default': 'val'})  # testing s.get with default
    {'default': 'val'}
    >>> list(s.values())
    [{'val': 'bar'}]
    >>> k in s  # testing __contains__ again
    True
    >>> del s[k]
    >>> len(s)
    0
    >>> s = ArangoDbPersister(db_name='py2store', key_fields=('name',))
    >>> for _key in s:
    ...     del s[_key]
    ...
    >>> s[{'name': 'guido'}] = {'yob': 1956, 'proj': 'python', 'bdfl': False}
    >>> s[{'name': 'guido'}]
    {'yob': 1956, 'proj': 'python', 'bdfl': False}
    >>> s[{'name': 'vitalik'}] = {'yob': 1994, 'proj': 'ethereum', 'bdfl': True}
    >>> s[{'name': 'vitalik'}]
    {'yob': 1994, 'proj': 'ethereum', 'bdfl': True}
    >>> for key, val in s.items():
    ...     print(f"{key}: {val}")
    {'name': 'guido'}: {'yob': 1956, 'proj': 'python', 'bdfl': False}
    {'name': 'vitalik'}: {'yob': 1994, 'proj': 'ethereum', 'bdfl': True}
    """

    # reserved by the database fields
    _reserved = {"_key", "_id", "_rev"}

    def __init__(
            self,
            user='******',
            password='******',
            url='http://127.0.0.1:8529',
            db_name='py2store',
            collection_name='test',
            key_fields=('key', )  # _id, _key and _rev are reserved by db
    ):
        self._connection = Connection(arangoURL=url,
                                      username=user,
                                      password=password)
        self._db_name = db_name
        self._collection_name = collection_name
        # if db not created
        if not self._connection.hasDatabase(self._db_name):
            self._connection.createDatabase(self._db_name)

        self._adb = self._connection[self._db_name]
        # if collection not created
        if not self._adb.hasCollection(self._collection_name):
            self._collection = self._adb.createCollection(
                name=self._collection_name)

        self._collection = self._adb[self._collection_name]

        if isinstance(key_fields, str):
            key_fields = (key_fields, )

        self._key_fields = key_fields

    def __fetchitem__(self, k):
        f = self._collection.fetchFirstExample(k)
        if f is not None and len(f) == 1:
            return f[0]

        return None

    def __getitem__(self, k):
        f = self.__fetchitem__(k)
        if f is not None:
            d = f.getStore()
            # exclude reserved keys and corresponded values
            d = {
                x: d[x]
                for x in d
                if x not in self._reserved and x not in self._key_fields
            }
            return d
        else:
            raise KeyError(f"No document found for query: {k}")

    def __setitem__(self, k, v):
        doc = self._collection.createDocument(dict(k, **v))
        doc.save()

    def __delitem__(self, k):
        if len(k) > 0:
            f = self.__fetchitem__(k)
            if f is not None:
                return f.delete()

        raise KeyError(f"You can't removed that key: {k}")

    def __iter__(self):
        docs = self._collection.fetchAll()

        yield from [{x: d[x]
                     for x in d.getStore() if x in self._key_fields}
                    for d in docs]

    def __len__(self):
        return self._collection.count()
示例#32
0
from pyArango.connection import Connection

conn = Connection(username="******", password="******")
db_name = "logistics"
if not conn.databases.get(db_name, None):
    db = conn.createDatabase(name="logistics")

db_conn = conn[db_name]

if not db_conn.hasCollection("provider"):
    db_conn.createCollection(name="provider")
    # pc = db_conn.createDocument()
    """
    Name, Email, Phone Number, Language, Currency
    # name, email, mobile, language, currency
    """
if not db_conn.hasCollection("service_area"):
    db_conn.createCollection(name="service_area")
    """
    provider id, geojson polygons, polygon name, price
    """