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
data/lib/bson/regex.py ADDED
@@ -0,0 +1,135 @@
1
+ # Copyright 2013-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 MongoDB regular expressions.
16
+ """
17
+
18
+ import re
19
+ from typing import Any, Generic, Pattern, Type, TypeVar, Union
20
+
21
+ from bson._helpers import _getstate_slots, _setstate_slots
22
+ from bson.son import RE_TYPE
23
+
24
+
25
+ def str_flags_to_int(str_flags: str) -> int:
26
+ flags = 0
27
+ if "i" in str_flags:
28
+ flags |= re.IGNORECASE
29
+ if "l" in str_flags:
30
+ flags |= re.LOCALE
31
+ if "m" in str_flags:
32
+ flags |= re.MULTILINE
33
+ if "s" in str_flags:
34
+ flags |= re.DOTALL
35
+ if "u" in str_flags:
36
+ flags |= re.UNICODE
37
+ if "x" in str_flags:
38
+ flags |= re.VERBOSE
39
+
40
+ return flags
41
+
42
+
43
+ _T = TypeVar("_T", str, bytes)
44
+
45
+
46
+ class Regex(Generic[_T]):
47
+ """BSON regular expression data."""
48
+
49
+ __slots__ = ("pattern", "flags")
50
+
51
+ __getstate__ = _getstate_slots
52
+ __setstate__ = _setstate_slots
53
+
54
+ _type_marker = 11
55
+
56
+ @classmethod
57
+ def from_native(cls: Type["Regex"], regex: "Pattern[_T]") -> "Regex[_T]":
58
+ """Convert a Python regular expression into a ``Regex`` instance.
59
+
60
+ Note that in Python 3, a regular expression compiled from a
61
+ :class:`str` has the ``re.UNICODE`` flag set. If it is undesirable
62
+ to store this flag in a BSON regular expression, unset it first::
63
+
64
+ >>> pattern = re.compile('.*')
65
+ >>> regex = Regex.from_native(pattern)
66
+ >>> regex.flags ^= re.UNICODE
67
+ >>> db.collection.insert_one({'pattern': regex})
68
+
69
+ :Parameters:
70
+ - `regex`: A regular expression object from ``re.compile()``.
71
+
72
+ .. warning::
73
+ Python regular expressions use a different syntax and different
74
+ set of flags than MongoDB, which uses `PCRE`_. A regular
75
+ expression retrieved from the server may not compile in
76
+ Python, or may match a different set of strings in Python than
77
+ when used in a MongoDB query.
78
+
79
+ .. _PCRE: http://www.pcre.org/
80
+ """
81
+ if not isinstance(regex, RE_TYPE):
82
+ raise TypeError("regex must be a compiled regular expression, not %s" % type(regex))
83
+
84
+ return Regex(regex.pattern, regex.flags)
85
+
86
+ def __init__(self, pattern: _T, flags: Union[str, int] = 0) -> None:
87
+ """BSON regular expression data.
88
+
89
+ This class is useful to store and retrieve regular expressions that are
90
+ incompatible with Python's regular expression dialect.
91
+
92
+ :Parameters:
93
+ - `pattern`: string
94
+ - `flags`: (optional) an integer bitmask, or a string of flag
95
+ characters like "im" for IGNORECASE and MULTILINE
96
+ """
97
+ if not isinstance(pattern, (str, bytes)):
98
+ raise TypeError("pattern must be a string, not %s" % type(pattern))
99
+ self.pattern: _T = pattern
100
+
101
+ if isinstance(flags, str):
102
+ self.flags = str_flags_to_int(flags)
103
+ elif isinstance(flags, int):
104
+ self.flags = flags
105
+ else:
106
+ raise TypeError("flags must be a string or int, not %s" % type(flags))
107
+
108
+ def __eq__(self, other: Any) -> bool:
109
+ if isinstance(other, Regex):
110
+ return self.pattern == other.pattern and self.flags == other.flags
111
+ else:
112
+ return NotImplemented
113
+
114
+ __hash__ = None # type: ignore
115
+
116
+ def __ne__(self, other: Any) -> bool:
117
+ return not self == other
118
+
119
+ def __repr__(self):
120
+ return "Regex(%r, %r)" % (self.pattern, self.flags)
121
+
122
+ def try_compile(self) -> "Pattern[_T]":
123
+ """Compile this :class:`Regex` as a Python regular expression.
124
+
125
+ .. warning::
126
+ Python regular expressions use a different syntax and different
127
+ set of flags than MongoDB, which uses `PCRE`_. A regular
128
+ expression retrieved from the server may not compile in
129
+ Python, or may match a different set of strings in Python than
130
+ when used in a MongoDB query. :meth:`try_compile()` may raise
131
+ :exc:`re.error`.
132
+
133
+ .. _PCRE: http://www.pcre.org/
134
+ """
135
+ return re.compile(self.pattern, self.flags)
data/lib/bson/son.py ADDED
@@ -0,0 +1,208 @@
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 creating and manipulating SON, the Serialized Ocument Notation.
16
+
17
+ Regular dictionaries can be used instead of SON objects, but not when the order
18
+ of keys is important. A SON object can be used just like a normal Python
19
+ dictionary."""
20
+
21
+ import copy
22
+ import re
23
+ from collections.abc import Mapping as _Mapping
24
+ from typing import (
25
+ Any,
26
+ Dict,
27
+ Iterable,
28
+ Iterator,
29
+ List,
30
+ Mapping,
31
+ Optional,
32
+ Pattern,
33
+ Tuple,
34
+ Type,
35
+ TypeVar,
36
+ Union,
37
+ )
38
+
39
+ # This sort of sucks, but seems to be as good as it gets...
40
+ # This is essentially the same as re._pattern_type
41
+ RE_TYPE: Type[Pattern[Any]] = type(re.compile(""))
42
+
43
+ _Key = TypeVar("_Key")
44
+ _Value = TypeVar("_Value")
45
+ _T = TypeVar("_T")
46
+
47
+
48
+ class SON(Dict[_Key, _Value]):
49
+ """SON data.
50
+
51
+ A subclass of dict that maintains ordering of keys and provides a
52
+ few extra niceties for dealing with SON. SON provides an API
53
+ similar to collections.OrderedDict.
54
+ """
55
+
56
+ __keys: List[Any]
57
+
58
+ def __init__(
59
+ self,
60
+ data: Optional[Union[Mapping[_Key, _Value], Iterable[Tuple[_Key, _Value]]]] = None,
61
+ **kwargs: Any
62
+ ) -> None:
63
+ self.__keys = []
64
+ dict.__init__(self)
65
+ self.update(data)
66
+ self.update(kwargs)
67
+
68
+ def __new__(cls: Type["SON[_Key, _Value]"], *args: Any, **kwargs: Any) -> "SON[_Key, _Value]":
69
+ instance = super(SON, cls).__new__(cls, *args, **kwargs)
70
+ instance.__keys = []
71
+ return instance
72
+
73
+ def __repr__(self):
74
+ result = []
75
+ for key in self.__keys:
76
+ result.append("(%r, %r)" % (key, self[key]))
77
+ return "SON([%s])" % ", ".join(result)
78
+
79
+ def __setitem__(self, key: _Key, value: _Value) -> None:
80
+ if key not in self.__keys:
81
+ self.__keys.append(key)
82
+ dict.__setitem__(self, key, value)
83
+
84
+ def __delitem__(self, key: _Key) -> None:
85
+ self.__keys.remove(key)
86
+ dict.__delitem__(self, key)
87
+
88
+ def copy(self) -> "SON[_Key, _Value]":
89
+ other: SON[_Key, _Value] = SON()
90
+ other.update(self)
91
+ return other
92
+
93
+ # TODO this is all from UserDict.DictMixin. it could probably be made more
94
+ # efficient.
95
+ # second level definitions support higher levels
96
+ def __iter__(self) -> Iterator[_Key]:
97
+ for k in self.__keys:
98
+ yield k
99
+
100
+ def has_key(self, key: _Key) -> bool:
101
+ return key in self.__keys
102
+
103
+ def iterkeys(self) -> Iterator[_Key]:
104
+ return self.__iter__()
105
+
106
+ # fourth level uses definitions from lower levels
107
+ def itervalues(self) -> Iterator[_Value]:
108
+ for _, v in self.items():
109
+ yield v
110
+
111
+ def values(self) -> List[_Value]: # type: ignore[override]
112
+ return [v for _, v in self.items()]
113
+
114
+ def clear(self) -> None:
115
+ self.__keys = []
116
+ super(SON, self).clear()
117
+
118
+ def setdefault(self, key: _Key, default: _Value) -> _Value: # type: ignore[override]
119
+ try:
120
+ return self[key]
121
+ except KeyError:
122
+ self[key] = default
123
+ return default
124
+
125
+ def pop(self, key: _Key, *args: Union[_Value, _T]) -> Union[_Value, _T]:
126
+ if len(args) > 1:
127
+ raise TypeError("pop expected at most 2 arguments, got " + repr(1 + len(args)))
128
+ try:
129
+ value = self[key]
130
+ except KeyError:
131
+ if args:
132
+ return args[0]
133
+ raise
134
+ del self[key]
135
+ return value
136
+
137
+ def popitem(self) -> Tuple[_Key, _Value]:
138
+ try:
139
+ k, v = next(iter(self.items()))
140
+ except StopIteration:
141
+ raise KeyError("container is empty")
142
+ del self[k]
143
+ return (k, v)
144
+
145
+ def update(self, other: Optional[Any] = None, **kwargs: _Value) -> None: # type: ignore[override]
146
+ # Make progressively weaker assumptions about "other"
147
+ if other is None:
148
+ pass
149
+ elif hasattr(other, "items"):
150
+ for k, v in other.items():
151
+ self[k] = v
152
+ elif hasattr(other, "keys"):
153
+ for k in other.keys():
154
+ self[k] = other[k]
155
+ else:
156
+ for k, v in other:
157
+ self[k] = v
158
+ if kwargs:
159
+ self.update(kwargs)
160
+
161
+ def get(self, key: _Key, default: Optional[Union[_Value, _T]] = None) -> Union[_Value, _T, None]: # type: ignore[override]
162
+ try:
163
+ return self[key]
164
+ except KeyError:
165
+ return default
166
+
167
+ def __eq__(self, other: Any) -> bool:
168
+ """Comparison to another SON is order-sensitive while comparison to a
169
+ regular dictionary is order-insensitive.
170
+ """
171
+ if isinstance(other, SON):
172
+ return len(self) == len(other) and list(self.items()) == list(other.items())
173
+ return self.to_dict() == other
174
+
175
+ def __ne__(self, other: Any) -> bool:
176
+ return not self == other
177
+
178
+ def __len__(self) -> int:
179
+ return len(self.__keys)
180
+
181
+ def to_dict(self) -> Dict[_Key, _Value]:
182
+ """Convert a SON document to a normal Python dictionary instance.
183
+
184
+ This is trickier than just *dict(...)* because it needs to be
185
+ recursive.
186
+ """
187
+
188
+ def transform_value(value: Any) -> Any:
189
+ if isinstance(value, list):
190
+ return [transform_value(v) for v in value]
191
+ elif isinstance(value, _Mapping):
192
+ return dict([(k, transform_value(v)) for k, v in value.items()])
193
+ else:
194
+ return value
195
+
196
+ return transform_value(dict(self))
197
+
198
+ def __deepcopy__(self, memo: Dict[int, "SON[_Key, _Value]"]) -> "SON[_Key, _Value]":
199
+ out: SON[_Key, _Value] = SON()
200
+ val_id = id(self)
201
+ if val_id in memo:
202
+ return memo[val_id]
203
+ memo[val_id] = out
204
+ for k, v in self.items():
205
+ if not isinstance(v, RE_TYPE):
206
+ v = copy.deepcopy(v, memo)
207
+ out[k] = v
208
+ return out
@@ -0,0 +1,124 @@
1
+ # Copyright 2010-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 representing MongoDB internal Timestamps.
16
+ """
17
+
18
+ import calendar
19
+ import datetime
20
+ from typing import Any, Union
21
+
22
+ from bson._helpers import _getstate_slots, _setstate_slots
23
+ from bson.tz_util import utc
24
+
25
+ UPPERBOUND = 4294967296
26
+
27
+
28
+ class Timestamp(object):
29
+ """MongoDB internal timestamps used in the opLog."""
30
+
31
+ __slots__ = ("__time", "__inc")
32
+
33
+ __getstate__ = _getstate_slots
34
+ __setstate__ = _setstate_slots
35
+
36
+ _type_marker = 17
37
+
38
+ def __init__(self, time: Union[datetime.datetime, int], inc: int) -> None:
39
+ """Create a new :class:`Timestamp`.
40
+
41
+ This class is only for use with the MongoDB opLog. If you need
42
+ to store a regular timestamp, please use a
43
+ :class:`~datetime.datetime`.
44
+
45
+ Raises :class:`TypeError` if `time` is not an instance of
46
+ :class: `int` or :class:`~datetime.datetime`, or `inc` is not
47
+ an instance of :class:`int`. Raises :class:`ValueError` if
48
+ `time` or `inc` is not in [0, 2**32).
49
+
50
+ :Parameters:
51
+ - `time`: time in seconds since epoch UTC, or a naive UTC
52
+ :class:`~datetime.datetime`, or an aware
53
+ :class:`~datetime.datetime`
54
+ - `inc`: the incrementing counter
55
+ """
56
+ if isinstance(time, datetime.datetime):
57
+ offset = time.utcoffset()
58
+ if offset is not None:
59
+ time = time - offset
60
+ time = int(calendar.timegm(time.timetuple()))
61
+ if not isinstance(time, int):
62
+ raise TypeError("time must be an instance of int")
63
+ if not isinstance(inc, int):
64
+ raise TypeError("inc must be an instance of int")
65
+ if not 0 <= time < UPPERBOUND:
66
+ raise ValueError("time must be contained in [0, 2**32)")
67
+ if not 0 <= inc < UPPERBOUND:
68
+ raise ValueError("inc must be contained in [0, 2**32)")
69
+
70
+ self.__time = time
71
+ self.__inc = inc
72
+
73
+ @property
74
+ def time(self) -> int:
75
+ """Get the time portion of this :class:`Timestamp`."""
76
+ return self.__time
77
+
78
+ @property
79
+ def inc(self) -> int:
80
+ """Get the inc portion of this :class:`Timestamp`."""
81
+ return self.__inc
82
+
83
+ def __eq__(self, other: Any) -> bool:
84
+ if isinstance(other, Timestamp):
85
+ return self.__time == other.time and self.__inc == other.inc
86
+ else:
87
+ return NotImplemented
88
+
89
+ def __hash__(self) -> int:
90
+ return hash(self.time) ^ hash(self.inc)
91
+
92
+ def __ne__(self, other: Any) -> bool:
93
+ return not self == other
94
+
95
+ def __lt__(self, other: Any) -> bool:
96
+ if isinstance(other, Timestamp):
97
+ return (self.time, self.inc) < (other.time, other.inc)
98
+ return NotImplemented
99
+
100
+ def __le__(self, other: Any) -> bool:
101
+ if isinstance(other, Timestamp):
102
+ return (self.time, self.inc) <= (other.time, other.inc)
103
+ return NotImplemented
104
+
105
+ def __gt__(self, other: Any) -> bool:
106
+ if isinstance(other, Timestamp):
107
+ return (self.time, self.inc) > (other.time, other.inc)
108
+ return NotImplemented
109
+
110
+ def __ge__(self, other: Any) -> bool:
111
+ if isinstance(other, Timestamp):
112
+ return (self.time, self.inc) >= (other.time, other.inc)
113
+ return NotImplemented
114
+
115
+ def __repr__(self):
116
+ return "Timestamp(%s, %s)" % (self.__time, self.__inc)
117
+
118
+ def as_datetime(self) -> datetime.datetime:
119
+ """Return a :class:`~datetime.datetime` instance corresponding
120
+ to the time portion of this :class:`Timestamp`.
121
+
122
+ The returned datetime's timezone is UTC.
123
+ """
124
+ return datetime.datetime.fromtimestamp(self.__time, utc)
@@ -0,0 +1,52 @@
1
+ # Copyright 2010-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
+ """Timezone related utilities for BSON."""
16
+
17
+ from datetime import datetime, timedelta, tzinfo
18
+ from typing import Optional, Tuple, Union
19
+
20
+ ZERO: timedelta = timedelta(0)
21
+
22
+
23
+ class FixedOffset(tzinfo):
24
+ """Fixed offset timezone, in minutes east from UTC.
25
+
26
+ Implementation based from the Python `standard library documentation
27
+ <http://docs.python.org/library/datetime.html#tzinfo-objects>`_.
28
+ Defining __getinitargs__ enables pickling / copying.
29
+ """
30
+
31
+ def __init__(self, offset: Union[float, timedelta], name: str) -> None:
32
+ if isinstance(offset, timedelta):
33
+ self.__offset = offset
34
+ else:
35
+ self.__offset = timedelta(minutes=offset)
36
+ self.__name = name
37
+
38
+ def __getinitargs__(self) -> Tuple[timedelta, str]:
39
+ return self.__offset, self.__name
40
+
41
+ def utcoffset(self, dt: Optional[datetime]) -> timedelta:
42
+ return self.__offset
43
+
44
+ def tzname(self, dt: Optional[datetime]) -> str:
45
+ return self.__name
46
+
47
+ def dst(self, dt: Optional[datetime]) -> timedelta:
48
+ return ZERO
49
+
50
+
51
+ utc: FixedOffset = FixedOffset(0, "UTC")
52
+ """Fixed offset timezone representing UTC."""