xenoslib 0.3.2__py3-none-any.whl → 0.4.1__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.
xenoslib/__init__.py CHANGED
@@ -10,3 +10,4 @@ elif sys.platform == "linux":
10
10
  elif sys.platform == "darwin":
11
11
  from .linux import * # noqa
12
12
  from .base import * # noqa
13
+ from .time_utils import * # noqa
xenoslib/about.py CHANGED
@@ -4,4 +4,4 @@ __url__ = "https://github.com/XenosLu/xenoslib.git"
4
4
  __author__ = "Xenocider"
5
5
  __author_email__ = "xenos.lu@gmail.com"
6
6
  __license__ = "MIT License"
7
- __version__ = "0.3.2"
7
+ __version__ = "0.4.1"
xenoslib/time_utils.py ADDED
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ # @Author : Xenos Lu
4
+ # @Created : 2023-01
5
+ # @Updated : 2025-08-14
6
+ import os
7
+ import logging
8
+ import inspect
9
+ from datetime import datetime
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+
14
+ # 添加多语言支持
15
+ def get_system_language():
16
+ """获取系统语言环境"""
17
+ lang = os.getenv("LANG", "").split(".")[0]
18
+ return lang if lang and "_" in lang else "en_US"
19
+
20
+
21
+ # 创建翻译字典
22
+ TRANSLATIONS = {
23
+ "zh_CN": {
24
+ "days": ("天", "天"),
25
+ "hours": ("小时", "小时"),
26
+ "minutes": ("分钟", "分钟"),
27
+ "seconds": ("秒", "秒"),
28
+ "updated": "最近更新",
29
+ "ago": "前",
30
+ "file_error": "获取文件信息失败",
31
+ },
32
+ "en_US": {
33
+ "days": ("day", "days"),
34
+ "hours": ("hour", "hours"),
35
+ "minutes": ("minute", "minutes"),
36
+ "seconds": ("second", "seconds"),
37
+ "updated": "last updated",
38
+ "ago": "ago",
39
+ "file_error": "Failed to get file info",
40
+ },
41
+ }
42
+
43
+
44
+ def timedelta_to_human_readable(delta, lang=None):
45
+ """将时间差转换为人类可读格式(支持中英双语)"""
46
+ if lang is None:
47
+ lang = get_system_language()
48
+
49
+ trans = TRANSLATIONS.get(lang, TRANSLATIONS["en_US"])
50
+ days = delta.days
51
+ hours, remainder = divmod(delta.seconds, 3600)
52
+ minutes, seconds = divmod(remainder, 60)
53
+
54
+ time_units = {
55
+ "days": (days, trans["days"]),
56
+ "hours": (hours, trans["hours"]),
57
+ "minutes": (minutes, trans["minutes"]),
58
+ "seconds": (seconds, trans["seconds"]),
59
+ }
60
+
61
+ for unit, (value, labels) in time_units.items():
62
+ if value > 0:
63
+ # 处理英文单复数
64
+ if lang.startswith("en_") and value > 1:
65
+ return f"{value} {labels[1]}"
66
+ return f"{value}{labels[0]}" if lang.startswith("zh_") else f"{value} {labels[0]}"
67
+
68
+ # 默认返回0秒
69
+ zero_label = "0" + trans["seconds"][0] if lang.startswith("zh_") else f"0 {trans['seconds'][1]}"
70
+ return zero_label
71
+
72
+
73
+ def log_file_update_time(lang=None):
74
+ """显示调用文件最近更新时间(支持中英双语)"""
75
+ if lang is None:
76
+ lang = get_system_language()
77
+ trans = TRANSLATIONS.get(lang, TRANSLATIONS["en_US"])
78
+
79
+ try:
80
+ stack = inspect.stack()
81
+ for frame_info in stack:
82
+ if frame_info.filename != __file__:
83
+ file_path = frame_info.filename
84
+ break
85
+ else:
86
+ file_path = __file__
87
+
88
+ mtime = datetime.fromtimestamp(os.path.getmtime(file_path))
89
+ delta_str = timedelta_to_human_readable(datetime.now() - mtime, lang)
90
+ filename = os.path.basename(file_path)
91
+
92
+ # 双语日志输出
93
+ log_msg = (
94
+ f"{filename} {trans['updated']}: "
95
+ f"{mtime:%Y-%m-%d %H:%M} ({delta_str} {trans['ago']})"
96
+ )
97
+ logger.info(log_msg)
98
+ except Exception as e:
99
+ logger.error(f"{trans['file_error']}: {e}")
@@ -244,7 +244,13 @@ class SectionProxy:
244
244
 
245
245
  def __repr__(self):
246
246
  """String representation of the section's configuration."""
247
- return yaml.dump(self._loader._raw_config[self._section])
247
+ return yaml.dump(self.to_dict())
248
+
249
+ def __contains__(self, key):
250
+ return key in self.self.to_dict()
251
+
252
+ def to_dict(self):
253
+ return self._loader._raw_config[self._section]
248
254
 
