codec_options.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. # Copyright 2014-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. """Tools for specifying BSON codec options."""
  15. import datetime
  16. import warnings
  17. from abc import abstractmethod
  18. from collections import namedtuple
  19. from bson.py3compat import ABC, abc, abstractproperty, string_type
  20. from bson.binary import (UuidRepresentation,
  21. ALL_UUID_REPRESENTATIONS,
  22. UUID_REPRESENTATION_NAMES)
  23. _RAW_BSON_DOCUMENT_MARKER = 101
  24. def _raw_document_class(document_class):
  25. """Determine if a document_class is a RawBSONDocument class."""
  26. marker = getattr(document_class, '_type_marker', None)
  27. return marker == _RAW_BSON_DOCUMENT_MARKER
  28. class TypeEncoder(ABC):
  29. """Base class for defining type codec classes which describe how a
  30. custom type can be transformed to one of the types BSON understands.
  31. Codec classes must implement the ``python_type`` attribute, and the
  32. ``transform_python`` method to support encoding.
  33. See :ref:`custom-type-type-codec` documentation for an example.
  34. """
  35. @abstractproperty
  36. def python_type(self):
  37. """The Python type to be converted into something serializable."""
  38. pass
  39. @abstractmethod
  40. def transform_python(self, value):
  41. """Convert the given Python object into something serializable."""
  42. pass
  43. class TypeDecoder(ABC):
  44. """Base class for defining type codec classes which describe how a
  45. BSON type can be transformed to a custom type.
  46. Codec classes must implement the ``bson_type`` attribute, and the
  47. ``transform_bson`` method to support decoding.
  48. See :ref:`custom-type-type-codec` documentation for an example.
  49. """
  50. @abstractproperty
  51. def bson_type(self):
  52. """The BSON type to be converted into our own type."""
  53. pass
  54. @abstractmethod
  55. def transform_bson(self, value):
  56. """Convert the given BSON value into our own type."""
  57. pass
  58. class TypeCodec(TypeEncoder, TypeDecoder):
  59. """Base class for defining type codec classes which describe how a
  60. custom type can be transformed to/from one of the types :mod:`bson`
  61. can already encode/decode.
  62. Codec classes must implement the ``python_type`` attribute, and the
  63. ``transform_python`` method to support encoding, as well as the
  64. ``bson_type`` attribute, and the ``transform_bson`` method to support
  65. decoding.
  66. See :ref:`custom-type-type-codec` documentation for an example.
  67. """
  68. pass
  69. class TypeRegistry(object):
  70. """Encapsulates type codecs used in encoding and / or decoding BSON, as
  71. well as the fallback encoder. Type registries cannot be modified after
  72. instantiation.
  73. ``TypeRegistry`` can be initialized with an iterable of type codecs, and
  74. a callable for the fallback encoder::
  75. >>> from bson.codec_options import TypeRegistry
  76. >>> type_registry = TypeRegistry([Codec1, Codec2, Codec3, ...],
  77. ... fallback_encoder)
  78. See :ref:`custom-type-type-registry` documentation for an example.
  79. :Parameters:
  80. - `type_codecs` (optional): iterable of type codec instances. If
  81. ``type_codecs`` contains multiple codecs that transform a single
  82. python or BSON type, the transformation specified by the type codec
  83. occurring last prevails. A TypeError will be raised if one or more
  84. type codecs modify the encoding behavior of a built-in :mod:`bson`
  85. type.
  86. - `fallback_encoder` (optional): callable that accepts a single,
  87. unencodable python value and transforms it into a type that
  88. :mod:`bson` can encode. See :ref:`fallback-encoder-callable`
  89. documentation for an example.
  90. """
  91. def __init__(self, type_codecs=None, fallback_encoder=None):
  92. self.__type_codecs = list(type_codecs or [])
  93. self._fallback_encoder = fallback_encoder
  94. self._encoder_map = {}
  95. self._decoder_map = {}
  96. if self._fallback_encoder is not None:
  97. if not callable(fallback_encoder):
  98. raise TypeError("fallback_encoder %r is not a callable" % (
  99. fallback_encoder))
  100. for codec in self.__type_codecs:
  101. is_valid_codec = False
  102. if isinstance(codec, TypeEncoder):
  103. self._validate_type_encoder(codec)
  104. is_valid_codec = True
  105. self._encoder_map[codec.python_type] = codec.transform_python
  106. if isinstance(codec, TypeDecoder):
  107. is_valid_codec = True
  108. self._decoder_map[codec.bson_type] = codec.transform_bson
  109. if not is_valid_codec:
  110. raise TypeError(
  111. "Expected an instance of %s, %s, or %s, got %r instead" % (
  112. TypeEncoder.__name__, TypeDecoder.__name__,
  113. TypeCodec.__name__, codec))
  114. def _validate_type_encoder(self, codec):
  115. from bson import _BUILT_IN_TYPES
  116. for pytype in _BUILT_IN_TYPES:
  117. if issubclass(codec.python_type, pytype):
  118. err_msg = ("TypeEncoders cannot change how built-in types are "
  119. "encoded (encoder %s transforms type %s)" %
  120. (codec, pytype))
  121. raise TypeError(err_msg)
  122. def __repr__(self):
  123. return ('%s(type_codecs=%r, fallback_encoder=%r)' % (
  124. self.__class__.__name__, self.__type_codecs,
  125. self._fallback_encoder))
  126. def __eq__(self, other):
  127. if not isinstance(other, type(self)):
  128. return NotImplemented
  129. return ((self._decoder_map == other._decoder_map) and
  130. (self._encoder_map == other._encoder_map) and
  131. (self._fallback_encoder == other._fallback_encoder))
  132. _options_base = namedtuple(
  133. 'CodecOptions',
  134. ('document_class', 'tz_aware', 'uuid_representation',
  135. 'unicode_decode_error_handler', 'tzinfo', 'type_registry'))
  136. class CodecOptions(_options_base):
  137. """Encapsulates options used encoding and / or decoding BSON.
  138. The `document_class` option is used to define a custom type for use
  139. decoding BSON documents. Access to the underlying raw BSON bytes for
  140. a document is available using the :class:`~bson.raw_bson.RawBSONDocument`
  141. type::
  142. >>> from bson.raw_bson import RawBSONDocument
  143. >>> from bson.codec_options import CodecOptions
  144. >>> codec_options = CodecOptions(document_class=RawBSONDocument)
  145. >>> coll = db.get_collection('test', codec_options=codec_options)
  146. >>> doc = coll.find_one()
  147. >>> doc.raw
  148. '\\x16\\x00\\x00\\x00\\x07_id\\x00[0\\x165\\x91\\x10\\xea\\x14\\xe8\\xc5\\x8b\\x93\\x00'
  149. The document class can be any type that inherits from
  150. :class:`~collections.MutableMapping`::
  151. >>> class AttributeDict(dict):
  152. ... # A dict that supports attribute access.
  153. ... def __getattr__(self, key):
  154. ... return self[key]
  155. ... def __setattr__(self, key, value):
  156. ... self[key] = value
  157. ...
  158. >>> codec_options = CodecOptions(document_class=AttributeDict)
  159. >>> coll = db.get_collection('test', codec_options=codec_options)
  160. >>> doc = coll.find_one()
  161. >>> doc._id
  162. ObjectId('5b3016359110ea14e8c58b93')
  163. See :doc:`/examples/datetimes` for examples using the `tz_aware` and
  164. `tzinfo` options.
  165. See :class:`~bson.binary.UUIDLegacy` for examples using the
  166. `uuid_representation` option.
  167. :Parameters:
  168. - `document_class`: BSON documents returned in queries will be decoded
  169. to an instance of this class. Must be a subclass of
  170. :class:`~collections.MutableMapping`. Defaults to :class:`dict`.
  171. - `tz_aware`: If ``True``, BSON datetimes will be decoded to timezone
  172. aware instances of :class:`~datetime.datetime`. Otherwise they will be
  173. naive. Defaults to ``False``.
  174. - `uuid_representation`: The BSON representation to use when encoding
  175. and decoding instances of :class:`~uuid.UUID`. Defaults to
  176. :data:`~bson.binary.UuidRepresentation.PYTHON_LEGACY`. New
  177. applications should consider setting this to
  178. :data:`~bson.binary.UuidRepresentation.STANDARD` for cross language
  179. compatibility. See :ref:`handling-uuid-data-example` for details.
  180. - `unicode_decode_error_handler`: The error handler to apply when
  181. a Unicode-related error occurs during BSON decoding that would
  182. otherwise raise :exc:`UnicodeDecodeError`. Valid options include
  183. 'strict', 'replace', and 'ignore'. Defaults to 'strict'.
  184. - `tzinfo`: A :class:`~datetime.tzinfo` subclass that specifies the
  185. timezone to/from which :class:`~datetime.datetime` objects should be
  186. encoded/decoded.
  187. - `type_registry`: Instance of :class:`TypeRegistry` used to customize
  188. encoding and decoding behavior.
  189. .. versionadded:: 3.8
  190. `type_registry` attribute.
  191. .. warning:: Care must be taken when changing
  192. `unicode_decode_error_handler` from its default value ('strict').
  193. The 'replace' and 'ignore' modes should not be used when documents
  194. retrieved from the server will be modified in the client application
  195. and stored back to the server.
  196. """
  197. def __new__(cls, document_class=dict,
  198. tz_aware=False,
  199. uuid_representation=None,
  200. unicode_decode_error_handler="strict",
  201. tzinfo=None, type_registry=None):
  202. if not (issubclass(document_class, abc.MutableMapping) or
  203. _raw_document_class(document_class)):
  204. raise TypeError("document_class must be dict, bson.son.SON, "
  205. "bson.raw_bson.RawBSONDocument, or a "
  206. "sublass of collections.MutableMapping")
  207. if not isinstance(tz_aware, bool):
  208. raise TypeError("tz_aware must be True or False")
  209. if uuid_representation is None:
  210. uuid_representation = UuidRepresentation.PYTHON_LEGACY
  211. elif uuid_representation not in ALL_UUID_REPRESENTATIONS:
  212. raise ValueError("uuid_representation must be a value "
  213. "from bson.binary.UuidRepresentation")
  214. if not isinstance(unicode_decode_error_handler, (string_type, None)):
  215. raise ValueError("unicode_decode_error_handler must be a string "
  216. "or None")
  217. if tzinfo is not None:
  218. if not isinstance(tzinfo, datetime.tzinfo):
  219. raise TypeError(
  220. "tzinfo must be an instance of datetime.tzinfo")
  221. if not tz_aware:
  222. raise ValueError(
  223. "cannot specify tzinfo without also setting tz_aware=True")
  224. type_registry = type_registry or TypeRegistry()
  225. if not isinstance(type_registry, TypeRegistry):
  226. raise TypeError("type_registry must be an instance of TypeRegistry")
  227. return tuple.__new__(
  228. cls, (document_class, tz_aware, uuid_representation,
  229. unicode_decode_error_handler, tzinfo, type_registry))
  230. def _arguments_repr(self):
  231. """Representation of the arguments used to create this object."""
  232. document_class_repr = (
  233. 'dict' if self.document_class is dict
  234. else repr(self.document_class))
  235. uuid_rep_repr = UUID_REPRESENTATION_NAMES.get(self.uuid_representation,
  236. self.uuid_representation)
  237. return ('document_class=%s, tz_aware=%r, uuid_representation=%s, '
  238. 'unicode_decode_error_handler=%r, tzinfo=%r, '
  239. 'type_registry=%r' %
  240. (document_class_repr, self.tz_aware, uuid_rep_repr,
  241. self.unicode_decode_error_handler, self.tzinfo,
  242. self.type_registry))
  243. def _options_dict(self):
  244. """Dictionary of the arguments used to create this object."""
  245. # TODO: PYTHON-2442 use _asdict() instead
  246. return {
  247. 'document_class': self.document_class,
  248. 'tz_aware': self.tz_aware,
  249. 'uuid_representation': self.uuid_representation,
  250. 'unicode_decode_error_handler': self.unicode_decode_error_handler,
  251. 'tzinfo': self.tzinfo,
  252. 'type_registry': self.type_registry}
  253. def __repr__(self):
  254. return '%s(%s)' % (self.__class__.__name__, self._arguments_repr())
  255. def with_options(self, **kwargs):
  256. """Make a copy of this CodecOptions, overriding some options::
  257. >>> from bson.codec_options import DEFAULT_CODEC_OPTIONS
  258. >>> DEFAULT_CODEC_OPTIONS.tz_aware
  259. False
  260. >>> options = DEFAULT_CODEC_OPTIONS.with_options(tz_aware=True)
  261. >>> options.tz_aware
  262. True
  263. .. versionadded:: 3.5
  264. """
  265. opts = self._options_dict()
  266. opts.update(kwargs)
  267. return CodecOptions(**opts)
  268. DEFAULT_CODEC_OPTIONS = CodecOptions(
  269. uuid_representation=UuidRepresentation.PYTHON_LEGACY)
  270. def _parse_codec_options(options):
  271. """Parse BSON codec options."""
  272. return CodecOptions(
  273. document_class=options.get(
  274. 'document_class', DEFAULT_CODEC_OPTIONS.document_class),
  275. tz_aware=options.get(
  276. 'tz_aware', DEFAULT_CODEC_OPTIONS.tz_aware),
  277. uuid_representation=options.get('uuidrepresentation'),
  278. unicode_decode_error_handler=options.get(
  279. 'unicode_decode_error_handler',
  280. DEFAULT_CODEC_OPTIONS.unicode_decode_error_handler),
  281. tzinfo=options.get('tzinfo', DEFAULT_CODEC_OPTIONS.tzinfo),
  282. type_registry=options.get(
  283. 'type_registry', DEFAULT_CODEC_OPTIONS.type_registry))