code.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. """Tools for representing JavaScript code in BSON.
  15. """
  16. from bson.py3compat import abc, string_type, PY3, text_type
  17. class Code(str):
  18. """BSON's JavaScript code type.
  19. Raises :class:`TypeError` if `code` is not an instance of
  20. :class:`basestring` (:class:`str` in python 3) or `scope`
  21. is not ``None`` or an instance of :class:`dict`.
  22. Scope variables can be set by passing a dictionary as the `scope`
  23. argument or by using keyword arguments. If a variable is set as a
  24. keyword argument it will override any setting for that variable in
  25. the `scope` dictionary.
  26. :Parameters:
  27. - `code`: A string containing JavaScript code to be evaluated or another
  28. instance of Code. In the latter case, the scope of `code` becomes this
  29. Code's :attr:`scope`.
  30. - `scope` (optional): dictionary representing the scope in which
  31. `code` should be evaluated - a mapping from identifiers (as
  32. strings) to values. Defaults to ``None``. This is applied after any
  33. scope associated with a given `code` above.
  34. - `**kwargs` (optional): scope variables can also be passed as
  35. keyword arguments. These are applied after `scope` and `code`.
  36. .. versionchanged:: 3.4
  37. The default value for :attr:`scope` is ``None`` instead of ``{}``.
  38. """
  39. _type_marker = 13
  40. def __new__(cls, code, scope=None, **kwargs):
  41. if not isinstance(code, string_type):
  42. raise TypeError("code must be an "
  43. "instance of %s" % (string_type.__name__))
  44. if not PY3 and isinstance(code, text_type):
  45. self = str.__new__(cls, code.encode('utf8'))
  46. else:
  47. self = str.__new__(cls, code)
  48. try:
  49. self.__scope = code.scope
  50. except AttributeError:
  51. self.__scope = None
  52. if scope is not None:
  53. if not isinstance(scope, abc.Mapping):
  54. raise TypeError("scope must be an instance of dict")
  55. if self.__scope is not None:
  56. self.__scope.update(scope)
  57. else:
  58. self.__scope = scope
  59. if kwargs:
  60. if self.__scope is not None:
  61. self.__scope.update(kwargs)
  62. else:
  63. self.__scope = kwargs
  64. return self
  65. @property
  66. def scope(self):
  67. """Scope dictionary for this instance or ``None``.
  68. """
  69. return self.__scope
  70. def __repr__(self):
  71. return "Code(%s, %r)" % (str.__repr__(self), self.__scope)
  72. def __eq__(self, other):
  73. if isinstance(other, Code):
  74. return (self.__scope, str(self)) == (other.__scope, str(other))
  75. return False
  76. __hash__ = None
  77. def __ne__(self, other):
  78. return not self == other