### Implement and Add a Custom Filter Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Provides an example of a `custom_filter` function that checks for the presence of 'password' in text data (case-insensitive). This custom filter can be added to any handler, here demonstrated with `text_handler`. ```python def custom_filter(data, source_info=None): """自定义过滤器示例""" if isinstance(data, str): return "password" not in data.lower() # 过滤包含密码的文本 return True text_handler.add_filter(custom_filter) ``` -------------------------------- ### Best Practice for Source Application Filtering Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Outlines a best practice for setting up source application filters. It shows how to create separate handlers for specific content types (e.g., editor text, browser images) and apply `SourceApplicationFilter` to either allow or block content based on the originating process, enhancing control over clipboard data. ```python def setup_source_filters(): """设置源应用过滤器""" # 编辑器文本 - 只允许来自编辑器的文本 editor_text_handler = TextHandler(on_editor_text) editor_filter = SourceApplicationFilter( allowed_processes=['notepad.exe', 'code.exe', 'cursor.exe', 'sublime_text.exe'] ) editor_text_handler.add_filter(editor_filter) # 浏览器图片 - 禁止浏览器图片 image_handler = ImageHandler(on_image) no_browser_filter = SourceApplicationFilter( blocked_processes=['chrome.exe', 'firefox.exe', 'edge.exe'] ) image_handler.add_filter(no_browser_filter) return editor_text_handler, image_handler ``` -------------------------------- ### Initialize and Register ImageHandler Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Demonstrates how to initialize an `ImageHandler` with a callback function `image_callback` that processes image data. The handler is then added to the `monitor` to process 'image' events. The `image_callback` function prints the size of the captured image. ```python from ci_board.handlers import ImageHandler def image_callback(image_data, source_info=None): # image_data 包含: # - 'format': 'DIB' 或 'BMP' # - 'size': (width, height) # - 'bit_count': 色深 # - 'data': 原始数据 print(f"图片尺寸: {image_data.get('size')}") image_handler = ImageHandler(image_callback) monitor.add_handler('image', image_handler) ``` -------------------------------- ### Initialize and Register FileHandler Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Illustrates the initialization of a `FileHandler` with a `file_callback` function that iterates through a list of file paths. The handler is configured to allow specific file extensions (`.txt`, `.py`, `.md`) and then added to the `monitor` for 'files' events. ```python from ci_board.handlers import FileHandler def file_callback(file_list, source_info=None): # file_list 是文件路径列表 for file_path in file_list: print(f"文件: {file_path}") file_handler = FileHandler(file_callback) file_handler.set_allowed_extensions(['.txt', '.py', '.md']) monitor.add_handler('files', file_handler) ``` -------------------------------- ### Apply Image Size and Quality Filters Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Demonstrates the use of `ImageSizeFilter` to enforce minimum dimensions and `ImageQualityFilter` to set a minimum bit count for images. These filters are added to an `image_handler`. ```python from ci_board.handlers.image_handler import ImageSizeFilter, ImageQualityFilter # 尺寸过滤 size_filter = ImageSizeFilter(min_width=100, min_height=100) image_handler.add_filter(size_filter) # 质量过滤 quality_filter = ImageQualityFilter(min_bit_count=16) image_handler.add_filter(quality_filter) ``` -------------------------------- ### Apply File Extension and Size Filters Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Explains how to use `FileExtensionFilter` to restrict files to allowed extensions and `FileSizeFilter` to limit file size. These filters are applied to a `file_handler`. ```python from ci_board.handlers.file_handler import FileExtensionFilter, FileSizeFilter # 扩展名过滤 ext_filter = FileExtensionFilter(allowed_extensions=['.txt', '.py', '.md']) file_handler.add_filter(ext_filter) # 文件大小过滤 size_filter = FileSizeFilter(max_size_mb=10) file_handler.add_filter(size_filter) ``` -------------------------------- ### Configure ClipboardMonitor for Low Resource Usage Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Illustrates a `ClipboardMonitor` configuration for environments with limited resources. It disables asynchronous processing, reduces worker threads, maintains event-driven mode, and shows how to disable optional features like source tracking. ```python monitor = ClipboardMonitor( async_processing=False, # 同步处理节省内存 max_workers=2, # 减少线程数 event_driven=True # 保持事件驱动 ) # 禁用不需要的功能 monitor.disable_source_tracking() # 如果不需要源追踪 ``` -------------------------------- ### Implement a Robust Clipboard Monitoring Loop Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Presents a `robust_monitor` function that demonstrates how to create a resilient monitoring loop with retry logic. It handles `KeyboardInterrupt` and other exceptions, retrying the monitor startup a specified number of times with a delay, ensuring the monitor stops gracefully. ```python import time import logging def robust_monitor(): """健壮的监控实现""" max_retries = 3 retry_count = 0 while retry_count < max_retries: try: monitor = create_monitor() # 添加处理器 monitor.add_handler('text', text_callback) monitor.add_handler('image', image_callback) if not monitor.start(): raise RuntimeError("监控器启动失败") print("监控器启动成功") monitor.wait() break except KeyboardInterrupt: print("用户中断") break except Exception as e: retry_count += 1 logging.error(f"监控器异常 (尝试 {retry_count}/{max_retries}): {e}") if retry_count < max_retries: print("5秒后重试...") time.sleep(5) finally: if 'monitor' in locals(): monitor.stop() if __name__ == "__main__": robust_monitor() ``` -------------------------------- ### Apply Text Length and Source Application Filters Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Shows how to apply `TextLengthFilter` to restrict text based on its length and `SourceApplicationFilter` to allow text only from specified applications like 'notepad.exe' or 'code.exe'. These filters are added to an existing `text_handler`. ```python from ci_board.handlers.text_handler import TextLengthFilter, SourceApplicationFilter # 长度过滤 length_filter = TextLengthFilter(min_length=10, max_length=500) text_handler.add_filter(length_filter) # 源应用过滤(只允许特定应用) app_filter = SourceApplicationFilter( allowed_processes=['notepad.exe', 'code.exe', 'cursor.exe'] ) text_handler.add_filter(app_filter) ``` -------------------------------- ### Configure ClipboardMonitor for High Performance Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Shows a `ClipboardMonitor` configuration optimized for high performance, enabling asynchronous processing, increasing worker threads, setting a reasonable handler timeout, and using an event-driven mode. ```python monitor = ClipboardMonitor( async_processing=True, # 启用异步处理 max_workers=8, # 增加工作线程 handler_timeout=10.0, # 合理的超时时间 event_driven=True # 使用事件驱动模式 ) ``` -------------------------------- ### Monitor ClipboardMonitor Performance Statistics Source: https://github.com/xuehua-meaw/ci_board/blob/main/README.md Provides a function `print_performance_stats` to retrieve and display performance metrics from the `ClipboardMonitor`. It also includes a `monitor_performance` function that runs in a separate thread to periodically print these statistics, demonstrating continuous monitoring. ```python def print_performance_stats(monitor): """打印性能统计""" stats = monitor.get_async_stats() print(f""" 性能统计: 提交任务: {stats['tasks_submitted']} 完成任务: {stats['tasks_completed']} 成功率: {stats.get('success_rate', 0):.1f}% 活跃任务: {stats['active_tasks']} """) # 定期打印统计 import threading import time def monitor_performance(): while monitor.is_running(): print_performance_stats(monitor) time.sleep(10) perf_thread = threading.Thread(target=monitor_performance, daemon=True) perf_thread.start() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.