driver_info.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright 2018-present MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you
  4. # may not use this file except in compliance with the License. You
  5. # 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
  12. # implied. See the License for the specific language governing
  13. # permissions and limitations under the License.
  14. """Advanced options for MongoDB drivers implemented on top of PyMongo."""
  15. from collections import namedtuple
  16. from bson.py3compat import string_type
  17. class DriverInfo(namedtuple('DriverInfo', ['name', 'version', 'platform'])):
  18. """Info about a driver wrapping PyMongo.
  19. The MongoDB server logs PyMongo's name, version, and platform whenever
  20. PyMongo establishes a connection. A driver implemented on top of PyMongo
  21. can add its own info to this log message. Initialize with three strings
  22. like 'MyDriver', '1.2.3', 'some platform info'. Any of these strings may be
  23. None to accept PyMongo's default.
  24. """
  25. def __new__(cls, name=None, version=None, platform=None):
  26. self = super(DriverInfo, cls).__new__(cls, name, version, platform)
  27. for name, value in self._asdict().items():
  28. if value is not None and not isinstance(value, string_type):
  29. raise TypeError("Wrong type for DriverInfo %s option, value "
  30. "must be an instance of %s" % (
  31. name, string_type.__name__))
  32. return self