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,41 @@
1
+ # Copyright 2021-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
+ """Setstate and getstate functions for objects with __slots__, allowing
16
+ compatibility with default pickling protocol
17
+ """
18
+ from typing import Any, Mapping
19
+
20
+
21
+ def _setstate_slots(self: Any, state: Any) -> None:
22
+ for slot, value in state.items():
23
+ setattr(self, slot, value)
24
+
25
+
26
+ def _mangle_name(name: str, prefix: str) -> str:
27
+ if name.startswith("__"):
28
+ prefix = "_" + prefix
29
+ else:
30
+ prefix = ""
31
+ return prefix + name
32
+
33
+
34
+ def _getstate_slots(self: Any) -> Mapping[Any, Any]:
35
+ prefix = self.__class__.__name__
36
+ ret = dict()
37
+ for name in self.__slots__:
38
+ mangled_name = _mangle_name(name, prefix)
39
+ if hasattr(self, mangled_name):
40
+ ret[mangled_name] = getattr(self, mangled_name)
41
+ return ret
@@ -0,0 +1,364 @@
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
+ from typing import TYPE_CHECKING, Any, Tuple, Type, Union
16
+ from uuid import UUID
17
+
18
+ """Tools for representing BSON binary data.
19
+ """
20
+
21
+ BINARY_SUBTYPE = 0
22
+ """BSON binary subtype for binary data.
23
+
24
+ This is the default subtype for binary data.
25
+ """
26
+
27
+ FUNCTION_SUBTYPE = 1
28
+ """BSON binary subtype for functions.
29
+ """
30
+
31
+ OLD_BINARY_SUBTYPE = 2
32
+ """Old BSON binary subtype for binary data.
33
+
34
+ This is the old default subtype, the current
35
+ default is :data:`BINARY_SUBTYPE`.
36
+ """
37
+
38
+ OLD_UUID_SUBTYPE = 3
39
+ """Old BSON binary subtype for a UUID.
40
+
41
+ :class:`uuid.UUID` instances will automatically be encoded
42
+ by :mod:`bson` using this subtype when using
43
+ :data:`UuidRepresentation.PYTHON_LEGACY`,
44
+ :data:`UuidRepresentation.JAVA_LEGACY`, or
45
+ :data:`UuidRepresentation.CSHARP_LEGACY`.
46
+
47
+ .. versionadded:: 2.1
48
+ """
49
+
50
+ UUID_SUBTYPE = 4
51
+ """BSON binary subtype for a UUID.
52
+
53
+ This is the standard BSON binary subtype for UUIDs.
54
+ :class:`uuid.UUID` instances will automatically be encoded
55
+ by :mod:`bson` using this subtype when using
56
+ :data:`UuidRepresentation.STANDARD`.
57
+ """
58
+
59
+
60
+ if TYPE_CHECKING:
61
+ from array import array as _array
62
+ from mmap import mmap as _mmap
63
+
64
+
65
+ class UuidRepresentation:
66
+ UNSPECIFIED = 0
67
+ """An unspecified UUID representation.
68
+
69
+ When configured, :class:`uuid.UUID` instances will **not** be
70
+ automatically encoded to or decoded from :class:`~bson.binary.Binary`.
71
+ When encoding a :class:`uuid.UUID` instance, an error will be raised.
72
+ To encode a :class:`uuid.UUID` instance with this configuration, it must
73
+ be wrapped in the :class:`~bson.binary.Binary` class by the application
74
+ code. When decoding a BSON binary field with a UUID subtype, a
75
+ :class:`~bson.binary.Binary` instance will be returned instead of a
76
+ :class:`uuid.UUID` instance.
77
+
78
+ See :ref:`unspecified-representation-details` for details.
79
+
80
+ .. versionadded:: 3.11
81
+ """
82
+
83
+ STANDARD = UUID_SUBTYPE
84
+ """The standard UUID representation.
85
+
86
+ :class:`uuid.UUID` instances will automatically be encoded to
87
+ and decoded from BSON binary, using RFC-4122 byte order with
88
+ binary subtype :data:`UUID_SUBTYPE`.
89
+
90
+ See :ref:`standard-representation-details` for details.
91
+
92
+ .. versionadded:: 3.11
93
+ """
94
+
95
+ PYTHON_LEGACY = OLD_UUID_SUBTYPE
96
+ """The Python legacy UUID representation.
97
+
98
+ :class:`uuid.UUID` instances will automatically be encoded to
99
+ and decoded from BSON binary, using RFC-4122 byte order with
100
+ binary subtype :data:`OLD_UUID_SUBTYPE`.
101
+
102
+ See :ref:`python-legacy-representation-details` for details.
103
+
104
+ .. versionadded:: 3.11
105
+ """
106
+
107
+ JAVA_LEGACY = 5
108
+ """The Java legacy UUID representation.
109
+
110
+ :class:`uuid.UUID` instances will automatically be encoded to
111
+ and decoded from BSON binary subtype :data:`OLD_UUID_SUBTYPE`,
112
+ using the Java driver's legacy byte order.
113
+
114
+ See :ref:`java-legacy-representation-details` for details.
115
+
116
+ .. versionadded:: 3.11
117
+ """
118
+
119
+ CSHARP_LEGACY = 6
120
+ """The C#/.net legacy UUID representation.
121
+
122
+ :class:`uuid.UUID` instances will automatically be encoded to
123
+ and decoded from BSON binary subtype :data:`OLD_UUID_SUBTYPE`,
124
+ using the C# driver's legacy byte order.
125
+
126
+ See :ref:`csharp-legacy-representation-details` for details.
127
+
128
+ .. versionadded:: 3.11
129
+ """
130
+
131
+
132
+ STANDARD = UuidRepresentation.STANDARD
133
+ """An alias for :data:`UuidRepresentation.STANDARD`.
134
+
135
+ .. versionadded:: 3.0
136
+ """
137
+
138
+ PYTHON_LEGACY = UuidRepresentation.PYTHON_LEGACY
139
+ """An alias for :data:`UuidRepresentation.PYTHON_LEGACY`.
140
+
141
+ .. versionadded:: 3.0
142
+ """
143
+
144
+ JAVA_LEGACY = UuidRepresentation.JAVA_LEGACY
145
+ """An alias for :data:`UuidRepresentation.JAVA_LEGACY`.
146
+
147
+ .. versionchanged:: 3.6
148
+ BSON binary subtype 4 is decoded using RFC-4122 byte order.
149
+ .. versionadded:: 2.3
150
+ """
151
+
152
+ CSHARP_LEGACY = UuidRepresentation.CSHARP_LEGACY
153
+ """An alias for :data:`UuidRepresentation.CSHARP_LEGACY`.
154
+
155
+ .. versionchanged:: 3.6
156
+ BSON binary subtype 4 is decoded using RFC-4122 byte order.
157
+ .. versionadded:: 2.3
158
+ """
159
+
160
+ ALL_UUID_SUBTYPES = (OLD_UUID_SUBTYPE, UUID_SUBTYPE)
161
+ ALL_UUID_REPRESENTATIONS = (
162
+ UuidRepresentation.UNSPECIFIED,
163
+ UuidRepresentation.STANDARD,
164
+ UuidRepresentation.PYTHON_LEGACY,
165
+ UuidRepresentation.JAVA_LEGACY,
166
+ UuidRepresentation.CSHARP_LEGACY,
167
+ )
168
+ UUID_REPRESENTATION_NAMES = {
169
+ UuidRepresentation.UNSPECIFIED: "UuidRepresentation.UNSPECIFIED",
170
+ UuidRepresentation.STANDARD: "UuidRepresentation.STANDARD",
171
+ UuidRepresentation.PYTHON_LEGACY: "UuidRepresentation.PYTHON_LEGACY",
172
+ UuidRepresentation.JAVA_LEGACY: "UuidRepresentation.JAVA_LEGACY",
173
+ UuidRepresentation.CSHARP_LEGACY: "UuidRepresentation.CSHARP_LEGACY",
174
+ }
175
+
176
+ MD5_SUBTYPE = 5
177
+ """BSON binary subtype for an MD5 hash.
178
+ """
179
+
180
+ COLUMN_SUBTYPE = 7
181
+ """BSON binary subtype for columns.
182
+
183
+ .. versionadded:: 4.0
184
+ """
185
+
186
+ USER_DEFINED_SUBTYPE = 128
187
+ """BSON binary subtype for any user defined structure.
188
+ """
189
+
190
+
191
+ class Binary(bytes):
192
+ """Representation of BSON binary data.
193
+
194
+ This is necessary because we want to represent Python strings as
195
+ the BSON string type. We need to wrap binary data so we can tell
196
+ the difference between what should be considered binary data and
197
+ what should be considered a string when we encode to BSON.
198
+
199
+ Raises TypeError if `data` is not an instance of :class:`bytes`
200
+ (:class:`str` in python 2) or `subtype` is not an instance of
201
+ :class:`int`. Raises ValueError if `subtype` is not in [0, 256).
202
+
203
+ .. note::
204
+ In python 3 instances of Binary with subtype 0 will be decoded
205
+ directly to :class:`bytes`.
206
+
207
+ :Parameters:
208
+ - `data`: the binary data to represent. Can be any bytes-like type
209
+ that implements the buffer protocol.
210
+ - `subtype` (optional): the `binary subtype
211
+ <https://bsonspec.org/spec.html>`_
212
+ to use
213
+
214
+ .. versionchanged:: 3.9
215
+ Support any bytes-like type that implements the buffer protocol.
216
+ """
217
+
218
+ _type_marker = 5
219
+ __subtype: int
220
+
221
+ def __new__(
222
+ cls: Type["Binary"],
223
+ data: Union[memoryview, bytes, "_mmap", "_array"],
224
+ subtype: int = BINARY_SUBTYPE,
225
+ ) -> "Binary":
226
+ if not isinstance(subtype, int):
227
+ raise TypeError("subtype must be an instance of int")
228
+ if subtype >= 256 or subtype < 0:
229
+ raise ValueError("subtype must be contained in [0, 256)")
230
+ # Support any type that implements the buffer protocol.
231
+ self = bytes.__new__(cls, memoryview(data).tobytes())
232
+ self.__subtype = subtype
233
+ return self
234
+
235
+ @classmethod
236
+ def from_uuid(
237
+ cls: Type["Binary"], uuid: UUID, uuid_representation: int = UuidRepresentation.STANDARD
238
+ ) -> "Binary":
239
+ """Create a BSON Binary object from a Python UUID.
240
+
241
+ Creates a :class:`~bson.binary.Binary` object from a
242
+ :class:`uuid.UUID` instance. Assumes that the native
243
+ :class:`uuid.UUID` instance uses the byte-order implied by the
244
+ provided ``uuid_representation``.
245
+
246
+ Raises :exc:`TypeError` if `uuid` is not an instance of
247
+ :class:`~uuid.UUID`.
248
+
249
+ :Parameters:
250
+ - `uuid`: A :class:`uuid.UUID` instance.
251
+ - `uuid_representation`: A member of
252
+ :class:`~bson.binary.UuidRepresentation`. Default:
253
+ :const:`~bson.binary.UuidRepresentation.STANDARD`.
254
+ See :ref:`handling-uuid-data-example` for details.
255
+
256
+ .. versionadded:: 3.11
257
+ """
258
+ if not isinstance(uuid, UUID):
259
+ raise TypeError("uuid must be an instance of uuid.UUID")
260
+
261
+ if uuid_representation not in ALL_UUID_REPRESENTATIONS:
262
+ raise ValueError(
263
+ "uuid_representation must be a value from bson.binary.UuidRepresentation"
264
+ )
265
+
266
+ if uuid_representation == UuidRepresentation.UNSPECIFIED:
267
+ raise ValueError(
268
+ "cannot encode native uuid.UUID with "
269
+ "UuidRepresentation.UNSPECIFIED. UUIDs can be manually "
270
+ "converted to bson.Binary instances using "
271
+ "bson.Binary.from_uuid() or a different UuidRepresentation "
272
+ "can be configured. See the documentation for "
273
+ "UuidRepresentation for more information."
274
+ )
275
+
276
+ subtype = OLD_UUID_SUBTYPE
277
+ if uuid_representation == UuidRepresentation.PYTHON_LEGACY:
278
+ payload = uuid.bytes
279
+ elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
280
+ from_uuid = uuid.bytes
281
+ payload = from_uuid[0:8][::-1] + from_uuid[8:16][::-1]
282
+ elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
283
+ payload = uuid.bytes_le
284
+ else:
285
+ # uuid_representation == UuidRepresentation.STANDARD
286
+ subtype = UUID_SUBTYPE
287
+ payload = uuid.bytes
288
+
289
+ return cls(payload, subtype)
290
+
291
+ def as_uuid(self, uuid_representation: int = UuidRepresentation.STANDARD) -> UUID:
292
+ """Create a Python UUID from this BSON Binary object.
293
+
294
+ Decodes this binary object as a native :class:`uuid.UUID` instance
295
+ with the provided ``uuid_representation``.
296
+
297
+ Raises :exc:`ValueError` if this :class:`~bson.binary.Binary` instance
298
+ does not contain a UUID.
299
+
300
+ :Parameters:
301
+ - `uuid_representation`: A member of
302
+ :class:`~bson.binary.UuidRepresentation`. Default:
303
+ :const:`~bson.binary.UuidRepresentation.STANDARD`.
304
+ See :ref:`handling-uuid-data-example` for details.
305
+
306
+ .. versionadded:: 3.11
307
+ """
308
+ if self.subtype not in ALL_UUID_SUBTYPES:
309
+ raise ValueError("cannot decode subtype %s as a uuid" % (self.subtype,))
310
+
311
+ if uuid_representation not in ALL_UUID_REPRESENTATIONS:
312
+ raise ValueError(
313
+ "uuid_representation must be a value from bson.binary.UuidRepresentation"
314
+ )
315
+
316
+ if uuid_representation == UuidRepresentation.UNSPECIFIED:
317
+ raise ValueError("uuid_representation cannot be UNSPECIFIED")
318
+ elif uuid_representation == UuidRepresentation.PYTHON_LEGACY:
319
+ if self.subtype == OLD_UUID_SUBTYPE:
320
+ return UUID(bytes=self)
321
+ elif uuid_representation == UuidRepresentation.JAVA_LEGACY:
322
+ if self.subtype == OLD_UUID_SUBTYPE:
323
+ return UUID(bytes=self[0:8][::-1] + self[8:16][::-1])
324
+ elif uuid_representation == UuidRepresentation.CSHARP_LEGACY:
325
+ if self.subtype == OLD_UUID_SUBTYPE:
326
+ return UUID(bytes_le=self)
327
+ else:
328
+ # uuid_representation == UuidRepresentation.STANDARD
329
+ if self.subtype == UUID_SUBTYPE:
330
+ return UUID(bytes=self)
331
+
332
+ raise ValueError(
333
+ "cannot decode subtype %s to %s"
334
+ % (self.subtype, UUID_REPRESENTATION_NAMES[uuid_representation])
335
+ )
336
+
337
+ @property
338
+ def subtype(self) -> int:
339
+ """Subtype of this binary data."""
340
+ return self.__subtype
341
+
342
+ def __getnewargs__(self) -> Tuple[bytes, int]: # type: ignore[override]
343
+ # Work around http://bugs.python.org/issue7382
344
+ data = super(Binary, self).__getnewargs__()[0]
345
+ if not isinstance(data, bytes):
346
+ data = data.encode("latin-1")
347
+ return data, self.__subtype
348
+
349
+ def __eq__(self, other: Any) -> bool:
350
+ if isinstance(other, Binary):
351
+ return (self.__subtype, bytes(self)) == (other.subtype, bytes(other))
352
+ # We don't return NotImplemented here because if we did then
353
+ # Binary("foo") == "foo" would return True, since Binary is a
354
+ # subclass of str...
355
+ return False
356
+
357
+ def __hash__(self) -> int:
358
+ return super(Binary, self).__hash__() ^ hash(self.__subtype)
359
+
360
+ def __ne__(self, other: Any) -> bool:
361
+ return not self == other
362
+
363
+ def __repr__(self):
364
+ return "Binary(%s, %s)" % (bytes.__repr__(self), self.__subtype)
data/lib/bson/code.py ADDED
@@ -0,0 +1,101 @@
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
+ """Tools for representing JavaScript code in BSON.
16
+ """
17
+
18
+ from collections.abc import Mapping as _Mapping
19
+ from typing import Any, Mapping, Optional, Type, Union
20
+
21
+
22
+ class Code(str):
23
+ """BSON's JavaScript code type.
24
+
25
+ Raises :class:`TypeError` if `code` is not an instance of
26
+ :class:`basestring` (:class:`str` in python 3) or `scope`
27
+ is not ``None`` or an instance of :class:`dict`.
28
+
29
+ Scope variables can be set by passing a dictionary as the `scope`
30
+ argument or by using keyword arguments. If a variable is set as a
31
+ keyword argument it will override any setting for that variable in
32
+ the `scope` dictionary.
33
+
34
+ :Parameters:
35
+ - `code`: A string containing JavaScript code to be evaluated or another
36
+ instance of Code. In the latter case, the scope of `code` becomes this
37
+ Code's :attr:`scope`.
38
+ - `scope` (optional): dictionary representing the scope in which
39
+ `code` should be evaluated - a mapping from identifiers (as
40
+ strings) to values. Defaults to ``None``. This is applied after any
41
+ scope associated with a given `code` above.
42
+ - `**kwargs` (optional): scope variables can also be passed as
43
+ keyword arguments. These are applied after `scope` and `code`.
44
+
45
+ .. versionchanged:: 3.4
46
+ The default value for :attr:`scope` is ``None`` instead of ``{}``.
47
+
48
+ """
49
+
50
+ _type_marker = 13
51
+ __scope: Union[Mapping[str, Any], None]
52
+
53
+ def __new__(
54
+ cls: Type["Code"],
55
+ code: Union[str, "Code"],
56
+ scope: Optional[Mapping[str, Any]] = None,
57
+ **kwargs: Any
58
+ ) -> "Code":
59
+ if not isinstance(code, str):
60
+ raise TypeError("code must be an instance of str")
61
+
62
+ self = str.__new__(cls, code)
63
+
64
+ try:
65
+ self.__scope = code.scope # type: ignore
66
+ except AttributeError:
67
+ self.__scope = None
68
+
69
+ if scope is not None:
70
+ if not isinstance(scope, _Mapping):
71
+ raise TypeError("scope must be an instance of dict")
72
+ if self.__scope is not None:
73
+ self.__scope.update(scope) # type: ignore
74
+ else:
75
+ self.__scope = scope
76
+
77
+ if kwargs:
78
+ if self.__scope is not None:
79
+ self.__scope.update(kwargs) # type: ignore
80
+ else:
81
+ self.__scope = kwargs
82
+
83
+ return self
84
+
85
+ @property
86
+ def scope(self) -> Optional[Mapping[str, Any]]:
87
+ """Scope dictionary for this instance or ``None``."""
88
+ return self.__scope
89
+
90
+ def __repr__(self):
91
+ return "Code(%s, %r)" % (str.__repr__(self), self.__scope)
92
+
93
+ def __eq__(self, other: Any) -> bool:
94
+ if isinstance(other, Code):
95
+ return (self.__scope, str(self)) == (other.__scope, str(other))
96
+ return False
97
+
98
+ __hash__: Any = None
99
+
100
+ def __ne__(self, other: Any) -> bool:
101
+ return not self == other