INSTRUCTION stringlengths 1 46.3k | RESPONSE stringlengths 75 80.2k |
|---|---|
Recursive helper method that converts BSON types so they can be
converted into json. | def _json_convert(obj, json_options=DEFAULT_JSON_OPTIONS):
"""Recursive helper method that converts BSON types so they can be
converted into json.
"""
if hasattr(obj, 'iteritems') or hasattr(obj, 'items'): # PY3 support
return SON(((k, _json_convert(v, json_options))
for k, ... |
Decode a JSON datetime to python datetime.datetime. | def _parse_canonical_datetime(doc, json_options):
"""Decode a JSON datetime to python datetime.datetime."""
dtm = doc["$date"]
if len(doc) != 1:
raise TypeError('Bad $date, extra field(s): %s' % (doc,))
# mongoexport 2.6 and newer
if isinstance(dtm, string_type):
# Parse offset
... |
Decode a JSON symbol to Python string. | def _parse_canonical_symbol(doc):
"""Decode a JSON symbol to Python string."""
symbol = doc['$symbol']
if len(doc) != 1:
raise TypeError('Bad $symbol, extra field(s): %s' % (doc,))
return text_type(symbol) |
Decode a JSON code to bson.code.Code. | def _parse_canonical_code(doc):
"""Decode a JSON code to bson.code.Code."""
for key in doc:
if key not in ('$code', '$scope'):
raise TypeError('Bad $code, extra field(s): %s' % (doc,))
return Code(doc['$code'], scope=doc.get('$scope')) |
Decode a JSON regex to bson.regex.Regex. | def _parse_canonical_regex(doc):
"""Decode a JSON regex to bson.regex.Regex."""
regex = doc['$regularExpression']
if len(doc) != 1:
raise TypeError('Bad $regularExpression, extra field(s): %s' % (doc,))
if len(regex) != 2:
raise TypeError('Bad $regularExpression must include only "patter... |
Decode a JSON DBRef to bson.dbref.DBRef. | def _parse_canonical_dbref(doc):
"""Decode a JSON DBRef to bson.dbref.DBRef."""
for key in doc:
if key.startswith('$') and key not in _DBREF_KEYS:
# Other keys start with $, so dct cannot be parsed as a DBRef.
return doc
return DBRef(doc.pop('$ref'), doc.pop('$id'),
... |
Decode a JSON (deprecated) DBPointer to bson.dbref.DBRef. | def _parse_canonical_dbpointer(doc):
"""Decode a JSON (deprecated) DBPointer to bson.dbref.DBRef."""
dbref = doc['$dbPointer']
if len(doc) != 1:
raise TypeError('Bad $dbPointer, extra field(s): %s' % (doc,))
if isinstance(dbref, DBRef):
dbref_doc = dbref.as_doc()
# DBPointer must... |
Decode a JSON int32 to python int. | def _parse_canonical_int32(doc):
"""Decode a JSON int32 to python int."""
i_str = doc['$numberInt']
if len(doc) != 1:
raise TypeError('Bad $numberInt, extra field(s): %s' % (doc,))
if not isinstance(i_str, string_type):
raise TypeError('$numberInt must be string: %s' % (doc,))
return... |
Decode a JSON int64 to bson.int64.Int64. | def _parse_canonical_int64(doc):
"""Decode a JSON int64 to bson.int64.Int64."""
l_str = doc['$numberLong']
if len(doc) != 1:
raise TypeError('Bad $numberLong, extra field(s): %s' % (doc,))
return Int64(l_str) |
Decode a JSON double to python float. | def _parse_canonical_double(doc):
"""Decode a JSON double to python float."""
d_str = doc['$numberDouble']
if len(doc) != 1:
raise TypeError('Bad $numberDouble, extra field(s): %s' % (doc,))
if not isinstance(d_str, string_type):
raise TypeError('$numberDouble must be string: %s' % (doc,... |
Decode a JSON decimal128 to bson.decimal128.Decimal128. | def _parse_canonical_decimal128(doc):
"""Decode a JSON decimal128 to bson.decimal128.Decimal128."""
d_str = doc['$numberDecimal']
if len(doc) != 1:
raise TypeError('Bad $numberDecimal, extra field(s): %s' % (doc,))
if not isinstance(d_str, string_type):
raise TypeError('$numberDecimal mu... |
Decode a JSON MinKey to bson.min_key.MinKey. | def _parse_canonical_minkey(doc):
"""Decode a JSON MinKey to bson.min_key.MinKey."""
if doc['$minKey'] is not 1:
raise TypeError('$minKey value must be 1: %s' % (doc,))
if len(doc) != 1:
raise TypeError('Bad $minKey, extra field(s): %s' % (doc,))
return MinKey() |
Decode a JSON MaxKey to bson.max_key.MaxKey. | def _parse_canonical_maxkey(doc):
"""Decode a JSON MaxKey to bson.max_key.MaxKey."""
if doc['$maxKey'] is not 1:
raise TypeError('$maxKey value must be 1: %s', (doc,))
if len(doc) != 1:
raise TypeError('Bad $minKey, extra field(s): %s' % (doc,))
return MaxKey() |
Parse authentication credentials. | def _parse_credentials(username, password, database, options):
"""Parse authentication credentials."""
mechanism = options.get('authmechanism', 'DEFAULT')
if username is None and mechanism != 'MONGODB-X509':
return None
source = options.get('authsource', database or 'admin')
return _build_cr... |
Parse read preference options. | def _parse_read_preference(options):
"""Parse read preference options."""
if 'read_preference' in options:
return options['read_preference']
name = options.get('readpreference', 'primary')
mode = read_pref_mode_from_name(name)
tags = options.get('readpreferencetags')
max_staleness = opt... |
Parse connection pool options. | def _parse_pool_options(options):
"""Parse connection pool options."""
max_pool_size = options.get('maxpoolsize', common.MAX_POOL_SIZE)
min_pool_size = options.get('minpoolsize', common.MIN_POOL_SIZE)
max_idle_time_ms = options.get('maxidletimems', common.MAX_IDLE_TIME_MS)
if max_pool_size is not No... |
Validate all `service_checks.json` files. | def service_checks():
"""Validate all `service_checks.json` files."""
root = get_root()
echo_info("Validating all service_checks.json files...")
failed_checks = 0
ok_checks = 0
for check_name in sorted(os.listdir(root)):
service_checks_file = os.path.join(root, check_name, 'service_check... |
Convenience wrapper to take a series of date/time elements and return a WMI time
of the form `yyyymmddHHMMSS.mmmmmm+UUU`. All elements may be int, string or
omitted altogether. If omitted, they will be replaced in the output string
by a series of stars of the appropriate length.
:param year: The year el... | def from_time(
year=None, month=None, day=None, hours=None, minutes=None, seconds=None, microseconds=None, timezone=None
):
"""Convenience wrapper to take a series of date/time elements and return a WMI time
of the form `yyyymmddHHMMSS.mmmmmm+UUU`. All elements may be int, string or
omitted altogether. ... |
Convenience wrapper to take a WMI datetime string of the form
yyyymmddHHMMSS.mmmmmm+UUU and return a 9-tuple containing the
individual elements, or None where string contains placeholder
stars.
:param wmi_time: The WMI datetime string in `yyyymmddHHMMSS.mmmmmm+UUU` format
:returns: A 9-tuple of (y... | def to_time(wmi_time):
"""Convenience wrapper to take a WMI datetime string of the form
yyyymmddHHMMSS.mmmmmm+UUU and return a 9-tuple containing the
individual elements, or None where string contains placeholder
stars.
:param wmi_time: The WMI datetime string in `yyyymmddHHMMSS.mmmmmm+UUU` format
... |
Format `tag_query` or raise on incorrect parameters. | def _format_tag_query(self, sampler, wmi_obj, tag_query):
"""
Format `tag_query` or raise on incorrect parameters.
"""
try:
link_source_property = int(wmi_obj[tag_query[0]])
target_class = tag_query[1]
link_target_class_property = tag_query[2]
... |
Design a query based on the given WMIObject to extract a tag.
Returns: tag or TagQueryUniquenessFailure exception. | def _get_tag_query_tag(self, sampler, wmi_obj, tag_query):
"""
Design a query based on the given WMIObject to extract a tag.
Returns: tag or TagQueryUniquenessFailure exception.
"""
self.log.debug(
u"`tag_queries` parameter found."
" wmi_object={wmi_obj} ... |
Extract and tag metrics from the WMISampler.
Raise when multiple WMIObject were returned by the sampler with no `tag_by` specified.
Returns: List of WMIMetric
```
[
WMIMetric("freemegabytes", 19742, ["name:_total"]),
WMIMetric("avgdiskbytesperwrite", 1536, ["nam... | def _extract_metrics(self, wmi_sampler, tag_by, tag_queries, constant_tags):
"""
Extract and tag metrics from the WMISampler.
Raise when multiple WMIObject were returned by the sampler with no `tag_by` specified.
Returns: List of WMIMetric
```
[
WMIMetric("f... |
Resolve metric names and types and submit it. | def _submit_metrics(self, metrics, metric_name_and_type_by_property):
"""
Resolve metric names and types and submit it.
"""
for metric in metrics:
if (
metric.name not in metric_name_and_type_by_property
and metric.name.lower() not in metric_na... |
Return an index key for a given instance. Useful for caching. | def _get_instance_key(self, host, namespace, wmi_class, other=None):
"""
Return an index key for a given instance. Useful for caching.
"""
if other:
return "{host}:{namespace}:{wmi_class}-{other}".format(
host=host, namespace=namespace, wmi_class=wmi_class, ot... |
Create and cache a WMISampler for the given (class, properties) | def _get_wmi_sampler(self, instance_key, wmi_class, properties, tag_by="", **kwargs):
"""
Create and cache a WMISampler for the given (class, properties)
"""
properties = list(properties) + [tag_by] if tag_by else list(properties)
if instance_key not in self.wmi_samplers:
... |
Create and cache a (metric name, metric type) by WMI property map and a property list. | def _get_wmi_properties(self, instance_key, metrics, tag_queries):
"""
Create and cache a (metric name, metric type) by WMI property map and a property list.
"""
if instance_key not in self.wmi_props:
metric_name_by_property = dict(
(wmi_property.lower(), (met... |
Submit a metric as a rate, additional tags provided will be added to
the ones from the label provided via the metrics object.
`custom_tags` is an array of 'tag:value' that will be added to the
metric when sending the rate to Datadog. | def _submit_rate(self, metric_name, val, metric, custom_tags=None, hostname=None):
"""
Submit a metric as a rate, additional tags provided will be added to
the ones from the label provided via the metrics object.
`custom_tags` is an array of 'tag:value' that will be added to the
... |
Submit a metric as a monotonic count, additional tags provided will be added to
the ones from the label provided via the metrics object.
`custom_tags` is an array of 'tag:value' that will be added to the
metric when sending the monotonic count to Datadog. | def _submit_monotonic_count(self, metric_name, val, metric, custom_tags=None, hostname=None):
"""
Submit a metric as a monotonic count, additional tags provided will be added to
the ones from the label provided via the metrics object.
`custom_tags` is an array of 'tag:value' that will b... |
Report all the metrics based on the configuration in instance
If a metric is not well configured or is not present in the payload,
continue processing metrics but log the information to the info page | def parse_expvar_data(self, data, tags, metrics, max_metrics, namespace):
'''
Report all the metrics based on the configuration in instance
If a metric is not well configured or is not present in the payload,
continue processing metrics but log the information to the info page
''... |
Allow to retrieve content nested inside a several layers deep dict/list
Examples: -content: {
"key1": {
"key2" : [
{
"name" : "object1",
... | def deep_get(self, content, keys, traversed_path=None):
'''
Allow to retrieve content nested inside a several layers deep dict/list
Examples: -content: {
"key1": {
"key2" : [
{
... |
Split a host:port string into (host, int(port)) pair. | def partition_node(node):
"""Split a host:port string into (host, int(port)) pair."""
host = node
port = 27017
idx = node.rfind(':')
if idx != -1:
host, port = node[:idx], int(node[idx + 1:])
if host.startswith('['):
host = host[1:-1]
return host, port |
Validates that value is True, False, 'true', or 'false'. | def validate_boolean_or_string(option, value):
"""Validates that value is True, False, 'true', or 'false'."""
if isinstance(value, string_type):
if value not in ('true', 'false'):
raise ValueError("The value of %s must be "
"'true' or 'false'" % (option,))
... |
Validate that 'value' is a positive integer, which does not include 0. | def validate_positive_integer(option, value):
"""Validate that 'value' is a positive integer, which does not include 0.
"""
val = validate_integer(option, value)
if val <= 0:
raise ValueError("The value of %s must be "
"a positive integer" % (option,))
return val |
Validate that 'value' is a positive integer or 0. | def validate_non_negative_integer(option, value):
"""Validate that 'value' is a positive integer or 0.
"""
val = validate_integer(option, value)
if val < 0:
raise ValueError("The value of %s must be "
"a non negative integer" % (option,))
return val |
Validates that 'value' is file-like and readable. | def validate_readable(option, value):
"""Validates that 'value' is file-like and readable.
"""
if value is None:
return value
# First make sure its a string py3.3 open(True, 'r') succeeds
# Used in ssl cert checking due to poor ssl module error reporting
value = validate_string(option, v... |
Validates that 'value' is an instance of `basestring` for Python 2
or `str` for Python 3. | def validate_string(option, value):
"""Validates that 'value' is an instance of `basestring` for Python 2
or `str` for Python 3.
"""
if isinstance(value, string_type):
return value
raise TypeError("Wrong type for %s, value must be "
"an instance of %s" % (option, string_t... |
Validates that 'value' is a float, or can be converted to one, and is
positive. | def validate_positive_float(option, value):
"""Validates that 'value' is a float, or can be converted to one, and is
positive.
"""
errmsg = "%s must be an integer or float" % (option,)
try:
value = float(value)
except ValueError:
raise ValueError(errmsg)
except TypeError:
... |
Validates a timeout specified in milliseconds returning
a value in floating point seconds for the case where None is an error
and 0 is valid. Setting the timeout to nothing in the URI string is a
config error. | def validate_timeout_or_zero(option, value):
"""Validates a timeout specified in milliseconds returning
a value in floating point seconds for the case where None is an error
and 0 is valid. Setting the timeout to nothing in the URI string is a
config error.
"""
if value is None:
raise Co... |
Parse readPreferenceTags if passed as a client kwarg. | def validate_read_preference_tags(name, value):
"""Parse readPreferenceTags if passed as a client kwarg.
"""
if not isinstance(value, list):
value = [value]
tag_sets = []
for tag_set in value:
if tag_set == '':
tag_sets.append({})
continue
try:
... |
Validate the authMechanism URI option. | def validate_auth_mechanism(option, value):
"""Validate the authMechanism URI option.
"""
# CRAM-MD5 is for server testing only. Undocumented,
# unsupported, may be removed at any time. You have
# been warned.
if value not in MECHANISMS and value != 'CRAM-MD5':
raise ValueError("%s must ... |
Validate the uuid representation option selected in the URI. | def validate_uuid_representation(dummy, value):
"""Validate the uuid representation option selected in the URI.
"""
try:
return _UUID_REPRESENTATIONS[value]
except KeyError:
raise ValueError("%s is an invalid UUID representation. "
"Must be one of "
... |
Validate authMechanismProperties. | def validate_auth_mechanism_properties(option, value):
"""Validate authMechanismProperties."""
value = validate_string(option, value)
props = {}
for opt in value.split(','):
try:
key, val = opt.split(':')
except ValueError:
raise ValueError("auth mechanism propert... |
Validate the document_class option. | def validate_document_class(option, value):
"""Validate the document_class option."""
if not issubclass(value, (collections.MutableMapping, RawBSONDocument)):
raise TypeError("%s must be dict, bson.son.SON, "
"bson.raw_bson.RawBSONDocument, or a "
"sublass... |
Validate the appname option. | def validate_appname_or_none(option, value):
"""Validate the appname option."""
if value is None:
return value
validate_string(option, value)
# We need length in bytes, so encode utf8 first.
if len(value.encode('utf-8')) > 128:
raise ValueError("%s must be <= 128 bytes" % (option,))
... |
Validate the type of method arguments that expect a MongoDB document. | def validate_is_document_type(option, value):
"""Validate the type of method arguments that expect a MongoDB document."""
if not isinstance(value, (collections.MutableMapping, RawBSONDocument)):
raise TypeError("%s must be an instance of dict, bson.son.SON, "
"bson.raw_bson.RawBS... |
Validate a replacement document. | def validate_ok_for_replace(replacement):
"""Validate a replacement document."""
validate_is_mapping("replacement", replacement)
# Replacement can be {}
if replacement and not isinstance(replacement, RawBSONDocument):
first = next(iter(replacement))
if first.startswith('$'):
... |
Validate an update document. | def validate_ok_for_update(update):
"""Validate an update document."""
validate_is_mapping("update", update)
# Update can not be {}
if not update:
raise ValueError('update only works with $ operators')
first = next(iter(update))
if not first.startswith('$'):
raise ValueError('upd... |
Validate the Unicode decode error handler option of CodecOptions. | 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 "
... |
Validate the tzinfo option | def validate_tzinfo(dummy, value):
"""Validate the tzinfo option
"""
if value is not None and not isinstance(value, datetime.tzinfo):
raise TypeError("%s must be an instance of datetime.tzinfo" % value)
return value |
Validate optional authentication parameters. | def validate_auth_option(option, value):
"""Validate optional authentication parameters.
"""
lower, value = validate(option, value)
if lower not in _AUTH_OPTIONS:
raise ConfigurationError('Unknown '
'authentication option: %s' % (option,))
return lower, value |
Generic validation function. | def validate(option, value):
"""Generic validation function.
"""
lower = option.lower()
validator = VALIDATORS.get(lower, raise_config_error)
value = validator(option, value)
return lower, value |
Validate each entry in options and raise a warning if it is not valid.
Returns a copy of options with invalid entries removed | def get_validated_options(options, warn=True):
"""Validate each entry in options and raise a warning if it is not valid.
Returns a copy of options with invalid entries removed
"""
validated_options = {}
for opt, value in iteritems(options):
lower = opt.lower()
try:
valida... |
Take from a more recent redis.py, parse_info | def _parse_dict_string(self, string, key, default):
"""Take from a more recent redis.py, parse_info"""
try:
for item in string.split(','):
k, v = item.rsplit('=', 1)
if k == key:
try:
return int(v)
... |
Compute the length of the configured keys across all the databases | def _check_key_lengths(self, conn, instance, tags):
"""
Compute the length of the configured keys across all the databases
"""
key_list = instance.get('keys')
if key_list is None:
return
if not isinstance(key_list, list) or len(key_list) == 0:
se... |
Retrieve length and entries from Redis' SLOWLOG
This will parse through all entries of the SLOWLOG and select ones
within the time range between the last seen entries and now | def _check_slowlog(self, instance, custom_tags):
"""Retrieve length and entries from Redis' SLOWLOG
This will parse through all entries of the SLOWLOG and select ones
within the time range between the last seen entries and now
"""
conn = self._get_conn(instance)
tags =... |
Get command-specific statistics from redis' INFO COMMANDSTATS command | def _check_command_stats(self, conn, tags):
"""Get command-specific statistics from redis' INFO COMMANDSTATS command
"""
try:
command_stats = conn.info("commandstats")
except Exception:
self.warning('Could not retrieve command stats from Redis. INFO COMMANDSTATS o... |
Transform each CoreDNS instance into a OpenMetricsBaseCheck instance | def create_generic_instances(self, instances):
"""
Transform each CoreDNS instance into a OpenMetricsBaseCheck instance
"""
generic_instances = []
for instance in instances:
transformed_instance = self._create_core_dns_instance(instance)
generic_instances.... |
Set up coredns instance so it can be used in OpenMetricsBaseCheck | def _create_core_dns_instance(self, instance):
"""
Set up coredns instance so it can be used in OpenMetricsBaseCheck
"""
endpoint = instance.get('prometheus_url')
if endpoint is None:
raise ConfigurationError("Unable to find prometheus endpoint in config file.")
... |
Removes a project's build artifacts.
If `check` is not specified, the current working directory will be used.
All `*.pyc`/`*.pyd`/`*.pyo`/`*.whl` files and `__pycache__` directories will be
removed. Additionally, the following patterns will be removed from the root of
the path: `.cache`, `.coverage`, ... | def clean(ctx, check, compiled_only, all_matches, force, verbose):
"""Removes a project's build artifacts.
If `check` is not specified, the current working directory will be used.
All `*.pyc`/`*.pyd`/`*.pyo`/`*.whl` files and `__pycache__` directories will be
removed. Additionally, the following patte... |
Hit a given URL and return the parsed json | def get(self, url, instance, service_check_tags, run_check=False):
"Hit a given URL and return the parsed json"
self.log.debug('Fetching CouchDB stats at url: %s' % url)
auth = None
if 'user' in instance and 'password' in instance:
auth = (instance['user'], instance['passwor... |
Convert a SON document to a normal Python dictionary instance.
This is trickier than just *dict(...)* because it needs to be
recursive. | def to_dict(self):
"""Convert a SON document to a normal Python dictionary instance.
This is trickier than just *dict(...)* because it needs to be
recursive.
"""
def transform_value(value):
if isinstance(value, list):
return [transform_value(v) for v... |
Save a set of authentication credentials.
The credentials are used to login a socket whenever one is created.
If `connect` is True, verify the credentials on the server first. | def _cache_credentials(self, source, credentials, connect=False):
"""Save a set of authentication credentials.
The credentials are used to login a socket whenever one is created.
If `connect` is True, verify the credentials on the server first.
"""
# Don't let other threads affe... |
Test if `index` is cached. | def _cached(self, dbname, coll, index):
"""Test if `index` is cached."""
cache = self.__index_cache
now = datetime.datetime.utcnow()
with self.__index_cache_lock:
return (dbname in cache and
coll in cache[dbname] and
index in cache[dbna... |
Purge an index from the index cache.
If `index_name` is None purge an entire collection.
If `collection_name` is None purge an entire database. | def _purge_index(self, database_name,
collection_name=None, index_name=None):
"""Purge an index from the index cache.
If `index_name` is None purge an entire collection.
If `collection_name` is None purge an entire database.
"""
with self.__index_cache_lock... |
An attribute of the current server's description.
If the client is not connected, this will block until a connection is
established or raise ServerSelectionTimeoutError if no server is
available.
Not threadsafe if used multiple times in a single method, since
the server may cha... | def _server_property(self, attr_name):
"""An attribute of the current server's description.
If the client is not connected, this will block until a connection is
established or raise ServerSelectionTimeoutError if no server is
available.
Not threadsafe if used multiple times in... |
(host, port) of the current standalone, primary, or mongos, or None.
Accessing :attr:`address` raises :exc:`~.errors.InvalidOperation` if
the client is load-balancing among mongoses, since there is no single
address. Use :attr:`nodes` instead.
If the client is not connected, this will ... | def address(self):
"""(host, port) of the current standalone, primary, or mongos, or None.
Accessing :attr:`address` raises :exc:`~.errors.InvalidOperation` if
the client is load-balancing among mongoses, since there is no single
address. Use :attr:`nodes` instead.
If the clien... |
Set of all currently connected servers.
.. warning:: When connected to a replica set the value of :attr:`nodes`
can change over time as :class:`MongoClient`'s view of the replica
set changes. :attr:`nodes` can also be an empty set when
:class:`MongoClient` is first instantiated an... | def nodes(self):
"""Set of all currently connected servers.
.. warning:: When connected to a replica set the value of :attr:`nodes`
can change over time as :class:`MongoClient`'s view of the replica
set changes. :attr:`nodes` can also be an empty set when
:class:`MongoClie... |
Attempt to connect to a writable server, or return False. | def _is_writable(self):
"""Attempt to connect to a writable server, or return False.
"""
topology = self._get_topology() # Starts monitors if necessary.
try:
svr = topology.select_server(writable_server_selector)
# When directly connected to a secondary, arbiter... |
DEPRECATED - Set this client's cursor manager.
Raises :class:`TypeError` if `manager_class` is not a subclass of
:class:`~pymongo.cursor_manager.CursorManager`. A cursor manager
handles closing cursors. Different managers can implement different
policies in terms of when to actually kil... | def set_cursor_manager(self, manager_class):
"""DEPRECATED - Set this client's cursor manager.
Raises :class:`TypeError` if `manager_class` is not a subclass of
:class:`~pymongo.cursor_manager.CursorManager`. A cursor manager
handles closing cursors. Different managers can implement dif... |
Send a message to MongoDB and return a Response.
:Parameters:
- `operation`: a _Query or _GetMore object.
- `read_preference` (optional): A ReadPreference.
- `exhaust` (optional): If True, the socket used stays checked out.
It is returned along with its Pool in the Res... | def _send_message_with_response(self, operation, read_preference=None,
exhaust=False, address=None):
"""Send a message to MongoDB and return a Response.
:Parameters:
- `operation`: a _Query or _GetMore object.
- `read_preference` (optional): A Rea... |
Execute an operation. Reset the server on network error.
Returns fn()'s return value on success. On error, clears the server's
pool and marks the server Unknown.
Re-raises any exception thrown by fn(). | def _reset_on_error(self, server, func, *args, **kwargs):
"""Execute an operation. Reset the server on network error.
Returns fn()'s return value on success. On error, clears the server's
pool and marks the server Unknown.
Re-raises any exception thrown by fn().
"""
try... |
Send a kill cursors message soon with the given id.
Raises :class:`TypeError` if `cursor_id` is not an instance of
``(int, long)``. What closing the cursor actually means
depends on this client's cursor manager.
This method may be called from a :class:`~pymongo.cursor.Cursor`
d... | def close_cursor(self, cursor_id, address=None):
"""Send a kill cursors message soon with the given id.
Raises :class:`TypeError` if `cursor_id` is not an instance of
``(int, long)``. What closing the cursor actually means
depends on this client's cursor manager.
This method ma... |
Send a kill cursors message with the given id.
What closing the cursor actually means depends on this client's
cursor manager. If there is none, the cursor is closed synchronously
on the current thread. | def _close_cursor_now(self, cursor_id, address=None):
"""Send a kill cursors message with the given id.
What closing the cursor actually means depends on this client's
cursor manager. If there is none, the cursor is closed synchronously
on the current thread.
"""
if not ... |
DEPRECATED - Send a kill cursors message soon with the given ids.
Raises :class:`TypeError` if `cursor_ids` is not an instance of
``list``.
:Parameters:
- `cursor_ids`: list of cursor ids to kill
- `address` (optional): (host, port) pair of the cursor's server.
... | def kill_cursors(self, cursor_ids, address=None):
"""DEPRECATED - Send a kill cursors message soon with the given ids.
Raises :class:`TypeError` if `cursor_ids` is not an instance of
``list``.
:Parameters:
- `cursor_ids`: list of cursor ids to kill
- `address` (opti... |
Drop a database.
Raises :class:`TypeError` if `name_or_database` is not an instance of
:class:`basestring` (:class:`str` in python 3) or
:class:`~pymongo.database.Database`.
:Parameters:
- `name_or_database`: the name of a database to drop, or a
:class:`~pymongo.d... | def drop_database(self, name_or_database):
"""Drop a database.
Raises :class:`TypeError` if `name_or_database` is not an instance of
:class:`basestring` (:class:`str` in python 3) or
:class:`~pymongo.database.Database`.
:Parameters:
- `name_or_database`: the name of a... |
DEPRECATED - Get the database named in the MongoDB connection URI.
>>> uri = 'mongodb://host/my_database'
>>> client = MongoClient(uri)
>>> db = client.get_default_database()
>>> assert db.name == 'my_database'
>>> db = client.get_database()
>>> assert db.name == 'my_dat... | def get_default_database(self):
"""DEPRECATED - Get the database named in the MongoDB connection URI.
>>> uri = 'mongodb://host/my_database'
>>> client = MongoClient(uri)
>>> db = client.get_default_database()
>>> assert db.name == 'my_database'
>>> db = client.get_datab... |
Get a :class:`~pymongo.database.Database` with the given name and
options.
Useful for creating a :class:`~pymongo.database.Database` with
different codec options, read preference, and/or write concern from
this :class:`MongoClient`.
>>> client.read_preference
Primar... | def get_database(self, name=None, codec_options=None, read_preference=None,
write_concern=None, read_concern=None):
"""Get a :class:`~pymongo.database.Database` with the given name and
options.
Useful for creating a :class:`~pymongo.database.Database` with
different... |
Unlock a previously locked server. | def unlock(self):
"""Unlock a previously locked server.
"""
cmd = {"fsyncUnlock": 1}
with self._socket_for_writes() as sock_info:
if sock_info.max_wire_version >= 4:
try:
sock_info.command("admin", cmd)
except OperationFailu... |
Flush all pending writes to datafiles.
:Parameters:
Optional parameters can be passed as keyword arguments:
- `lock`: If True lock the server to disallow writes.
- `async`: If True don't block while synchronizing.
.. warning:: `async` and `lock` can not be use... | def fsync(self, **kwargs):
"""Flush all pending writes to datafiles.
:Parameters:
Optional parameters can be passed as keyword arguments:
- `lock`: If True lock the server to disallow writes.
- `async`: If True don't block while synchronizing.
.. warni... |
Collect metrics for the given gunicorn instance. | def check(self, instance):
""" Collect metrics for the given gunicorn instance. """
self.log.debug("Running instance: %s", instance)
custom_tags = instance.get('tags', [])
# Validate the config.
if not instance or self.PROC_NAME not in instance:
raise GUnicornCheckEr... |
Return a psutil process for the master gunicorn process with the given name. | def _get_master_proc_by_name(self, name, tags):
""" Return a psutil process for the master gunicorn process with the given name. """
master_name = GUnicornCheck._get_master_proc_name(name)
master_procs = [p for p in psutil.process_iter() if p.cmdline() and p.cmdline()[0] == master_name]
... |
Limits the number of documents returned in one batch. Each batch
requires a round trip to the server. It can be adjusted to optimize
performance and limit data transfer.
.. note:: batch_size can not override MongoDB's internal limits on the
amount of data it will return to the client... | def batch_size(self, batch_size):
"""Limits the number of documents returned in one batch. Each batch
requires a round trip to the server. It can be adjusted to optimize
performance and limit data transfer.
.. note:: batch_size can not override MongoDB's internal limits on the
... |
Send a getmore message and handle the response. | def __send_message(self, operation):
"""Send a getmore message and handle the response.
"""
client = self.__collection.database.client
listeners = client._event_listeners
publish = listeners.enabled_for_commands
try:
response = client._send_message_with_respon... |
Refreshes the cursor with more data from the server.
Returns the length of self.__data after refresh. Will exit early if
self.__data is already non-empty. Raises OperationFailure when the
cursor cannot be refreshed due to an error on the query. | def _refresh(self):
"""Refreshes the cursor with more data from the server.
Returns the length of self.__data after refresh. Will exit early if
self.__data is already non-empty. Raises OperationFailure when the
cursor cannot be refreshed due to an error on the query.
"""
... |
Advance the cursor. | def next(self):
"""Advance the cursor."""
if len(self.__data) or self._refresh():
coll = self.__collection
return coll.database._fix_outgoing(self.__data.popleft(), coll)
else:
raise StopIteration |
Return True for disks we don't want or that match regex in the config file | def _exclude_disk(self, device, file_system, mount_point):
"""
Return True for disks we don't want or that match regex in the config file
"""
self.log.debug('_exclude_disk: {}, {}, {}'.format(device, file_system, mount_point))
if not device or device == 'none':
devic... |
Given raw output for the df command, transform it into a normalized
list devices. A 'device' is a list with fields corresponding to the
output of df output on each platform. | def _list_devices(self, df_output):
"""
Given raw output for the df command, transform it into a normalized
list devices. A 'device' is a list with fields corresponding to the
output of df output on each platform.
"""
all_devices = [l.strip().split() for l in df_output.sp... |
Compile regex strings from device_tag_re option and return list of compiled regex/tag pairs | def _compile_tag_re(self):
"""
Compile regex strings from device_tag_re option and return list of compiled regex/tag pairs
"""
device_tag_list = []
for regex_str, tags in iteritems(self._device_tag_re):
try:
device_tag_list.append([re.compile(regex_str... |
Set a dict mapping (resouce_type --> objects[]) for a given key | def fill(self, key, mor_dict):
"""
Set a dict mapping (resouce_type --> objects[]) for a given key
"""
with self._objects_queue_lock:
self._objects_queue[key] = mor_dict |
Return the size of the queue for a given key and resource type.
If the key is not in the cache, this will raise a KeyError. | def size(self, key, resource_type):
"""
Return the size of the queue for a given key and resource type.
If the key is not in the cache, this will raise a KeyError.
"""
with self._objects_queue_lock:
return len(self._objects_queue[key].get(resource_type, [])) |
Extract an object from the list.
If the key is not in the cache, this will raise a KeyError.
If the list is empty, method will return None | def pop(self, key, resource_type):
"""
Extract an object from the list.
If the key is not in the cache, this will raise a KeyError.
If the list is empty, method will return None
"""
with self._objects_queue_lock:
objects = self._objects_queue[key].get(resource... |
Exact matching of IP addresses.
RFC 6125 explicitly doesn't define an algorithm for this
(section 1.7.2 - "Out of Scope"). | def _ipaddress_match(ipname, host_ip):
"""Exact matching of IP addresses.
RFC 6125 explicitly doesn't define an algorithm for this
(section 1.7.2 - "Out of Scope").
"""
# OpenSSL may add a trailing newline to a subjectAltName's IP address
ip = ip_address(_unicode(ipname).rstrip())
return ip... |
Return a TLS object to establish a secure connection to a server | def _get_tls_object(self, ssl_params):
"""
Return a TLS object to establish a secure connection to a server
"""
if ssl_params is None:
return None
if not ssl_params["verify"] and ssl_params["ca_certs"]:
self.warning(
"Incorrect configurati... |
Parse instance configuration and perform minimal verification | def _get_instance_params(cls, instance):
"""
Parse instance configuration and perform minimal verification
"""
url = instance.get("url")
if url is None:
raise ConfigurationError("You must specify a url for your instance in `conf.yaml`")
username = instance.get... |
Collect metrics from the monitor backend | def _collect_monitor_metrics(self, conn, tags):
"""
Collect metrics from the monitor backend
"""
for entry in conn.entries:
# Get metrics from monitor backend
dn = entry.entry_dn.lower()
if dn.endswith(self.CONNECTIONS_METRICS_DN):
self... |
Perform custom queries to collect additional metrics like number of result and duration of the query | def _perform_custom_queries(self, conn, custom_queries, tags, instance):
"""
Perform custom queries to collect additional metrics like number of result and duration of the query
"""
for query in custom_queries:
name = query.get("name")
if name is None:
... |
extract first common name (cn) from DN that looks like "cn=max file descriptors,cn=connections,cn=monitor" | def _extract_common_name(cls, dn):
"""
extract first common name (cn) from DN that looks like "cn=max file descriptors,cn=connections,cn=monitor"
"""
dn = dn.lower().replace(" ", "_")
return dn.split(",")[0].split("=")[1] |
Create an event object | def _create_event(self, alert_type, msg_title, msg, server, tags=None):
"""
Create an event object
"""
msg_title = 'Couchbase {}: {}'.format(server, msg_title)
msg = 'Couchbase instance {} {}'.format(server, msg)
return {
'timestamp': int(time.time()),
... |
Process and send cluster health data (i.e. cluster membership status and node health | def _process_cluster_health_data(self, node_name, node_stats, tags):
"""
Process and send cluster health data (i.e. cluster membership status and node health
"""
# Tags for service check
cluster_health_tags = list(tags) + ['node:{}'.format(node_name)]
# Get the membersh... |
Hit a given URL and return the parsed json. | def _get_stats(self, url, instance):
"""
Hit a given URL and return the parsed json.
"""
self.log.debug('Fetching Couchbase stats at url: {}'.format(url))
ssl_verify = instance.get('ssl_verify', True)
timeout = float(instance.get('timeout', DEFAULT_TIMEOUT))
au... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.