|
| 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 | + |
| 15 | +"""Tools for specifying BSON codec options.""" |
| 16 | + |
| 17 | +from bson.binary import (ALL_UUID_REPRESENTATIONS, |
| 18 | + PYTHON_LEGACY, |
| 19 | + UUID_REPRESENTATION_NAMES) |
| 20 | + |
| 21 | + |
| 22 | +class CodecOptions(tuple): |
| 23 | + """Encapsulates BSON options used in CRUD operations. |
| 24 | +
|
| 25 | + :Parameters: |
| 26 | + - `document_class`: BSON documents returned in queries will be decoded |
| 27 | + to an instance of this class. |
| 28 | + - `tz_aware`: If ``True``, BSON datetimes will be decoded to timezone |
| 29 | + aware instances of :class:`~datetime.datetime`. Otherwise they will be |
| 30 | + naive. Defaults to ``False``. |
| 31 | + - `uuid_representation`: The BSON representation to use when encoding |
| 32 | + and decoding instances of :class:`~uuid.UUID`. Defaults to |
| 33 | + :data:`~bson.binary.PYTHON_LEGACY`. |
| 34 | + """ |
| 35 | + |
| 36 | + __slots__ = () |
| 37 | + |
| 38 | + def __new__(cls, document_class=dict, |
| 39 | + tz_aware=False, uuid_representation=PYTHON_LEGACY): |
| 40 | + if not isinstance(tz_aware, bool): |
| 41 | + raise TypeError("tz_aware must be True or False") |
| 42 | + if uuid_representation not in ALL_UUID_REPRESENTATIONS: |
| 43 | + raise ValueError("uuid_representation must be a value " |
| 44 | + "from bson.binary.ALL_UUID_REPRESENTATIONS") |
| 45 | + |
| 46 | + return tuple.__new__( |
| 47 | + cls, (document_class, tz_aware, uuid_representation)) |
| 48 | + |
| 49 | + def __repr__(self): |
| 50 | + if self.document_class is dict: |
| 51 | + document_class_repr = 'dict' |
| 52 | + else: |
| 53 | + document_class_repr = repr(self.document_class) |
| 54 | + |
| 55 | + uuid_rep_repr = UUID_REPRESENTATION_NAMES.get(self.uuid_representation, |
| 56 | + self.uuid_representation) |
| 57 | + |
| 58 | + return ( |
| 59 | + 'CodecOptions(document_class=%s, tz_aware=%r, uuid_representation=' |
| 60 | + '%s)' % (document_class_repr, self.tz_aware, uuid_rep_repr)) |
| 61 | + |
| 62 | + def __getnewargs__(self): |
| 63 | + return tuple(self) |
| 64 | + |
| 65 | + @property |
| 66 | + def document_class(self): |
| 67 | + return self[0] |
| 68 | + |
| 69 | + @property |
| 70 | + def tz_aware(self): |
| 71 | + return self[1] |
| 72 | + |
| 73 | + @property |
| 74 | + def uuid_representation(self): |
| 75 | + return self[2] |
| 76 | + |
| 77 | +DEFAULT_CODEC_OPTIONS = CodecOptions() |
0 commit comments