def mongo_connect(uri, conn_timeout=None, **kwargs):
    conn_timeout_mills = (conn_timeout or CONN_TIMEOUT) * 1000

    kwargs = kwargs or {}
    kwargs["connectTimeoutMS"] = conn_timeout_mills
    kwargs["socketTimeoutMS"] = SOCKET_TIMEOUT * 1000
    # default connection timeout and convert to mills

    uri_wrapper = parse_mongo_uri(uri)

    try:
        dbname = uri_wrapper.database
        if not dbname:
            if uri.endswith("/"):
                uri += "admin"
            else:
                uri += "/admin"

        # add serverSelectionTimeoutMS for pymongo 3.2
        if pymongo.get_version_string().startswith("3.2"):
            kwargs["serverSelectionTimeoutMS"] = conn_timeout_mills

        kwargs["maxPoolSize"] = 1

        mongo_client = _mongo_client(uri, **kwargs)

        return mongo_client

    except Exception, e:
        if is_connection_exception(e):
            raise ConnectionError(uri_wrapper.masked_uri, cause=e)
        elif "authentication failed" in safe_stringify(e):
            raise AuthenticationFailedError(uri_wrapper.masked_uri, cause=e)
        else:
            raise
Example #2
0
def mongo_client(*args, **kwargs):
    """
    wrapper around mongo client
    :param args:
    :param kwargs:
    :return:
    """

    kwargs = kwargs or {}
    connection_timeout_ms = kwargs.get("connectTimeoutMS") or CONN_TIMEOUT_MS

    kwargs.update({
        "socketTimeoutMS": connection_timeout_ms,
        "connectTimeoutMS": connection_timeout_ms,
        "maxPoolSize": 1
    })

    if pymongo.get_version_string().startswith("3.2"):
        if kwargs and kwargs.get("serverSelectionTimeoutMS") is None:
            kwargs["connect"] = True
            kwargs["serverSelectionTimeoutMS"] = connection_timeout_ms

    mongoctl_logging.log_debug("(BEGIN) create MongoClient %s" % args[0])

    return _mongo_client(*args, **kwargs)
Example #3
0
    def __init__(self, spec, host='127.0.0.1', port=27017, buff=1000, id_field=None, updated_field='_updated',
                 upsert=False):
        """
        Constructs a MongoDB output
        :param spec: A string for embedding options, e.g. 'host=example.com,port=80,id=_id,ts=created:updated
        :param host: The MongoDB host, defaults to localhost
        :param port: The MongoDB port, defaults to 27017, the default MongoDB port
        :param buff: The number of documents to accumulate for each DB TX
        :param id_field: The event field that should be used as the ID
        """
        import pymongo
        LOG.warn(pymongo.get_version_string())
        args = spec.split(",", 1)
        self.db_name, self.coll = args[0].split(".")
        opts = utils.to_dict(args[1]) if len(args) > 1 else {}
        self.host = opts.get("host", host)
        self.port = opts.get("port", port)
        self.buffer = opts.get("b", buff)
        self.id_field = opts.get("id", id_field)
        self.updated_field = opts.get("updated", updated_field)
        self.upsert = ('%s' % opts.get("upsert", upsert)).lower() == "true"
        self.pymongo_client = pymongo.MongoClient
        self.pymongo_errors = pymongo.errors

        def do_connect():
            return self.do_connect()

        def do_close():
            return self.do_close(self.conn)

        self.conn = utils.Connector(do_connect, do_close)
        self.conn.connect()
