raytoolsbox 2.0.1__tar.gz → 2.1.0__tar.gz

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 (53) hide show
  1. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/.gitignore +2 -0
  2. raytoolsbox-2.1.0/CHANGELOG.md +113 -0
  3. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/PKG-INFO +24 -11
  4. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/README.md +20 -7
  5. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/pyproject.toml +43 -55
  6. raytoolsbox-2.1.0/src/raytoolsbox/__init__.py +36 -0
  7. raytoolsbox-2.1.0/src/raytoolsbox/_lazy.py +55 -0
  8. raytoolsbox-2.1.0/src/raytoolsbox/security/__init__.py +31 -0
  9. raytoolsbox-2.1.0/src/raytoolsbox/security/authenticator.py +334 -0
  10. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/src/raytoolsbox/security/crypto_manager.py +216 -190
  11. raytoolsbox-2.1.0/src/raytoolsbox/security/pin_manager.py +220 -0
  12. raytoolsbox-2.1.0/src/raytoolsbox/utils/__init__.py +47 -0
  13. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/src/raytoolsbox/utils/mailToPhone.py +48 -16
  14. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/src/raytoolsbox/utils/monitor_func.py +14 -26
  15. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/src/raytoolsbox/utils/useful_func.py +86 -12
  16. {raytoolsbox-2.0.1/tests → raytoolsbox-2.1.0/tests/security}/test_authenticator.py +54 -6
  17. {raytoolsbox-2.0.1/tests → raytoolsbox-2.1.0/tests/security}/test_crypto_manager.py +75 -3
  18. {raytoolsbox-2.0.1/tests → raytoolsbox-2.1.0/tests/security}/test_pin_manager.py +41 -0
  19. raytoolsbox-2.1.0/tests/test_lazy_imports.py +98 -0
  20. {raytoolsbox-2.0.1/tests → raytoolsbox-2.1.0/tests/utils}/test_useful_func.py +39 -0
  21. raytoolsbox-2.0.1/.python-version +0 -1
  22. raytoolsbox-2.0.1/C++/350/260/203/347/224/250/cpp/346/211/223/345/214/205/344/270/272dll.py +0 -176
  23. raytoolsbox-2.0.1/C++/350/260/203/347/224/250/fa.py +0 -14
  24. raytoolsbox-2.0.1/C++/350/260/203/347/224/250/mylib.cpp +0 -6
  25. raytoolsbox-2.0.1/C++/350/260/203/347/224/250/mylib.dll +0 -0
  26. raytoolsbox-2.0.1/CHANGELOG.md +0 -77
  27. raytoolsbox-2.0.1/CLAUDE.md +0 -108
  28. raytoolsbox-2.0.1/main.py +0 -6
  29. raytoolsbox-2.0.1/src/raytoolsbox/__init__.py +0 -0
  30. raytoolsbox-2.0.1/src/raytoolsbox/security/__init__.py +0 -0
  31. raytoolsbox-2.0.1/src/raytoolsbox/security/authenticator.py +0 -282
  32. raytoolsbox-2.0.1/src/raytoolsbox/security/pin_manager.py +0 -114
  33. raytoolsbox-2.0.1/src/raytoolsbox/utils/__init__.py +0 -0
  34. raytoolsbox-2.0.1/uv.lock +0 -1619
  35. raytoolsbox-2.0.1/uv.toml +0 -1
  36. raytoolsbox-2.0.1/web/345/217/212/346/225/260/346/215/256/345/267/245/345/205/267/Flask/345/222/214FastAPI/345/257/271/346/257/224/FastAPI/351/243/216/346/240/274.py +0 -14
  37. raytoolsbox-2.0.1/web/345/217/212/346/225/260/346/215/256/345/267/245/345/205/267/Flask/345/222/214FastAPI/345/257/271/346/257/224/Flask/351/243/216/346/240/274.py +0 -13
  38. raytoolsbox-2.0.1/web/345/217/212/346/225/260/346/215/256/345/267/245/345/205/267//345/256/211/345/205/250/346/200/247//345/212/240/345/257/206/350/247/243/345/257/206/344/270/216/347/255/276/345/220/215/351/252/214/347/255/276.py +0 -73
  39. raytoolsbox-2.0.1/web/345/217/212/346/225/260/346/215/256/345/267/245/345/205/267//345/256/211/345/205/250/346/200/247//351/252/214/350/257/201/345/231/250.py +0 -241
  40. raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/DPI/347/252/227/345/217/243/346/265/213/351/207/217.py +0 -135
  41. raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/USB/347/275/221/347/273/234/345/205/261/344/272/253.py +0 -208
  42. raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/resource/ICON_running.png +0 -0
  43. raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/resource/ICON_stopped.png +0 -0
  44. raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/resource/favicon (1).ico +0 -0
  45. raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267/USB/347/275/221/347/273/234/345/205/261/344/272/253/usb_watcher.py +0 -79
  46. raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267//350/257/255/351/237/263/350/275/254/346/226/207/345/255/227.py +0 -104
  47. raytoolsbox-2.0.1//345/256/236/347/224/250/345/267/245/345/205/267//350/275/256/350/257/242/347/216/207/346/237/245/350/257/242.py +0 -159
  48. raytoolsbox-2.0.1//351/202/256/344/273/266/345/217/221/351/200/201/mailToPhone.py +0 -81
  49. raytoolsbox-2.0.1//351/202/256/344/273/266/345/217/221/351/200/201//351/207/215/350/246/201/346/227/245/346/234/237/346/217/220/351/206/222/date_emalil_note.py +0 -147
  50. raytoolsbox-2.0.1//351/202/256/344/273/266/345/217/221/351/200/201//351/207/215/350/246/201/346/227/245/346/234/237/346/217/220/351/206/222/html_get.py +0 -55
  51. {raytoolsbox-2.0.1 → raytoolsbox-2.1.0}/tests/readme.md +0 -0
  52. {raytoolsbox-2.0.1/tests → raytoolsbox-2.1.0/tests/utils}/test_mail_to_phone.py +0 -0
  53. {raytoolsbox-2.0.1/tests → raytoolsbox-2.1.0/tests/utils}/test_monitor_func.py +0 -0
