pysfi 0.1.7__py3-none-any.whl → 0.1.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. {pysfi-0.1.7.dist-info → pysfi-0.1.11.dist-info}/METADATA +11 -9
  2. pysfi-0.1.11.dist-info/RECORD +60 -0
  3. pysfi-0.1.11.dist-info/entry_points.txt +28 -0
  4. sfi/__init__.py +1 -1
  5. sfi/alarmclock/alarmclock.py +40 -40
  6. sfi/bumpversion/__init__.py +1 -1
  7. sfi/cleanbuild/cleanbuild.py +155 -0
  8. sfi/condasetup/condasetup.py +116 -0
  9. sfi/docscan/__init__.py +1 -1
  10. sfi/docscan/docscan.py +407 -103
  11. sfi/docscan/docscan_gui.py +1282 -596
  12. sfi/docscan/lang/eng.py +152 -0
  13. sfi/docscan/lang/zhcn.py +170 -0
  14. sfi/filedate/filedate.py +185 -112
  15. sfi/gittool/__init__.py +2 -0
  16. sfi/gittool/gittool.py +401 -0
  17. sfi/llmclient/llmclient.py +592 -0
  18. sfi/llmquantize/llmquantize.py +480 -0
  19. sfi/llmserver/llmserver.py +335 -0
  20. sfi/makepython/makepython.py +31 -30
  21. sfi/pdfsplit/pdfsplit.py +173 -173
  22. sfi/pyarchive/pyarchive.py +418 -0
  23. sfi/pyembedinstall/pyembedinstall.py +629 -0
  24. sfi/pylibpack/__init__.py +0 -0
  25. sfi/pylibpack/pylibpack.py +1457 -0
  26. sfi/pylibpack/rules/numpy.json +22 -0
  27. sfi/pylibpack/rules/pymupdf.json +10 -0
  28. sfi/pylibpack/rules/pyqt5.json +19 -0
  29. sfi/pylibpack/rules/pyside2.json +23 -0
  30. sfi/pylibpack/rules/scipy.json +23 -0
  31. sfi/pylibpack/rules/shiboken2.json +24 -0
  32. sfi/pyloadergen/pyloadergen.py +512 -227
  33. sfi/pypack/__init__.py +0 -0
  34. sfi/pypack/pypack.py +1142 -0
  35. sfi/pyprojectparse/__init__.py +0 -0
  36. sfi/pyprojectparse/pyprojectparse.py +500 -0
  37. sfi/pysourcepack/pysourcepack.py +308 -0
  38. sfi/quizbase/__init__.py +0 -0
  39. sfi/quizbase/quizbase.py +828 -0
  40. sfi/quizbase/quizbase_gui.py +987 -0
  41. sfi/regexvalidate/__init__.py +0 -0
  42. sfi/regexvalidate/regex_help.html +284 -0
  43. sfi/regexvalidate/regexvalidate.py +468 -0
  44. sfi/taskkill/taskkill.py +0 -2
  45. sfi/workflowengine/__init__.py +0 -0
  46. sfi/workflowengine/workflowengine.py +444 -0
  47. pysfi-0.1.7.dist-info/RECORD +0 -31
  48. pysfi-0.1.7.dist-info/entry_points.txt +0 -15
  49. sfi/embedinstall/embedinstall.py +0 -418
  50. sfi/projectparse/projectparse.py +0 -152
  51. sfi/pypacker/fspacker.py +0 -91
  52. {pysfi-0.1.7.dist-info → pysfi-0.1.11.dist-info}/WHEEL +0 -0
  53. /sfi/{embedinstall → docscan/lang}/__init__.py +0 -0
  54. /sfi/{projectparse → llmquantize}/__init__.py +0 -0
  55. /sfi/{pypacker → pyembedinstall}/__init__.py +0 -0