Example #4
0
def tail(client, db, lines, follow, verbose, metadata):
    if verbose:
        fields = None  # All fields
    elif metadata:
        fields = LOG_FIELDS + metadata
    else:
        fields = LOG_FIELDS
    if get_version_string() >= "3.0":
        # Since PyMongo 3.0 fields are passed to `find` function with the parameter `projection`
        cursor = db.system.profile.find(LOG_QUERY, projection=fields)
    else:
        # Until PyMongo 2.8 fields are passed to `find` function with the parameter `fields`
        cursor = db.system.profile.find(LOG_QUERY, fields=fields)
    if lines.upper() != "ALL":
        try:
            skip = cursor.count() - int(lines)
        except ValueError:
            error_parsing('Invalid lines number "%s"' % lines)
        if skip > 0:
            cursor.skip(skip)
    if follow:
        cursor.add_option(2)  # Set the tailable flag
    while cursor.alive:
        try:
            result = next(cursor)
            print_obj(result, verbose, metadata,
                      client.server_info()['version'])
        except StopIteration:
            pass
def mongo_connect(uri, conn_timeout=None, **kwargs):
    conn_timeout_mills = (conn_timeout or CONN_TIMEOUT) * 1000

    kwargs = kwargs or {}
    kwargs["connectTimeoutMS"] = conn_timeout_mills
    kwargs["socketTimeoutMS"] = SOCKET_TIMEOUT * 1000
    # default connection timeout and convert to mills

    uri_wrapper = parse_mongo_uri(uri)

    try:
        dbname = uri_wrapper.database
        if not dbname:
            if uri.endswith("/"):
                uri += "admin"
            else:
                uri += "/admin"

        # add serverSelectionTimeoutMS for pymongo 3.2
        if pymongo.get_version_string().startswith("3.2"):
            kwargs["serverSelectionTimeoutMS"] = conn_timeout_mills

        kwargs["maxPoolSize"] = 1

        mongo_client = _mongo_client(uri, **kwargs)

        return mongo_client

    except Exception, e:
        if is_connection_exception(e):
            raise ConnectionError(uri_wrapper.masked_uri, cause=e)
        elif "authentication failed" in safe_stringify(e):
            raise AuthenticationFailedError(uri_wrapper.masked_uri, cause=e)
        else:
            raise
Example #6
0
 def connect(self, root, port, db_name):
     try:
         if int(pymongo.get_version_string()[0]) >= 3:
             connection = pymongo.MongoClient(root, port)
         else:
             connection = pymongo.Connection(root, port)
         self.db = connection[db_name]
     except pymongo.errors, e:
         print e
Example #7
0
 def connect(self, root, port, db_name):
     try:
         if int(pymongo.get_version_string()[0]) >= 3:
             connection = pymongo.MongoClient(root, port)
         else:
             connection = pymongo.Connection(root, port)
         self.db = connection[db_name]
     except pymongo.errors, e:
         print e
 def mongo_client(self):
     if not self._mongo_client:
         # default connection timeout and convert to mills
         conn_timeout_mills = self.conn_timeout * 1000
         kwargs = {
             "socketTimeoutMS": conn_timeout_mills,
             "connectTimeoutMS": conn_timeout_mills
         }
         # add slaveOk for older pymongo versions
         if pymongo.get_version_string().startswith("2"):
             kwargs["slaveOk"] = True
         self._mongo_client = mongo_connect(self.uri, **kwargs)
     return self._mongo_client
 def mongo_client(self):
     if not self._mongo_client:
         # default connection timeout and convert to mills
         conn_timeout_mills = self.conn_timeout * 1000
         kwargs = {
             "socketTimeoutMS": conn_timeout_mills,
             "connectTimeoutMS": conn_timeout_mills
         }
         # add slaveOk for older pymongo versions
         if pymongo.get_version_string().startswith("2"):
             kwargs["slaveOk"] = True
         self._mongo_client = mongo_connect(self.uri, **kwargs)
     return self._mongo_client
Example #10
0
def _db_repo_connect():
    db_conf = config.get_database_repository_conf()
    uri = db_conf["databaseURI"]
    client_args = {
        "read_preference":
        pymongo.read_preferences.ReadPreference.PRIMARY_PREFERRED
    }

    if pymongo.get_version_string().startswith("3.2"):
        ## TODO XXX MAYBE? This makes things much slower
        client_args["serverSelectionTimeoutMS"] = 1

    client = pymongo.MongoClient(uri, **client_args)

    return client
