本文整理匯總了Python中bson.codec_options.CodecOptions方法的典型用法代碼示例。如果您正苦於以下問題:Python codec_options.CodecOptions方法的具體用法?Python codec_options.CodecOptions怎麽用?Python codec_options.CodecOptions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類bson.codec_options
的用法示例。
在下文中一共展示了codec_options.CodecOptions方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def __init__(self, codec_options, read_preference, write_concern,
read_concern):
if not isinstance(codec_options, CodecOptions):
raise TypeError("codec_options must be an instance of "
"bson.codec_options.CodecOptions")
self.__codec_options = codec_options
if not isinstance(read_preference, _ServerMode):
raise TypeError("%r is not valid for read_preference. See "
"pymongo.read_preferences for valid "
"options." % (read_preference,))
self.__read_preference = read_preference
if not isinstance(write_concern, WriteConcern):
raise TypeError("write_concern must be an instance of "
"pymongo.write_concern.WriteConcern")
self.__write_concern = write_concern
if not isinstance(read_concern, ReadConcern):
raise TypeError("read_concern must be an instance of "
"pymongo.read_concern.ReadConcern")
self.__read_concern = read_concern
示例2: run
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def run(self):
try:
self.oplogs['backup'] = Oplog(self.mongodump_oplog['file'], self.do_gzip(), 'a+', self.flush_docs, self.flush_secs)
self.oplogs['tailed'] = Oplog(self.tailed_oplog['file'], self.do_gzip())
logging.info("Resolving oplog for %s to max ts: %s" % (self.uri, self.max_end_ts))
self.state.set('running', True)
self.state.set('first_ts', self.mongodump_oplog['first_ts'])
if not self.state.get('first_ts'):
self.state.set('first_ts', self.tailed_oplog['first_ts'])
for change in decode_file_iter(self.oplogs['tailed'], CodecOptions(unicode_decode_error_handler="ignore")):
self.last_ts = change['ts']
if not self.mongodump_oplog['last_ts'] or self.last_ts > self.mongodump_oplog['last_ts']:
if self.last_ts < self.max_end_ts:
self.oplogs['backup'].add(change)
self.changes += 1
elif self.last_ts > self.max_end_ts:
break
self.state.set('count', self.mongodump_oplog['count'] + self.changes)
self.state.set('last_ts', self.last_ts)
self.state.set('running', False)
self.exit_code = 0
except Exception, e:
raise Error("Resolving of oplogs failed! Error: %s" % e)
示例3: index
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def index():
'''an endpoint to return alert schedules'''
if request.body:
request.body.read()
request.body.close()
response.content_type = "application/json"
mongoclient = MongoClient(options.mongohost, options.mongoport)
schedulers_db = mongoclient.meteor['alertschedules'].with_options(codec_options=CodecOptions(tz_aware=True))
mongodb_alerts = schedulers_db.find()
alert_schedules_dict = {}
for mongodb_alert in mongodb_alerts:
if mongodb_alert['last_run_at']:
mongodb_alert['last_run_at'] = mongodb_alert['last_run_at'].isoformat()
if 'modifiedat' in mongodb_alert:
mongodb_alert['modifiedat'] = mongodb_alert['modifiedat'].isoformat()
alert_schedules_dict[mongodb_alert['name']] = mongodb_alert
response.body = json.dumps(alert_schedules_dict)
response.status = 200
return response
示例4: sync_alert_schedules
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def sync_alert_schedules():
'''an endpoint to return alerts schedules'''
if not request.body:
response.status = 503
return response
alert_schedules = json.loads(request.body.read())
request.body.close()
response.content_type = "application/json"
mongoclient = MongoClient(options.mongohost, options.mongoport)
schedulers_db = mongoclient.meteor['alertschedules'].with_options(codec_options=CodecOptions(tz_aware=True))
results = schedulers_db.find()
for result in results:
if result['name'] in alert_schedules:
new_sched = alert_schedules[result['name']]
result['total_run_count'] = new_sched['total_run_count']
result['last_run_at'] = new_sched['last_run_at']
if result['last_run_at']:
result['last_run_at'] = toUTC(result['last_run_at'])
logger.debug("Inserting schedule for {0} into mongodb".format(result['name']))
schedulers_db.save(result)
response.status = 200
return response
示例5: update_alert_schedules
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def update_alert_schedules():
'''an endpoint to return alerts schedules'''
if not request.body:
response.status = 503
return response
alert_schedules = json.loads(request.body.read())
request.body.close()
response.content_type = "application/json"
mongoclient = MongoClient(options.mongohost, options.mongoport)
schedulers_db = mongoclient.meteor['alertschedules'].with_options(codec_options=CodecOptions(tz_aware=True))
schedulers_db.remove()
for alert_name, alert_schedule in alert_schedules.items():
if alert_schedule['last_run_at']:
alert_schedule['last_run_at'] = toUTC(alert_schedule['last_run_at'])
logger.debug("Inserting schedule for {0} into mongodb".format(alert_name))
schedulers_db.insert(alert_schedule)
response.status = 200
return response
示例6: validate_unicode_decode_error_handler
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def validate_unicode_decode_error_handler(dummy, value):
"""Validate the Unicode decode error handler option of CodecOptions.
"""
if value not in _UNICODE_DECODE_ERROR_HANDLERS:
raise ValueError("%s is an invalid Unicode decode error handler. "
"Must be one of "
"%s" % (value, tuple(_UNICODE_DECODE_ERROR_HANDLERS)))
return value
示例7: codec_options
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def codec_options(self):
"""Read only access to the :class:`~bson.codec_options.CodecOptions`
of this instance.
"""
return self.__codec_options
示例8: with_options
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def with_options(
self, codec_options=None, read_preference=None,
write_concern=None, read_concern=None):
"""Get a clone of this collection changing the specified settings.
>>> coll1.read_preference
Primary()
>>> from pymongo import ReadPreference
>>> coll2 = coll1.with_options(read_preference=ReadPreference.SECONDARY)
>>> coll1.read_preference
Primary()
>>> coll2.read_preference
Secondary(tag_sets=None)
:Parameters:
- `codec_options` (optional): An instance of
:class:`~bson.codec_options.CodecOptions`. If ``None`` (the
default) the :attr:`codec_options` of this :class:`Collection`
is used.
- `read_preference` (optional): The read preference to use. If
``None`` (the default) the :attr:`read_preference` of this
:class:`Collection` is used. See :mod:`~pymongo.read_preferences`
for options.
- `write_concern` (optional): An instance of
:class:`~pymongo.write_concern.WriteConcern`. If ``None`` (the
default) the :attr:`write_concern` of this :class:`Collection`
is used.
- `read_concern` (optional): An instance of
:class:`~pymongo.read_concern.ReadConcern`. If ``None`` (the
default) the :attr:`read_concern` of this :class:`Collection`
is used.
"""
return Collection(self.__database,
self.__name,
False,
codec_options or self.codec_options,
read_preference or self.read_preference,
write_concern or self.write_concern,
read_concern or self.read_concern)
示例9: get_oplog_rs
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def get_oplog_rs(self):
if not self._conn:
self.connect()
db = self._conn['local']
return db.oplog.rs.with_options(codec_options=CodecOptions(unicode_decode_error_handler="ignore"))
示例10: load
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def load(self):
try:
oplog = self.open()
logging.debug("Reading oplog file %s" % self.oplog_file)
for change in decode_file_iter(oplog, CodecOptions(unicode_decode_error_handler="ignore")):
if 'ts' in change:
self._last_ts = change['ts']
if self._first_ts is None and self._last_ts is not None:
self._first_ts = self._last_ts
self._count += 1
oplog.close()
except Exception, e:
logging.fatal("Error reading oplog file %s! Error: %s" % (self.oplog_file, e))
raise OperationError(e)
示例11: _command
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def _command(self, sock_info, command, slave_ok=False,
read_preference=None,
codec_options=None, check=True, allowable_errors=None,
read_concern=DEFAULT_READ_CONCERN):
"""Internal command helper.
:Parameters:
- `sock_info` - A SocketInfo instance.
- `command` - The command itself, as a SON instance.
- `slave_ok`: whether to set the SlaveOkay wire protocol bit.
- `codec_options` (optional) - An instance of
:class:`~bson.codec_options.CodecOptions`.
- `check`: raise OperationFailure if there are errors
- `allowable_errors`: errors to ignore if `check` is True
- `read_concern` (optional) - An instance of
:class:`~pymongo.read_concern.ReadConcern`.
:Returns:
# todo: don't return address
(result document, address of server the command was run on)
"""
return sock_info.command(self.__database.name,
command,
slave_ok,
read_preference or self.read_preference,
codec_options or self.codec_options,
check,
allowable_errors,
read_concern=read_concern)
示例12: _pack_request
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def _pack_request(self, ns, slave_ok):
flags = 4 if slave_ok else 0
request_id, msg_bytes, max_doc_size = message.query(
flags, ns, 0, 0, {}, None, CodecOptions())
# Skip 16-byte standard header.
return msg_bytes[16:], request_id
示例13: encrypt
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def encrypt(self, database, cmd, check_keys, codec_options):
"""Encrypt a MongoDB command.
:Parameters:
- `database`: The database for this command.
- `cmd`: A command document.
- `check_keys`: If True, check `cmd` for invalid keys.
- `codec_options`: The CodecOptions to use while encoding `cmd`.
:Returns:
The encrypted command to execute.
"""
self._check_closed()
# Workaround for $clusterTime which is incompatible with
# check_keys.
cluster_time = check_keys and cmd.pop('$clusterTime', None)
encoded_cmd = _dict_to_bson(cmd, check_keys, codec_options)
max_cmd_size = _MAX_ENC_BSON_SIZE + _COMMAND_OVERHEAD
if len(encoded_cmd) > max_cmd_size:
raise _raise_document_too_large(
next(iter(cmd)), len(encoded_cmd), max_cmd_size)
with _wrap_encryption_errors():
encrypted_cmd = self._auto_encrypter.encrypt(database, encoded_cmd)
# TODO: PYTHON-1922 avoid decoding the encrypted_cmd.
encrypt_cmd = _inflate_bson(
encrypted_cmd, DEFAULT_RAW_BSON_OPTIONS)
if cluster_time:
encrypt_cmd['$clusterTime'] = cluster_time
return encrypt_cmd
示例14: to_object
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def to_object(bson_bytes):
"""Return deserialized object from BSON bytes"""
return bson.BSON(bson_bytes).decode(CodecOptions(document_class=SON,
tz_aware=True))
示例15: list_indexes
# 需要導入模塊: from bson import codec_options [as 別名]
# 或者: from bson.codec_options import CodecOptions [as 別名]
def list_indexes(self):
"""Get a cursor over the index documents for this collection.
>>> for index in db.test.list_indexes():
... print(index)
...
SON([(u'v', 1), (u'key', SON([(u'_id', 1)])),
(u'name', u'_id_'), (u'ns', u'test.test')])
:Returns:
An instance of :class:`~pymongo.command_cursor.CommandCursor`.
.. versionadded:: 3.0
"""
codec_options = CodecOptions(SON)
coll = self.with_options(codec_options)
with self._socket_for_primary_reads() as (sock_info, slave_ok):
cmd = SON([("listIndexes", self.__name), ("cursor", {})])
if sock_info.max_wire_version > 2:
cursor = self._command(sock_info, cmd, slave_ok,
ReadPreference.PRIMARY,
codec_options)["cursor"]
return CommandCursor(coll, cursor, sock_info.address)
else:
namespace = _UJOIN % (self.__database.name, "system.indexes")
res = helpers._first_batch(
sock_info, self.__database.name, "system.indexes",
{"ns": self.__full_name}, 0, slave_ok, codec_options,
ReadPreference.PRIMARY, cmd,
self.database.client._event_listeners)
data = res["data"]
cursor = {
"id": res["cursor_id"],
"firstBatch": data,
"ns": namespace,
}
# Note that a collection can only have 64 indexes, so we don't
# technically have to pass len(data) here. There will never be
# an OP_GET_MORE call.
return CommandCursor(
coll, cursor, sock_info.address, len(data))