1
0
forked from bot/app
LiteyukiBot/assets/utils.html-BoBT4z_Z.js

76 lines
20 KiB
JavaScript

import{_ as n,o as s,c as a,e as t}from"./app-BvUYPzLF.js";const e={},o=t(`<h1 id="liteyuki-utils" tabindex="-1"><a class="header-anchor" href="#liteyuki-utils"><span>liteyuki.utils</span></a></h1><p><strong>说明</strong>: 一些常用的工具类,部分来源于 nonebot 并遵循其许可进行修改</p><h3 id="def-is-coroutine-callable-call-callable-any-bool" tabindex="-1"><a class="header-anchor" href="#def-is-coroutine-callable-call-callable-any-bool"><span><em><strong>def</strong></em> <code>is_coroutine_callable(call: Callable[..., Any]) -&gt; bool</code></span></a></h3><p><strong>说明</strong>: 判断是否为协程可调用对象</p><p><strong>参数</strong>:</p><blockquote><ul><li>call: 可调用对象</li></ul></blockquote><p><strong>返回</strong>: bool: 是否为协程可调用对象</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/utils.py#L17" target="_blank">在GitHub上查看</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">is_coroutine_callable</span><span class="token punctuation">(</span>call<span class="token punctuation">:</span> Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> Any<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token builtin">bool</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
判断是否为协程可调用对象
Args:
call: 可调用对象
Returns:
bool: 是否为协程可调用对象
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> inspect<span class="token punctuation">.</span>isroutine<span class="token punctuation">(</span>call<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> inspect<span class="token punctuation">.</span>iscoroutinefunction<span class="token punctuation">(</span>call<span class="token punctuation">)</span>
<span class="token keyword">if</span> inspect<span class="token punctuation">.</span>isclass<span class="token punctuation">(</span>call<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token boolean">False</span>
func_ <span class="token operator">=</span> <span class="token builtin">getattr</span><span class="token punctuation">(</span>call<span class="token punctuation">,</span> <span class="token string">&#39;__call__&#39;</span><span class="token punctuation">,</span> <span class="token boolean">None</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> inspect<span class="token punctuation">.</span>iscoroutinefunction<span class="token punctuation">(</span>func_<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></details><h3 id="def-run-coroutine-coro-coroutine" tabindex="-1"><a class="header-anchor" href="#def-run-coroutine-coro-coroutine"><span><em><strong>def</strong></em> <code>run_coroutine(*coro: Coroutine)</code></span></a></h3><p><strong>说明</strong>: 运行协程</p><p><strong>参数</strong>:</p><blockquote><ul><li>coro:</li></ul></blockquote><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/utils.py#L33" target="_blank">在GitHub上查看</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">run_coroutine</span><span class="token punctuation">(</span><span class="token operator">*</span>coro<span class="token punctuation">:</span> Coroutine<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
运行协程
Args:
coro:
Returns:
&quot;&quot;&quot;</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
loop <span class="token operator">=</span> asyncio<span class="token punctuation">.</span>get_running_loop<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> loop<span class="token punctuation">.</span>is_running<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">for</span> c <span class="token keyword">in</span> coro<span class="token punctuation">:</span>
asyncio<span class="token punctuation">.</span>ensure_future<span class="token punctuation">(</span>c<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">for</span> c <span class="token keyword">in</span> coro<span class="token punctuation">:</span>
loop<span class="token punctuation">.</span>run_until_complete<span class="token punctuation">(</span>c<span class="token punctuation">)</span>
<span class="token keyword">except</span> RuntimeError<span class="token punctuation">:</span>
loop <span class="token operator">=</span> asyncio<span class="token punctuation">.</span>new_event_loop<span class="token punctuation">(</span><span class="token punctuation">)</span>
asyncio<span class="token punctuation">.</span>set_event_loop<span class="token punctuation">(</span>loop<span class="token punctuation">)</span>
loop<span class="token punctuation">.</span>run_until_complete<span class="token punctuation">(</span>asyncio<span class="token punctuation">.</span>gather<span class="token punctuation">(</span><span class="token operator">*</span>coro<span class="token punctuation">)</span><span class="token punctuation">)</span>
loop<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
logger<span class="token punctuation">.</span>error<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Exception occurred: </span><span class="token interpolation"><span class="token punctuation">{</span>e<span class="token punctuation">}</span></span><span class="token string">&#39;</span></span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></details><h3 id="def-run-coroutine-in-thread-coro-coroutine" tabindex="-1"><a class="header-anchor" href="#def-run-coroutine-in-thread-coro-coroutine"><span><em><strong>def</strong></em> <code>run_coroutine_in_thread(*coro: Coroutine)</code></span></a></h3><p><strong>说明</strong>: 在新线程中运行协程</p><p><strong>参数</strong>:</p><blockquote><ul><li>coro:</li></ul></blockquote><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/utils.py#L66" target="_blank">在GitHub上查看</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">run_coroutine_in_thread</span><span class="token punctuation">(</span><span class="token operator">*</span>coro<span class="token punctuation">:</span> Coroutine<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
在新线程中运行协程
Args:
coro:
Returns:
&quot;&quot;&quot;</span>
threading<span class="token punctuation">.</span>Thread<span class="token punctuation">(</span>target<span class="token operator">=</span>run_coroutine<span class="token punctuation">,</span> args<span class="token operator">=</span>coro<span class="token punctuation">,</span> daemon<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></details><h3 id="def-path-to-module-name-path-path-str" tabindex="-1"><a class="header-anchor" href="#def-path-to-module-name-path-path-str"><span><em><strong>def</strong></em> <code>path_to_module_name(path: Path) -&gt; str</code></span></a></h3><p><strong>说明</strong>: 转换路径为模块名</p><p><strong>参数</strong>:</p><blockquote><ul><li>path: 路径a/b/c/d -&gt; a.b.c.d</li></ul></blockquote><p><strong>返回</strong>: str: 模块名</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/utils.py#L78" target="_blank">在GitHub上查看</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">path_to_module_name</span><span class="token punctuation">(</span>path<span class="token punctuation">:</span> Path<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token builtin">str</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
转换路径为模块名
Args:
path: 路径a/b/c/d -&gt; a.b.c.d
Returns:
str: 模块名
&quot;&quot;&quot;</span>
rel_path <span class="token operator">=</span> path<span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>relative_to<span class="token punctuation">(</span>Path<span class="token punctuation">.</span>cwd<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> rel_path<span class="token punctuation">.</span>stem <span class="token operator">==</span> <span class="token string">&#39;__init__&#39;</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token string">&#39;.&#39;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>rel_path<span class="token punctuation">.</span>parts<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token string">&#39;.&#39;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>rel_path<span class="token punctuation">.</span>parts<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token punctuation">(</span>rel_path<span class="token punctuation">.</span>stem<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></details><h3 id="def-async-wrapper-func-callable-any-callable-coroutine" tabindex="-1"><a class="header-anchor" href="#def-async-wrapper-func-callable-any-callable-coroutine"><span><em><strong>def</strong></em> <code>async_wrapper(func: Callable[..., Any]) -&gt; Callable[..., Coroutine]</code></span></a></h3><p><strong>说明</strong>: 异步包装器</p><p><strong>参数</strong>:</p><blockquote><ul><li>func: Sync Callable</li></ul></blockquote><p><strong>返回</strong>: Coroutine: Asynchronous Callable</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/utils.py#L93" target="_blank">在GitHub上查看</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">async_wrapper</span><span class="token punctuation">(</span>func<span class="token punctuation">:</span> Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> Any<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> Coroutine<span class="token punctuation">]</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
异步包装器
Args:
func: Sync Callable
Returns:
Coroutine: Asynchronous Callable
&quot;&quot;&quot;</span>
<span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">wrapper</span><span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> func<span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span>
wrapper<span class="token punctuation">.</span>__signature__ <span class="token operator">=</span> inspect<span class="token punctuation">.</span>signature<span class="token punctuation">(</span>func<span class="token punctuation">)</span>
<span class="token keyword">return</span> wrapper
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></details>`,30),p=[o];function l(i,c){return s(),a("div",null,p)}const r=n(e,[["render",l],["__file","utils.html.vue"]]),d=JSON.parse('{"path":"/dev/api/utils.html","title":"liteyuki.utils","lang":"zh-CN","frontmatter":{"title":"liteyuki.utils","lastUpdated":false,"description":"liteyuki.utils 说明: 一些常用的工具类,部分来源于 nonebot 并遵循其许可进行修改 def is_coroutine_callable(call: Callable[..., Any]) -> bool 说明: 判断是否为协程可调用对象 参数: call: 可调用对象 返回: bool: 是否为协程可调用对象 源代码 或 在Git...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/utils.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/utils.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot 轻雪机器人"}],["meta",{"property":"og:title","content":"liteyuki.utils"}],["meta",{"property":"og:description","content":"liteyuki.utils 说明: 一些常用的工具类,部分来源于 nonebot 并遵循其许可进行修改 def is_coroutine_callable(call: Callable[..., Any]) -> bool 说明: 判断是否为协程可调用对象 参数: call: 可调用对象 返回: bool: 是否为协程可调用对象 源代码 或 在Git..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2024-08-29T06:19:39.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-29T06:19:39.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.utils\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-29T06:19:39.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"def is_coroutine_callable(call: Callable[..., Any]) -> bool","slug":"def-is-coroutine-callable-call-callable-any-bool","link":"#def-is-coroutine-callable-call-callable-any-bool","children":[]},{"level":3,"title":"def run_coroutine(*coro: Coroutine)","slug":"def-run-coroutine-coro-coroutine","link":"#def-run-coroutine-coro-coroutine","children":[]},{"level":3,"title":"def run_coroutine_in_thread(*coro: Coroutine)","slug":"def-run-coroutine-in-thread-coro-coroutine","link":"#def-run-coroutine-in-thread-coro-coroutine","children":[]},{"level":3,"title":"def path_to_module_name(path: Path) -> str","slug":"def-path-to-module-name-path-path-str","link":"#def-path-to-module-name-path-path-str","children":[]},{"level":3,"title":"def async_wrapper(func: Callable[..., Any]) -> Callable[..., Coroutine]","slug":"def-async-wrapper-func-callable-any-callable-coroutine","link":"#def-async-wrapper-func-callable-any-callable-coroutine","children":[]}],"git":{"createdTime":1724031826000,"updatedTime":1724912379000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":5}]},"readingTime":{"minutes":1.62,"words":487},"filePathRelative":"dev/api/utils.md","localizedDate":"2024年8月19日","autoDesc":true}');export{r as comp,d as data};