@@ -6,6 +6,7 @@ dist/
6
6
  wheels/
7
7
  *.egg-info
8
8
  .hypothesis/
9
+ .pytest_cache/
9
10
 
10
11
 
11
12
  # Virtual environments
@@ -21,4 +22,5 @@ wheels/
21
22
  %数据
22
23
  data/**/*.csv
23
24
  tests/testData/*
25
+ tests/mytestData/
24
26
  *.bin
@@ -0,0 +1,113 @@
1
+ # Changelog
2
+
3
+ 所有对本项目的显著变更都将记录在此文件中。
4
+
5
+ 格式基于 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),
6
+ 版本管理遵循 [Semantic Versioning](https://semver.org/lang/zh-CN/)。
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [2.1.0] - 2026-12-06
11
+
12
+ ### Added
13
+
14
+ - 子进程/子线程执行器(`run_in_subprocess` / `run_in_subthread`),支持超时与异常透传
15
+ - 顶层与子包公共 API:可直接 `from raytoolsbox.security import CryptoManager`、`from raytoolsbox.utils import make_timer`,并提供 `raytoolsbox.__version__`
16
+ - `Authenticator.get_secret(force=True)`:已绑定时可重新生成 secret 并重新打印二维码,新 secret 验证通过前原绑定保持有效
17
+ - `PinManager` 在 keyring 不可用时回退到文件存储(权限 600),与 `Authenticator` 行为一致
18
+ - `send_email()` 新增 `timeout` 与 `use_ssl` 参数:465 端口走隐式 SSL,587/25 自动升级 STARTTLS
19
+ - `scripts/release.py` 一键发布脚本:发布前校验 → 测试 → 构建 → 上传 PyPI → 打 tag 推送
20
+
21
+ ### Fixed
22
+
23
+ - `CryptoManager` 空 PIN 生成密钥后无法加载("能存不能读"):现在保存与加载对 PIN 的处理严格对称
24
+ - `CryptoManager` 未指定密钥目录时加载密钥抛 `AttributeError`,改为抛明确的 `ValueError`
25
+ - `CryptoManager` 解密/验签会把调用方错误(如 `packet` 传 `str`)当成解密失败静默返回 `None`,现在一律抛 `TypeError`
26
+ - `CryptoManager.sign()` 先对非法数据签名再报类型错误,现改为先校验参数
27
+ - `run_in_subprocess()` 返回值或异常无法 pickle 时父进程只收到 `EOFError: ""`,现改为抛带原始原因的 `TypeError`,并把子进程 traceback 附加到异常上
28
+ - `PinManager` 的 PIN 哈希比较改用 `hmac.compare_digest`,避免时序侧信道
29
+ - `make_timer()` 改用单调时钟 `time.perf_counter()`,不再受系统对时/夏令时影响
30
+ - 移除 `monitor_func.py` 中永不退出的 `while 1` 演示死循环
31
+
32
+ ### Changed
33
+
34
+ - 子包 `__init__` 改为惰性导入(PEP 562):`import raytoolsbox.utils.monitor_func` 不再连带加载 `smtplib` / `yaml` / `multiprocessing`,`import raytoolsbox.security.crypto_manager` 不再连带加载 `keyring` / `qrcode` / `pyotp`;`raytoolsbox.__version__` 也改为用到时才查 `importlib.metadata`
35
+ - 库内所有 `print` 改为 `logging`(`getLogger(__name__)`),不再污染调用方的 stdout
36
+ - `Authenticator` / `PinManager` 增加输入校验:`service_name`/`account` 拒绝路径分隔符,验证码与 PIN 自动清洗并校验
37
+ - `Authenticator` 写文件改为以 600 权限原子创建,消除先写后改权限的窗口期
38
+ - 移除 `src/` 下三处 `__main__` 演示块,演示统一放到 `example/`
39
+ - `pyproject.toml` 补全中文 description,`qrcode` 改为 `qrcode[pil]`(终端二维码依赖 pillow)
40
+ - 测试体系重构,切换至 Hatchling 构建系统
41
+ - 引入 `src/` layout 项目布局
42
+ - 移除 `python-semantic-release`,改为手动管理版本和 CHANGELOG
43
+ - 显式指定 wheel 打包目录 `src/raytoolsbox`
44
+ - 版本号改用 `uv version --bump` 升级(同步 `uv.lock`),tag 改为发布成功后创建
45
+ - 仓库结构整理:根目录草稿文件夹(`实用工具/`、`邮件发送/`、`C++调用/`、`web及数据工具/`)合并到 `example/` 下的中文分类目录
46
+ - 项目指令文件 `CLAUDE.md` 改名为通用的 `AGENTS.md`,`CLAUDE.md` 仅保留指向
47
+ - `example/` 与 `tests/` 按 `security/`、`utils/` 两个子目录组织,与 `src/raytoolsbox` 一一对应
48
+ - 发布流程文档并入 `AGENTS.md`(主流程 + release 命令内部流程图、失败处理),不再单独维护 `包发布流程.md`
49
+
50
+ ### Removed
51
+
52
+ - `python-semantic-release` 自动版本管理工具及其 `[tool.semantic_release]` 配置
53
+ - 早期重复实现(`邮件发送/mailToPhone.py`、`web及数据工具/安全性/验证器.py`)与编译产物 `mylib.dll`
54
+ - hatch 脚手架残留 `main.py`
55
+ - 示例不再需要的开发依赖:`flask`、`fastapi`、`uvicorn`、`matplotlib`、`pynput`、`pyside6`、`tkinterdnd2`、`ttkbootstrap`、`zhdate`,以及重复声明的 `pyyaml`
56
+
57
+ ### ⚠ Breaking
58
+
59
+ - `Authenticator.get_secret()` 在账户已绑定时由"返回错误说明字符串"改为抛出 `RuntimeError`,返回类型恒为 `dict`;依赖旧字符串返回值的调用方需改为捕获异常,或传 `force=True` 重新绑定
60
+ - `CryptoManager.decrypt()` / `verify()` 对参数类型错误(如 `packet` 传 `str`)由"静默返回 `None`"改为抛出 `TypeError`,只有数据损坏或密钥不匹配才返回 `None`
61
+
62
+ ## [2.0.1] - 2026-07-03
63
+
64
+ ### Added
65
+
66
+ - 集成 `python-semantic-release` 自动版本管理
67
+
68
+ ### Changed
69
+
70
+ - `uv` 全面替代 `pip`/`poetry` 管理依赖和构建
71
+
72
+ ## [1.1.2] - 2026-02-05
73
+
74
+ ### Fixed
75
+
76
+ - 修复资源路径获取逻辑,增强 PyInstaller 打包兼容性
77
+
78
+ ### Changed
79
+
80
+ - 重构函数注释体系,统一文档风格
81
+
82
+ ## [1.1.1] - 2026-02-02
83
+
84
+ ### Fixed
85
+
86
+ - 调整 Python 支持范围为 `>=3.11`
87
+
88
+ ### Changed
89
+
90
+ - Python 版本从 3.13 降级至 3.11,提升兼容性
91
+
92
+ ## [1.1.0] - 2026-02-02
93
+
94
+ ### Added
95
+
96
+ - **PinManager** — PBKDF2-HMAC-SHA256 PIN 码哈希存储模块
97
+ - **Authenticator** — TOTP 双因素认证模块
98
+
99
+ ### Changed
100
+
101
+ - 重构项目结构,将所有密码学相关操作归入 `security` 子包
102
+
103
+ ## [1.0.0] - 2026-02-01
104
+
105
+ ### Added
106
+
107
+ - 项目初始发布
108
+ - **CryptoManager** — ECC 加密套件(X25519 + ChaCha20-Poly1305 + Ed25519 签名)
109
+ - 邮件发送、性能监控、资源路径解析等工具模块
110
+
111
+ ---
112
+
113
+ 各版本标签:`v1.0.0` `v1.1.0` `v1.1.1` `v1.1.2` `2.0.1`
@@ -1,7 +1,7 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: raytoolsbox
3
- Version: 2.0.1
4
- Summary: Add your description here
3
+ Version: 2.1.0
4
+ Summary: 个人 Python 工具箱:常用开发工具、性能监控与安全模块(加密签名 / TOTP / PIN)
5
5
  Author: TIMLES@https://github.com/TIMLES
6
6
  License: MIT
7
7
  Requires-Python: >=3.11
@@ -10,7 +10,7 @@ Requires-Dist: keyring>=25.7.0
10
10
  Requires-Dist: psutil>=7.2.2
11
11
  Requires-Dist: pyotp>=2.9.0
12
12
  Requires-Dist: pyyaml>=6.0.3
13
- Requires-Dist: qrcode>=8.2
13
+ Requires-Dist: qrcode[pil]>=8.2
14
14
  Description-Content-Type: text/markdown
15
15
 
16
16
 
@@ -112,6 +112,8 @@ raytoolsbox/
112
112
  │
113
113
  ├── pyproject.toml # 项目信息 + 依赖管理(uv)
114
114
  ├── README.md # 项目文档
115
+ ├── AGENTS.md # AI 编码助手指令(含发布流程)
116
+ ├── CHANGELOG.md # 变更日志
115
117
  ├── src/
116
118
  │ └── raytoolsbox/
117
119
  │ ├── __init__.py
@@ -127,12 +129,21 @@ raytoolsbox/
127
129
  │ └── pin_manager.py
128
130
  │
129
131
  └── tests/
130
- ├── test_authenticator.py # TOTP 认证测试
131
- ├── test_crypto_manager.py # 加密/签名 + fuzz 测试
132
- ├── test_mail_to_phone.py # 邮件发送测试
133
- ├── test_monitor_func.py # 性能监控测试
134
- ├── test_pin_manager.py # PIN 管理测试
135
- └── test_useful_func.py # 路径/窗口/子进程工具测试
132
+ ├── security/
133
+ │ ├── test_authenticator.py # TOTP 认证测试
134
+ │ ├── test_crypto_manager.py # 加密/签名 + fuzz 测试
135
+ │ └── test_pin_manager.py # PIN 管理测试
136
+ └── utils/
137
+ ├── test_mail_to_phone.py # 邮件发送测试
138
+ ├── test_monitor_func.py # 性能监控测试
139
+ └── test_useful_func.py # 路径/窗口/子进程工具测试
140
+
141
+ example/ # 与 src 对应的示例(不随包发布)
142
+ ├── security/ # 加密与签名.py / 验证器.py / PIN码.py
143
+ └── utils/ # 邮件发送.py / 监控与计时.py / 通用工具.py
144
+
145
+ scripts/
146
+ └── release.py # 一键发布脚本
136
147
  ```
137
148
 
138
149
  ---
@@ -142,7 +153,9 @@ raytoolsbox/
142
153
  本项目使用 pytest:
143
154
 
144
155
  ```
145
- uv run pytest
156
+ uv run pytest # 全部
157
+ uv run pytest tests/security # 安全模块
158
+ uv run pytest tests/utils # 工具模块
146
159
  ```
147
160
 
148
161
  全部测试应通过:
@@ -97,6 +97,8 @@ raytoolsbox/
97
97
  │
98
98
  ├── pyproject.toml # 项目信息 + 依赖管理(uv)
99
99
  ├── README.md # 项目文档
100
+ ├── AGENTS.md # AI 编码助手指令(含发布流程)
101
+ ├── CHANGELOG.md # 变更日志
100
102
  ├── src/
101
103
  │ └── raytoolsbox/
102
104
  │ ├── __init__.py
@@ -112,12 +114,21 @@ raytoolsbox/
112
114
  │ └── pin_manager.py
113
115
  │
114
116
  └── tests/
115
- ├── test_authenticator.py # TOTP 认证测试
116
- ├── test_crypto_manager.py # 加密/签名 + fuzz 测试
117
- ├── test_mail_to_phone.py # 邮件发送测试
118
- ├── test_monitor_func.py # 性能监控测试
119
- ├── test_pin_manager.py # PIN 管理测试
120
- └── test_useful_func.py # 路径/窗口/子进程工具测试
117
+ ├── security/
118
+ │ ├── test_authenticator.py # TOTP 认证测试
119
+ │ ├── test_crypto_manager.py # 加密/签名 + fuzz 测试
120
+ │ └── test_pin_manager.py # PIN 管理测试
121
+ └── utils/
122
+ ├── test_mail_to_phone.py # 邮件发送测试
123
+ ├── test_monitor_func.py # 性能监控测试
124
+ └── test_useful_func.py # 路径/窗口/子进程工具测试
125
+
126
+ example/ # 与 src 对应的示例(不随包发布)
127
+ ├── security/ # 加密与签名.py / 验证器.py / PIN码.py
128
+ └── utils/ # 邮件发送.py / 监控与计时.py / 通用工具.py
129
+
130
+ scripts/
131
+ └── release.py # 一键发布脚本
121
132
  ```
122
133
 
123
134
  ---
@@ -127,7 +138,9 @@ raytoolsbox/
127
138
  本项目使用 pytest:
128
139
 
129
140
  ```
130
- uv run pytest
141
+ uv run pytest # 全部
142
+ uv run pytest tests/security # 安全模块
143
+ uv run pytest tests/utils # 工具模块
131
144
  ```
132
145
 
133
146
  全部测试应通过:
@@ -1,55 +1,43 @@
1
- [project]
2
- name = "raytoolsbox"
3
- version = "2.0.1"
4
- description = "Add your description here"
5
- authors = [{ name = "TIMLES@https://github.com/TIMLES" }]
6
- license = {text = "MIT"}
7
- readme = "README.md"
8
- requires-python = ">=3.11"
9
- dependencies = [
10
- "cryptography>=46.0.4",
11
- "keyring>=25.7.0",
12
- "psutil>=7.2.2",
13
- "pyotp>=2.9.0",
14
- "pyyaml>=6.0.3",
15
- "qrcode>=8.2",
16
- ]
17
-
18
- # 开发依赖(不会随包发布)
19
- [dependency-groups]
20
- dev = [
21
- "fastapi>=0.128.0",
22
- "flask>=3.1.2",
23
- "hypothesis>=6.151.4",
24
- "matplotlib>=3.10.8",
25
- "pynput>=1.8.1",
26
- "pyside6>=6.10.1",
27
- "pytest>=9.0.2",
28
- "pyyaml>=6.0.3",
29
- "ruff>=0.11.0",
30
- "tkinterdnd2>=0.4.3",
31
- "ttkbootstrap>=1.20.1",
32
- "uvicorn>=0.40.0",
33
- "zhdate>=0.1",
34
- ]
35
-
36
- # 指定构建系统
37
- [build-system]
38
- requires = ["hatchling"]
39
- build-backend = "hatchling.build"
40
-
41
- # 指定源码目录(src/ layout)
42
- [tool.hatch.build.targets.wheel]
43
- packages = ["src/raytoolsbox"]
44
-
45
- [tool.ruff]
46
- target-version = "py311"
47
- line-length = 120
48
-
49
- [tool.ruff.lint]
50
- select = ["E", "F", "W", "I", "N", "UP", "B", "SIM", "ARG", "PIE", "PT"]
51
- ignore = ["E501"]
52
-
53
- [tool.ruff.format]
54
- quote-style = "double"
55
-
1
+ [project]
2
+ name = "raytoolsbox"
3
+ version = "2.1.0"
4
+ description = "个人 Python 工具箱:常用开发工具、性能监控与安全模块(加密签名 / TOTP / PIN)"
5
+ authors = [{ name = "TIMLES@https://github.com/TIMLES" }]
6
+ license = {text = "MIT"}
7
+ readme = "README.md"
8
+ requires-python = ">=3.11"
9
+ dependencies = [
10
+ "cryptography>=46.0.4",
11
+ "keyring>=25.7.0",
12
+ "psutil>=7.2.2",
13
+ "pyotp>=2.9.0",
14
+ "pyyaml>=6.0.3",
15
+ "qrcode[pil]>=8.2",
16
+ ]
17
+
18
+ # 开发依赖(不会随包发布)
19
+ [dependency-groups]
20
+ dev = [
21
+ "hypothesis>=6.151.4",
22
+ "pytest>=9.0.2",
23
+ ]
24
+
25
+ # 指定构建系统
26
+ [build-system]
27
+ requires = ["hatchling"]
28
+ build-backend = "hatchling.build"
29
+
30
+ # 指定源码目录(src/ layout)
31
+ [tool.hatch.build.targets.wheel]
32
+ packages = ["src/raytoolsbox"]
33
+
34
+ # sdist 只包含包源码与测试,example/ 与 scripts/ 不随包发布
35
+ [tool.hatch.build.targets.sdist]
36
+ include = [
37
+ "src/raytoolsbox",
38
+ "tests",
39
+ "pyproject.toml",
40
+ "README.md",
41
+ "CHANGELOG.md",
42
+ ]
43
+ exclude = ["example", "scripts"]
@@ -0,0 +1,36 @@
1
+ """raytoolsbox — 个人 Python 工具箱:常用开发工具 + 安全模块。
2
+
3
+ 顶层只暴露版本号与两个子包,具体功能请从子包导入,例如::
4
+
5
+ from raytoolsbox.security import CryptoManager
6
+ from raytoolsbox.utils import make_timer
7
+
8
+ 子包内部同样按需导入(PEP 562),因此导入某个具体模块不会连带加载同包
9
+ 其他模块的依赖。
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ from typing import Any
15
+
16
+ __all__ = ["__version__", "security", "utils"]
17
+
18
+
19
+ def __getattr__(name: str) -> Any:
20
+ # __version__ 惰性计算:importlib.metadata 的导入本身就要几十毫秒,
21
+ # 不该让「只想 import raytoolsbox」的调用方为它买单。
22
+ if name != "__version__":
23
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
24
+
25
+ from importlib.metadata import PackageNotFoundError, version
26
+
27
+ try:
28
+ value = version("raytoolsbox")
29
+ except PackageNotFoundError: # 源码目录下未安装为发行版时
30
+ value = "0.0.0"
31
+ globals()["__version__"] = value # 缓存,后续访问走正常属性查找
32
+ return value
33
+
34
+
35
+ def __dir__() -> list[str]:
36
+ return sorted(__all__)
@@ -0,0 +1,55 @@
1
+ """子包惰性导入辅助。
2
+
3
+ 包 ``__init__.py`` 里如果直接 ``from .xxx import Yyy``,那么用户哪怕只想用
4
+ 其中一个模块,也会把兄弟模块的依赖全部导入(例如只想用 ``monitor_func``,
5
+ 却连带拉进 ``smtplib``、``yaml``、``multiprocessing``)。这里用 PEP 562 的
6
+ 模块级 ``__getattr__`` 把导入推迟到真正访问的那一刻。
7
+
8
+ 用法:
9
+
10
+ .. code-block:: python
11
+
12
+ from raytoolsbox._lazy import make_module_getattr
13
+
14
+ _EXPORTS = {"make_timer": "monitor_func"}
15
+
16
+ __getattr__ = make_module_getattr(globals(), _EXPORTS)
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import importlib
22
+ from collections.abc import Callable
23
+ from typing import Any
24
+
25
+ __all__ = ["make_module_getattr"]
26
+
27
+
28
+ def make_module_getattr(module_globals: dict[str, Any], exports: dict[str, str]) -> Callable[[str], Any]:
29
+ """构造一个模块级 ``__getattr__``,按需导入 ``exports`` 里的名字。
30
+
31
+ Parameters
32
+ ----------
33
+ module_globals : dict
34
+ 调用方(包 ``__init__``)的 ``globals()``。命中的名字导入后会写回
35
+ 这里做缓存,后续访问不再进入 ``__getattr__``。
36
+ exports : dict[str, str]
37
+ 公开名字 → 所在子模块名的映射,例如 ``{"make_timer": "monitor_func"}``。
38
+
39
+ Returns
40
+ -------
41
+ Callable[[str], Any]
42
+ 可直接赋给模块的 ``__getattr__``。
43
+ """
44
+ module_name = module_globals["__name__"]
45
+
46
+ def __getattr__(name: str) -> Any:
47
+ submodule_name = exports.get(name)
48
+ if submodule_name is None:
49
+ raise AttributeError(f"module {module_name!r} has no attribute {name!r}")
50
+ module = importlib.import_module(f".{submodule_name}", module_name)
51
+ value = getattr(module, name)
52
+ module_globals[name] = value # 缓存,后续访问走正常属性查找
53
+ return value
54
+
55
+ return __getattr__
@@ -0,0 +1,31 @@
1
+ """安全模块:加密与签名、TOTP 双因素认证、PIN 码管理。
2
+
3
+ 各管理器按需导入(PEP 562),因此用 ``CryptoManager`` 时不会连带把
4
+ ``keyring`` / ``qrcode`` / ``pyotp`` 拉进内存。
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TYPE_CHECKING
10
+
11
+ from raytoolsbox._lazy import make_module_getattr
12
+
13
+ if TYPE_CHECKING: # 仅供类型检查器与 IDE 补全,运行时不执行
14
+ from .authenticator import Authenticator
15
+ from .crypto_manager import CryptoManager
16
+ from .pin_manager import PinManager
17
+
18
+ __all__ = ["Authenticator", "CryptoManager", "PinManager"]
19
+
20
+ # 公开名字 → 所在子模块
21
+ _EXPORTS = {
22
+ "Authenticator": "authenticator",
23
+ "CryptoManager": "crypto_manager",
24
+ "PinManager": "pin_manager",
25
+ }
26
+
27
+ __getattr__ = make_module_getattr(globals(), _EXPORTS)
28
+
29
+
30
+ def __dir__() -> list[str]:
31
+ return sorted(__all__)