PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/daycapture_venv/lib/python3.8/site-packages/django/utils/deprecation.py

https://gitlab.com/llndhlov/journly
Python | 147 lines | 95 code | 27 blank | 25 comment | 15 complexity | 630ac0f4e2c7923fc35f9eef83fc6032 MD5 | raw file
  1. import asyncio
  2. import inspect
  3. import warnings
  4. from asgiref.sync import sync_to_async
  5. class RemovedInDjango40Warning(DeprecationWarning):
  6. pass
  7. class RemovedInDjango41Warning(PendingDeprecationWarning):
  8. pass
  9. RemovedInNextVersionWarning = RemovedInDjango40Warning
  10. class warn_about_renamed_method:
  11. def __init__(self, class_name, old_method_name, new_method_name, deprecation_warning):
  12. self.class_name = class_name
  13. self.old_method_name = old_method_name
  14. self.new_method_name = new_method_name
  15. self.deprecation_warning = deprecation_warning
  16. def __call__(self, f):
  17. def wrapped(*args, **kwargs):
  18. warnings.warn(
  19. "`%s.%s` is deprecated, use `%s` instead." %
  20. (self.class_name, self.old_method_name, self.new_method_name),
  21. self.deprecation_warning, 2)
  22. return f(*args, **kwargs)
  23. return wrapped
  24. class RenameMethodsBase(type):
  25. """
  26. Handles the deprecation paths when renaming a method.
  27. It does the following:
  28. 1) Define the new method if missing and complain about it.
  29. 2) Define the old method if missing.
  30. 3) Complain whenever an old method is called.
  31. See #15363 for more details.
  32. """
  33. renamed_methods = ()
  34. def __new__(cls, name, bases, attrs):
  35. new_class = super().__new__(cls, name, bases, attrs)
  36. for base in inspect.getmro(new_class):
  37. class_name = base.__name__
  38. for renamed_method in cls.renamed_methods:
  39. old_method_name = renamed_method[0]
  40. old_method = base.__dict__.get(old_method_name)
  41. new_method_name = renamed_method[1]
  42. new_method = base.__dict__.get(new_method_name)
  43. deprecation_warning = renamed_method[2]
  44. wrapper = warn_about_renamed_method(class_name, *renamed_method)
  45. # Define the new method if missing and complain about it
  46. if not new_method and old_method:
  47. warnings.warn(
  48. "`%s.%s` method should be renamed `%s`." %
  49. (class_name, old_method_name, new_method_name),
  50. deprecation_warning, 2)
  51. setattr(base, new_method_name, old_method)
  52. setattr(base, old_method_name, wrapper(old_method))
  53. # Define the old method as a wrapped call to the new method.
  54. if not old_method and new_method:
  55. setattr(base, old_method_name, wrapper(new_method))
  56. return new_class
  57. class DeprecationInstanceCheck(type):
  58. def __instancecheck__(self, instance):
  59. warnings.warn(
  60. "`%s` is deprecated, use `%s` instead." % (self.__name__, self.alternative),
  61. self.deprecation_warning, 2
  62. )
  63. return super().__instancecheck__(instance)
  64. class MiddlewareMixin:
  65. sync_capable = True
  66. async_capable = True
  67. # RemovedInDjango40Warning: when the deprecation ends, replace with:
  68. # def __init__(self, get_response):
  69. def __init__(self, get_response=None):
  70. self._get_response_none_deprecation(get_response)
  71. self.get_response = get_response
  72. self._async_check()
  73. super().__init__()
  74. def _async_check(self):
  75. """
  76. If get_response is a coroutine function, turns us into async mode so
  77. a thread is not consumed during a whole request.
  78. """
  79. if asyncio.iscoroutinefunction(self.get_response):
  80. # Mark the class as async-capable, but do the actual switch
  81. # inside __call__ to avoid swapping out dunder methods
  82. self._is_coroutine = asyncio.coroutines._is_coroutine
  83. def __call__(self, request):
  84. # Exit out to async mode, if needed
  85. if asyncio.iscoroutinefunction(self.get_response):
  86. return self.__acall__(request)
  87. response = None
  88. if hasattr(self, 'process_request'):
  89. response = self.process_request(request)
  90. response = response or self.get_response(request)
  91. if hasattr(self, 'process_response'):
  92. response = self.process_response(request, response)
  93. return response
  94. async def __acall__(self, request):
  95. """
  96. Async version of __call__ that is swapped in when an async request
  97. is running.
  98. """
  99. response = None
  100. if hasattr(self, 'process_request'):
  101. response = await sync_to_async(
  102. self.process_request,
  103. thread_sensitive=True,
  104. )(request)
  105. response = response or await self.get_response(request)
  106. if hasattr(self, 'process_response'):
  107. response = await sync_to_async(
  108. self.process_response,
  109. thread_sensitive=True,
  110. )(request, response)
  111. return response
  112. def _get_response_none_deprecation(self, get_response):
  113. if get_response is None:
  114. warnings.warn(
  115. 'Passing None for the middleware get_response argument is '
  116. 'deprecated.',
  117. RemovedInDjango40Warning, stacklevel=3,
  118. )