def get_default_database(self):
"""Get the database named in the MongoDB connection URI.
.. doctest::
>>> uri = 'mongodb://localhost/my_database'
>>> client = MotorClient(uri)
>>> db = client.get_default_database()
>>> assert db.name == 'my_database'
Useful in scripts where you want to choose which database to use
based only on the URI in a configuration file.
"""
attr_name = mangle_delegate_name(
self.__class__,
'__default_database_name')
default_db_name = getattr(self.delegate, attr_name)
if default_db_name is None:
raise pymongo.errors.ConfigurationError(
'No default database defined')
return self[default_db_name]
python类database()的实例源码
def add_son_manipulator(self, manipulator):
"""Add a new son manipulator to this database.
Newly added manipulators will be applied before existing ones.
:Parameters:
- `manipulator`: the manipulator to add
"""
# We override add_son_manipulator to unwrap the AutoReference's
# database attribute.
if isinstance(manipulator, pymongo.son_manipulator.AutoReference):
db = manipulator.database
if isinstance(db, MotorDatabase):
manipulator.database = db.delegate
self.delegate.add_son_manipulator(manipulator)
def __init__(self, database, collection="fs"):
"""
An instance of GridFS on top of a single Database.
:Parameters:
- `database`: a :class:`MotorDatabase`
- `collection` (optional): A string, name of root collection to use,
such as "fs" or "my_files"
.. mongodoc:: gridfs
.. versionchanged:: 0.2
``open`` method removed; no longer needed.
"""
if not isinstance(database, MotorDatabase):
raise TypeError("First argument to MotorGridFS must be "
"MotorDatabase, not %r" % database)
self.io_loop = database.get_io_loop()
self.collection = database[collection]
self.delegate = self.__delegate_class__(
database.delegate,
collection,
_connect=False)
def get_default_database(self):
"""Get the database named in the MongoDB connection URI.
.. doctest::
>>> uri = 'mongodb://localhost/my_database'
>>> client = MotorClient(uri)
>>> db = client.get_default_database()
>>> assert db.name == 'my_database'
Useful in scripts where you want to choose which database to use
based only on the URI in a configuration file.
"""
attr_name = mangle_delegate_name(
self.__class__,
'__default_database_name')
default_db_name = getattr(self.delegate, attr_name)
if default_db_name is None:
raise pymongo.errors.ConfigurationError(
'No default database defined')
return self[default_db_name]
def add_son_manipulator(self, manipulator):
"""Add a new son manipulator to this database.
Newly added manipulators will be applied before existing ones.
:Parameters:
- `manipulator`: the manipulator to add
"""
# We override add_son_manipulator to unwrap the AutoReference's
# database attribute.
if isinstance(manipulator, pymongo.son_manipulator.AutoReference):
db = manipulator.database
if isinstance(db, MotorDatabase):
manipulator.database = db.delegate
self.delegate.add_son_manipulator(manipulator)
def __init__(self, database, collection="fs"):
"""
An instance of GridFS on top of a single Database.
:Parameters:
- `database`: a :class:`MotorDatabase`
- `collection` (optional): A string, name of root collection to use,
such as "fs" or "my_files"
.. mongodoc:: gridfs
.. versionchanged:: 0.2
``open`` method removed; no longer needed.
"""
if not isinstance(database, MotorDatabase):
raise TypeError("First argument to MotorGridFS must be "
"MotorDatabase, not %r" % database)
self.io_loop = database.get_io_loop()
self.collection = database[collection]
self.delegate = self.__delegate_class__(
database.delegate,
collection,
_connect=False)
def get_default_database(self):
"""Get the database named in the MongoDB connection URI.
.. doctest::
>>> uri = 'mongodb://localhost/my_database'
>>> client = MotorClient(uri)
>>> db = client.get_default_database()
>>> assert db.name == 'my_database'
Useful in scripts where you want to choose which database to use
based only on the URI in a configuration file.
"""
attr_name = mangle_delegate_name(
self.__class__,
'__default_database_name')
default_db_name = getattr(self.delegate, attr_name)
if default_db_name is None:
raise pymongo.errors.ConfigurationError(
'No default database defined')
return self[default_db_name]
def add_son_manipulator(self, manipulator):
"""Add a new son manipulator to this database.
Newly added manipulators will be applied before existing ones.
:Parameters:
- `manipulator`: the manipulator to add
"""
# We override add_son_manipulator to unwrap the AutoReference's
# database attribute.
if isinstance(manipulator, pymongo.son_manipulator.AutoReference):
db = manipulator.database
if isinstance(db, MotorDatabase):
manipulator.database = db.delegate
self.delegate.add_son_manipulator(manipulator)
def __init__(self, database, collection="fs"):
"""
An instance of GridFS on top of a single Database.
:Parameters:
- `database`: a :class:`MotorDatabase`
- `collection` (optional): A string, name of root collection to use,
such as "fs" or "my_files"
.. mongodoc:: gridfs
.. versionchanged:: 0.2
``open`` method removed; no longer needed.
"""
if not isinstance(database, MotorDatabase):
raise TypeError("First argument to MotorGridFS must be "
"MotorDatabase, not %r" % database)
self.io_loop = database.get_io_loop()
self.collection = database[collection]
self.delegate = self.__delegate_class__(
database.delegate,
collection,
_connect=False)
def get_default_database(self):
"""Get the database named in the MongoDB connection URI.
.. doctest::
>>> uri = 'mongodb://localhost/my_database'
>>> client = MotorClient(uri)
>>> db = client.get_default_database()
>>> assert db.name == 'my_database'
Useful in scripts where you want to choose which database to use
based only on the URI in a configuration file.
"""
attr_name = mangle_delegate_name(
self.__class__,
'__default_database_name')
default_db_name = getattr(self.delegate, attr_name)
if default_db_name is None:
raise pymongo.errors.ConfigurationError(
'No default database defined')
return self[default_db_name]
def add_son_manipulator(self, manipulator):
"""Add a new son manipulator to this database.
Newly added manipulators will be applied before existing ones.
:Parameters:
- `manipulator`: the manipulator to add
"""
# We override add_son_manipulator to unwrap the AutoReference's
# database attribute.
if isinstance(manipulator, pymongo.son_manipulator.AutoReference):
db = manipulator.database
if isinstance(db, MotorDatabase):
manipulator.database = db.delegate
self.delegate.add_son_manipulator(manipulator)
def __init__(self, database, collection="fs"):
"""
An instance of GridFS on top of a single Database.
:Parameters:
- `database`: a :class:`MotorDatabase`
- `collection` (optional): A string, name of root collection to use,
such as "fs" or "my_files"
.. mongodoc:: gridfs
.. versionchanged:: 0.2
``open`` method removed; no longer needed.
"""
if not isinstance(database, MotorDatabase):
raise TypeError("First argument to MotorGridFS must be "
"MotorDatabase, not %r" % database)
self.io_loop = database.get_io_loop()
self.collection = database[collection]
self.delegate = self.__delegate_class__(
database.delegate,
collection,
_connect=False)
def finish_startup(self):
self.log.info('Using MongoDB database %r', self.config['MONGO_DBNAME'])
self._config_celery()
api.setup_app(self)
web.setup_app(self)
authentication.setup_app(self)
for ext in self.pillar_extensions.values():
self.log.info('Setting up extension %s', ext.name)
ext.setup_app(self)
self._config_jinja_env()
self._config_static_dirs()
self._config_user_roles()
self._config_user_caps()
# Only enable this when debugging.
# self._list_routes()
def __init__(self, database, name, _delegate=None):
db_class = create_class_with_framework(
SanicMongoAgnosticDatabase, self._framework, self.__module__)
if not isinstance(database, db_class):
raise TypeError("First argument to SanicMongoCollection must be "
"SanicMongoDatabase, not %r" % database)
delegate = _delegate or Collection(database.delegate, name)
super(AgnosticCollection, self).__init__(delegate)
self.database = database
def __getitem__(self, name):
collection_class = create_class_with_framework(
SanicMongoAgnosticCollection, self._framework, self.__module__)
return collection_class(self.database, self.name + '.' + name)
def __getattr__(self, name):
if name.startswith('_'):
# samething. try get from delegate first
try:
ret = getattr(self.delegate, name)
except AttributeError:
raise AttributeError(
"%s has no attribute %r. To access the %s"
" collection, use database['%s']." % (
self.__class__.__name__, name, name,
name))
return ret
return self[name]
def __getattr__(self, name):
if name.startswith('_'):
# the same. Try get from delegate.
try:
ret = getattr(self.delegate, name)
except AttributeError:
raise AttributeError(
"%s has no attribute %r. To access the %s"
" database, use client['%s']." % (
self.__class__.__name__, name, name, name))
return ret
return self[name]
def __init__(self, database, name):
if not isinstance(database, MotorDatabase):
raise TypeError("First argument to MotorCollection must be "
"MotorDatabase, not %r" % database)
delegate = Collection(database.delegate, name)
super(MotorCollection, self).__init__(delegate)
self.database = database
def __getattr__(self, name):
# Dotted collection name, like "foo.bar".
return MotorCollection(self.database, self.name + '.' + name)
def wrap(self, obj):
if obj.__class__ is Collection:
# Replace pymongo.collection.Collection with MotorCollection
return self.database[obj.name]
elif obj.__class__ is Cursor:
return MotorCursor(obj, self)
elif obj.__class__ is CommandCursor:
return MotorCommandCursor(obj, self)
else:
return obj
def __init__(self, database, name):
if not isinstance(database, MotorDatabase):
raise TypeError("First argument to MotorCollection must be "
"MotorDatabase, not %r" % database)
delegate = Collection(database.delegate, name)
super(MotorCollection, self).__init__(delegate)
self.database = database
def __getattr__(self, name):
# Dotted collection name, like "foo.bar".
return MotorCollection(self.database, self.name + '.' + name)
def get_io_loop(self):
return self.database.get_io_loop()
def __init__(self, database, name):
if not isinstance(database, MotorDatabase):
raise TypeError("First argument to MotorCollection must be "
"MotorDatabase, not %r" % database)
delegate = Collection(database.delegate, name)
super(MotorCollection, self).__init__(delegate)
self.database = database
def wrap(self, obj):
if obj.__class__ is Collection:
# Replace pymongo.collection.Collection with MotorCollection
return self.database[obj.name]
elif obj.__class__ is Cursor:
return MotorCursor(obj, self)
elif obj.__class__ is CommandCursor:
return MotorCommandCursor(obj, self)
else:
return obj
def get_io_loop(self):
return self.database.get_io_loop()
def __getattr__(self, name):
# Dotted collection name, like "foo.bar".
return MotorCollection(self.database, self.name + '.' + name)
def wrap(self, obj):
if obj.__class__ is Collection:
# Replace pymongo.collection.Collection with MotorCollection
return self.database[obj.name]
elif obj.__class__ is Cursor:
return MotorCursor(obj, self)
elif obj.__class__ is CommandCursor:
return MotorCommandCursor(obj, self)
else:
return obj
def get_io_loop(self):
return self.database.get_io_loop()
def __init__(self, config, api):
"""
Constructor.
parameters
----------
* config: DBConfig object; specifies database configs.
* api: PyApi object.
"""
self._api = api # Set Datayes PyApi.
if config.body:
try:
self._config = config.body
self._client = config.body['client']
self._dbs = config.body['dbs']
self._dbNames = config.body['dbNames']
self._connected = True
except KeyError:
msg = '[MONGOD]: Unable to configure database; ' + \
'config file is incomplete.'
raise VNPAST_ConfigError(msg)
except Exception,e:
msg = '[MONGOD]: Unable to configure database; ' + str(e)
raise VNPAST_ConfigError(msg)
if self._connected:
#self._get_coll_names()
#self._ensure_index()
pass
#----------------------------------------------------------------------
# Get collection names methods.