Example #11
0
def main():
    print(pymongo.get_version_string())

    dbc = DatabaseMongo('localhost', 27017)

    dbc.add_note('note', {'name': 'hangout', 'desc': 'Just hang out'})
    mod_id = dbc.add_note('NoteDate', {
        'name': 'Remember!',
        'text': 'Eat shower',
        'date': '12018'
    })
    dbc.add_note('check', {'name': 'Eat', 'what': 'a grue'})

    for nt in dbc.get_all_notes():
        print(nt)
    print()

    note = note_builder.construct_note(dbc.get_note(mod_id))
    note.set_text('Order a rift')
    note.set_date('27.05.2018')
    note_writer.write_note(dbc, note)

    notes = [
        NoteSimple(text='Walk the cat.'),
        NoteSimple(text='Feed the dog.'),
        NoteDate(text='Receive the rift', date='30.05.2018'),
        NoteSimple(text='Pet the parrot.'),
    ]

    for note in notes:
        note_writer.write_note(dbc, note)

    for nt in dbc.get_all_notes():
        print(nt)
    print()

    notes = []
    note_data = dbc.get_all_notes()
    for data in note_data:
        note = note_builder.construct_note(data)
        if note:
            notes.append(note)

    for note in notes:
        print(note)

    dbc.drop_database()
Example #12
0
def tail(client, db, lines, follow):
    if get_version_string() >= "3.0":
        # From PyMongo 3.0 fields are passed to `find` function with the parameter `projection`
        cursor = db.system.profile.find(LOG_QUERY, projection=LOG_FIELDS)
    else:
        # From PyMongo <= 2.8 fields are passed to `find` function with the parameter `fields`
        cursor = db.system.profile.find(LOG_QUERY, fields=LOG_FIELDS)
    if lines.upper() != "ALL":
        try:
            skip = cursor.count() - int(lines)
        except ValueError:
            error_parsing('Invalid lines number "%s"' % lines)
        if skip > 0:
            cursor.skip(skip)
    if follow:
        cursor.add_option(2)  # Set the tailable flag
    while cursor.alive:
        try:
            result = next(cursor)
            print_obj(result)
        except StopIteration:
            pass
Example #13
0
def tail(client, db, lines, follow):
    if get_version_string() >= "3.0":
        # From PyMongo 3.0 fields are passed to `find` function with the parameter `projection`
        cursor = db.system.profile.find(LOG_QUERY, projection=LOG_FIELDS)
    else:
        # From PyMongo <= 2.8 fields are passed to `find` function with the parameter `fields`
        cursor = db.system.profile.find(LOG_QUERY, fields=LOG_FIELDS)
    if lines.upper() != "ALL":
        try:
            skip = cursor.count() - int(lines)
        except ValueError:
            error_parsing('Invalid lines number "%s"' % lines)
        if skip > 0:
            cursor.skip(skip)
    if follow:
        cursor.add_option(2)  # Set the tailable flag
    while cursor.alive:
        try:
            result = next(cursor)
            print_obj(result)
        except StopIteration:
            pass
Example #14
0
def mongo_client(*args, **kwargs):
    """
    wrapper around mongo client
    :param args:
    :param kwargs:
    :return:
    """

    kwargs = kwargs or {}
    connection_timeout_ms = kwargs.get("connectTimeoutMS") or CONN_TIMEOUT_MS

    kwargs.update({
        "socketTimeoutMS": connection_timeout_ms,
        "connectTimeoutMS": connection_timeout_ms,
        "maxPoolSize": 1
    })

    if pymongo.get_version_string().startswith("3.2"):
        if kwargs and kwargs.get("serverSelectionTimeoutMS") is None:
            kwargs["connect"] = True
            kwargs["serverSelectionTimeoutMS"] = connection_timeout_ms

    return _mongo_client(*args, **kwargs)
