sensu-plugins-mongodb-mrtrotl 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1 -0
  3. data/LICENSE +22 -0
  4. data/README.md +27 -0
  5. data/bin/check-mongodb-metric.rb +144 -0
  6. data/bin/check-mongodb-query-count.rb +267 -0
  7. data/bin/check-mongodb.py +1644 -0
  8. data/bin/check-mongodb.rb +5 -0
  9. data/bin/metrics-mongodb-replication.rb +254 -0
  10. data/bin/metrics-mongodb.rb +133 -0
  11. data/lib/bson/__init__.py +1347 -0
  12. data/lib/bson/__pycache__/__init__.cpython-310.pyc +0 -0
  13. data/lib/bson/__pycache__/_helpers.cpython-310.pyc +0 -0
  14. data/lib/bson/__pycache__/binary.cpython-310.pyc +0 -0
  15. data/lib/bson/__pycache__/code.cpython-310.pyc +0 -0
  16. data/lib/bson/__pycache__/codec_options.cpython-310.pyc +0 -0
  17. data/lib/bson/__pycache__/dbref.cpython-310.pyc +0 -0
  18. data/lib/bson/__pycache__/decimal128.cpython-310.pyc +0 -0
  19. data/lib/bson/__pycache__/errors.cpython-310.pyc +0 -0
  20. data/lib/bson/__pycache__/int64.cpython-310.pyc +0 -0
  21. data/lib/bson/__pycache__/json_util.cpython-310.pyc +0 -0
  22. data/lib/bson/__pycache__/max_key.cpython-310.pyc +0 -0
  23. data/lib/bson/__pycache__/min_key.cpython-310.pyc +0 -0
  24. data/lib/bson/__pycache__/objectid.cpython-310.pyc +0 -0
  25. data/lib/bson/__pycache__/raw_bson.cpython-310.pyc +0 -0
  26. data/lib/bson/__pycache__/regex.cpython-310.pyc +0 -0
  27. data/lib/bson/__pycache__/son.cpython-310.pyc +0 -0
  28. data/lib/bson/__pycache__/timestamp.cpython-310.pyc +0 -0
  29. data/lib/bson/__pycache__/tz_util.cpython-310.pyc +0 -0
  30. data/lib/bson/_cbson.cpython-310-x86_64-linux-gnu.so +0 -0
  31. data/lib/bson/_helpers.py +41 -0
  32. data/lib/bson/binary.py +364 -0
  33. data/lib/bson/code.py +101 -0
  34. data/lib/bson/codec_options.py +414 -0
  35. data/lib/bson/codec_options.pyi +100 -0
  36. data/lib/bson/dbref.py +133 -0
  37. data/lib/bson/decimal128.py +314 -0
  38. data/lib/bson/errors.py +35 -0
  39. data/lib/bson/int64.py +39 -0
  40. data/lib/bson/json_util.py +874 -0
  41. data/lib/bson/max_key.py +55 -0
  42. data/lib/bson/min_key.py +55 -0
  43. data/lib/bson/objectid.py +286 -0
  44. data/lib/bson/py.typed +2 -0
  45. data/lib/bson/raw_bson.py +175 -0
  46. data/lib/bson/regex.py +135 -0
  47. data/lib/bson/son.py +208 -0
  48. data/lib/bson/timestamp.py +124 -0
  49. data/lib/bson/tz_util.py +52 -0
  50. data/lib/gridfs/__init__.py +1015 -0
  51. data/lib/gridfs/__pycache__/__init__.cpython-310.pyc +0 -0
  52. data/lib/gridfs/__pycache__/errors.cpython-310.pyc +0 -0
  53. data/lib/gridfs/__pycache__/grid_file.cpython-310.pyc +0 -0
  54. data/lib/gridfs/errors.py +33 -0
  55. data/lib/gridfs/grid_file.py +907 -0
  56. data/lib/gridfs/py.typed +2 -0
  57. data/lib/pymongo/__init__.py +185 -0
  58. data/lib/pymongo/__pycache__/__init__.cpython-310.pyc +0 -0
  59. data/lib/pymongo/__pycache__/_csot.cpython-310.pyc +0 -0
  60. data/lib/pymongo/__pycache__/aggregation.cpython-310.pyc +0 -0
  61. data/lib/pymongo/__pycache__/auth.cpython-310.pyc +0 -0
  62. data/lib/pymongo/__pycache__/auth_aws.cpython-310.pyc +0 -0
  63. data/lib/pymongo/__pycache__/bulk.cpython-310.pyc +0 -0
  64. data/lib/pymongo/__pycache__/change_stream.cpython-310.pyc +0 -0
  65. data/lib/pymongo/__pycache__/client_options.cpython-310.pyc +0 -0
  66. data/lib/pymongo/__pycache__/client_session.cpython-310.pyc +0 -0
  67. data/lib/pymongo/__pycache__/collation.cpython-310.pyc +0 -0
  68. data/lib/pymongo/__pycache__/collection.cpython-310.pyc +0 -0
  69. data/lib/pymongo/__pycache__/command_cursor.cpython-310.pyc +0 -0
  70. data/lib/pymongo/__pycache__/common.cpython-310.pyc +0 -0
  71. data/lib/pymongo/__pycache__/compression_support.cpython-310.pyc +0 -0
  72. data/lib/pymongo/__pycache__/cursor.cpython-310.pyc +0 -0
  73. data/lib/pymongo/__pycache__/daemon.cpython-310.pyc +0 -0
  74. data/lib/pymongo/__pycache__/database.cpython-310.pyc +0 -0
  75. data/lib/pymongo/__pycache__/driver_info.cpython-310.pyc +0 -0
  76. data/lib/pymongo/__pycache__/encryption.cpython-310.pyc +0 -0
  77. data/lib/pymongo/__pycache__/encryption_options.cpython-310.pyc +0 -0
  78. data/lib/pymongo/__pycache__/errors.cpython-310.pyc +0 -0
  79. data/lib/pymongo/__pycache__/event_loggers.cpython-310.pyc +0 -0
  80. data/lib/pymongo/__pycache__/hello.cpython-310.pyc +0 -0
  81. data/lib/pymongo/__pycache__/helpers.cpython-310.pyc +0 -0
  82. data/lib/pymongo/__pycache__/max_staleness_selectors.cpython-310.pyc +0 -0
  83. data/lib/pymongo/__pycache__/message.cpython-310.pyc +0 -0
  84. data/lib/pymongo/__pycache__/mongo_client.cpython-310.pyc +0 -0
  85. data/lib/pymongo/__pycache__/monitor.cpython-310.pyc +0 -0
  86. data/lib/pymongo/__pycache__/monitoring.cpython-310.pyc +0 -0
  87. data/lib/pymongo/__pycache__/network.cpython-310.pyc +0 -0
  88. data/lib/pymongo/__pycache__/ocsp_cache.cpython-310.pyc +0 -0
  89. data/lib/pymongo/__pycache__/ocsp_support.cpython-310.pyc +0 -0
  90. data/lib/pymongo/__pycache__/operations.cpython-310.pyc +0 -0
  91. data/lib/pymongo/__pycache__/periodic_executor.cpython-310.pyc +0 -0
  92. data/lib/pymongo/__pycache__/pool.cpython-310.pyc +0 -0
  93. data/lib/pymongo/__pycache__/pyopenssl_context.cpython-310.pyc +0 -0
  94. data/lib/pymongo/__pycache__/read_concern.cpython-310.pyc +0 -0
  95. data/lib/pymongo/__pycache__/read_preferences.cpython-310.pyc +0 -0
  96. data/lib/pymongo/__pycache__/response.cpython-310.pyc +0 -0
  97. data/lib/pymongo/__pycache__/results.cpython-310.pyc +0 -0
  98. data/lib/pymongo/__pycache__/saslprep.cpython-310.pyc +0 -0
  99. data/lib/pymongo/__pycache__/server.cpython-310.pyc +0 -0
  100. data/lib/pymongo/__pycache__/server_api.cpython-310.pyc +0 -0
  101. data/lib/pymongo/__pycache__/server_description.cpython-310.pyc +0 -0
  102. data/lib/pymongo/__pycache__/server_selectors.cpython-310.pyc +0 -0
  103. data/lib/pymongo/__pycache__/server_type.cpython-310.pyc +0 -0
  104. data/lib/pymongo/__pycache__/settings.cpython-310.pyc +0 -0
  105. data/lib/pymongo/__pycache__/socket_checker.cpython-310.pyc +0 -0
  106. data/lib/pymongo/__pycache__/srv_resolver.cpython-310.pyc +0 -0
  107. data/lib/pymongo/__pycache__/ssl_context.cpython-310.pyc +0 -0
  108. data/lib/pymongo/__pycache__/ssl_support.cpython-310.pyc +0 -0
  109. data/lib/pymongo/__pycache__/topology.cpython-310.pyc +0 -0
  110. data/lib/pymongo/__pycache__/topology_description.cpython-310.pyc +0 -0
  111. data/lib/pymongo/__pycache__/typings.cpython-310.pyc +0 -0
  112. data/lib/pymongo/__pycache__/uri_parser.cpython-310.pyc +0 -0
  113. data/lib/pymongo/__pycache__/write_concern.cpython-310.pyc +0 -0
  114. data/lib/pymongo/_cmessage.cpython-310-x86_64-linux-gnu.so +0 -0
  115. data/lib/pymongo/_csot.py +118 -0
  116. data/lib/pymongo/aggregation.py +229 -0
  117. data/lib/pymongo/auth.py +549 -0
  118. data/lib/pymongo/auth_aws.py +94 -0
  119. data/lib/pymongo/bulk.py +513 -0
  120. data/lib/pymongo/change_stream.py +457 -0
  121. data/lib/pymongo/client_options.py +302 -0
  122. data/lib/pymongo/client_session.py +1112 -0
  123. data/lib/pymongo/collation.py +224 -0
  124. data/lib/pymongo/collection.py +3204 -0
  125. data/lib/pymongo/command_cursor.py +353 -0
  126. data/lib/pymongo/common.py +984 -0
  127. data/lib/pymongo/compression_support.py +149 -0
  128. data/lib/pymongo/cursor.py +1345 -0
  129. data/lib/pymongo/daemon.py +141 -0
  130. data/lib/pymongo/database.py +1202 -0
  131. data/lib/pymongo/driver_info.py +42 -0
  132. data/lib/pymongo/encryption.py +884 -0
  133. data/lib/pymongo/encryption_options.py +221 -0
  134. data/lib/pymongo/errors.py +365 -0
  135. data/lib/pymongo/event_loggers.py +221 -0
  136. data/lib/pymongo/hello.py +219 -0
  137. data/lib/pymongo/helpers.py +259 -0
  138. data/lib/pymongo/max_staleness_selectors.py +114 -0
  139. data/lib/pymongo/message.py +1440 -0
  140. data/lib/pymongo/mongo_client.py +2144 -0
  141. data/lib/pymongo/monitor.py +440 -0
  142. data/lib/pymongo/monitoring.py +1801 -0
  143. data/lib/pymongo/network.py +311 -0
  144. data/lib/pymongo/ocsp_cache.py +87 -0
  145. data/lib/pymongo/ocsp_support.py +372 -0
  146. data/lib/pymongo/operations.py +507 -0
  147. data/lib/pymongo/periodic_executor.py +183 -0
  148. data/lib/pymongo/pool.py +1660 -0
  149. data/lib/pymongo/py.typed +2 -0
  150. data/lib/pymongo/pyopenssl_context.py +383 -0
  151. data/lib/pymongo/read_concern.py +75 -0
  152. data/lib/pymongo/read_preferences.py +609 -0
  153. data/lib/pymongo/response.py +109 -0
  154. data/lib/pymongo/results.py +217 -0
  155. data/lib/pymongo/saslprep.py +113 -0
  156. data/lib/pymongo/server.py +247 -0
  157. data/lib/pymongo/server_api.py +170 -0
  158. data/lib/pymongo/server_description.py +285 -0
  159. data/lib/pymongo/server_selectors.py +153 -0
  160. data/lib/pymongo/server_type.py +32 -0
  161. data/lib/pymongo/settings.py +159 -0
  162. data/lib/pymongo/socket_checker.py +104 -0
  163. data/lib/pymongo/srv_resolver.py +126 -0
  164. data/lib/pymongo/ssl_context.py +39 -0
  165. data/lib/pymongo/ssl_support.py +99 -0
  166. data/lib/pymongo/topology.py +890 -0
  167. data/lib/pymongo/topology_description.py +639 -0
  168. data/lib/pymongo/typings.py +39 -0
  169. data/lib/pymongo/uri_parser.py +624 -0
  170. data/lib/pymongo/write_concern.py +129 -0
  171. data/lib/pymongo-4.2.0.dist-info/INSTALLER +1 -0
  172. data/lib/pymongo-4.2.0.dist-info/LICENSE +201 -0
  173. data/lib/pymongo-4.2.0.dist-info/METADATA +250 -0
  174. data/lib/pymongo-4.2.0.dist-info/RECORD +167 -0
  175. data/lib/pymongo-4.2.0.dist-info/REQUESTED +0 -0
  176. data/lib/pymongo-4.2.0.dist-info/WHEEL +6 -0
  177. data/lib/pymongo-4.2.0.dist-info/top_level.txt +3 -0
  178. data/lib/sensu-plugins-mongodb/metrics.rb +391 -0
  179. data/lib/sensu-plugins-mongodb/version.rb +9 -0
  180. data/lib/sensu-plugins-mongodb.rb +1 -0
  181. metadata +407 -0
