1
0

utils.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import asyncio
  2. import functools
  3. import inspect
  4. import queue
  5. import random
  6. import socket
  7. import string
  8. import time
  9. from collections import OrderedDict
  10. from contextlib import closing
  11. from os.path import abspath, dirname
  12. project_dir = dirname(abspath(__file__))
  13. STATIC_PATH = '%s/html' % project_dir
  14. class Setter:
  15. """
  16. 可以在对象属性上保存数据。
  17. 访问数据对象不存在的属性时会返回None而不是抛出异常。
  18. """
  19. def __getattribute__(self, name):
  20. try:
  21. return super().__getattribute__(name)
  22. except AttributeError:
  23. return None
  24. class ObjectDictProxy:
  25. """
  26. 通过属性访问的字典。实例不维护底层字典,而是每次在访问时使用回调函数获取
  27. 在对象属性上保存的数据会被保存到底层字典中
  28. 访问数据对象不存在的属性时会返回None而不是抛出异常。
  29. 不能保存下划线开始的属性
  30. 用 ``obj._dict`` 获取对象的字典表示
  31. Example::
  32. d = {}
  33. data = LazyObjectDict(lambda: d)
  34. data.name = "Wang"
  35. data.age = 22
  36. assert data.foo is None
  37. data[10] = "10"
  38. for key in data:
  39. print(key)
  40. assert 'bar' not in data
  41. assert 'name' in data
  42. assert data._dict is d
  43. print(data._dict)
  44. """
  45. def __init__(self, dict_getter):
  46. # 使用 self.__dict__ 避免触发 __setattr__
  47. self.__dict__['_dict_getter'] = dict_getter
  48. @property
  49. def _dict(self):
  50. return self._dict_getter()
  51. def __len__(self):
  52. return len(self._dict)
  53. def __getitem__(self, key):
  54. if key in self._dict:
  55. return self._dict[key]
  56. raise KeyError(key)
  57. def __setitem__(self, key, item):
  58. self._dict[key] = item
  59. def __delitem__(self, key):
  60. del self._dict[key]
  61. def __iter__(self):
  62. return iter(self._dict)
  63. def __contains__(self, key):
  64. return key in self._dict
  65. def __repr__(self):
  66. return repr(self._dict)
  67. def __setattr__(self, key, value):
  68. """
  69. 无论属性是否存在都会被调用
  70. 使用 self.__dict__[name] = value 避免递归
  71. """
  72. assert not key.startswith('_'), "Cannot set attributes starting with underscore"
  73. self._dict.__setitem__(key, value)
  74. def __getattr__(self, item):
  75. """访问一个不存在的属性时触发"""
  76. return self._dict.get(item, None)
  77. def __delattr__(self, item):
  78. try:
  79. del self._dict[item]
  80. except KeyError:
  81. pass
  82. class ObjectDict(dict):
  83. """
  84. Object like dict, every dict[key] can visite by dict.key
  85. If dict[key] is `Get`, calculate it's value.
  86. """
  87. def __getattr__(self, name):
  88. ret = self.__getitem__(name)
  89. if hasattr(ret, '__get__'):
  90. return ret.__get__(self, ObjectDict)
  91. return ret
  92. def catch_exp_call(func, logger):
  93. """运行函数,将捕获异常记录到日志
  94. :param func: 函数
  95. :param logger: 日志
  96. :return: ``func`` 返回值
  97. """
  98. try:
  99. return func()
  100. except Exception:
  101. logger.exception("Error when invoke `%s`" % func)
  102. def iscoroutinefunction(object):
  103. while isinstance(object, functools.partial):
  104. object = object.func
  105. return asyncio.iscoroutinefunction(object)
  106. def isgeneratorfunction(object):
  107. while isinstance(object, functools.partial):
  108. object = object.func
  109. return inspect.isgeneratorfunction(object)
  110. def get_function_name(func, default=None):
  111. while isinstance(func, functools.partial):
  112. func = func.func
  113. return getattr(func, '__name__', default)
  114. def get_function_doc(func):
  115. """获取函数的doc注释
  116. 如果函数被functools.partial包装,则返回内部原始函数的文档,可以通过设置新函数的 func.__doc__ 属性来更新doc注释
  117. """
  118. partial_doc = inspect.getdoc(functools.partial)
  119. if isinstance(func, functools.partial) and getattr(func, '__doc__', '') == partial_doc:
  120. while isinstance(func, functools.partial):
  121. func = func.func
  122. return inspect.getdoc(func) or ''
  123. def get_function_seo_info(func):
  124. """获取使用 pywebio.platform.utils.seo() 设置在函数上的SEO信息
  125. """
  126. if hasattr(func, '_pywebio_title'):
  127. return func._pywebio_title, func._pywebio_description
  128. while isinstance(func, functools.partial):
  129. func = func.func
  130. if hasattr(func, '_pywebio_title'):
  131. return func._pywebio_title, func._pywebio_description
  132. return '', ''
  133. class LimitedSizeQueue(queue.Queue):
  134. """
  135. 有限大小的队列
  136. `get()` 返回全部数据
  137. 队列满时,再 `put()` 会阻塞
  138. """
  139. def get(self):
  140. """获取队列全部数据"""
  141. try:
  142. return super().get(block=False)
  143. except queue.Empty:
  144. return []
  145. def wait_empty(self, timeout=None):
  146. """等待队列内的数据被取走"""
  147. with self.not_full:
  148. if self._qsize() == 0:
  149. return
  150. if timeout is None:
  151. self.not_full.wait()
  152. elif timeout < 0:
  153. raise ValueError("'timeout' must be a non-negative number")
  154. else:
  155. self.not_full.wait(timeout)
  156. def _init(self, maxsize):
  157. self.queue = []
  158. def _qsize(self):
  159. return len(self.queue)
  160. # Put a new item in the queue
  161. def _put(self, item):
  162. self.queue.append(item)
  163. # Get an item from the queue
  164. def _get(self):
  165. all_data = self.queue
  166. self.queue = []
  167. return all_data
  168. async def wait_host_port(host, port, duration=10, delay=2):
  169. """Repeatedly try if a port on a host is open until duration seconds passed
  170. from: https://gist.github.com/betrcode/0248f0fda894013382d7#gistcomment-3161499
  171. :param str host: host ip address or hostname
  172. :param int port: port number
  173. :param int/float duration: Optional. Total duration in seconds to wait, by default 10
  174. :param int/float delay: Optional. Delay in seconds between each try, by default 2
  175. :return: awaitable bool
  176. """
  177. tmax = time.time() + duration
  178. while time.time() < tmax:
  179. try:
  180. _, writer = await asyncio.wait_for(asyncio.open_connection(host, port), timeout=5)
  181. writer.close()
  182. # asyncio.StreamWriter.wait_closed is introduced in py 3.7
  183. # See https://docs.python.org/3/library/asyncio-stream.html#asyncio.StreamWriter.wait_closed
  184. if hasattr(writer, 'wait_closed'):
  185. await writer.wait_closed()
  186. return True
  187. except Exception:
  188. if delay:
  189. await asyncio.sleep(delay)
  190. return False
  191. def get_free_port():
  192. """
  193. pick a free port number
  194. :return int: port number
  195. """
  196. with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
  197. s.bind(('', 0))
  198. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  199. return s.getsockname()[1]
  200. def random_str(length=16):
  201. """生成字母和数组组成的随机字符串
  202. :param int length: 字符串长度
  203. """
  204. candidates = string.ascii_letters + string.digits
  205. return ''.join(random.SystemRandom().choice(candidates) for _ in range(length))
  206. def run_as_function(gen):
  207. res = None
  208. while 1:
  209. try:
  210. res = gen.send(res)
  211. except StopIteration as e:
  212. if len(e.args) == 1:
  213. return e.args[0]
  214. return
  215. async def to_coroutine(gen):
  216. res = None
  217. while 1:
  218. try:
  219. c = gen.send(res)
  220. res = await c
  221. except StopIteration as e:
  222. if len(e.args) == 1:
  223. return e.args[0]
  224. return
  225. class LRUDict(OrderedDict):
  226. """
  227. Store items in the order the keys were last recent updated.
  228. The last recent updated item was in end.
  229. The last furthest updated item was in front.
  230. """
  231. def __setitem__(self, key, value):
  232. OrderedDict.__setitem__(self, key, value)
  233. self.move_to_end(key)
  234. _html_value_chars = set(string.ascii_letters + string.digits + '_-')
  235. def is_html_safe_value(val):
  236. """检查是字符串是否可以作为html属性值"""
  237. return all(i in _html_value_chars for i in val)