Example #15
0
import pymongo
from bson.son import SON
from pymongo import MongoClient

# encoding=utf-8
__author__ = 'Hinsteny'

print(pymongo.get_version_string())


class SingleClient(object):
    '''
    Single Client hold the client object
    '''

    client = MongoClient('127.0.0.1', 27017)
    client.the_database.authenticate('hinsteny', 'welcome', source='admin', mechanism='SCRAM-SHA-1')

    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            orig = super(SingleClient, cls)
            cls._instance = orig.__new__(cls, *args, **kw)
        return cls._instance

def getClient():
    client = MongoClient('127.0.0.1', 27017)
    client.the_database.authenticate('hinsteny', 'welcome', source='admin', mechanism='SCRAM-SHA-1')
    return client

def test_connection():
    client = getClient()
            return None
        return self.client[self.db][self.table]

    def getClient(self):
        return self.client

    def close(self):
        return self.client.close()


def getInstance(table, db=None, ip=None, ds=None, port=None):
    return Mongo(table, db, ds, ip, port)


if __name__ == "__main__":
    print(pymongo.get_version_string())
    client = pymongo.MongoClient("192.168.7.216", 27017)
    db = client["echartdb"]
    collection = db["echart_collection"]
    print(collection.find().count())
    # collection.insert({"A":"aa","B":"bb"})
    # collection.insert({"A":"aa"})
    # collection.insert({"A1":"aa1","B1":"bb1"})
    # collection.insert({"C1":"cc1","B1":"bb1"})
    # collection.insert({"C1":"cc1","A1":"aa1"})
    # collection.insert({"C1":"cc1","A1":"aa"})
    # collection.insert({"C1":"cc1","A":"aa"})
    print(db.collection_names())
    print(str(collection.find()))
    print(str(collection.find().count()))
    print(str(collection.find({"A": "aa"})))
Example #17
0
 def __init__(self):
     self.client = pymongo.MongoClient(host="localhost", port=27017)
     self.db = self.client["newster"]
     self.col = self.db["news"]
     print("Connecting to MongoDB : ", pymongo.get_version_string())
Example #18
0
# A global config that is set through --use-alt-address option that will use
# a different "address" property of when making connections to servers
USE_ALT_ADDRESS = None

###############################################################################
VERSION_2_6 = make_version_info("2.6.0")
VERSION_3_0 = make_version_info("3.0.0")

###############################################################################
DEFAULT_CLIENT_OPTIONS = {
    "socketTimeoutMS": CONN_TIMEOUT,
    "connectTimeoutMS": CONN_TIMEOUT
}

if pymongo.get_version_string().startswith("3.2"):
    ## TODO XXX MAYBE? This makes things much slower
    DEFAULT_CLIENT_OPTIONS["serverSelectionTimeoutMS"] = 1


###############################################################################
class ClientSslMode(object):
    DISABLED = "disabled"
    ALLOW = "allow"
    REQUIRE = "require"
    PREFER = "prefer"


###############################################################################
# Server Class
###############################################################################
Example #19
0
# A global config that is set through --use-alt-address option that will use
# a different "address" property of when making connections to servers
USE_ALT_ADDRESS = None


###############################################################################
VERSION_2_6 = make_version_info("2.6.0")
VERSION_3_0 = make_version_info("3.0.0")

###############################################################################
DEFAULT_CLIENT_OPTIONS = {
    "socketTimeoutMS": CONN_TIMEOUT,
    "connectTimeoutMS": CONN_TIMEOUT
}

if pymongo.get_version_string().startswith("3.2"):
    ## TODO XXX MAYBE? This makes things much slower
    DEFAULT_CLIENT_OPTIONS["serverSelectionTimeoutMS"] = 1

###############################################################################
class ClientSslMode(object):
    DISABLED = "disabled"
    ALLOW = "allow"
    REQUIRE = "require"
    PREFER = "prefer"

###############################################################################
# Server Class
###############################################################################

class Server(DocumentWrapper):