codec_options.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2014-2015 MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Tools for specifying BSON codec options."""
  15. from collections import MutableMapping, namedtuple
  16. from bson.binary import (ALL_UUID_REPRESENTATIONS,
  17. PYTHON_LEGACY,
  18. UUID_REPRESENTATION_NAMES)
  19. _options_base = namedtuple(
  20. 'CodecOptions', ('document_class', 'tz_aware', 'uuid_representation'))
  21. class CodecOptions(_options_base):
  22. """Encapsulates BSON options used in CRUD operations.
  23. :Parameters:
  24. - `document_class`: BSON documents returned in queries will be decoded
  25. to an instance of this class. Must be a subclass of
  26. :class:`~collections.MutableMapping`. Defaults to :class:`dict`.
  27. - `tz_aware`: If ``True``, BSON datetimes will be decoded to timezone
  28. aware instances of :class:`~datetime.datetime`. Otherwise they will be
  29. naive. Defaults to ``False``.
  30. - `uuid_representation`: The BSON representation to use when encoding
  31. and decoding instances of :class:`~uuid.UUID`. Defaults to
  32. :data:`~bson.binary.PYTHON_LEGACY`.
  33. """
  34. def __new__(cls, document_class=dict,
  35. tz_aware=False, uuid_representation=PYTHON_LEGACY):
  36. if not issubclass(document_class, MutableMapping):
  37. raise TypeError("document_class must be dict, bson.son.SON, or "
  38. "another subclass of collections.MutableMapping")
  39. if not isinstance(tz_aware, bool):
  40. raise TypeError("tz_aware must be True or False")
  41. if uuid_representation not in ALL_UUID_REPRESENTATIONS:
  42. raise ValueError("uuid_representation must be a value "
  43. "from bson.binary.ALL_UUID_REPRESENTATIONS")
  44. return tuple.__new__(
  45. cls, (document_class, tz_aware, uuid_representation))
  46. def __repr__(self):
  47. document_class_repr = (
  48. 'dict' if self.document_class is dict
  49. else repr(self.document_class))
  50. uuid_rep_repr = UUID_REPRESENTATION_NAMES.get(self.uuid_representation,
  51. self.uuid_representation)
  52. return (
  53. 'CodecOptions(document_class=%s, tz_aware=%r, uuid_representation='
  54. '%s)' % (document_class_repr, self.tz_aware, uuid_rep_repr))
  55. DEFAULT_CODEC_OPTIONS = CodecOptions()
  56. def _parse_codec_options(options):
  57. """Parse BSON codec options."""
  58. return CodecOptions(
  59. document_class=options.get(
  60. 'document_class', DEFAULT_CODEC_OPTIONS.document_class),
  61. tz_aware=options.get(
  62. 'tz_aware', DEFAULT_CODEC_OPTIONS.tz_aware),
  63. uuid_representation=options.get(
  64. 'uuidrepresentation', DEFAULT_CODEC_OPTIONS.uuid_representation))