sensu-plugins-mongodb-mrtrotl 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1 -0
- data/LICENSE +22 -0
- data/README.md +27 -0
- data/bin/check-mongodb-metric.rb +144 -0
- data/bin/check-mongodb-query-count.rb +267 -0
- data/bin/check-mongodb.py +1644 -0
- data/bin/check-mongodb.rb +5 -0
- data/bin/metrics-mongodb-replication.rb +254 -0
- data/bin/metrics-mongodb.rb +133 -0
- data/lib/bson/__init__.py +1347 -0
- data/lib/bson/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/_helpers.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/binary.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/code.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/codec_options.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/dbref.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/decimal128.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/int64.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/json_util.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/max_key.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/min_key.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/objectid.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/raw_bson.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/regex.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/son.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/timestamp.cpython-310.pyc +0 -0
- data/lib/bson/__pycache__/tz_util.cpython-310.pyc +0 -0
- data/lib/bson/_cbson.cpython-310-x86_64-linux-gnu.so +0 -0
- data/lib/bson/_helpers.py +41 -0
- data/lib/bson/binary.py +364 -0
- data/lib/bson/code.py +101 -0
- data/lib/bson/codec_options.py +414 -0
- data/lib/bson/codec_options.pyi +100 -0
- data/lib/bson/dbref.py +133 -0
- data/lib/bson/decimal128.py +314 -0
- data/lib/bson/errors.py +35 -0
- data/lib/bson/int64.py +39 -0
- data/lib/bson/json_util.py +874 -0
- data/lib/bson/max_key.py +55 -0
- data/lib/bson/min_key.py +55 -0
- data/lib/bson/objectid.py +286 -0
- data/lib/bson/py.typed +2 -0
- data/lib/bson/raw_bson.py +175 -0
- data/lib/bson/regex.py +135 -0
- data/lib/bson/son.py +208 -0
- data/lib/bson/timestamp.py +124 -0
- data/lib/bson/tz_util.py +52 -0
- data/lib/gridfs/__init__.py +1015 -0
- data/lib/gridfs/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/gridfs/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/gridfs/__pycache__/grid_file.cpython-310.pyc +0 -0
- data/lib/gridfs/errors.py +33 -0
- data/lib/gridfs/grid_file.py +907 -0
- data/lib/gridfs/py.typed +2 -0
- data/lib/pymongo/__init__.py +185 -0
- data/lib/pymongo/__pycache__/__init__.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/_csot.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/aggregation.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/auth.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/auth_aws.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/bulk.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/change_stream.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/client_options.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/client_session.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/collation.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/collection.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/command_cursor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/common.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/compression_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/cursor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/daemon.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/database.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/driver_info.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/encryption.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/encryption_options.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/errors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/event_loggers.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/hello.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/helpers.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/max_staleness_selectors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/message.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/mongo_client.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/monitor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/monitoring.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/network.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ocsp_cache.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ocsp_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/operations.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/periodic_executor.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/pool.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/pyopenssl_context.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/read_concern.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/read_preferences.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/response.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/results.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/saslprep.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_api.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_description.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_selectors.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/server_type.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/settings.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/socket_checker.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/srv_resolver.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ssl_context.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/ssl_support.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/topology.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/topology_description.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/typings.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/uri_parser.cpython-310.pyc +0 -0
- data/lib/pymongo/__pycache__/write_concern.cpython-310.pyc +0 -0
- data/lib/pymongo/_cmessage.cpython-310-x86_64-linux-gnu.so +0 -0
- data/lib/pymongo/_csot.py +118 -0
- data/lib/pymongo/aggregation.py +229 -0
- data/lib/pymongo/auth.py +549 -0
- data/lib/pymongo/auth_aws.py +94 -0
- data/lib/pymongo/bulk.py +513 -0
- data/lib/pymongo/change_stream.py +457 -0
- data/lib/pymongo/client_options.py +302 -0
- data/lib/pymongo/client_session.py +1112 -0
- data/lib/pymongo/collation.py +224 -0
- data/lib/pymongo/collection.py +3204 -0
- data/lib/pymongo/command_cursor.py +353 -0
- data/lib/pymongo/common.py +984 -0
- data/lib/pymongo/compression_support.py +149 -0
- data/lib/pymongo/cursor.py +1345 -0
- data/lib/pymongo/daemon.py +141 -0
- data/lib/pymongo/database.py +1202 -0
- data/lib/pymongo/driver_info.py +42 -0
- data/lib/pymongo/encryption.py +884 -0
- data/lib/pymongo/encryption_options.py +221 -0
- data/lib/pymongo/errors.py +365 -0
- data/lib/pymongo/event_loggers.py +221 -0
- data/lib/pymongo/hello.py +219 -0
- data/lib/pymongo/helpers.py +259 -0
- data/lib/pymongo/max_staleness_selectors.py +114 -0
- data/lib/pymongo/message.py +1440 -0
- data/lib/pymongo/mongo_client.py +2144 -0
- data/lib/pymongo/monitor.py +440 -0
- data/lib/pymongo/monitoring.py +1801 -0
- data/lib/pymongo/network.py +311 -0
- data/lib/pymongo/ocsp_cache.py +87 -0
- data/lib/pymongo/ocsp_support.py +372 -0
- data/lib/pymongo/operations.py +507 -0
- data/lib/pymongo/periodic_executor.py +183 -0
- data/lib/pymongo/pool.py +1660 -0
- data/lib/pymongo/py.typed +2 -0
- data/lib/pymongo/pyopenssl_context.py +383 -0
- data/lib/pymongo/read_concern.py +75 -0
- data/lib/pymongo/read_preferences.py +609 -0
- data/lib/pymongo/response.py +109 -0
- data/lib/pymongo/results.py +217 -0
- data/lib/pymongo/saslprep.py +113 -0
- data/lib/pymongo/server.py +247 -0
- data/lib/pymongo/server_api.py +170 -0
- data/lib/pymongo/server_description.py +285 -0
- data/lib/pymongo/server_selectors.py +153 -0
- data/lib/pymongo/server_type.py +32 -0
- data/lib/pymongo/settings.py +159 -0
- data/lib/pymongo/socket_checker.py +104 -0
- data/lib/pymongo/srv_resolver.py +126 -0
- data/lib/pymongo/ssl_context.py +39 -0
- data/lib/pymongo/ssl_support.py +99 -0
- data/lib/pymongo/topology.py +890 -0
- data/lib/pymongo/topology_description.py +639 -0
- data/lib/pymongo/typings.py +39 -0
- data/lib/pymongo/uri_parser.py +624 -0
- data/lib/pymongo/write_concern.py +129 -0
- data/lib/pymongo-4.2.0.dist-info/INSTALLER +1 -0
- data/lib/pymongo-4.2.0.dist-info/LICENSE +201 -0
- data/lib/pymongo-4.2.0.dist-info/METADATA +250 -0
- data/lib/pymongo-4.2.0.dist-info/RECORD +167 -0
- data/lib/pymongo-4.2.0.dist-info/REQUESTED +0 -0
- data/lib/pymongo-4.2.0.dist-info/WHEEL +6 -0
- data/lib/pymongo-4.2.0.dist-info/top_level.txt +3 -0
- data/lib/sensu-plugins-mongodb/metrics.rb +391 -0
- data/lib/sensu-plugins-mongodb/version.rb +9 -0
- data/lib/sensu-plugins-mongodb.rb +1 -0
- metadata +407 -0
@@ -0,0 +1,314 @@
|
|
1
|
+
# Copyright 2016-present MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""Tools for working with the BSON decimal128 type.
|
16
|
+
|
17
|
+
.. versionadded:: 3.4
|
18
|
+
"""
|
19
|
+
|
20
|
+
import decimal
|
21
|
+
import struct
|
22
|
+
from typing import Any, Sequence, Tuple, Type, Union
|
23
|
+
|
24
|
+
_PACK_64 = struct.Struct("<Q").pack
|
25
|
+
_UNPACK_64 = struct.Struct("<Q").unpack
|
26
|
+
|
27
|
+
_EXPONENT_MASK = 3 << 61
|
28
|
+
_EXPONENT_BIAS = 6176
|
29
|
+
_EXPONENT_MAX = 6144
|
30
|
+
_EXPONENT_MIN = -6143
|
31
|
+
_MAX_DIGITS = 34
|
32
|
+
|
33
|
+
_INF = 0x7800000000000000
|
34
|
+
_NAN = 0x7C00000000000000
|
35
|
+
_SNAN = 0x7E00000000000000
|
36
|
+
_SIGN = 0x8000000000000000
|
37
|
+
|
38
|
+
_NINF = (_INF + _SIGN, 0)
|
39
|
+
_PINF = (_INF, 0)
|
40
|
+
_NNAN = (_NAN + _SIGN, 0)
|
41
|
+
_PNAN = (_NAN, 0)
|
42
|
+
_NSNAN = (_SNAN + _SIGN, 0)
|
43
|
+
_PSNAN = (_SNAN, 0)
|
44
|
+
|
45
|
+
_CTX_OPTIONS = {
|
46
|
+
"prec": _MAX_DIGITS,
|
47
|
+
"rounding": decimal.ROUND_HALF_EVEN,
|
48
|
+
"Emin": _EXPONENT_MIN,
|
49
|
+
"Emax": _EXPONENT_MAX,
|
50
|
+
"capitals": 1,
|
51
|
+
"flags": [],
|
52
|
+
"traps": [decimal.InvalidOperation, decimal.Overflow, decimal.Inexact],
|
53
|
+
"clamp": 1,
|
54
|
+
}
|
55
|
+
|
56
|
+
_DEC128_CTX = decimal.Context(**_CTX_OPTIONS.copy()) # type: ignore
|
57
|
+
_VALUE_OPTIONS = Union[decimal.Decimal, float, str, Tuple[int, Sequence[int], int]]
|
58
|
+
|
59
|
+
|
60
|
+
def create_decimal128_context() -> decimal.Context:
|
61
|
+
"""Returns an instance of :class:`decimal.Context` appropriate
|
62
|
+
for working with IEEE-754 128-bit decimal floating point values.
|
63
|
+
"""
|
64
|
+
opts = _CTX_OPTIONS.copy()
|
65
|
+
opts["traps"] = []
|
66
|
+
return decimal.Context(**opts) # type: ignore
|
67
|
+
|
68
|
+
|
69
|
+
def _decimal_to_128(value: _VALUE_OPTIONS) -> Tuple[int, int]:
|
70
|
+
"""Converts a decimal.Decimal to BID (high bits, low bits).
|
71
|
+
|
72
|
+
:Parameters:
|
73
|
+
- `value`: An instance of decimal.Decimal
|
74
|
+
"""
|
75
|
+
with decimal.localcontext(_DEC128_CTX) as ctx:
|
76
|
+
value = ctx.create_decimal(value)
|
77
|
+
|
78
|
+
if value.is_infinite():
|
79
|
+
return _NINF if value.is_signed() else _PINF
|
80
|
+
|
81
|
+
sign, digits, exponent = value.as_tuple()
|
82
|
+
|
83
|
+
if value.is_nan():
|
84
|
+
if digits:
|
85
|
+
raise ValueError("NaN with debug payload is not supported")
|
86
|
+
if value.is_snan():
|
87
|
+
return _NSNAN if value.is_signed() else _PSNAN
|
88
|
+
return _NNAN if value.is_signed() else _PNAN
|
89
|
+
|
90
|
+
significand = int("".join([str(digit) for digit in digits]))
|
91
|
+
bit_length = significand.bit_length()
|
92
|
+
|
93
|
+
high = 0
|
94
|
+
low = 0
|
95
|
+
for i in range(min(64, bit_length)):
|
96
|
+
if significand & (1 << i):
|
97
|
+
low |= 1 << i
|
98
|
+
|
99
|
+
for i in range(64, bit_length):
|
100
|
+
if significand & (1 << i):
|
101
|
+
high |= 1 << (i - 64)
|
102
|
+
|
103
|
+
biased_exponent = exponent + _EXPONENT_BIAS
|
104
|
+
|
105
|
+
if high >> 49 == 1:
|
106
|
+
high = high & 0x7FFFFFFFFFFF
|
107
|
+
high |= _EXPONENT_MASK
|
108
|
+
high |= (biased_exponent & 0x3FFF) << 47
|
109
|
+
else:
|
110
|
+
high |= biased_exponent << 49
|
111
|
+
|
112
|
+
if sign:
|
113
|
+
high |= _SIGN
|
114
|
+
|
115
|
+
return high, low
|
116
|
+
|
117
|
+
|
118
|
+
class Decimal128(object):
|
119
|
+
"""BSON Decimal128 type::
|
120
|
+
|
121
|
+
>>> Decimal128(Decimal("0.0005"))
|
122
|
+
Decimal128('0.0005')
|
123
|
+
>>> Decimal128("0.0005")
|
124
|
+
Decimal128('0.0005')
|
125
|
+
>>> Decimal128((3474527112516337664, 5))
|
126
|
+
Decimal128('0.0005')
|
127
|
+
|
128
|
+
:Parameters:
|
129
|
+
- `value`: An instance of :class:`decimal.Decimal`, string, or tuple of
|
130
|
+
(high bits, low bits) from Binary Integer Decimal (BID) format.
|
131
|
+
|
132
|
+
.. note:: :class:`~Decimal128` uses an instance of :class:`decimal.Context`
|
133
|
+
configured for IEEE-754 Decimal128 when validating parameters.
|
134
|
+
Signals like :class:`decimal.InvalidOperation`, :class:`decimal.Inexact`,
|
135
|
+
and :class:`decimal.Overflow` are trapped and raised as exceptions::
|
136
|
+
|
137
|
+
>>> Decimal128(".13.1")
|
138
|
+
Traceback (most recent call last):
|
139
|
+
File "<stdin>", line 1, in <module>
|
140
|
+
...
|
141
|
+
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
|
142
|
+
>>>
|
143
|
+
>>> Decimal128("1E-6177")
|
144
|
+
Traceback (most recent call last):
|
145
|
+
File "<stdin>", line 1, in <module>
|
146
|
+
...
|
147
|
+
decimal.Inexact: [<class 'decimal.Inexact'>]
|
148
|
+
>>>
|
149
|
+
>>> Decimal128("1E6145")
|
150
|
+
Traceback (most recent call last):
|
151
|
+
File "<stdin>", line 1, in <module>
|
152
|
+
...
|
153
|
+
decimal.Overflow: [<class 'decimal.Overflow'>, <class 'decimal.Rounded'>]
|
154
|
+
|
155
|
+
To ensure the result of a calculation can always be stored as BSON
|
156
|
+
Decimal128 use the context returned by
|
157
|
+
:func:`create_decimal128_context`::
|
158
|
+
|
159
|
+
>>> import decimal
|
160
|
+
>>> decimal128_ctx = create_decimal128_context()
|
161
|
+
>>> with decimal.localcontext(decimal128_ctx) as ctx:
|
162
|
+
... Decimal128(ctx.create_decimal(".13.3"))
|
163
|
+
...
|
164
|
+
Decimal128('NaN')
|
165
|
+
>>>
|
166
|
+
>>> with decimal.localcontext(decimal128_ctx) as ctx:
|
167
|
+
... Decimal128(ctx.create_decimal("1E-6177"))
|
168
|
+
...
|
169
|
+
Decimal128('0E-6176')
|
170
|
+
>>>
|
171
|
+
>>> with decimal.localcontext(DECIMAL128_CTX) as ctx:
|
172
|
+
... Decimal128(ctx.create_decimal("1E6145"))
|
173
|
+
...
|
174
|
+
Decimal128('Infinity')
|
175
|
+
|
176
|
+
To match the behavior of MongoDB's Decimal128 implementation
|
177
|
+
str(Decimal(value)) may not match str(Decimal128(value)) for NaN values::
|
178
|
+
|
179
|
+
>>> Decimal128(Decimal('NaN'))
|
180
|
+
Decimal128('NaN')
|
181
|
+
>>> Decimal128(Decimal('-NaN'))
|
182
|
+
Decimal128('NaN')
|
183
|
+
>>> Decimal128(Decimal('sNaN'))
|
184
|
+
Decimal128('NaN')
|
185
|
+
>>> Decimal128(Decimal('-sNaN'))
|
186
|
+
Decimal128('NaN')
|
187
|
+
|
188
|
+
However, :meth:`~Decimal128.to_decimal` will return the exact value::
|
189
|
+
|
190
|
+
>>> Decimal128(Decimal('NaN')).to_decimal()
|
191
|
+
Decimal('NaN')
|
192
|
+
>>> Decimal128(Decimal('-NaN')).to_decimal()
|
193
|
+
Decimal('-NaN')
|
194
|
+
>>> Decimal128(Decimal('sNaN')).to_decimal()
|
195
|
+
Decimal('sNaN')
|
196
|
+
>>> Decimal128(Decimal('-sNaN')).to_decimal()
|
197
|
+
Decimal('-sNaN')
|
198
|
+
|
199
|
+
Two instances of :class:`Decimal128` compare equal if their Binary
|
200
|
+
Integer Decimal encodings are equal::
|
201
|
+
|
202
|
+
>>> Decimal128('NaN') == Decimal128('NaN')
|
203
|
+
True
|
204
|
+
>>> Decimal128('NaN').bid == Decimal128('NaN').bid
|
205
|
+
True
|
206
|
+
|
207
|
+
This differs from :class:`decimal.Decimal` comparisons for NaN::
|
208
|
+
|
209
|
+
>>> Decimal('NaN') == Decimal('NaN')
|
210
|
+
False
|
211
|
+
"""
|
212
|
+
|
213
|
+
__slots__ = ("__high", "__low")
|
214
|
+
|
215
|
+
_type_marker = 19
|
216
|
+
|
217
|
+
def __init__(self, value: _VALUE_OPTIONS) -> None:
|
218
|
+
if isinstance(value, (str, decimal.Decimal)):
|
219
|
+
self.__high, self.__low = _decimal_to_128(value)
|
220
|
+
elif isinstance(value, (list, tuple)):
|
221
|
+
if len(value) != 2:
|
222
|
+
raise ValueError(
|
223
|
+
"Invalid size for creation of Decimal128 "
|
224
|
+
"from list or tuple. Must have exactly 2 "
|
225
|
+
"elements."
|
226
|
+
)
|
227
|
+
self.__high, self.__low = value # type: ignore
|
228
|
+
else:
|
229
|
+
raise TypeError("Cannot convert %r to Decimal128" % (value,))
|
230
|
+
|
231
|
+
def to_decimal(self) -> decimal.Decimal:
|
232
|
+
"""Returns an instance of :class:`decimal.Decimal` for this
|
233
|
+
:class:`Decimal128`.
|
234
|
+
"""
|
235
|
+
high = self.__high
|
236
|
+
low = self.__low
|
237
|
+
sign = 1 if (high & _SIGN) else 0
|
238
|
+
|
239
|
+
if (high & _SNAN) == _SNAN:
|
240
|
+
return decimal.Decimal((sign, (), "N")) # type: ignore
|
241
|
+
elif (high & _NAN) == _NAN:
|
242
|
+
return decimal.Decimal((sign, (), "n")) # type: ignore
|
243
|
+
elif (high & _INF) == _INF:
|
244
|
+
return decimal.Decimal((sign, (), "F")) # type: ignore
|
245
|
+
|
246
|
+
if (high & _EXPONENT_MASK) == _EXPONENT_MASK:
|
247
|
+
exponent = ((high & 0x1FFFE00000000000) >> 47) - _EXPONENT_BIAS
|
248
|
+
return decimal.Decimal((sign, (0,), exponent))
|
249
|
+
else:
|
250
|
+
exponent = ((high & 0x7FFF800000000000) >> 49) - _EXPONENT_BIAS
|
251
|
+
|
252
|
+
arr = bytearray(15)
|
253
|
+
mask = 0x00000000000000FF
|
254
|
+
for i in range(14, 6, -1):
|
255
|
+
arr[i] = (low & mask) >> ((14 - i) << 3)
|
256
|
+
mask = mask << 8
|
257
|
+
|
258
|
+
mask = 0x00000000000000FF
|
259
|
+
for i in range(6, 0, -1):
|
260
|
+
arr[i] = (high & mask) >> ((6 - i) << 3)
|
261
|
+
mask = mask << 8
|
262
|
+
|
263
|
+
mask = 0x0001000000000000
|
264
|
+
arr[0] = (high & mask) >> 48
|
265
|
+
|
266
|
+
# cdecimal only accepts a tuple for digits.
|
267
|
+
digits = tuple(int(digit) for digit in str(int.from_bytes(arr, "big")))
|
268
|
+
|
269
|
+
with decimal.localcontext(_DEC128_CTX) as ctx:
|
270
|
+
return ctx.create_decimal((sign, digits, exponent))
|
271
|
+
|
272
|
+
@classmethod
|
273
|
+
def from_bid(cls: Type["Decimal128"], value: bytes) -> "Decimal128":
|
274
|
+
"""Create an instance of :class:`Decimal128` from Binary Integer
|
275
|
+
Decimal string.
|
276
|
+
|
277
|
+
:Parameters:
|
278
|
+
- `value`: 16 byte string (128-bit IEEE 754-2008 decimal floating
|
279
|
+
point in Binary Integer Decimal (BID) format).
|
280
|
+
"""
|
281
|
+
if not isinstance(value, bytes):
|
282
|
+
raise TypeError("value must be an instance of bytes")
|
283
|
+
if len(value) != 16:
|
284
|
+
raise ValueError("value must be exactly 16 bytes")
|
285
|
+
return cls((_UNPACK_64(value[8:])[0], _UNPACK_64(value[:8])[0])) # type: ignore
|
286
|
+
|
287
|
+
@property
|
288
|
+
def bid(self) -> bytes:
|
289
|
+
"""The Binary Integer Decimal (BID) encoding of this instance."""
|
290
|
+
return _PACK_64(self.__low) + _PACK_64(self.__high)
|
291
|
+
|
292
|
+
def __str__(self) -> str:
|
293
|
+
dec = self.to_decimal()
|
294
|
+
if dec.is_nan():
|
295
|
+
# Required by the drivers spec to match MongoDB behavior.
|
296
|
+
return "NaN"
|
297
|
+
return str(dec)
|
298
|
+
|
299
|
+
def __repr__(self):
|
300
|
+
return "Decimal128('%s')" % (str(self),)
|
301
|
+
|
302
|
+
def __setstate__(self, value: Tuple[int, int]) -> None:
|
303
|
+
self.__high, self.__low = value
|
304
|
+
|
305
|
+
def __getstate__(self) -> Tuple[int, int]:
|
306
|
+
return self.__high, self.__low
|
307
|
+
|
308
|
+
def __eq__(self, other: Any) -> bool:
|
309
|
+
if isinstance(other, Decimal128):
|
310
|
+
return self.bid == other.bid
|
311
|
+
return NotImplemented
|
312
|
+
|
313
|
+
def __ne__(self, other: Any) -> bool:
|
314
|
+
return not self == other
|
data/lib/bson/errors.py
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Copyright 2009-present MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""Exceptions raised by the BSON package."""
|
16
|
+
|
17
|
+
|
18
|
+
class BSONError(Exception):
|
19
|
+
"""Base class for all BSON exceptions."""
|
20
|
+
|
21
|
+
|
22
|
+
class InvalidBSON(BSONError):
|
23
|
+
"""Raised when trying to create a BSON object from invalid data."""
|
24
|
+
|
25
|
+
|
26
|
+
class InvalidStringData(BSONError):
|
27
|
+
"""Raised when trying to encode a string containing non-UTF8 data."""
|
28
|
+
|
29
|
+
|
30
|
+
class InvalidDocument(BSONError):
|
31
|
+
"""Raised when trying to create a BSON object from an invalid document."""
|
32
|
+
|
33
|
+
|
34
|
+
class InvalidId(BSONError):
|
35
|
+
"""Raised when trying to create an ObjectId from invalid data."""
|
data/lib/bson/int64.py
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Copyright 2014-2015 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""A BSON wrapper for long (int in python3)"""
|
16
|
+
|
17
|
+
from typing import Any
|
18
|
+
|
19
|
+
|
20
|
+
class Int64(int):
|
21
|
+
"""Representation of the BSON int64 type.
|
22
|
+
|
23
|
+
This is necessary because every integral number is an :class:`int` in
|
24
|
+
Python 3. Small integral numbers are encoded to BSON int32 by default,
|
25
|
+
but Int64 numbers will always be encoded to BSON int64.
|
26
|
+
|
27
|
+
:Parameters:
|
28
|
+
- `value`: the numeric value to represent
|
29
|
+
"""
|
30
|
+
|
31
|
+
__slots__ = ()
|
32
|
+
|
33
|
+
_type_marker = 18
|
34
|
+
|
35
|
+
def __getstate__(self) -> Any:
|
36
|
+
return {}
|
37
|
+
|
38
|
+
def __setstate__(self, state: Any) -> None:
|
39
|
+
pass
|