import{_ as i,c as e,a7 as a,o as n}from"./chunks/framework.Z6UMW4JZ.js";const c=JSON.parse('{"title":"liteyuki.dev.observer","description":"","frontmatter":{"title":"liteyuki.dev.observer"},"headers":[],"relativePath":"dev/api/dev/observer.md","filePath":"zh/dev/api/dev/observer.md","lastUpdated":null}'),t={name:"dev/api/dev/observer.md"};function l(h,s,p,r,k,d){return n(),e("div",null,s[0]||(s[0]=[a(`

模块 liteyuki.dev.observer

此模块用于注册观察者函数,使用watchdog监控文件变化并重启bot 启用该模块需要在配置文件中设置dev_mode为True

var CALLBACK_FUNC

var FILTER_FUNC


func debounce(wait)

说明: 防抖函数

源代码在GitHub上查看
python
def debounce(wait):

    def decorator(func):

        def wrapper(*args, **kwargs):
            nonlocal last_call_time
            current_time = time.time()
            if current_time - last_call_time > wait:
                last_call_time = current_time
                return func(*args, **kwargs)
        last_call_time = None
        return wrapper
    return decorator

class CodeModifiedHandler(FileSystemEventHandler)


@debounce(1)

func on_modified(self, event)

源代码在GitHub上查看
python
@debounce(1)
def on_modified(self, event):
    raise NotImplementedError('on_modified must be implemented')

func on_created(self, event)

源代码在GitHub上查看
python
def on_created(self, event):
    self.on_modified(event)

func on_deleted(self, event)

源代码在GitHub上查看
python
def on_deleted(self, event):
    self.on_modified(event)

func on_moved(self, event)

源代码在GitHub上查看
python
def on_moved(self, event):
    self.on_modified(event)

func on_any_event(self, event)

源代码在GitHub上查看
python
def on_any_event(self, event):
    self.on_modified(event)

func on_file_system_event(directories: tuple[str], recursive: bool = True, event_filter: FILTER_FUNC = None) -> Callable[[CALLBACK_FUNC], CALLBACK_FUNC]

说明: 注册文件系统变化监听器

参数:

返回: 装饰器,装饰一个函数在接收到数据后执行

源代码在GitHub上查看
python
def on_file_system_event(directories: tuple[str], recursive: bool=True, event_filter: FILTER_FUNC=None) -> Callable[[CALLBACK_FUNC], CALLBACK_FUNC]:

    def decorator(func: CALLBACK_FUNC) -> CALLBACK_FUNC:

        def wrapper(event: FileSystemEvent):
            if event_filter is not None and (not event_filter(event)):
                return
            func(event)
        code_modified_handler = CodeModifiedHandler()
        code_modified_handler.on_modified = wrapper
        for directory in directories:
            observer.schedule(code_modified_handler, directory, recursive=recursive)
        return func
    return decorator
`,34)]))}const E=i(t,[["render",l]]);export{c as __pageData,E as default};