app/assets/observer.html-DvLhUcl9.js

52 lines
18 KiB
JavaScript
Raw Normal View History

import{_ as n,o as e,c as s,e as a}from"./app-BvUYPzLF.js";const t={},o=a(`<h1 id="liteyuki-dev-observer" tabindex="-1"><a class="header-anchor" href="#liteyuki-dev-observer"><span>liteyuki.dev.observer</span></a></h1><p><strong>说明</strong>: 此模块用于注册观察者函数使用watchdog监控文件变化并重启bot 启用该模块需要在配置文件中设置<code>dev_mode</code>为True</p><h3 id="var-callback-func" tabindex="-1"><a class="header-anchor" href="#var-callback-func"><span>var <code>CALLBACK_FUNC</code></span></a></h3><ul><li><p><strong>说明</strong>: 位置1为FileSystemEvent</p></li><li><p><strong>类型</strong>: <code>TypeAlias</code></p></li><li><p><strong>默认值</strong>: <code>Callable[[FileSystemEvent], None]</code></p></li></ul><h3 id="var-filter-func" tabindex="-1"><a class="header-anchor" href="#var-filter-func"><span>var <code>FILTER_FUNC</code></span></a></h3><ul><li><p><strong>说明</strong>: 位置1为FileSystemEvent</p></li><li><p><strong>类型</strong>: <code>TypeAlias</code></p></li><li><p><strong>默认值</strong>: <code>Callable[[FileSystemEvent], bool]</code></p></li></ul><h3 id="def-debounce-wait" tabindex="-1"><a class="header-anchor" href="#def-debounce-wait"><span><em><strong>def</strong></em> <code>debounce(wait)</code></span></a></h3><p><strong>说明</strong>: 防抖函数</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/dev/observer.py#L20" 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">debounce</span><span class="token punctuation">(</span>wait<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
防抖函数
&quot;&quot;&quot;</span>
<span class="token keyword">def</span> <span class="token function">decorator</span><span class="token punctuation">(</span>func<span class="token punctuation">)</span><span class="token punctuation">:</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">nonlocal</span> last_call_time
current_time <span class="token operator">=</span> time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> current_time <span class="token operator">-</span> last_call_time <span class="token operator">&gt;</span> wait<span class="token punctuation">:</span>
last_call_time <span class="token operator">=</span> current_time
<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>
last_call_time <span class="token operator">=</span> <span class="token boolean">None</span>
<span class="token keyword">return</span> wrapper
<span class="token keyword">return</span> decorator
</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></div></details><h3 id="class-codemodifiedhandler-filesystemeventhandler" tabindex="-1"><a class="header-anchor" href="#class-codemodifiedhandler-filesystemeventhandler"><span><em><strong>class</strong></em> <code>CodeModifiedHandler(FileSystemEventHandler)</code></span></a></h3><p><code>@debounce(1)</code></p><h4 id="def-on-modified-self-event" tabindex="-1"><a class="header-anchor" href="#def-on-modified-self-event"><span><em><strong>def</strong></em> <code>on_modified(self, event)</code></span></a></h4><details><summary><b></b> <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/dev/observer.py#L50" 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 decorator annotation punctuation">@debounce</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">on_modified</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">raise</span> NotImplementedError<span class="token punctuation">(</span><span class="token string">&#39;on_modified must be implemented&#39;</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></div></details><h4 id="def-on-created-self-event" tabindex="-1"><a class="header-anchor" href="#def-on-created-self-event"><span><em><strong>def</strong></em> <code>on_created(self, event)</code></span></a></h4><details><summary><b></b> <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/dev/observer.py#L53" 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">on_created</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>on_modified<span class="token punctuation">(</span>event<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></div></details><h4 id="def-on-deleted-self-event" tabindex="-1"><a class="header-anchor" href="#def-on-deleted-self-event"><span><em><strong>def</strong></em> <code>on_deleted(self, event)</code></span></a></h4><details><summary><b></b> <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/dev/observer.py#L56" 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">on_deleted</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>on_modified<span class="token punctuation">(</span>event<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></div></details><h4 id="def-on-moved-self-event" tabindex="-1"><a class="header-anchor" href="#def-on-moved-self-event"><span><em><strong>def</strong></em> <code>on_moved(self, event)</code></span></a></h4><details><summary><b></b> <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/dev/observer.py#L59" 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">on_moved</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>on_modified<span class="token punctuation">(</span>event<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></div></details><h4 id="def-on-any-event-self-event" tabindex="-1"><a class="header-anchor" href="#def-on-any-event-self-event"><span><em><strong>def</strong></em> <code>on_any_event(self, event)</code></span></a></h4><details><summary><b></b> <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/dev/observer.py#L62" 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">on_any_event</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>on_modified<span class="token punctuation">(</span>event<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></div></details><h3 id="def-on-file-system-event-directories-tuple-str-recursive-bool-true-event-filter-filter-func-none-callable-callback-func-callback-func" tabindex="-1"><a class="header-anchor" href="#def-on-file-system-event-directories-tuple-str-recursive-bool-true-event-filter-filter-func-none-callable-callback-func-callback-func"><span><em><strong>def</strong></em> <code>on_file_system_event(directories: tuple[str], recursive: bool = True, event_filter: FILTER_FUNC = None) -&gt; Callable[[CALLBACK_FUNC], CALLBACK_FUNC]</code></span></a></h3><p><strong></strong>: </p><p><strong></strong>:</p><blockquote><ul><li>directories: </li><li>recursive: </li><li>event_filter: , True</li></ul></blockquote><p><strong></strong>: </p><details><summary><b></b> <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/dev/observer.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">on_file_system_event</span><span class="token punctuation">(</span>directories<span class="token punctuation">:</span> <span class="token builtin">tuple</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">]</span><span class="token punctuation">,</span> recursive<span class="token punctuation">:</span> <span class="token builtin">bool</span><span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> event_filter<span class="token punctuation">:</span> FILTER_FUNC<span class="token operator">=</span><span class="token boolean">None</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>CALLBACK_FUNC<span class="token punctuation">]</span><span class="token punctuation">,</span> CALLBACK_FUNC<span class="token punctuation">]</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
注册文件系统变化监听器
Args:
directories: 监听目录们
recursive: 是否递归监听子目录
event_filter: 事件过滤器, 返回True则执行回调函数
Returns:
装饰器装饰一个函数在接收到数据后执行
&quot;&quot;&quot;</span>
<span class="token keyword">def</span> <span class="token function">decorator</span><span class="token punctuation">(</span>func<span class="token punctuation">:</span> CALLBACK_FUNC<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> CALLBACK_FUNC<span class="token punctuation">:</span>
<span class="token keyword">def</span> <span class="token function">wrapper</span><span class="token punctuation">(</span>event<span class="token punctuation">:</span> FileSystemEvent<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> event_filter <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">and</span> <span class="token punctuation">(</span><span class="token keyword">not</span> event_filter<span class="token punctuation">(</span>event<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span>
func<span class="token punctuation">(</span>event<span class="token punctuation">)</span>
code_modified_handler <span class="token operator">=</span> CodeModifiedHandler<span class="token punctuation">(</span><span class="token punctuation">)</span>
code_modified_handler<span class="token punctuation">.</span>on_modified <span class="token operator">=</span> wrapper
<span class="token keyword">for</span> directory <span class="token keyword">in</span> directories<span class="token punctuation">:</span>
observer<span class="token punctuation">.</span>schedule<span class="token punctuation">(</span>code_modified_handler<span class="token punctuation">,</span> directory<span class="token punctuation">,</span> recursive<span class="token operator">=</span>recursive<span class="token punctuation">)</span>
<span class="token keyword">return</span> func
<span class="token keyword">return</span> decorator
</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></div></details>`,27),i=[o];function l(p,c){return e(),s("div",null,i)}const d=n(t,[["render",l],["__file","observer.html.vue"]]),u=JSON.parse('{"path":"/dev/api/dev/observer.html","title":"liteyuki.dev.observer","lang":"zh-CN","frontmatter":{"title":"liteyuki.dev.observer","lastUpdated":false,"description":"liteyuki.dev.observer : 使watchdogbot dev_modeTrue var CALLBACK_FUNC : 1FileSystemEvent : TypeAlias : Callable[[FileSystemEvent...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/dev/observer.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/dev/observer.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot "}],["meta",{"property":"og:title","content":"liteyuki.dev.observer"}],["meta",{"property":"og:description","content":"liteyuki.dev.observer : 使watchdogbot dev_modeTrue var CALLBACK_FUNC : 1FileSystemEvent : TypeAlias : Callable[[FileSystemEvent..."}],["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.dev.observer\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-29T06:19:39.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"var CALLBACK_FUNC","slug":"var-callback-func","link":"#var-callback-func","children":[]},{"level":3,"title":"var FILTER_FUNC","slug":"var-filter-func","link":"#var-filter-func","children":[]},{"level":3,"title":"def debounce(wait)","slug":"def-debounce-wait","link":"#def-debounce-wait","children":[]},{"level":3,"title":"class CodeModifiedHandler(FileSystemEventHandler)","slug":"class-codemodifiedhandler-filesystemeventhandler","link":"#class-codemodifiedhandler-filesystemeventhandler","children":[]},{"level":3,"title":"def on_file_system_event(directories: tuple[str], recursive: bool = True, event_filter: FILTER_FUNC = None) -> Callable[[CALLBACK_FUNC], CALLBACK_FUNC]","slug":"def-on-file-system-event-directories-tuple-str-recursive-bool-true-event-filter-filter-func-none-callable-callback-func-callback-func","link":"#def-on-file-system-event-directories-tuple-str-recursive-bool-true-event-filter-filter-func-none-callable-callback-func-callback-func","children":[]}],"git":{"createdTime":1724031826000,"updatedTime":1724912379000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":6}]},"readingTime":{"minutes":1.85,"words":554},"filePathRelative":"dev/api/dev/observer.md","localizedDate":"2024819","autoDesc":true}');export{d as comp,u as data};