util.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (c) "Neo4j"
  2. # Neo4j Sweden AB [https://neo4j.com]
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # https://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import annotations
  16. import asyncio
  17. import inspect
  18. import traceback
  19. import typing as t
  20. from functools import wraps
  21. if t.TYPE_CHECKING:
  22. import typing_extensions as te
  23. _T = t.TypeVar("_T")
  24. _P = te.ParamSpec("_P")
  25. __all__ = [
  26. "AsyncUtil",
  27. "Util",
  28. ]
  29. class AsyncUtil:
  30. @staticmethod
  31. async def iter(it):
  32. async for x in it:
  33. yield x
  34. @staticmethod
  35. async def next(it):
  36. return await it.__anext__()
  37. @staticmethod
  38. async def list(it):
  39. return [x async for x in it]
  40. @staticmethod
  41. @t.overload
  42. async def callback(cb: None, *args: object, **kwargs: object) -> None: ...
  43. @staticmethod
  44. @t.overload
  45. async def callback(
  46. cb: (
  47. t.Callable[_P, _T | t.Awaitable[_T]]
  48. | t.Callable[_P, t.Awaitable[_T]]
  49. | t.Callable[_P, _T]
  50. ),
  51. *args: _P.args,
  52. **kwargs: _P.kwargs,
  53. ) -> _T: ...
  54. @staticmethod
  55. async def callback(cb, *args, **kwargs):
  56. if callable(cb):
  57. res = cb(*args, **kwargs)
  58. if inspect.isawaitable(res):
  59. return await res
  60. return res
  61. return None
  62. @staticmethod
  63. def shielded(coro_function):
  64. assert asyncio.iscoroutinefunction(coro_function)
  65. @wraps(coro_function)
  66. async def shielded_function(*args, **kwargs):
  67. return await asyncio.shield(coro_function(*args, **kwargs))
  68. return shielded_function
  69. is_async_code: t.ClassVar = True
  70. @staticmethod
  71. def extract_stack(limit=None):
  72. # can maybe be improved in the future
  73. # https://github.com/python/cpython/issues/91048
  74. stack = asyncio.current_task().get_stack(limit=limit)
  75. stack_walk = ((f, f.f_lineno) for f in stack)
  76. return traceback.StackSummary.extract(stack_walk, limit=limit)
  77. class Util:
  78. iter: t.ClassVar = iter
  79. next: t.ClassVar = next
  80. list: t.ClassVar = list
  81. @staticmethod
  82. @t.overload
  83. def callback(cb: None, *args: object, **kwargs: object) -> None: ...
  84. @staticmethod
  85. @t.overload
  86. def callback(
  87. cb: t.Callable[_P, _T], *args: _P.args, **kwargs: _P.kwargs
  88. ) -> _T: ...
  89. @staticmethod
  90. def callback(cb, *args, **kwargs):
  91. if callable(cb):
  92. return cb(*args, **kwargs)
  93. return None
  94. @staticmethod
  95. def shielded(coro_function):
  96. return coro_function
  97. is_async_code: t.ClassVar = False
  98. @staticmethod
  99. def extract_stack(limit=None):
  100. return traceback.extract_stack(limit=limit)[:-1]