staran 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.
@@ -0,0 +1,189 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ 跨平台C扩展加载器
6
+ 支持 macOS、Linux、Windows 平台的自动检测和加载
7
+ """
8
+
9
+ import os
10
+ import sys
11
+ import platform
12
+ import importlib.util
13
+ from datetime import datetime
14
+
15
+ class PlatformDateUtils:
16
+ """跨平台日期工具类,优先使用C扩展,回退到Python实现"""
17
+
18
+ def __init__(self):
19
+ """初始化平台工具"""
20
+ self.tools_dir = os.path.dirname(os.path.abspath(__file__))
21
+ self.c_utils = self._load_c_extension()
22
+ self.is_available = self.c_utils is not None
23
+ self.has_c_extension = self.is_available
24
+
25
+ def _get_extension_filename(self):
26
+ """根据平台获取C扩展文件名"""
27
+ system = platform.system().lower()
28
+
29
+ # Python版本信息
30
+ python_version = f"cpython-{sys.version_info.major}{sys.version_info.minor}"
31
+
32
+ if system == 'darwin': # macOS
33
+ base_names = [
34
+ f'date_utils_macos.{python_version}-darwin.so',
35
+ 'date_utils_macos.so'
36
+ ]
37
+ elif system == 'linux':
38
+ base_names = [
39
+ f'date_utils_linux.{python_version}-linux.so',
40
+ 'date_utils_linux.so'
41
+ ]
42
+ elif system == 'windows':
43
+ base_names = [
44
+ f'date_utils_windows.{python_version}.pyd',
45
+ 'date_utils_windows.pyd'
46
+ ]
47
+ else:
48
+ base_names = ['date_utils.so']
49
+
50
+ return base_names
51
+
52
+ def _load_c_extension(self):
53
+ """尝试加载C扩展"""
54
+ try:
55
+ # 获取可能的扩展文件名列表
56
+ extension_files = self._get_extension_filename()
57
+
58
+ # 尝试每个文件名
59
+ for extension_file in extension_files:
60
+ extension_path = os.path.join(self.tools_dir, extension_file)
61
+
62
+ if os.path.exists(extension_path):
63
+ try:
64
+ # 加载扩展模块
65
+ spec = importlib.util.spec_from_file_location("date_utils", extension_path)
66
+ module = importlib.util.module_from_spec(spec)
67
+ spec.loader.exec_module(module)
68
+
69
+ # 验证必要的函数是否存在
70
+ required_functions = ['get_today', 'date_to_timestamp', 'days_between_c', 'is_leap_year_c']
71
+ for func_name in required_functions:
72
+ if not hasattr(module, func_name):
73
+ print(f"Warning: C extension missing function: {func_name}")
74
+ continue # 尝试下一个文件
75
+
76
+ print(f"Successfully loaded C extension: {extension_file}")
77
+ return module
78
+ except Exception as e:
79
+ print(f"Failed to load {extension_file}: {e}")
80
+ continue # 尝试下一个文件
81
+
82
+ # 如果没有找到文件,尝试直接导入
83
+ sys.path.insert(0, self.tools_dir)
84
+ try:
85
+ import date_utils as c_utils
86
+ print("Successfully loaded C extension via direct import")
87
+ return c_utils
88
+ except ImportError:
89
+ pass
90
+ finally:
91
+ if self.tools_dir in sys.path:
92
+ sys.path.remove(self.tools_dir)
93
+
94
+ except Exception as e:
95
+ print(f"Error loading C extension: {e}")
96
+
97
+ return None
98
+
99
+ def get_today(self):
100
+ """获取当前日期"""
101
+ if self.has_c_extension and self.c_utils:
102
+ try:
103
+ return self.c_utils.get_today()
104
+ except Exception as e:
105
+ print(f"⚠️ C扩展get_today失败,回退到Python: {e}")
106
+
107
+ # Python备用实现
108
+ import datetime
109
+ today = datetime.date.today()
110
+ return (today.year, today.month, today.day)
111
+
112
+ def date_to_timestamp(self, year, month, day):
113
+ """日期转时间戳"""
114
+ if self.has_c_extension and self.c_utils:
115
+ try:
116
+ return self.c_utils.date_to_timestamp(year, month, day)
117
+ except Exception as e:
118
+ print(f"⚠️ C扩展date_to_timestamp失败,回退到Python: {e}")
119
+
120
+ # Python备用实现
121
+ import datetime
122
+ try:
123
+ date = datetime.date(year, month, day)
124
+ dt = datetime.datetime.combine(date, datetime.time())
125
+ return dt.timestamp()
126
+ except ValueError as e:
127
+ raise ValueError(f"Invalid date: {e}")
128
+
129
+ def days_between(self, year1, month1, day1, year2, month2, day2):
130
+ """计算两个日期之间的天数差"""
131
+ if self.has_c_extension and self.c_utils:
132
+ try:
133
+ return self.c_utils.days_between_c(year1, month1, day1, year2, month2, day2)
134
+ except Exception as e:
135
+ print(f"⚠️ C扩展days_between失败,回退到Python: {e}")
136
+
137
+ # Python备用实现
138
+ import datetime
139
+ try:
140
+ date1 = datetime.date(year1, month1, day1)
141
+ date2 = datetime.date(year2, month2, day2)
142
+ return (date2 - date1).days
143
+ except ValueError as e:
144
+ raise ValueError(f"Invalid date: {e}")
145
+
146
+ def is_leap_year(self, year):
147
+ """判断是否为闰年"""
148
+ if self.has_c_extension and self.c_utils:
149
+ try:
150
+ return self.c_utils.is_leap_year_c(year)
151
+ except Exception as e:
152
+ print(f"⚠️ C扩展is_leap_year失败,回退到Python: {e}")
153
+
154
+ # Python备用实现
155
+ return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
156
+
157
+ # 创建全局实例
158
+ _platform_utils = PlatformDateUtils()
159
+
160
+ # 导出函数接口
161
+ def get_today():
162
+ """获取当前日期"""
163
+ return _platform_utils.get_today()
164
+
165
+ def date_to_timestamp(year, month, day):
166
+ """日期转时间戳"""
167
+ return _platform_utils.date_to_timestamp(year, month, day)
168
+
169
+ def days_between(year1, month1, day1, year2, month2, day2):
170
+ """计算日期间隔"""
171
+ return _platform_utils.days_between(year1, month1, day1, year2, month2, day2)
172
+
173
+ def is_leap_year_c(year):
174
+ """判断闰年"""
175
+ return _platform_utils.is_leap_year(year)
176
+
177
+ def has_c_extension():
178
+ """检查是否有C扩展可用"""
179
+ return _platform_utils.has_c_extension
180
+
181
+ def get_platform_info():
182
+ """获取平台信息"""
183
+ return {
184
+ 'system': platform.system(),
185
+ 'machine': platform.machine(),
186
+ 'python_version': platform.python_version(),
187
+ 'has_c_extension': _platform_utils.has_c_extension,
188
+ 'expected_extension': _platform_utils._get_extension_filename()
189
+ }