249
255
 
250
256
  if __name__ == "__main__":
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: xenoslib
3
- Version: 0.3.2
3
+ Version: 0.4.1
4
4
  Summary: Xenos' common lib
5
5
  Home-page: https://github.com/XenosLu/xenoslib.git
6
6
  Author: Xenocider
@@ -1,6 +1,6 @@
1
- xenoslib/__init__.py,sha256=vlMWwdD2gTb9ztpm_orYAm_Y0Be0Es64ifvMQwrE1jU,310
1
+ xenoslib/__init__.py,sha256=GbqgKQY6EksVq0kDvxEPbbhQ42L3__NI-fk-IdArIrE,344
2
2
  xenoslib/__main__.py,sha256=UpD3pl4l5ZUFxK6FzNMWRb1AZpEnwerulSFoPb_Pdgw,307
3
- xenoslib/about.py,sha256=WhSt9ExSk_XSML8eWGmrJD5XtZiiSDr5G-FJe9mLunI,228
3
+ xenoslib/about.py,sha256=P_xpQN8vwIUZvYZ8MII7R_SGiWpSNtIf7zz1P4G1lPM,228
4
4
  xenoslib/base.py,sha256=KVJ1n1jzdsDZ4PX5bGxmal9eKmiUy5MS0xqNhGGwoRk,13767
5
5
  xenoslib/dev.py,sha256=R6iwKuu-xvaYiBOUyP2gpePyvXss17TOZaCmQEihPCw,1236
6
6
  xenoslib/extend.py,sha256=Vl5d8carSGy6ERE2EjTmB9lOo2uIBWs1iz-fKm_TX-M,5947
@@ -8,12 +8,13 @@ xenoslib/linux.py,sha256=EFCQBq_JsmNJc3wGlJlzH9W0tjOA7sSwx0u62R5Ut2k,1318
8
8
  xenoslib/mail.py,sha256=pkC4gpMSxKbuvNR-0CdwbJQEJNbBCp5oEYAJS-uxq9s,7286
9
9
  xenoslib/mock.py,sha256=iNIplO7clejqLWfpY4r0WQH5BULy9dCoolDenZEci0c,3572
10
10
  xenoslib/onedrive.py,sha256=-bJJ8Cd_RjJSlDynYqKoZlFKE1HHM34l6NXOQrWOwtg,7783
11
+ xenoslib/time_utils.py,sha256=98YOD5u3T3JJkmkYkez7pLEmZHfO9qvlAbDKuOmFyjk,2969
11
12
  xenoslib/win_trayicon.py,sha256=7GJwX3c2CS1XWQjsyDK5EfM-MmEHdzPJCTX2sGpWmuU,13115
12
13
  xenoslib/windows.py,sha256=lUTD7TowaPqYHgIL6b-GM9PLd-VyJNNGzkzC3K9vOxo,4080
13
14
  xenoslib/tools/__init__.py,sha256=D1qP5tD2ZukXHgjKUh0fFl8jUipM3KaPBZBi0vGPypQ,48
14
- xenoslib/tools/config_loader.py,sha256=dHGqeRX-RvtBCdERsOYTYRIBD8nfYx04XRpps25ob_c,10947
15
- xenoslib-0.3.2.dist-info/licenses/LICENSE,sha256=lF6hjufhiR-xAje_Wo7ogxV5phz2d5TgkfL5SGshujg,1066
16
- xenoslib-0.3.2.dist-info/METADATA,sha256=Rdg2TRxxFq_LjuV2-41RVL-kRNXVCVnFliSUNwqMN7k,910
17
- xenoslib-0.3.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
18
- xenoslib-0.3.2.dist-info/top_level.txt,sha256=Rfm4GdW0NyA2AHDNUF2wFQOfAo53mAPibddSHGd3X60,9
19
- xenoslib-0.3.2.dist-info/RECORD,,
15
+ xenoslib/tools/config_loader.py,sha256=GWEcgdgYQyaq9rt1txYO5qaFOvqxB4i3kv5AIazquYk,11077
16
+ xenoslib-0.4.1.dist-info/licenses/LICENSE,sha256=lF6hjufhiR-xAje_Wo7ogxV5phz2d5TgkfL5SGshujg,1066
17
+ xenoslib-0.4.1.dist-info/METADATA,sha256=LlKcXFAKASonMaUPn5Df6Kqs29Kf38mlbM2u9t8BTDk,910
18
+ xenoslib-0.4.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
+ xenoslib-0.4.1.dist-info/top_level.txt,sha256=Rfm4GdW0NyA2AHDNUF2wFQOfAo53mAPibddSHGd3X60,9
20
+ xenoslib-0.4.1.dist-info/RECORD,,