lantool 0.1.0__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.
- lantool/__init__.py +261 -0
- lantool/__main__.py +70 -0
- lantool/_emoji_data.py +628 -0
- lantool/_fonts.py +74 -0
- lantool/_http_data.py +157 -0
- lantool/_optional.py +42 -0
- lantool/barcodex.py +95 -0
- lantool/bytesize.py +138 -0
- lantool/chardata.py +150 -0
- lantool/collx.py +98 -0
- lantool/convert.py +174 -0
- lantool/cronx.py +230 -0
- lantool/decorators.py +227 -0
- lantool/diffx.py +81 -0
- lantool/encoding.py +430 -0
- lantool/env.py +133 -0
- lantool/errors.py +57 -0
- lantool/files.py +165 -0
- lantool/formatx.py +144 -0
- lantool/hashing.py +181 -0
- lantool/ids.py +171 -0
- lantool/imagex.py +326 -0
- lantool/jsonx.py +165 -0
- lantool/jwtx.py +83 -0
- lantool/log.py +407 -0
- lantool/misc.py +185 -0
- lantool/password.py +127 -0
- lantool/pdfx.py +367 -0
- lantool/py.typed +0 -0
- lantool/qrcodex.py +111 -0
- lantool/retryx.py +131 -0
- lantool/security.py +441 -0
- lantool/text.py +314 -0
- lantool/times.py +240 -0
- lantool/validator.py +132 -0
- lantool/webdata.py +159 -0
- lantool/xmlix.py +141 -0
- lantool/yamlix.py +103 -0
- lantool-0.1.0.dist-info/METADATA +252 -0
- lantool-0.1.0.dist-info/RECORD +42 -0
- lantool-0.1.0.dist-info/WHEEL +4 -0
- lantool-0.1.0.dist-info/licenses/LICENSE +21 -0
lantool/__init__.py
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"""lantool —— 轻量实用的 Python 通用工具库。
|
|
2
|
+
|
|
3
|
+
零依赖核心 + 按需扩展(可选依赖缺失时给出友好安装提示);
|
|
4
|
+
所有公共 API 均可从包顶层直接导入,例如:
|
|
5
|
+
|
|
6
|
+
from lantool import base64_encode, sha256, now_str
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from importlib.metadata import PackageNotFoundError
|
|
10
|
+
from importlib.metadata import version as _pkg_version
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
__version__ = _pkg_version("lantool")
|
|
14
|
+
except PackageNotFoundError: # 源码拷贝未安装场景
|
|
15
|
+
__version__ = "0.0.0.dev0"
|
|
16
|
+
|
|
17
|
+
# 导出名 → 模块路径映射(PEP 562 懒加载:按需 import,保证 import lantool 零开销)
|
|
18
|
+
_EXPORTS: dict[str, str] = {
|
|
19
|
+
# errors —— 统一异常体系
|
|
20
|
+
"LanToolError": "lantool.errors",
|
|
21
|
+
"EncodeError": "lantool.errors",
|
|
22
|
+
"ParseError": "lantool.errors",
|
|
23
|
+
"ValidationError": "lantool.errors",
|
|
24
|
+
"CryptoError": "lantool.errors",
|
|
25
|
+
"OptionalDependencyError": "lantool.errors",
|
|
26
|
+
# encoding —— 编解码
|
|
27
|
+
"base64_encode": "lantool.encoding",
|
|
28
|
+
"base64_decode": "lantool.encoding",
|
|
29
|
+
"base64_encode_bytes": "lantool.encoding",
|
|
30
|
+
"base64_decode_bytes": "lantool.encoding",
|
|
31
|
+
"url_encode": "lantool.encoding",
|
|
32
|
+
"url_decode": "lantool.encoding",
|
|
33
|
+
"hex_encode": "lantool.encoding",
|
|
34
|
+
"hex_decode": "lantool.encoding",
|
|
35
|
+
"unicode_encode": "lantool.encoding",
|
|
36
|
+
"unicode_decode": "lantool.encoding",
|
|
37
|
+
"morse_encode": "lantool.encoding",
|
|
38
|
+
"morse_decode": "lantool.encoding",
|
|
39
|
+
"convert_base": "lantool.encoding",
|
|
40
|
+
"image_to_base64": "lantool.encoding",
|
|
41
|
+
"image_from_base64": "lantool.encoding",
|
|
42
|
+
# hashing —— 哈希
|
|
43
|
+
"SUPPORTED_ALGORITHMS": "lantool.hashing",
|
|
44
|
+
"md5": "lantool.hashing",
|
|
45
|
+
"sha1": "lantool.hashing",
|
|
46
|
+
"sha256": "lantool.hashing",
|
|
47
|
+
"sha512": "lantool.hashing",
|
|
48
|
+
"hash_text": "lantool.hashing",
|
|
49
|
+
"hash_file": "lantool.hashing",
|
|
50
|
+
"hmac_digest": "lantool.hashing",
|
|
51
|
+
"crc32": "lantool.hashing",
|
|
52
|
+
# bytesize —— 字节单位
|
|
53
|
+
"convert_size": "lantool.bytesize",
|
|
54
|
+
"human_size": "lantool.bytesize",
|
|
55
|
+
"parse_size": "lantool.bytesize",
|
|
56
|
+
# ids —— 唯一标识
|
|
57
|
+
"uuid1": "lantool.ids",
|
|
58
|
+
"uuid3": "lantool.ids",
|
|
59
|
+
"uuid4": "lantool.ids",
|
|
60
|
+
"uuid5": "lantool.ids",
|
|
61
|
+
"uuid7": "lantool.ids",
|
|
62
|
+
"short_id": "lantool.ids",
|
|
63
|
+
"nano_id": "lantool.ids",
|
|
64
|
+
# password —— 密码与随机串
|
|
65
|
+
"random_password": "lantool.password",
|
|
66
|
+
"random_string": "lantool.password",
|
|
67
|
+
"password_strength": "lantool.password",
|
|
68
|
+
# convert —— 类型安全转换
|
|
69
|
+
"to_int": "lantool.convert",
|
|
70
|
+
"to_float": "lantool.convert",
|
|
71
|
+
"to_bool": "lantool.convert",
|
|
72
|
+
"to_str": "lantool.convert",
|
|
73
|
+
"to_datetime": "lantool.convert",
|
|
74
|
+
# times —— 时间
|
|
75
|
+
"DEFAULT_DATETIME_FMT": "lantool.times",
|
|
76
|
+
"timestamp": "lantool.times",
|
|
77
|
+
"now": "lantool.times",
|
|
78
|
+
"now_utc": "lantool.times",
|
|
79
|
+
"now_str": "lantool.times",
|
|
80
|
+
"now_formats": "lantool.times",
|
|
81
|
+
"from_timestamp": "lantool.times",
|
|
82
|
+
"to_timestamp": "lantool.times",
|
|
83
|
+
"timestamp_to_str": "lantool.times",
|
|
84
|
+
"str_to_timestamp": "lantool.times",
|
|
85
|
+
"human_duration": "lantool.times",
|
|
86
|
+
# text —— 文本
|
|
87
|
+
"to_snake": "lantool.text",
|
|
88
|
+
"to_camel": "lantool.text",
|
|
89
|
+
"to_pascal": "lantool.text",
|
|
90
|
+
"to_kebab": "lantool.text",
|
|
91
|
+
"to_upper": "lantool.text",
|
|
92
|
+
"to_lower": "lantool.text",
|
|
93
|
+
"invert_case": "lantool.text",
|
|
94
|
+
"alternate_case": "lantool.text",
|
|
95
|
+
"capitalize_words": "lantool.text",
|
|
96
|
+
"count_chars": "lantool.text",
|
|
97
|
+
"natural_sort": "lantool.text",
|
|
98
|
+
"mask": "lantool.text",
|
|
99
|
+
"truncate": "lantool.text",
|
|
100
|
+
"java_fields": "lantool.text",
|
|
101
|
+
# jsonx —— JSON
|
|
102
|
+
"dumps": "lantool.jsonx",
|
|
103
|
+
"loads": "lantool.jsonx",
|
|
104
|
+
"pretty": "lantool.jsonx",
|
|
105
|
+
"compact": "lantool.jsonx",
|
|
106
|
+
"validate": "lantool.jsonx",
|
|
107
|
+
# xmlix —— XML
|
|
108
|
+
"format_xml": "lantool.xmlix",
|
|
109
|
+
"compact_xml": "lantool.xmlix",
|
|
110
|
+
"validate_xml": "lantool.xmlix",
|
|
111
|
+
"escape_xml": "lantool.xmlix",
|
|
112
|
+
"unescape_xml": "lantool.xmlix",
|
|
113
|
+
# diffx —— 文本差异
|
|
114
|
+
"unified_diff": "lantool.diffx",
|
|
115
|
+
"ndiff": "lantool.diffx",
|
|
116
|
+
"diff_ratio": "lantool.diffx",
|
|
117
|
+
"html_diff": "lantool.diffx",
|
|
118
|
+
# formatx —— 数字格式化
|
|
119
|
+
"format_number": "lantool.formatx",
|
|
120
|
+
"format_currency": "lantool.formatx",
|
|
121
|
+
"format_percent": "lantool.formatx",
|
|
122
|
+
# validator —— 校验
|
|
123
|
+
"is_email": "lantool.validator",
|
|
124
|
+
"is_phone_cn": "lantool.validator",
|
|
125
|
+
"is_ipv4": "lantool.validator",
|
|
126
|
+
"is_ipv6": "lantool.validator",
|
|
127
|
+
"is_ip": "lantool.validator",
|
|
128
|
+
"is_url": "lantool.validator",
|
|
129
|
+
"is_id_card_cn": "lantool.validator",
|
|
130
|
+
# collx —— 集合工具
|
|
131
|
+
"deep_merge": "lantool.collx",
|
|
132
|
+
"chunk": "lantool.collx",
|
|
133
|
+
"flatten": "lantool.collx",
|
|
134
|
+
"unique": "lantool.collx",
|
|
135
|
+
"unique_by": "lantool.collx",
|
|
136
|
+
# retry —— 重试
|
|
137
|
+
"retry": "lantool.retryx",
|
|
138
|
+
# cronx —— Cron
|
|
139
|
+
"cron_parse": "lantool.cronx",
|
|
140
|
+
"cron_next_run": "lantool.cronx",
|
|
141
|
+
"cron_describe": "lantool.cronx",
|
|
142
|
+
# misc —— 杂项工具
|
|
143
|
+
"bmi": "lantool.misc",
|
|
144
|
+
"bmi_info": "lantool.misc",
|
|
145
|
+
"curl": "lantool.misc",
|
|
146
|
+
"regex_test": "lantool.misc",
|
|
147
|
+
# webdata —— Web 数据
|
|
148
|
+
"HTTP_STATUS_CODES": "lantool.webdata",
|
|
149
|
+
"CONTENT_TYPES": "lantool.webdata",
|
|
150
|
+
"USER_AGENTS": "lantool.webdata",
|
|
151
|
+
"http_status": "lantool.webdata",
|
|
152
|
+
"http_status_description": "lantool.webdata",
|
|
153
|
+
"search_status": "lantool.webdata",
|
|
154
|
+
"content_type": "lantool.webdata",
|
|
155
|
+
"random_user_agent": "lantool.webdata",
|
|
156
|
+
"parse_user_agent": "lantool.webdata",
|
|
157
|
+
# chardata —— 字符数据
|
|
158
|
+
"HTML_ENTITIES": "lantool.chardata",
|
|
159
|
+
"EMOJI_CATEGORIES": "lantool.chardata",
|
|
160
|
+
"ascii_table": "lantool.chardata",
|
|
161
|
+
"ascii_info": "lantool.chardata",
|
|
162
|
+
"html_escape": "lantool.chardata",
|
|
163
|
+
"html_unescape": "lantool.chardata",
|
|
164
|
+
"emoji_list": "lantool.chardata",
|
|
165
|
+
"random_emoji": "lantool.chardata",
|
|
166
|
+
# decorators —— 通用装饰器
|
|
167
|
+
"timing": "lantool.decorators",
|
|
168
|
+
"suppress_errors": "lantool.decorators",
|
|
169
|
+
"rate_limit": "lantool.decorators",
|
|
170
|
+
"module_block": "lantool.decorators",
|
|
171
|
+
# files —— 文件工具
|
|
172
|
+
"safe_read_text": "lantool.files",
|
|
173
|
+
"safe_write_text": "lantool.files",
|
|
174
|
+
"append_text": "lantool.files",
|
|
175
|
+
"ensure_dir": "lantool.files",
|
|
176
|
+
"mime_type": "lantool.files",
|
|
177
|
+
"file_info": "lantool.files",
|
|
178
|
+
"temp_dir": "lantool.files",
|
|
179
|
+
# env —— 环境变量
|
|
180
|
+
"get_env": "lantool.env",
|
|
181
|
+
"get_env_int": "lantool.env",
|
|
182
|
+
"get_env_float": "lantool.env",
|
|
183
|
+
"get_env_bool": "lantool.env",
|
|
184
|
+
"get_env_json": "lantool.env",
|
|
185
|
+
"get_env_list": "lantool.env",
|
|
186
|
+
# log —— 日志工厂
|
|
187
|
+
"DEFAULT_LOG_FMT": "lantool.log",
|
|
188
|
+
"LevelFilter": "lantool.log",
|
|
189
|
+
"DailySizeRotatingHandler": "lantool.log",
|
|
190
|
+
"get_logger": "lantool.log",
|
|
191
|
+
# security —— 加解密(可选 [security])
|
|
192
|
+
"aes_encrypt": "lantool.security",
|
|
193
|
+
"aes_decrypt": "lantool.security",
|
|
194
|
+
"des_encrypt": "lantool.security",
|
|
195
|
+
"des_decrypt": "lantool.security",
|
|
196
|
+
"des3_encrypt": "lantool.security",
|
|
197
|
+
"des3_decrypt": "lantool.security",
|
|
198
|
+
"rsa_generate_keys": "lantool.security",
|
|
199
|
+
"rsa_encrypt": "lantool.security",
|
|
200
|
+
"rsa_decrypt": "lantool.security",
|
|
201
|
+
"rsa_sign": "lantool.security",
|
|
202
|
+
"rsa_verify": "lantool.security",
|
|
203
|
+
"bcrypt_hash": "lantool.security",
|
|
204
|
+
"bcrypt_verify": "lantool.security",
|
|
205
|
+
# jwtx —— JWT(可选 [jwt])
|
|
206
|
+
"jwt_encode": "lantool.jwtx",
|
|
207
|
+
"jwt_decode": "lantool.jwtx",
|
|
208
|
+
# yamlix —— YAML(可选 [yaml])
|
|
209
|
+
"json_to_yaml": "lantool.yamlix",
|
|
210
|
+
"yaml_to_json": "lantool.yamlix",
|
|
211
|
+
"yaml_load": "lantool.yamlix",
|
|
212
|
+
# imagex —— 图片(可选 [image])
|
|
213
|
+
"compress_image": "lantool.imagex",
|
|
214
|
+
"watermark_text": "lantool.imagex",
|
|
215
|
+
"watermark_image": "lantool.imagex",
|
|
216
|
+
"make_favicon": "lantool.imagex",
|
|
217
|
+
# qrcodex —— 二维码(可选 [qr])
|
|
218
|
+
"qrcode_png": "lantool.qrcodex",
|
|
219
|
+
"qrcode_svg": "lantool.qrcodex",
|
|
220
|
+
# barcodex —— 条形码(可选 [barcode])
|
|
221
|
+
"barcode_png": "lantool.barcodex",
|
|
222
|
+
"barcode_svg": "lantool.barcodex",
|
|
223
|
+
# pdfx —— PDF(可选 [pdf])
|
|
224
|
+
"pdf_info": "lantool.pdfx",
|
|
225
|
+
"pdf_split": "lantool.pdfx",
|
|
226
|
+
"pdf_extract_pages": "lantool.pdfx",
|
|
227
|
+
"pdf_merge": "lantool.pdfx",
|
|
228
|
+
"pdf_watermark": "lantool.pdfx",
|
|
229
|
+
"images_to_pdf": "lantool.pdfx",
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
__all__ = list(_EXPORTS)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def __getattr__(name: str) -> object:
|
|
236
|
+
"""按需加载导出对象(PEP 562 模块级 __getattr__)。
|
|
237
|
+
|
|
238
|
+
首次访问后写入模块全局命名空间缓存,后续访问零查找开销。
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
name: 导出对象名称。
|
|
242
|
+
|
|
243
|
+
Returns:
|
|
244
|
+
目标模块中的对应对象。
|
|
245
|
+
|
|
246
|
+
Raises:
|
|
247
|
+
AttributeError: 名称不在导出映射中。
|
|
248
|
+
"""
|
|
249
|
+
module_path = _EXPORTS.get(name)
|
|
250
|
+
if module_path is None:
|
|
251
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
252
|
+
from importlib import import_module
|
|
253
|
+
|
|
254
|
+
value = getattr(import_module(module_path), name)
|
|
255
|
+
globals()[name] = value # 缓存:后续访问直接命中,不再经过本函数
|
|
256
|
+
return value
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def __dir__() -> list[str]:
|
|
260
|
+
"""返回模块属性与全部可导出名称(支持 dir(lantool) 与 IDE 补全)。"""
|
|
261
|
+
return sorted(set(globals()) | set(__all__))
|
lantool/__main__.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""lantool 自检命令:python -m lantool —— 输出版本、Python 环境与各可选依赖可用性。"""
|
|
2
|
+
|
|
3
|
+
import codecs
|
|
4
|
+
import sys
|
|
5
|
+
from contextlib import suppress
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _ensure_utf8_stdio() -> None:
|
|
9
|
+
"""管道/重定向场景下将标准输出/错误流统一为 UTF-8。
|
|
10
|
+
|
|
11
|
+
Windows 的 Python 在管道下默认跟随系统区域(中文系统为 GBK),中文输出到
|
|
12
|
+
UTF-8 终端或文件会乱码。交互终端(isatty)的编码与终端显示匹配,保持原样。
|
|
13
|
+
"""
|
|
14
|
+
for stream in (sys.stdout, sys.stderr):
|
|
15
|
+
if stream is None:
|
|
16
|
+
continue
|
|
17
|
+
isatty = getattr(stream, "isatty", None)
|
|
18
|
+
if isatty is not None and isatty():
|
|
19
|
+
continue
|
|
20
|
+
encoding = getattr(stream, "encoding", None)
|
|
21
|
+
if not encoding:
|
|
22
|
+
continue
|
|
23
|
+
try:
|
|
24
|
+
if codecs.lookup(encoding).name == "utf-8":
|
|
25
|
+
continue
|
|
26
|
+
except LookupError:
|
|
27
|
+
pass # 未知编码名:交由 reconfigure 尝试,失败再保持原样
|
|
28
|
+
# 嵌入式解释器等非标准流无法重配时保持原样
|
|
29
|
+
with suppress(AttributeError, OSError, ValueError):
|
|
30
|
+
stream.reconfigure(encoding="utf-8")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# CLI 执行路径先于包内导入统一输出编码,导入失败的回溯信息同样保持可读
|
|
34
|
+
if __name__ == "__main__":
|
|
35
|
+
_ensure_utf8_stdio()
|
|
36
|
+
|
|
37
|
+
from importlib.util import find_spec # noqa: E402
|
|
38
|
+
|
|
39
|
+
from lantool._optional import EXTRA_MODULES # noqa: E402
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _check_extra(extra: str) -> bool:
|
|
43
|
+
"""检测 extras 的全部依赖模块是否已安装。"""
|
|
44
|
+
return all(find_spec(module) is not None for module in EXTRA_MODULES[extra])
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def main() -> int:
|
|
48
|
+
"""打印环境自检报告(作为库函数调用时不对进程标准流做任何改动)。"""
|
|
49
|
+
import lantool
|
|
50
|
+
|
|
51
|
+
print("=" * 52)
|
|
52
|
+
print("lantool 自检报告")
|
|
53
|
+
print("=" * 52)
|
|
54
|
+
print(f"lantool 版本 : {lantool.__version__}")
|
|
55
|
+
print(f"Python 版本 : {sys.version.split()[0]} ({sys.platform})")
|
|
56
|
+
print(f"安装位置 : {lantool.__file__}")
|
|
57
|
+
print("-" * 52)
|
|
58
|
+
print("可选功能(extras)可用性:")
|
|
59
|
+
for extra in sorted(EXTRA_MODULES):
|
|
60
|
+
if _check_extra(extra):
|
|
61
|
+
print(f" [{extra:<8}] [OK] 可用")
|
|
62
|
+
else:
|
|
63
|
+
print(f' [{extra:<8}] [--] 未安装(pip install "lantool[{extra}]")')
|
|
64
|
+
print("-" * 52)
|
|
65
|
+
print("核心功能(纯标准库)始终可用 [OK]")
|
|
66
|
+
return 0
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
if __name__ == "__main__":
|
|
70
|
+
sys.exit(main())
|