scpkg 0.3.0__tar.gz → 0.3.1__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.
- {scpkg-0.3.0/src/scpkg.egg-info → scpkg-0.3.1}/PKG-INFO +1 -1
- {scpkg-0.3.0 → scpkg-0.3.1}/pyproject.toml +1 -1
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/__init__.py +6 -6
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/enable_exception_logging.py +1 -1
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/init_logger.py +11 -11
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/init_logging_config.py +5 -5
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/mqtt_publisher.py +2 -2
- {scpkg-0.3.0 → scpkg-0.3.1/src/scpkg.egg-info}/PKG-INFO +1 -1
- {scpkg-0.3.0 → scpkg-0.3.1}/LICENSE +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/README.md +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/setup.cfg +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/sc92_lib/__init__.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scbox/__init__.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/_alias.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/check_running_lock.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/dingtalk_robot.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/is_worktime.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/paths.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/py.typed +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/to_yaml.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/try_run.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg/yaml_config.py +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg.egg-info/SOURCES.txt +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg.egg-info/dependency_links.txt +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg.egg-info/requires.txt +0 -0
- {scpkg-0.3.0 → scpkg-0.3.1}/src/scpkg.egg-info/top_level.txt +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "scpkg"
|
|
7
|
-
version = "0.3.
|
|
7
|
+
version = "0.3.1"
|
|
8
8
|
description = "个人自用工具箱:单实例文件锁、轮转日志、全局异常钩子、MQTT 发布、YAML 配置读写、值转 YAML 文本、工作时段判断、局部异常兜底、钉钉机器人通知(导入名 scpkg,兼容别名 sc92_lib、scbox,三者指向同一份实现)"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.6"
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
单实例文件锁、轮转日志、全局异常钩子(主线程 + 子线程)、一次性 MQTT 发布、
|
|
4
4
|
YAML 配置读写、值转 YAML 文本、工作时段判断、局部异常兜底、钉钉机器人通知。
|
|
5
5
|
|
|
6
|
-
公开 API 一律从包根 `scpkg` 导入;`sc92_lib`
|
|
7
|
-
|
|
6
|
+
公开 API 一律从包根 `scpkg` 导入;`sc92_lib`、`scbox` 作为旧名保留(同一份
|
|
7
|
+
对象,分别由两个别名包转出)。完整说明见 README。
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
10
|
import logging
|
|
@@ -13,9 +13,9 @@ import logging
|
|
|
13
13
|
# atexit,而本包在 check_running_lock 里注册"退出时释放锁"更晚;atexit 是 LIFO
|
|
14
14
|
# (先注册后执行),所以退出顺序才是"先放锁、后关日志"(3.6 上靠它规避 Logging error)。
|
|
15
15
|
|
|
16
|
-
# 业务与库内共用的命名 logger:业务侧 from
|
|
16
|
+
# 业务与库内共用的命名 logger:业务侧 from scpkg import logger 后直接用
|
|
17
17
|
# logger.info / logger.debug;handler 仍由 init_logging_config() 挂在 root 上。
|
|
18
|
-
logger = logging.getLogger("
|
|
18
|
+
logger = logging.getLogger("scpkg")
|
|
19
19
|
logger.setLevel(logging.DEBUG)
|
|
20
20
|
|
|
21
21
|
# 导入顺序与脚本头部的调用顺序保持一致:① 日志 → ② 单实例锁 → ③ 异常钩子
|
|
@@ -41,14 +41,14 @@ from .yaml_config import read_yaml, write_yaml
|
|
|
41
41
|
from .to_yaml import to_yaml
|
|
42
42
|
from .is_worktime import is_worktime
|
|
43
43
|
|
|
44
|
-
__version__ = "0.3.
|
|
44
|
+
__version__ = "0.3.1"
|
|
45
45
|
# 每次更新功能后,手动把这里改成当时的日期时间(YYYY-MM-DD HH:MM:SS)
|
|
46
46
|
__build_time__ = "2026-11-16 02:49:35"
|
|
47
47
|
|
|
48
48
|
__all__ = [
|
|
49
49
|
"__version__", # 当前包版本号
|
|
50
50
|
"__build_time__", # 本版构建时间:YYYY-MM-DD HH:MM:SS(更新功能后手动改)
|
|
51
|
-
"logger", # 命名 logger(
|
|
51
|
+
"logger", # 命名 logger(scpkg),业务直接用 logger.info/debug
|
|
52
52
|
"init_logger", # 新业务日志初始化:命名 logger + 只收自己的日志(幂等)
|
|
53
53
|
"script_dir", # 入口脚本所在目录(兼容 exe 与 python -m)
|
|
54
54
|
"script_name", # 入口脚本名(不含扩展名,兼容 exe 与 python -m)
|
|
@@ -47,7 +47,7 @@ def enable_exception_logging() -> None:
|
|
|
47
47
|
_install_thread_exception_hook()
|
|
48
48
|
|
|
49
49
|
# 钩子挂上就留痕:看到这行说明主线程和子线程的未捕获异常都会写进日志
|
|
50
|
-
logger.info("
|
|
50
|
+
logger.info("scpkg 全局异常捕获已开启(主线程 + 子线程)")
|
|
51
51
|
|
|
52
52
|
|
|
53
53
|
def _install_thread_exception_hook() -> None:
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"""新业务日志初始化:配置命名 logger `
|
|
1
|
+
"""新业务日志初始化:配置命名 logger `scpkg`,只收自己的日志。
|
|
2
2
|
|
|
3
3
|
用法::
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from scpkg import init_logger, logger
|
|
6
6
|
|
|
7
7
|
init_logger() # 只需要在入口脚本调用一次;重复调用是幂等的
|
|
8
8
|
logger.info("业务日志")
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
与旧的 `init_logging_config()` 的区别:
|
|
12
12
|
|
|
13
|
-
- `init_logger()` 把 handler 挂在 `
|
|
13
|
+
- `init_logger()` 把 handler 挂在 `scpkg` 上,并设置 `propagate=False`;
|
|
14
14
|
- root 不动,第三方 logger(filelock / paho / urllib3 等)不会写进本文件;
|
|
15
15
|
- 业务必须使用 `logger.*`;漏改的 `logging.*` 不会落盘(WARNING 及以上只到
|
|
16
16
|
stderr 的 lastResort)。
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
幂等约定:
|
|
19
19
|
|
|
20
20
|
- 第一次调用生效;
|
|
21
|
-
- 后续调用(未传 force)不会重复添加 handler,只会重新确保 `
|
|
21
|
+
- 后续调用(未传 force)不会重复添加 handler,只会重新确保 `scpkg` 的级别为
|
|
22
22
|
DEBUG、`propagate=False`;
|
|
23
|
-
- `force=True` 会移除并关闭 `
|
|
23
|
+
- `force=True` 会移除并关闭 `scpkg` 上现有的 handler,然后重建;
|
|
24
24
|
- 多个模块都调用 `init_logger()` 时,以第一次调用的参数为准。
|
|
25
25
|
"""
|
|
26
26
|
|
|
@@ -36,7 +36,7 @@ from .paths import default_log_file
|
|
|
36
36
|
_FORMAT = "%(asctime)s [%(levelname)-8s] %(filename)12s:%(lineno)-4d %(funcName)-12s: %(message)s"
|
|
37
37
|
|
|
38
38
|
# 标记本函数创建的 handler,重复调用时只认这些,不会把用户自己加的 handler 误判为已初始化。
|
|
39
|
-
_MANAGED_ATTR = "
|
|
39
|
+
_MANAGED_ATTR = "_scpkg_managed"
|
|
40
40
|
|
|
41
41
|
_lock = threading.RLock()
|
|
42
42
|
|
|
@@ -90,25 +90,25 @@ def init_logger(
|
|
|
90
90
|
backup_count: int = 7,
|
|
91
91
|
force: bool = False,
|
|
92
92
|
) -> None:
|
|
93
|
-
"""配置命名 logger `
|
|
93
|
+
"""配置命名 logger `scpkg`:两个轮转文件 + 控制台,只收自己人写的日志。
|
|
94
94
|
|
|
95
95
|
参数:
|
|
96
96
|
log_file: 日志路径前缀,省略时为 `<脚本目录>/logs/<脚本名>`。
|
|
97
97
|
max_bytes: 单个日志文件的大小上限,默认 10 MB。
|
|
98
98
|
backup_count: 轮转保留的备份份数,默认 7。
|
|
99
|
-
force: 是否移除并关闭 `
|
|
99
|
+
force: 是否移除并关闭 `scpkg` 上已有的 handler 后重建,默认 False。
|
|
100
100
|
|
|
101
101
|
退出:
|
|
102
102
|
不退出;目录创建或日志文件打开失败时,异常照常抛出。
|
|
103
103
|
|
|
104
104
|
注意:
|
|
105
|
-
- handler 只挂在 `
|
|
105
|
+
- handler 只挂在 `scpkg` 上,`logger.propagate=False`,root 保持原样;
|
|
106
106
|
- 业务必须用 `logger.info/debug/error` 写日志;`logging.*` 不会进本文件;
|
|
107
107
|
- 第三方库的 WARNING 及以上只会走 stderr 的 `lastResort`,不会落盘;
|
|
108
108
|
- 幂等:重复调用不会重复添加 handler;多模块调用时第一次的参数生效;
|
|
109
109
|
- 与 `init_logging_config()` 不要在同一进程里混用(旧业务已结束,新业务只用本函数)。
|
|
110
110
|
"""
|
|
111
|
-
target = logging.getLogger("
|
|
111
|
+
target = logging.getLogger("scpkg")
|
|
112
112
|
|
|
113
113
|
with _lock:
|
|
114
114
|
managed = [h for h in target.handlers if getattr(h, _MANAGED_ATTR, False)]
|
|
@@ -143,5 +143,5 @@ def init_logger(
|
|
|
143
143
|
# __version__ 在包 __init__.py 里,延迟导入避免循环导入。
|
|
144
144
|
from . import __version__
|
|
145
145
|
|
|
146
|
-
target.info(f"新程序开始启动,
|
|
146
|
+
target.info(f"新程序开始启动,scpkg 已加载,包版本: {__version__}")
|
|
147
147
|
target.info(f"新程序开始启动,日志初始化完成:{debug_path} / {info_path}")
|
|
@@ -46,19 +46,19 @@ def init_logging_config(
|
|
|
46
46
|
- 根日志器已有 handler(pytest、Jupyter、别的库配置过,或业务自己先调了
|
|
47
47
|
`logging.*` 触发过 `logging.basicConfig()`)时默认什么都不做,只往 stderr
|
|
48
48
|
提示一句;确实要接管就传 `force=True`,它会先移除已有 handler。
|
|
49
|
-
- 无论是否接管 root,都会把命名 logger `
|
|
50
|
-
`from
|
|
49
|
+
- 无论是否接管 root,都会把命名 logger `scpkg` 的级别设为 DEBUG,保证
|
|
50
|
+
`from scpkg import logger` 的 `logger.debug()` 可用。
|
|
51
51
|
- `sys.stderr` 为 None(`pythonw.exe`、PyInstaller `--noconsole`)时跳过控制台
|
|
52
52
|
handler,只留文件日志。
|
|
53
53
|
- 配置成功后先记包版本、再记两个日志文件路径,两条都带"新程序开始启动"前缀,
|
|
54
54
|
用于在定时任务反复拉起时区分"新进程"和"常驻实例"(详见 README「日志」)。
|
|
55
55
|
"""
|
|
56
|
-
logging.getLogger("
|
|
56
|
+
logging.getLogger("scpkg").setLevel(logging.DEBUG)
|
|
57
57
|
|
|
58
58
|
if logging.root.handlers and not force:
|
|
59
59
|
if sys.stderr is not None:
|
|
60
60
|
print(
|
|
61
|
-
"[
|
|
61
|
+
"[scpkg] 根日志器已有 handler,init_logging_config() 没有生效,"
|
|
62
62
|
"日志不会写到文件;确认要接管请传 force=True。",
|
|
63
63
|
file=sys.stderr,
|
|
64
64
|
)
|
|
@@ -96,5 +96,5 @@ def init_logging_config(
|
|
|
96
96
|
# 这里延迟导入,避免 __init__ 与本模块循环导入。
|
|
97
97
|
from . import __version__
|
|
98
98
|
|
|
99
|
-
logger.info(f"新程序开始启动,
|
|
99
|
+
logger.info(f"新程序开始启动,scpkg 已加载,包版本: {__version__}")
|
|
100
100
|
logger.info(f"新程序开始启动,日志初始化完成:{debug_path} / {info_path}")
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
失败约定:构造只保存参数,不校验也不抛异常;`publish()` 负责校验和执行,任何可预期
|
|
7
7
|
失败(参数错误、paho-mqtt 不可用、连接 / 发布 / 超时)都 `logger.error` + 返回
|
|
8
8
|
False,不抛 Exception(SystemExit / KeyboardInterrupt 属于主动退出,照常向外抛)。
|
|
9
|
-
paho-mqtt 导入失败也不阻塞 `import
|
|
9
|
+
paho-mqtt 导入失败也不阻塞 `import scpkg`。
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
12
|
import logging
|
|
@@ -16,7 +16,7 @@ from typing import Optional
|
|
|
16
16
|
try:
|
|
17
17
|
import paho.mqtt.client as mqtt
|
|
18
18
|
_MQTT_IMPORT_ERROR = None
|
|
19
|
-
except Exception as _exc: # 缺包 / 包损坏都不阻塞 import
|
|
19
|
+
except Exception as _exc: # 缺包 / 包损坏都不阻塞 import scpkg,留到 publish() 报错
|
|
20
20
|
mqtt = None
|
|
21
21
|
_MQTT_IMPORT_ERROR = _exc
|
|
22
22
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|