@@ -0,0 +1,152 @@
1
+ """English translation defaults for docscan GUI."""
2
+
3
+ ENGLISH_DEFAULTS = {
4
+ "window_title": "Document Scanner GUI",
5
+ "input_config_tab": "Input Configuration",
6
+ "scan_options_tab": "Scan Options",
7
+ "input_directory": "Input Directory:",
8
+ "browse": "Browse...",
9
+ "rules_file": "Rules File:",
10
+ "file_types": "File Types:",
11
+ "use_pdf_ocr": "Use PDF OCR",
12
+ "use_process_pool": "Use Process Pool (CPU-intensive)",
13
+ "threads": "Threads:",
14
+ "batch_size": "Batch Size:",
15
+ "start_scan": "Start Scan",
16
+ "pause": "Pause",
17
+ "resume": "Resume",
18
+ "stop": "Stop",
19
+ "save_results": "Save Results",
20
+ "clear_results": "Clear Results",
21
+ "results": "Results",
22
+ "files_scanned": "Files Scanned:",
23
+ "files_with_matches": "Files with Matches:",
24
+ "progress_log": "Progress Log:",
25
+ "match_details": "Match Details:",
26
+ "selected_match_context": "Selected Match Context:",
27
+ "file": "File",
28
+ "type": "Type",
29
+ "matches": "Matches",
30
+ "time": "Time (s)",
31
+ "select_input_directory": "Select Input Directory",
32
+ "select_rules_file": "Select Rules File",
33
+ "json_files": "JSON Files (*.json)",
34
+ "save_results_dialog": "Save Results",
35
+ "default_results_filename": "scan_results_{datetime}.json",
36
+ "error": "Error",
37
+ "warning": "Warning",
38
+ "success": "Success",
39
+ "invalid_input_directory": "Invalid input directory",
40
+ "no_valid_rules": "No valid rules found",
41
+ "failed_to_load_rules": "Failed to load rules: {error}",
42
+ "no_results_to_save": "No results to save",
43
+ "failed_to_save_results": "Failed to save results: {error}",
44
+ "scan_failed": "Scan failed: {error}",
45
+ "starting_scan": "Starting scan...",
46
+ "scan_complete": "Scan complete!",
47
+ "pausing_scan": "Pausing scan...",
48
+ "stopping_scan": "Stopping scan...",
49
+ "scan_completed": "Scan completed",
50
+ "scan_stopped": "Scan stopped",
51
+ "found_matches_files": "Found matches in {count} files",
52
+ "file_info": "File",
53
+ "type_info": "Type",
54
+ "size": "Size",
55
+ "bytes": "bytes",
56
+ "rule": "Rule",
57
+ "description": "Description",
58
+ "line": "Line {line}: {match}",
59
+ "context": "Context:",
60
+ "default_file_types": "pdf,docx,xlsx,pptx,txt,odt,rtf,epub,csv,xml,html,md,jpg,jpeg,png,gif,bmp,tiff",
61
+ "default_rules_file": "rules.json",
62
+ "results_saved_to": "Results saved to:\n{path}",
63
+ "files_scanned_zero": "Files Scanned: 0",
64
+ "files_with_matches_zero": "Files with Matches: 0",
65
+ "language_settings": "Language Settings",
66
+ "processing_options": "Processing Options",
67
+ "performance_settings": "Performance Settings",
68
+ "language_label": "Language:",
69
+ "ocr_tooltip": "Enable OCR for scanned PDF files to extract text from images",
70
+ "process_pool_tooltip": "Use multiple processes for CPU-intensive operations (may increase memory usage)",
71
+ "threads_tooltip": "Number of worker threads (higher values may improve speed but use more CPU)",
72
+ "batch_size_tooltip": "Number of files to process in each batch (larger batches may improve throughput)",
73
+ "file_types_tooltip": "File types to scan (comma separated)",
74
+ "file_menu": "&File",
75
+ "settings_menu": "&Settings",
76
+ "help_menu": "&Help",
77
+ "preferences": "&Preferences...",
78
+ "exit": "E&xit",
79
+ "about": "&About",
80
+ "about_title": "About Document Scanner",
81
+ "about_text": "Document Scanner GUI\n\nVersion 1.0",
82
+ "language": "Language",
83
+ "open_results": "Open Results...",
84
+ "open_results_file": "Open Scan Results",
85
+ "loaded_results_from": "Loaded results from: {path}",
86
+ "results_loaded_successfully": "Results loaded successfully from:\n{path}",
87
+ "failed_to_load_results": "Failed to load results: {error}",
88
+ # Command-line specific translations
89
+ "document_scanner_description": "Scan documents and extract text, images, and metadata with certain rules.",
90
+ "input_directory_help": "Input directory containing documents to scan",
91
+ "rules_file_help": "Rules file (JSON)",
92
+ "recursive_help": "Scan files recursively",
93
+ "file_types_help": "File types to scan (comma-separated)",
94
+ "use_pdf_ocr_help": "Use PDF OCR for image-based PDFs",
95
+ "use_process_pool_help": "Use process pool instead of thread pool (better for CPU-intensive tasks)",
96
+ "batch_size_help": "Number of files to process in each batch",
97
+ "threads_help": "Number of threads for parallel scanning",
98
+ "progress_help": "Show progress bar",
99
+ "verbose_help": "Verbose output",
100
+ "language_help": "Set language (en for English, zh for Chinese)",
101
+ # Status and logging messages
102
+ "scanning_directory": "Scanning directory: {directory}",
103
+ "found_files_to_scan": "Found {count} files to scan",
104
+ "scan_resumed": "Scan resumed",
105
+ "scan_stopped_before_submitting_tasks": "Scan stopped by user before submitting all tasks",
106
+ "scan_paused": "Scan paused",
107
+ "scan_stopped_while_paused": "Scan stopped while paused",
108
+ "scan_stopped_by_user_canceling_tasks": "Scan stopped by user, cancelling remaining tasks...",
109
+ "task_timeout_scan_may_be_stopping": "Task timeout, scan may be stopping",
110
+ "error_scanning_file": "Error scanning file: {error}",
111
+ "progress_report": "Progress: {processed}/{total} files processed",
112
+ "force_shutting_down_executor": "Force shutting down executor...",
113
+ "scan_stopped_processed_files": "Scan stopped. Processed {processed} files",
114
+ "scan_complete_found_matches": "Scan complete. Found matches in {matches_count} files",
115
+ "found_matches_in_file": "Found matches in: {file_name}",
116
+ "processed_file_info": "Processed {file_name} ({ext}) in {time:.3f}s - {matches_count} matches found",
117
+ "could_not_extract_text_from_file": "Could not extract text from {file_path}: {error}",
118
+ "pymupdf_failed_for_file": "PyMuPDF failed for {file_name}: {error}",
119
+ "pypdf_also_failed_for_file": "pypdf also failed for {file_name}: {error}",
120
+ "no_pdf_library_installed": "No PDF library installed (pymupdf or pypdf)",
121
+ "pymupdf_not_installed": "PyMuPDF not installed",
122
+ "no_pages_found_in_file": "No pages found in {file_path}",
123
+ "no_metadata_found_in_file": "No metadata found in {file_path}",
124
+ "pymupdf_error_trying_fallback": "PyMuPDF error on {file_path}: {error}, trying pypdf fallback",
125
+ "pypdf_not_installed_skipping_extraction": "pypdf not installed, skipping PDF extraction",
126
+ "error_extracting_pdf_with_pypdf": "Error extracting PDF with pypdf: {error}",
127
+ "odfpy_not_installed_skipping_extraction": "odfpy not installed, skipping ODT extraction",
128
+ "error_extracting_odt": "Error extracting ODT: {error}",
129
+ "error_extracting_rtf": "Error extracting RTF: {error}",
130
+ "ebooklib_not_installed_skipping_extraction": "ebooklib not installed, skipping EPUB extraction",
131
+ "error_extracting_epub": "Error extracting EPUB: {error}",
132
+ "error_extracting_csv": "Error extracting CSV: {error}",
133
+ "error_extracting_xml": "Error extracting XML: {error}",
134
+ "error_extracting_html": "Error extracting HTML: {error}",
135
+ "error_extracting_markdown": "Error extracting Markdown: {error}",
136
+ "python_docx_not_installed_skipping_extraction": "python-docx not installed, skipping DOCX extraction",
137
+ "openpyxl_not_installed_skipping_extraction": "openpyxl not installed, skipping XLSX extraction",
138
+ "python_pptx_not_installed_skipping_extraction": "python-pptx not installed, skipping PPTX extraction",
139
+ "pillow_or_tesseract_not_installed_skipping_ocr": "PIL or pytesseract not installed, skipping image OCR",
140
+ "could_not_perform_ocr_on_file": "Could not perform OCR on {file_path}: {error}",
141
+ "input_directory_does_not_exist": "Input directory does not exist: {input_dir}",
142
+ "using_rules_file": "Using rules file: {rules_file}",
143
+ "invalid_json_in_rules_file": "Invalid JSON in rules file: {error}",
144
+ "invalid_rules_format": "Invalid rules format. Expected a list or dict with 'rules' key",
145
+ "no_valid_rules_found": "No valid rules found",
146
+ "total_time_elapsed": "Total time elapsed: {time:.2f}s",
147
+ "invalid_regex_pattern": "Invalid regex pattern '{pattern}': {error}",
148
+ "rules_file_does_not_exist_alt": "Rules file does not exist: {rules_file}",
149
+ "image_files_supported": "Image files supported (requires OCR)",
150
+ "include_image_formats": "Include Image Formats",
151
+ "include_image_formats_tooltip": "Include image formats (jpg, jpeg, png, gif, bmp, tiff) in scan",
152
+ }
@@ -0,0 +1,170 @@
1
+ """Chinese translations for docscan_gui."""
2
+
3
+ TRANSLATIONS = {
4
+ # Window and Tab Titles
5
+ "window_title": "文档扫描器",
6
+ "input_config_tab": "输入配置",
7
+ "scan_options_tab": "扫描选项",
8
+ # Input Section
9
+ "input_directory": "输入目录:",
10
+ "browse": "浏览...",
11
+ "rules_file": "规则文件:",
12
+ "file_types": "文件类型:",
13
+ # Options Section
14
+ "use_pdf_ocr": "使用 PDF OCR",
15
+ "use_process_pool": "使用进程池 (CPU 密集型)",
16
+ "threads": "线程数:",
17
+ "batch_size": "批处理大小:",
18
+ # Action Buttons
19
+ "start_scan": "开始扫描",
20
+ "pause": "暂停",
21
+ "resume": "恢复",
22
+ "stop": "停止",
23
+ "save_results": "保存结果",
24
+ "clear_results": "清除结果",
25
+ # Results Section
26
+ "results": "结果",
27
+ "files_scanned": "已扫描文件:",
28
+ "files_with_matches": "包含匹配项的文件:",
29
+ "progress_log": "进度日志:",
30
+ "match_details": "匹配详情:",
31
+ "selected_match_context": "选中匹配项的上下文:",
32
+ # Table Headers
33
+ "file": "文件",
34
+ "type": "类型",
35
+ "matches": "匹配数",
36
+ "time": "时间 (s)",
37
+ # Dialogs and Messages
38
+ "select_input_directory": "选择输入目录",
39
+ "select_rules_file": "选择规则文件",
40
+ "json_files": "JSON 文件 (*.json)",
41
+ "save_results_dialog": "保存结果",
42
+ "default_results_filename": "扫描结果_{datetime}.json",
43
+ # Error Messages
44
+ "error": "错误",
45
+ "warning": "警告",
46
+ "success": "成功",
47
+ "invalid_input_directory": "无效的输入目录",
48
+ "no_valid_rules": "没有找到有效的规则",
49
+ "failed_to_load_rules": "加载规则失败: {error}",
50
+ "no_results_to_save": "没有可保存的结果",
51
+ "failed_to_save_results": "保存结果失败: {error}",
52
+ "scan_failed": "扫描失败: {error}",
53
+ # Progress Messages
54
+ "starting_scan": "开始扫描...",
55
+ "scan_complete": "扫描完成!",
56
+ "pausing_scan": "暂停扫描...",
57
+ "stopping_scan": "停止扫描...",
58
+ "scan_completed": "扫描完成",
59
+ "scan_stopped": "扫描已停止",
60
+ "scan_resumed": "扫描已恢复",
61
+ "found_matches_files": "在 {count} 个文件中找到匹配项",
62
+ # File Info
63
+ "file_info": "文件",
64
+ "type_info": "类型",
65
+ "size": "大小",
66
+ "bytes": "字节",
67
+ # Match Info
68
+ "rule": "规则",
69
+ "description": "描述",
70
+ "line": "行 {line}: {match}",
71
+ "context": "上下文",
72
+ "default_file_types": "pdf,docx,xlsx,pptx,txt,odt,rtf,epub,csv,xml,html,md,jpg,jpeg,png,gif,bmp,tiff",
73
+ "default_rules_file": "rules.json",
74
+ # Menu Items
75
+ "file_menu": "文件(&F)",
76
+ "settings_menu": "设置(&S)",
77
+ "help_menu": "帮助(&H)",
78
+ "preferences": "首选项(&P)...",
79
+ "exit": "退出(&X)",
80
+ "about": "关于(&A)",
81
+ "about_title": "关于文档扫描器",
82
+ "about_text": "文档扫描器 GUI\n\n版本 1.0",
83
+ "language": "语言",
84
+ # Open Results
85
+ "open_results": "打开结果...",
86
+ "open_results_file": "打开扫描结果",
87
+ "loaded_results_from": "已从以下位置加载结果: {path}",
88
+ "results_loaded_successfully": "结果已成功从以下位置加载:\n{path}",
89
+ "failed_to_load_results": "加载结果失败: {error}",
90
+ # Settings Dialog
91
+ "language_settings": "语言设置",
92
+ "processing_options": "处理选项",
93
+ "performance_settings": "性能设置",
94
+ "language_label": "语言:",
95
+ "ocr_tooltip": "为扫描的 PDF 文件启用 OCR 以从图像中提取文本",
96
+ "process_pool_tooltip": "对 CPU 密集型操作使用多进程(可能会增加内存使用)",
97
+ "threads_tooltip": "工作线程数量(较高的值可能会提高速度但会使用更多 CPU)",
98
+ "batch_size_tooltip": "每批处理的文件数量(较大的批次可能会提高吞吐量)",
99
+ "file_types_tooltip": "要扫描的文件类型(逗号分隔)",
100
+ # Results Saved Message
101
+ "results_saved_to": "结果已保存至:\t{path}",
102
+ # Default Values
103
+ "files_scanned_zero": "已扫描文件: 0",
104
+ "files_with_matches_zero": "包含匹配项的文件: 0",
105
+ # Apply Button
106
+ "apply": "应用",
107
+ # Command-line specific translations
108
+ "document_scanner_description": "扫描文档并使用特定规则提取文本、图像和元数据。",
109
+ "input_directory_help": "包含待扫描文档的输入目录",
110
+ "rules_file_help": "规则文件 (JSON格式)",
111
+ "recursive_help": "递归扫描文件",
112
+ "file_types_help": "要扫描的文件类型(逗号分隔)",
113
+ "use_pdf_ocr_help": "对基于图像的PDF文件使用OCR",
114
+ "use_process_pool_help": "使用进程池而非线程池(更适合CPU密集型任务)",
115
+ "batch_size_help": "每批次处理的文件数量",
116
+ "threads_help": "并行扫描的线程数",
117
+ "progress_help": "显示进度条",
118
+ "verbose_help": "详细输出",
119
+ "language_help": "设置语言(en表示英文,zh表示中文)",
120
+ # Status and logging messages
121
+ "scanning_directory": "正在扫描目录: {directory}",
122
+ "found_files_to_scan": "发现 {count} 个文件待扫描",
123
+ "scan_stopped_before_submitting_tasks": "用户在提交所有任务之前停止扫描",
124
+ "scan_paused": "扫描已暂停",
125
+ "scan_stopped_while_paused": "扫描在暂停状态下停止",
126
+ "scan_stopped_by_user_canceling_tasks": "用户停止扫描,正在取消剩余任务...",
127
+ "task_timeout_scan_may_be_stopping": "任务超时,扫描可能正在停止",
128
+ "error_scanning_file": "扫描文件时出错: {error}",
129
+ "progress_report": "进度: {processed}/{total} 文件已处理",
130
+ "force_shutting_down_executor": "强制关闭执行器...",
131
+ "scan_stopped_processed_files": "扫描已停止。已处理 {processed} 个文件",
132
+ "scan_complete_found_matches": "扫描完成。在 {matches_count} 个文件中找到匹配项",
133
+ "found_matches_in_file": "找到匹配项: {file_name}",
134
+ "processed_file_info": "已处理 {file_name} ({ext}),耗时 {time:.3f}秒 - 找到 {matches_count} 个匹配项",
135
+ "could_not_extract_text_from_file": "无法从 {file_path} 提取文本: {error}",
136
+ "pymupdf_failed_for_file": "{file_name} 的 PyMuPDF 失败: {error}",
137
+ "pypdf_also_failed_for_file": "{file_name} 的 pypdf 也失败: {error}",
138
+ "no_pdf_library_installed": "未安装PDF库 (pymupdf 或 pypdf)",
139
+ "pymupdf_not_installed": "未安装 PyMuPDF",
140
+ "no_pages_found_in_file": "在 {file_path} 中未找到页面",
141
+ "no_metadata_found_in_file": "在 {file_path} 中未找到元数据",
142
+ "pymupdf_error_trying_fallback": "PyMuPDF 在 {file_path} 上出错: {error},尝试使用 pypdf 回退",
143
+ "pypdf_not_installed_skipping_extraction": "未安装 pypdf,跳过PDF提取",
144
+ "error_extracting_pdf_with_pypdf": "使用 pypdf 提取PDF时出错: {error}",
145
+ "odfpy_not_installed_skipping_extraction": "未安装 odfpy,跳过ODT提取",
146
+ "error_extracting_odt": "提取ODT时出错: {error}",
147
+ "error_extracting_rtf": "提取RTF时出错: {error}",
148
+ "ebooklib_not_installed_skipping_extraction": "未安装 ebooklib,跳过EPUB提取",
149
+ "error_extracting_epub": "提取EPUB时出错: {error}",
150
+ "error_extracting_csv": "提取CSV时出错: {error}",
151
+ "error_extracting_xml": "提取XML时出错: {error}",
152
+ "error_extracting_html": "提取HTML时出错: {error}",
153
+ "error_extracting_markdown": "提取Markdown时出错: {error}",
154
+ "python_docx_not_installed_skipping_extraction": "未安装 python-docx,跳过DOCX提取",
155
+ "openpyxl_not_installed_skipping_extraction": "未安装 openpyxl,跳过XLSX提取",
156
+ "python_pptx_not_installed_skipping_extraction": "未安装 python-pptx,跳过PPTX提取",
157
+ "pillow_or_tesseract_not_installed_skipping_ocr": "未安装 PIL 或 pytesseract,跳过图像OCR",
158
+ "could_not_perform_ocr_on_file": "无法对 {file_path} 执行OCR: {error}",
159
+ "input_directory_does_not_exist": "输入目录不存在: {input_dir}",
160
+ "using_rules_file": "使用规则文件: {rules_file}",
161
+ "invalid_json_in_rules_file": "规则文件中的JSON无效: {error}",
162
+ "invalid_rules_format": "无效的规则格式。期望包含'rules'键的列表或字典",
163
+ "no_valid_rules_found": "未找到有效规则",
164
+ "total_time_elapsed": "总耗时: {time:.2f}秒",
165
+ "invalid_regex_pattern": "无效的正则表达式模式 '{pattern}': {error}",
166
+ "rules_file_does_not_exist_alt": "规则文件不存在: {rules_file}",
167
+ "image_files_supported": "支持的图像文件(需要OCR)",
168
+ "include_image_formats": "包含图像格式",
169
+ "include_image_formats_tooltip": "在扫描中包含图像格式(jpg, jpeg, png, gif, bmp, tiff)",
170
+ }
sfi/filedate/filedate.py CHANGED
@@ -1,112 +1,185 @@
1
- """Remove file date prefix and replace with creation/modification date."""
2
-
3
- from __future__ import annotations
4
-
5
- import argparse
6
- import concurrent.futures
7
- import logging
8
- import re
9
- import time
10
- from functools import lru_cache
11
- from pathlib import Path
12
-
13
- DETECT_SEPARATORS: str = "-_#.~"
14
- SEP: str = "_"
15
- DATE_PATTERN = re.compile(r"(20|19)\d{2}((0[1-9])|(1[012]))((0[1-9])|([12]\d)|(3[01]))")
16
-
17
- logging.basicConfig(level=logging.INFO, format="%(message)s")
18
- logger = logging.getLogger(__name__)
19
-
20
-
21
- def rename_file(target_path: Path) -> None:
22
- """Rename file using time marker."""
23
- filestem = remove_date_prefix(target_path.stem)
24
- modified, created = target_path.stat().st_mtime, target_path.stat().st_ctime
25
- time_mark = time.strftime(
26
- "%Y%m%d",
27
- time.localtime(max((modified, created))),
28
- )
29
- dst_path = target_path.with_name(
30
- f"{time_mark}{SEP}{filestem}{target_path.suffix}",
31
- )
32
-
33
- if dst_path == target_path:
34
- logger.warning(f"{target_path} is the same as {dst_path}, skipping.")
35
- return
36
-
37
- sequence = 1
38
- while dst_path.exists() and sequence < 100:
39
- logger.warning(f"{dst_path} already exists, adding unique suffix.")
40
- dst_path = dst_path.with_name(
41
- f"{dst_path.stem}({sequence}){dst_path.suffix}",
42
- )
43
- sequence += 1
44
-
45
- try:
46
- target_path.rename(dst_path)
47
- except Exception:
48
- logger.error(f"Rename failed: {target_path} -> {dst_path}")
49
- else:
50
- logger.info(f"Rename: {target_path} -> {dst_path}")
51
-
52
-
53
- @lru_cache(maxsize=1024)
54
- def remove_date_prefix(filestem: str) -> str:
55
- """Remove date prefix from filename."""
56
- logger.debug(f"Removing date prefix from: {filestem}")
57
-
58
- match = re.search(DATE_PATTERN, filestem)
59
- if not match:
60
- logger.debug(f"No date prefix found: {filestem}")
61
- return filestem
62
- b, e = match.start(), match.end()
63
- if b >= 1 and filestem[b - 1] in DETECT_SEPARATORS:
64
- filestem = filestem[: b - 1] + filestem[e:]
65
- elif e < len(filestem) and filestem[e] in DETECT_SEPARATORS:
66
- filestem = filestem[:b] + filestem[e + 1 :]
67
-
68
- logger.debug(f"Removed date prefix: {filestem}")
69
- return remove_date_prefix(filestem)
70
-
71
-
72
- def get_filtered_path(target_paths: list[str]) -> list[Path]:
73
- """Get filtered path list."""
74
- logger.debug(f"Getting filtered path list: {target_paths}")
75
-
76
- converted_paths = [Path(t) for t in target_paths]
77
- valid_paths = [p for p in converted_paths if p.exists()]
78
- missing_paths = list(set(converted_paths) - set(valid_paths))
79
-
80
- if missing_paths:
81
- logger.warning(f"Files not found: {missing_paths}, skipped.")
82
-
83
- logger.debug(f"Filtered path list: {valid_paths}")
84
- return valid_paths
85
-
86
-
87
- def main() -> None:
88
- parser = argparse.ArgumentParser(
89
- prog="filedate", description="Remove file date prefix and replace with creation/modification date."
90
- )
91
- parser.add_argument("targets", type=str, nargs="+", help="List of input files")
92
- parser.add_argument("--debug", "-d", action="store_true", help="Enable debug mode")
93
- args = parser.parse_args()
94
-
95
- if args.debug:
96
- logger.setLevel(logging.DEBUG)
97
- else:
98
- logger.setLevel(logging.INFO)
99
-
100
- if not args.targets:
101
- logger.error("Please provide file list.")
102
- return
103
-
104
- valid_paths = get_filtered_path(args.targets)
105
- if not valid_paths:
106
- logger.error("No valid files to process.")
107
- return
108
-
109
- t0 = time.perf_counter()
110
- with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
111
- executor.map(rename_file, valid_paths)
112
- logger.info(f"Done in {time.perf_counter() - t0:.4f}s")
1
+ """Remove file date prefix and replace with creation/modification date."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import concurrent.futures
7
+ import logging
8
+ import os
9
+ import re
10
+ import time
11
+ from dataclasses import dataclass
12
+ from functools import cached_property, lru_cache
13
+ from pathlib import Path
14
+
15
+ DETECT_SEPARATORS: str = "-_#.~"
16
+ SEP: str = "_"
17
+ MAX_RETRY: int = 100
18
+ DATE_PATTERN = re.compile(r"(20|19)\d{2}((0[1-9])|(1[012]))((0[1-9])|([12]\d)|(3[01]))")
19
+
20
+ logging.basicConfig(level=logging.INFO, format="%(message)s")
21
+ logger = logging.getLogger(__name__)
22
+
23
+
24
+ @dataclass(frozen=True)
25
+ class SingleFileRenamer:
26
+ """Renamer for single file."""
27
+
28
+ path: Path
29
+
30
+ @staticmethod
31
+ def rename(path: Path) -> None:
32
+ renamer = SingleFileRenamer(path)
33
+
34
+ dest_path = renamer.filepath_renamed
35
+ sequence = 1
36
+ while dest_path.exists() and sequence <= MAX_RETRY:
37
+ logger.warning(f"{dest_path} already exists, adding unique suffix.")
38
+ dest_path = renamer.filepath_renamed.with_name(
39
+ f"{renamer.filestem_renamed}({sequence}){renamer.file_suffix}",
40
+ )
41
+ sequence += 1
42
+
43
+ # If we've reached the max retry limit and the path still exists, give up
44
+ if dest_path.exists() and sequence > MAX_RETRY:
45
+ logger.error(f"Max retry reached, giving up on {renamer.path}.")
46
+ return
47
+
48
+ try:
49
+ renamer.path.rename(dest_path)
50
+ except Exception:
51
+ logger.error(f"Rename failed: {renamer.path} -> {dest_path}")
52
+ else:
53
+ logger.info(f"Rename: {renamer.path} -> {dest_path}")
54
+
55
+ @cached_property
56
+ def file_suffix(self) -> str:
57
+ """Get file suffix"""
58
+ return self.path.suffix
59
+
60
+ @cached_property
61
+ def filepath_renamed(self) -> Path:
62
+ """Get renamed filepath"""
63
+ return self.path.with_name(self.filename_renamed)
64
+
65
+ @cached_property
66
+ def filestem_renamed(self) -> str:
67
+ """Get renamed file stem"""
68
+ # Extract stem from the filename string
69
+ suffix = self.path.suffix
70
+ return (
71
+ self.filename_renamed[: -len(suffix)]
72
+ if suffix and self.filename_renamed.endswith(suffix)
73
+ else self.filename_renamed
74
+ )
75
+
76
+ @cached_property
77
+ def filename_renamed(self) -> str:
78
+ """Get renamed filename"""
79
+ return f"{self.time_mark}{SEP}{self.filestem_without_date}{self.path.suffix}"
80
+
81
+ @cached_property
82
+ def filestem_without_date(self) -> str:
83
+ """Get file stem without date prefix"""
84
+
85
+ @lru_cache(maxsize=1024)
86
+ def remove_date_prefix(filestem: str) -> str:
87
+ """Remove date prefix from filename."""
88
+
89
+ # Handle the case where the filestem is just a hidden file (e.g., .txt) with no date
90
+ if (
91
+ filestem.startswith(".")
92
+ and not filestem[1:].isdigit()
93
+ and len(filestem) > 1
94
+ ) and not re.search(DATE_PATTERN, filestem):
95
+ return ""
96
+
97
+ match = re.search(DATE_PATTERN, filestem)
98
+ if not match:
99
+ logger.debug(f"No date prefix found: {filestem}")
100
+ return filestem
101
+ b, e = match.start(), match.end()
102
+ if b >= 1 and filestem[b - 1] in DETECT_SEPARATORS:
103
+ filestem = filestem[: b - 1] + filestem[e:]
104
+ elif e < len(filestem) and filestem[e] in DETECT_SEPARATORS:
105
+ filestem = filestem[:b] + filestem[e + 1 :]
106
+ return remove_date_prefix(filestem)
107
+
108
+ return remove_date_prefix(self.filestem)
109
+
110
+ @cached_property
111
+ def filestem(self) -> str:
112
+ """Get current file stem."""
113
+ return self.path.stem
114
+
115
+ @cached_property
116
+ def filestat(self) -> os.stat_result:
117
+ """Get file stat."""
118
+ return self.path.stat()
119
+
120
+ @cached_property
121
+ def modified_time(self) -> float:
122
+ """Get modified time."""
123
+ return self.filestat.st_mtime
124
+
125
+ @cached_property
126
+ def created_time(self) -> float:
127
+ """Get created time."""
128
+ return self.filestat.st_ctime
129
+
130
+ @cached_property
131
+ def time_mark(self) -> str:
132
+ """Get time mark."""
133
+ return time.strftime(
134
+ "%Y%m%d",
135
+ time.localtime(max((self.modified_time, self.created_time))),
136
+ )
137
+
138
+
139
+ @dataclass(frozen=True)
140
+ class MultiFileRenamer:
141
+ """Renamer for multiple files."""
142
+
143
+ paths: list[str]
144
+
145
+ @staticmethod
146
+ def rename_files(paths: list[str]) -> None:
147
+ renamer = MultiFileRenamer(paths)
148
+ if not renamer.filtered_paths:
149
+ logger.error("No valid files to process.")
150
+ return
151
+
152
+ t0 = time.perf_counter()
153
+ with concurrent.futures.ThreadPoolExecutor(max_workers=8) as executor:
154
+ executor.map(SingleFileRenamer.rename, renamer.filtered_paths)
155
+ logger.info(f"Done in {time.perf_counter() - t0:.4f}s")
156
+
157
+ @cached_property
158
+ def converted_paths(self) -> list[Path]:
159
+ """Get filtered paths."""
160
+ return [Path(p) for p in self.paths]
161
+
162
+ @cached_property
163
+ def filtered_paths(self) -> list[Path]:
164
+ """Get filtered paths."""
165
+ return [p for p in self.converted_paths if p.exists()]
166
+
167
+ @cached_property
168
+ def missing_paths(self) -> list[Path]:
169
+ """Get missing paths."""
170
+ return list(set(self.converted_paths) - set(self.filtered_paths))
171
+
172
+
173
+ def main() -> None:
174
+ parser = argparse.ArgumentParser(
175
+ prog="filedate",
176
+ description="Remove file date prefix and replace with creation/modification date.",
177
+ )
178
+ parser.add_argument("targets", type=str, nargs="+", help="List of input files")
179
+ parser.add_argument("--debug", "-d", action="store_true", help="Enable debug mode")
180
+ args = parser.parse_args()
181
+
182
+ if args.debug:
183
+ logger.setLevel(logging.DEBUG)
184
+
185
+ MultiFileRenamer.rename_files(args.targets)
@@ -0,0 +1,2 @@
1
+ """Git tool module.
2
+ """