@@ -0,0 +1,55 @@
1
+ # Copyright 2010-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
+ """Representation for the MongoDB internal MaxKey type.
16
+ """
17
+ from typing import Any
18
+
19
+
20
+ class MaxKey(object):
21
+ """MongoDB internal MaxKey type."""
22
+
23
+ __slots__ = ()
24
+
25
+ _type_marker = 127
26
+
27
+ def __getstate__(self) -> Any:
28
+ return {}
29
+
30
+ def __setstate__(self, state: Any) -> None:
31
+ pass
32
+
33
+ def __eq__(self, other: Any) -> bool:
34
+ return isinstance(other, MaxKey)
35
+
36
+ def __hash__(self) -> int:
37
+ return hash(self._type_marker)
38
+
39
+ def __ne__(self, other: Any) -> bool:
40
+ return not self == other
41
+
42
+ def __le__(self, other: Any) -> bool:
43
+ return isinstance(other, MaxKey)
44
+
45
+ def __lt__(self, dummy: Any) -> bool:
46
+ return False
47
+
48
+ def __ge__(self, dummy: Any) -> bool:
49
+ return True
50
+
51
+ def __gt__(self, other: Any) -> bool:
52
+ return not isinstance(other, MaxKey)
53
+
54
+ def __repr__(self):
55
+ return "MaxKey()"
@@ -0,0 +1,55 @@
1
+ # Copyright 2010-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
+ """Representation for the MongoDB internal MinKey type.
16
+ """
17
+ from typing import Any
18
+
19
+
20
+ class MinKey(object):
21
+ """MongoDB internal MinKey type."""
22
+
23
+ __slots__ = ()
24
+
25
+ _type_marker = 255
26
+
27
+ def __getstate__(self) -> Any:
28
+ return {}
29
+
30
+ def __setstate__(self, state: Any) -> None:
31
+ pass
32
+
33
+ def __eq__(self, other: Any) -> bool:
34
+ return isinstance(other, MinKey)
35
+
36
+ def __hash__(self) -> int:
37
+ return hash(self._type_marker)
38
+
39
+ def __ne__(self, other: Any) -> bool:
40
+ return not self == other
41
+
42
+ def __le__(self, dummy: Any) -> bool:
43
+ return True
44
+
45
+ def __lt__(self, other: Any) -> bool:
46
+ return not isinstance(other, MinKey)
47
+
48
+ def __ge__(self, other: Any) -> bool:
49
+ return isinstance(other, MinKey)
50
+
51
+ def __gt__(self, dummy: Any) -> bool:
52
+ return False
53
+
54
+ def __repr__(self):
55
+ return "MinKey()"
@@ -0,0 +1,286 @@
1
+ # Copyright 2009-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
+ """Tools for working with MongoDB ObjectIds.
16
+ """
17
+
18
+ import binascii
19
+ import calendar
20
+ import datetime
21
+ import os
22
+ import struct
23
+ import threading
24
+ import time
25
+ from random import SystemRandom
26
+ from typing import Any, NoReturn, Optional, Type, Union
27
+
28
+ from bson.errors import InvalidId
29
+ from bson.tz_util import utc
30
+
31
+ _MAX_COUNTER_VALUE = 0xFFFFFF
32
+
33
+
34
+ def _raise_invalid_id(oid: str) -> NoReturn:
35
+ raise InvalidId(
36
+ "%r is not a valid ObjectId, it must be a 12-byte input"
37
+ " or a 24-character hex string" % oid
38
+ )
39
+
40
+
41
+ def _random_bytes() -> bytes:
42
+ """Get the 5-byte random field of an ObjectId."""
43
+ return os.urandom(5)
44
+
45
+
46
+ class ObjectId(object):
47
+ """A MongoDB ObjectId."""
48
+
49
+ _pid = os.getpid()
50
+
51
+ _inc = SystemRandom().randint(0, _MAX_COUNTER_VALUE)
52
+ _inc_lock = threading.Lock()
53
+
54
+ __random = _random_bytes()
55
+
56
+ __slots__ = ("__id",)
57
+
58
+ _type_marker = 7
59
+
60
+ def __init__(self, oid: Optional[Union[str, "ObjectId", bytes]] = None) -> None:
61
+ """Initialize a new ObjectId.
62
+
63
+ An ObjectId is a 12-byte unique identifier consisting of:
64
+
65
+ - a 4-byte value representing the seconds since the Unix epoch,
66
+ - a 5-byte random value,
67
+ - a 3-byte counter, starting with a random value.
68
+
69
+ By default, ``ObjectId()`` creates a new unique identifier. The
70
+ optional parameter `oid` can be an :class:`ObjectId`, or any 12
71
+ :class:`bytes`.
72
+
73
+ For example, the 12 bytes b'foo-bar-quux' do not follow the ObjectId
74
+ specification but they are acceptable input::
75
+
76
+ >>> ObjectId(b'foo-bar-quux')
77
+ ObjectId('666f6f2d6261722d71757578')
78
+
79
+ `oid` can also be a :class:`str` of 24 hex digits::
80
+
81
+ >>> ObjectId('0123456789ab0123456789ab')
82
+ ObjectId('0123456789ab0123456789ab')
83
+
84
+ Raises :class:`~bson.errors.InvalidId` if `oid` is not 12 bytes nor
85
+ 24 hex digits, or :class:`TypeError` if `oid` is not an accepted type.
86
+
87
+ :Parameters:
88
+ - `oid` (optional): a valid ObjectId.
89
+
90
+ .. seealso:: The MongoDB documentation on `ObjectIds <http://dochub.mongodb.org/core/objectids>`_.
91
+
92
+ .. versionchanged:: 3.8
93
+ :class:`~bson.objectid.ObjectId` now implements the `ObjectID
94
+ specification version 0.2
95
+ <https://github.com/mongodb/specifications/blob/master/source/
96
+ objectid.rst>`_.
97
+ """
98
+ if oid is None:
99
+ self.__generate()
100
+ elif isinstance(oid, bytes) and len(oid) == 12:
101
+ self.__id = oid
102
+ else:
103
+ self.__validate(oid)
104
+
105
+ @classmethod
106
+ def from_datetime(cls: Type["ObjectId"], generation_time: datetime.datetime) -> "ObjectId":
107
+ """Create a dummy ObjectId instance with a specific generation time.
108
+
109
+ This method is useful for doing range queries on a field
110
+ containing :class:`ObjectId` instances.
111
+
112
+ .. warning::
113
+ It is not safe to insert a document containing an ObjectId
114
+ generated using this method. This method deliberately
115
+ eliminates the uniqueness guarantee that ObjectIds
116
+ generally provide. ObjectIds generated with this method
117
+ should be used exclusively in queries.
118
+
119
+ `generation_time` will be converted to UTC. Naive datetime
120
+ instances will be treated as though they already contain UTC.
121
+
122
+ An example using this helper to get documents where ``"_id"``
123
+ was generated before January 1, 2010 would be:
124
+
125
+ >>> gen_time = datetime.datetime(2010, 1, 1)
126
+ >>> dummy_id = ObjectId.from_datetime(gen_time)
127
+ >>> result = collection.find({"_id": {"$lt": dummy_id}})
128
+
129
+ :Parameters:
130
+ - `generation_time`: :class:`~datetime.datetime` to be used
131
+ as the generation time for the resulting ObjectId.
132
+ """
133
+ offset = generation_time.utcoffset()
134
+ if offset is not None:
135
+ generation_time = generation_time - offset
136
+ timestamp = calendar.timegm(generation_time.timetuple())
137
+ oid = struct.pack(">I", int(timestamp)) + b"\x00\x00\x00\x00\x00\x00\x00\x00"
138
+ return cls(oid)
139
+
140
+ @classmethod
141
+ def is_valid(cls: Type["ObjectId"], oid: Any) -> bool:
142
+ """Checks if a `oid` string is valid or not.
143
+
144
+ :Parameters:
145
+ - `oid`: the object id to validate
146
+
147
+ .. versionadded:: 2.3
148
+ """
149
+ if not oid:
150
+ return False
151
+
152
+ try:
153
+ ObjectId(oid)
154
+ return True
155
+ except (InvalidId, TypeError):
156
+ return False
157
+
158
+ @classmethod
159
+ def _random(cls) -> bytes:
160
+ """Generate a 5-byte random number once per process."""
161
+ pid = os.getpid()
162
+ if pid != cls._pid:
163
+ cls._pid = pid
164
+ cls.__random = _random_bytes()
165
+ return cls.__random
166
+
167
+ def __generate(self) -> None:
168
+ """Generate a new value for this ObjectId."""
169
+
170
+ # 4 bytes current time
171
+ oid = struct.pack(">I", int(time.time()))
172
+
173
+ # 5 bytes random
174
+ oid += ObjectId._random()
175
+
176
+ # 3 bytes inc
177
+ with ObjectId._inc_lock:
178
+ oid += struct.pack(">I", ObjectId._inc)[1:4]
179
+ ObjectId._inc = (ObjectId._inc + 1) % (_MAX_COUNTER_VALUE + 1)
180
+
181
+ self.__id = oid
182
+
183
+ def __validate(self, oid: Any) -> None:
184
+ """Validate and use the given id for this ObjectId.
185
+
186
+ Raises TypeError if id is not an instance of
187
+ (:class:`basestring` (:class:`str` or :class:`bytes`
188
+ in python 3), ObjectId) and InvalidId if it is not a
189
+ valid ObjectId.
190
+
191
+ :Parameters:
192
+ - `oid`: a valid ObjectId
193
+ """
194
+ if isinstance(oid, ObjectId):
195
+ self.__id = oid.binary
196
+ elif isinstance(oid, str):
197
+ if len(oid) == 24:
198
+ try:
199
+ self.__id = bytes.fromhex(oid)
200
+ except (TypeError, ValueError):
201
+ _raise_invalid_id(oid)
202
+ else:
203
+ _raise_invalid_id(oid)
204
+ else:
205
+ raise TypeError(
206
+ "id must be an instance of (bytes, str, ObjectId), not %s" % (type(oid),)
207
+ )
208
+
209
+ @property
210
+ def binary(self) -> bytes:
211
+ """12-byte binary representation of this ObjectId."""
212
+ return self.__id
213
+
214
+ @property
215
+ def generation_time(self) -> datetime.datetime:
216
+ """A :class:`datetime.datetime` instance representing the time of
217
+ generation for this :class:`ObjectId`.
218
+
219
+ The :class:`datetime.datetime` is timezone aware, and
220
+ represents the generation time in UTC. It is precise to the
221
+ second.
222
+ """
223
+ timestamp = struct.unpack(">I", self.__id[0:4])[0]
224
+ return datetime.datetime.fromtimestamp(timestamp, utc)
225
+
226
+ def __getstate__(self) -> bytes:
227
+ """return value of object for pickling.
228
+ needed explicitly because __slots__() defined.
229
+ """
230
+ return self.__id
231
+
232
+ def __setstate__(self, value: Any) -> None:
233
+ """explicit state set from pickling"""
234
+ # Provide backwards compatability with OIDs
235
+ # pickled with pymongo-1.9 or older.
236
+ if isinstance(value, dict):
237
+ oid = value["_ObjectId__id"]
238
+ else:
239
+ oid = value
240
+ # ObjectIds pickled in python 2.x used `str` for __id.
241
+ # In python 3.x this has to be converted to `bytes`
242
+ # by encoding latin-1.
243
+ if isinstance(oid, str):
244
+ self.__id = oid.encode("latin-1")
245
+ else:
246
+ self.__id = oid
247
+
248
+ def __str__(self) -> str:
249
+ return binascii.hexlify(self.__id).decode()
250
+
251
+ def __repr__(self):
252
+ return "ObjectId('%s')" % (str(self),)
253
+
254
+ def __eq__(self, other: Any) -> bool:
255
+ if isinstance(other, ObjectId):
256
+ return self.__id == other.binary
257
+ return NotImplemented
258
+
259
+ def __ne__(self, other: Any) -> bool:
260
+ if isinstance(other, ObjectId):
261
+ return self.__id != other.binary
262
+ return NotImplemented
263
+
264
+ def __lt__(self, other: Any) -> bool:
265
+ if isinstance(other, ObjectId):
266
+ return self.__id < other.binary
267
+ return NotImplemented
268
+
269
+ def __le__(self, other: Any) -> bool:
270
+ if isinstance(other, ObjectId):
271
+ return self.__id <= other.binary
272
+ return NotImplemented
273
+
274
+ def __gt__(self, other: Any) -> bool:
275
+ if isinstance(other, ObjectId):
276
+ return self.__id > other.binary
277
+ return NotImplemented
278
+
279
+ def __ge__(self, other: Any) -> bool:
280
+ if isinstance(other, ObjectId):
281
+ return self.__id >= other.binary
282
+ return NotImplemented
283
+
284
+ def __hash__(self) -> int:
285
+ """Get a hash value for this :class:`ObjectId`."""
286
+ return hash(self.__id)
data/lib/bson/py.typed ADDED
@@ -0,0 +1,2 @@
1
+ # PEP-561 Support File.
2
+ # "Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing".
@@ -0,0 +1,175 @@
1
+ # Copyright 2015-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 representing raw BSON documents.
16
+
17
+ Inserting and Retrieving RawBSONDocuments
18
+ =========================================
19
+
20
+ Example: Moving a document between different databases/collections
21
+
22
+ .. doctest::
23
+
24
+ >>> import bson
25
+ >>> from pymongo import MongoClient
26
+ >>> from bson.raw_bson import RawBSONDocument
27
+ >>> client = MongoClient(document_class=RawBSONDocument)
28
+ >>> client.drop_database('db')
29
+ >>> client.drop_database('replica_db')
30
+ >>> db = client.db
31
+ >>> result = db.test.insert_many([{'_id': 1, 'a': 1},
32
+ ... {'_id': 2, 'b': 1},
33
+ ... {'_id': 3, 'c': 1},
34
+ ... {'_id': 4, 'd': 1}])
35
+ >>> replica_db = client.replica_db
36
+ >>> for doc in db.test.find():
37
+ ... print(f"raw document: {doc.raw}")
38
+ ... print(f"decoded document: {bson.decode(doc.raw)}")
39
+ ... result = replica_db.test.insert_one(doc)
40
+ raw document: b'...'
41
+ decoded document: {'_id': 1, 'a': 1}
42
+ raw document: b'...'
43
+ decoded document: {'_id': 2, 'b': 1}
44
+ raw document: b'...'
45
+ decoded document: {'_id': 3, 'c': 1}
46
+ raw document: b'...'
47
+ decoded document: {'_id': 4, 'd': 1}
48
+
49
+ For use cases like moving documents across different databases or writing binary
50
+ blobs to disk, using raw BSON documents provides better speed and avoids the
51
+ overhead of decoding or encoding BSON.
52
+ """
53
+
54
+ from typing import Any, ItemsView, Iterator, Mapping, Optional
55
+
56
+ from bson import _get_object_size, _raw_to_dict
57
+ from bson.codec_options import _RAW_BSON_DOCUMENT_MARKER
58
+ from bson.codec_options import DEFAULT_CODEC_OPTIONS as DEFAULT
59
+ from bson.codec_options import CodecOptions
60
+ from bson.son import SON
61
+
62
+
63
+ class RawBSONDocument(Mapping[str, Any]):
64
+ """Representation for a MongoDB document that provides access to the raw
65
+ BSON bytes that compose it.
66
+
67
+ Only when a field is accessed or modified within the document does
68
+ RawBSONDocument decode its bytes.
69
+ """
70
+
71
+ __slots__ = ("__raw", "__inflated_doc", "__codec_options")
72
+ _type_marker = _RAW_BSON_DOCUMENT_MARKER
73
+
74
+ def __init__(self, bson_bytes: bytes, codec_options: Optional[CodecOptions] = None) -> None:
75
+ """Create a new :class:`RawBSONDocument`
76
+
77
+ :class:`RawBSONDocument` is a representation of a BSON document that
78
+ provides access to the underlying raw BSON bytes. Only when a field is
79
+ accessed or modified within the document does RawBSONDocument decode
80
+ its bytes.
81
+
82
+ :class:`RawBSONDocument` implements the ``Mapping`` abstract base
83
+ class from the standard library so it can be used like a read-only
84
+ ``dict``::
85
+
86
+ >>> from bson import encode
87
+ >>> raw_doc = RawBSONDocument(encode({'_id': 'my_doc'}))
88
+ >>> raw_doc.raw
89
+ b'...'
90
+ >>> raw_doc['_id']
91
+ 'my_doc'
92
+
93
+ :Parameters:
94
+ - `bson_bytes`: the BSON bytes that compose this document
95
+ - `codec_options` (optional): An instance of
96
+ :class:`~bson.codec_options.CodecOptions` whose ``document_class``
97
+ must be :class:`RawBSONDocument`. The default is
98
+ :attr:`DEFAULT_RAW_BSON_OPTIONS`.
99
+
100
+ .. versionchanged:: 3.8
101
+ :class:`RawBSONDocument` now validates that the ``bson_bytes``
102
+ passed in represent a single bson document.
103
+
104
+ .. versionchanged:: 3.5
105
+ If a :class:`~bson.codec_options.CodecOptions` is passed in, its
106
+ `document_class` must be :class:`RawBSONDocument`.
107
+ """
108
+ self.__raw = bson_bytes
109
+ self.__inflated_doc: Optional[Mapping[str, Any]] = None
110
+ # Can't default codec_options to DEFAULT_RAW_BSON_OPTIONS in signature,
111
+ # it refers to this class RawBSONDocument.
112
+ if codec_options is None:
113
+ codec_options = DEFAULT_RAW_BSON_OPTIONS
114
+ elif codec_options.document_class is not RawBSONDocument:
115
+ raise TypeError(
116
+ "RawBSONDocument cannot use CodecOptions with document "
117
+ "class %s" % (codec_options.document_class,)
118
+ )
119
+ self.__codec_options = codec_options
120
+ # Validate the bson object size.
121
+ _get_object_size(bson_bytes, 0, len(bson_bytes))
122
+
123
+ @property
124
+ def raw(self) -> bytes:
125
+ """The raw BSON bytes composing this document."""
126
+ return self.__raw
127
+
128
+ def items(self) -> ItemsView[str, Any]:
129
+ """Lazily decode and iterate elements in this document."""
130
+ return self.__inflated.items()
131
+
132
+ @property
133
+ def __inflated(self) -> Mapping[str, Any]:
134
+ if self.__inflated_doc is None:
135
+ # We already validated the object's size when this document was
136
+ # created, so no need to do that again.
137
+ # Use SON to preserve ordering of elements.
138
+ self.__inflated_doc = _inflate_bson(self.__raw, self.__codec_options)
139
+ return self.__inflated_doc
140
+
141
+ def __getitem__(self, item: str) -> Any:
142
+ return self.__inflated[item]
143
+
144
+ def __iter__(self) -> Iterator[str]:
145
+ return iter(self.__inflated)
146
+
147
+ def __len__(self) -> int:
148
+ return len(self.__inflated)
149
+
150
+ def __eq__(self, other: Any) -> bool:
151
+ if isinstance(other, RawBSONDocument):
152
+ return self.__raw == other.raw
153
+ return NotImplemented
154
+
155
+ def __repr__(self):
156
+ return "RawBSONDocument(%r, codec_options=%r)" % (self.raw, self.__codec_options)
157
+
158
+
159
+ def _inflate_bson(bson_bytes: bytes, codec_options: CodecOptions) -> Mapping[Any, Any]:
160
+ """Inflates the top level fields of a BSON document.
161
+
162
+ :Parameters:
163
+ - `bson_bytes`: the BSON bytes that compose this document
164
+ - `codec_options`: An instance of
165
+ :class:`~bson.codec_options.CodecOptions` whose ``document_class``
166
+ must be :class:`RawBSONDocument`.
167
+ """
168
+ # Use SON to preserve ordering of elements.
169
+ return _raw_to_dict(bson_bytes, 4, len(bson_bytes) - 1, codec_options, SON())
170
+
171
+
172
+ DEFAULT_RAW_BSON_OPTIONS: CodecOptions = DEFAULT.with_options(document_class=RawBSONDocument)
173
+ """The default :class:`~bson.codec_options.CodecOptions` for
174
+ :class:`RawBSONDocument`.
175
+ """