flaxfile 1.0.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.
flaxfile/__init__.py ADDED
@@ -0,0 +1,14 @@
1
+ """
2
+ FlaxFile - 高性能文件传输工具
3
+
4
+ 基于ZMQ优化的跨网络文件传输系统
5
+ 性能: 3800+ MB/s (本地), 1000+ MB/s (10Gbps网络)
6
+ """
7
+
8
+ __version__ = "1.0.0"
9
+ __author__ = "FlaxKV Team"
10
+
11
+ from .client import FlaxFileClient
12
+ from .server import FlaxFileServer
13
+
14
+ __all__ = ["FlaxFileClient", "FlaxFileServer"]
flaxfile/cli.py ADDED
@@ -0,0 +1,331 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ FlaxFile CLI - 高性能文件传输工具
4
+
5
+ 使用方法:
6
+ flaxfile serve # 启动服务器
7
+ flaxfile set myfile /path/to/file.bin # 上传文件
8
+ flaxfile get myfile output.bin # 下载文件
9
+ flaxfile list # 列出所有文件
10
+ flaxfile delete myfile # 删除文件
11
+ flaxfile config add-server prod 192.168.1.100 # 添加服务器
12
+ """
13
+
14
+ import sys
15
+ from pathlib import Path
16
+ from typing import Optional
17
+ import fire
18
+
19
+ from .client import FlaxFileClient
20
+ from .server import FlaxFileServer
21
+ from .config import Config, CONFIG_FILE_PATHS
22
+
23
+
24
+ class ConfigCommands:
25
+ """配置管理命令"""
26
+
27
+ def __init__(self, config_obj):
28
+ self._config = config_obj
29
+
30
+ def init(self, path: Optional[str] = None):
31
+ """
32
+ 初始化配置文件
33
+
34
+ Args:
35
+ path: 配置文件路径(可选,默认为当前目录的 flaxfile.toml)
36
+
37
+ 示例:
38
+ flaxfile config init # 在当前目录创建
39
+ flaxfile config init ~/flaxfile.toml # 在家目录创建
40
+ """
41
+ from pathlib import Path
42
+
43
+ try:
44
+ config_path = Path(path) if path else None
45
+ created_path = Config.init_config_file(config_path)
46
+ print(f"✓ 已创建配置文件: {created_path}")
47
+ print(f"\n请编辑配置文件以自定义设置")
48
+ except FileExistsError as e:
49
+ print(f"✗ {e}")
50
+ sys.exit(1)
51
+ except Exception as e:
52
+ print(f"✗ 创建配置文件失败: {e}")
53
+ sys.exit(1)
54
+
55
+ def show(self):
56
+ """显示当前配置"""
57
+ print("="*60)
58
+ print("FlaxFile 配置")
59
+ print("="*60)
60
+
61
+ # 显示配置文件来源
62
+ if self._config.config_file:
63
+ print(f"\n配置文件: {self._config.config_file}")
64
+ else:
65
+ print(f"\n配置文件: 未找到(使用默认配置)")
66
+
67
+ # 显示搜索路径
68
+ print(f"\n配置文件搜索路径(优先级从高到低):")
69
+ for i, path in enumerate(CONFIG_FILE_PATHS, 1):
70
+ exists = "✓" if Path(path).exists() else "✗"
71
+ print(f" {i}. {exists} {path}")
72
+
73
+ # 显示服务器配置
74
+ server_config = self._config.get_server_config()
75
+ print(f"\n服务器配置(flaxfile serve):")
76
+ print(f" 监听地址: {server_config['host']}")
77
+ print(f" 上传端口: {server_config['upload_port']}")
78
+ print(f" 下载端口: {server_config['download_port']}")
79
+ print(f" 控制端口: {server_config['control_port']}")
80
+ print(f" 存储目录: {server_config['storage_dir']}")
81
+
82
+ # 显示客户端配置
83
+ default_name = self._config.get_default_server_name()
84
+ print(f"\n客户端配置:")
85
+ print(f" 默认服务器: {default_name}")
86
+
87
+ # 显示远程服务器列表
88
+ servers = self._config.list_servers()
89
+ if servers:
90
+ print(f"\n远程服务器列表:")
91
+ for name, config in servers.items():
92
+ is_default = (name == default_name)
93
+ marker = " [默认]" if is_default else ""
94
+ print(f"\n {name}{marker}:")
95
+ print(f" 地址: {config['host']}")
96
+ print(f" 上传端口: {config['upload_port']}")
97
+ print(f" 下载端口: {config['download_port']}")
98
+ print(f" 控制端口: {config['control_port']}")
99
+ else:
100
+ print("\n未配置任何远程服务器")
101
+
102
+ def path(self):
103
+ """
104
+ 显示配置文件路径
105
+
106
+ 示例:
107
+ flaxfile config path
108
+ """
109
+ print("配置文件搜索路径(优先级从高到低):")
110
+ for i, path in enumerate(CONFIG_FILE_PATHS, 1):
111
+ from pathlib import Path
112
+ exists = "✓" if Path(path).exists() else "✗"
113
+ current = "← 当前使用" if (self._config.config_file and str(self._config.config_file) == path) else ""
114
+ print(f" {i}. {exists} {path} {current}")
115
+
116
+ if not self._config.config_file:
117
+ print(f"\n未找到配置文件,使用默认配置")
118
+ print(f"运行 'flaxfile config init' 创建配置文件")
119
+
120
+
121
+ class FlaxFileCLI:
122
+ """FlaxFile CLI主类"""
123
+
124
+ def __init__(self):
125
+ self._config_obj = Config()
126
+ self.config = ConfigCommands(self._config_obj)
127
+
128
+ def serve(
129
+ self,
130
+ host: Optional[str] = None,
131
+ upload_port: Optional[int] = None,
132
+ download_port: Optional[int] = None,
133
+ control_port: Optional[int] = None,
134
+ ):
135
+ """
136
+ 启动FlaxFile服务器
137
+
138
+ Args:
139
+ host: 监听地址 (可选,默认从配置文件读取)
140
+ upload_port: 上传端口 (可选,默认从配置文件读取)
141
+ download_port: 下载端口 (可选,默认从配置文件读取)
142
+ control_port: 控制端口 (可选,默认从配置文件读取)
143
+
144
+ 示例:
145
+ flaxfile serve # 使用配置文件
146
+ flaxfile serve --host 127.0.0.1 # 覆盖配置
147
+ flaxfile serve --upload-port 26555 # 覆盖端口
148
+ """
149
+ # 从配置文件读取服务器配置
150
+ server_config = self._config_obj.get_server_config()
151
+
152
+ # 命令行参数优先级更高
153
+ final_host = host if host is not None else server_config['host']
154
+ final_upload_port = upload_port if upload_port is not None else server_config['upload_port']
155
+ final_download_port = download_port if download_port is not None else server_config['download_port']
156
+ final_control_port = control_port if control_port is not None else server_config['control_port']
157
+
158
+ server = FlaxFileServer(
159
+ host=final_host,
160
+ upload_port=final_upload_port,
161
+ download_port=final_download_port,
162
+ control_port=final_control_port
163
+ )
164
+ server.start()
165
+
166
+ def set(
167
+ self,
168
+ file_path: str,
169
+ key: Optional[str] = None,
170
+ server: Optional[str] = None,
171
+ ):
172
+ """
173
+ 上传文件到服务器
174
+
175
+ Args:
176
+ file_path: 本地文件路径
177
+ key: 文件键名(可选,默认使用文件名)
178
+ server: 服务器名称(可选,默认使用配置中的默认服务器)
179
+
180
+ 示例:
181
+ flaxfile set /path/to/file.bin # key = file.bin
182
+ flaxfile set /path/to/file.bin myfile # key = myfile
183
+ flaxfile set /path/to/video.mp4 --server prod
184
+ """
185
+ # 如果未指定 key,使用文件名作为 key
186
+ if key is None:
187
+ from pathlib import Path
188
+ key = Path(file_path).name
189
+
190
+ # 获取服务器配置
191
+ server_config = self._config_obj.get_server(server)
192
+
193
+ # 创建客户端
194
+ client = FlaxFileClient(
195
+ server_host=server_config['host'],
196
+ upload_port=server_config['upload_port'],
197
+ download_port=server_config['download_port'],
198
+ control_port=server_config['control_port'],
199
+ )
200
+
201
+ try:
202
+ result = client.upload_file(file_path, key, show_progress=True)
203
+ print(f"\n✓ 上传成功")
204
+ print(f" 键名: {key}")
205
+ print(f" 大小: {result['size'] / (1024*1024):.2f} MB")
206
+ print(f" 吞吐量: {result['throughput']:.2f} MB/s")
207
+ finally:
208
+ client.close()
209
+
210
+ def get(
211
+ self,
212
+ key: str,
213
+ output_path: Optional[str] = None,
214
+ server: Optional[str] = None,
215
+ ):
216
+ """
217
+ 从服务器下载文件
218
+
219
+ Args:
220
+ key: 文件键名
221
+ output_path: 输出路径(可选,默认使用 key 作为文件名)
222
+ server: 服务器名称(可选)
223
+
224
+ 示例:
225
+ flaxfile get myfile # 保存为 ./myfile
226
+ flaxfile get myfile output.bin # 保存为 ./output.bin
227
+ flaxfile get video --server prod # 从 prod 服务器下载
228
+ """
229
+ # 默认输出路径使用 key 作为文件名
230
+ if output_path is None:
231
+ output_path = key
232
+
233
+ # 获取服务器配置
234
+ server_config = self._config_obj.get_server(server)
235
+
236
+ # 创建客户端
237
+ client = FlaxFileClient(
238
+ server_host=server_config['host'],
239
+ upload_port=server_config['upload_port'],
240
+ download_port=server_config['download_port'],
241
+ control_port=server_config['control_port'],
242
+ )
243
+
244
+ try:
245
+ result = client.download_file(key, output_path, show_progress=True)
246
+ print(f"\n✓ 下载成功")
247
+ print(f" 保存到: {output_path}")
248
+ print(f" 大小: {result['size'] / (1024*1024):.2f} MB")
249
+ print(f" 吞吐量: {result['throughput']:.2f} MB/s")
250
+ finally:
251
+ client.close()
252
+
253
+ def delete(
254
+ self,
255
+ key: str,
256
+ server: Optional[str] = None,
257
+ ):
258
+ """
259
+ 删除服务器上的文件
260
+
261
+ Args:
262
+ key: 文件键名
263
+ server: 服务器名称(可选)
264
+
265
+ 示例:
266
+ flaxfile delete myfile
267
+ flaxfile delete video --server prod
268
+ """
269
+ # 获取服务器配置
270
+ server_config = self._config_obj.get_server(server)
271
+
272
+ # 创建客户端
273
+ client = FlaxFileClient(
274
+ server_host=server_config['host'],
275
+ upload_port=server_config['upload_port'],
276
+ download_port=server_config['download_port'],
277
+ control_port=server_config['control_port'],
278
+ )
279
+
280
+ try:
281
+ success = client.delete_file(key)
282
+ if success:
283
+ print(f"✓ 删除成功: {key}")
284
+ else:
285
+ print(f"✗ 删除失败: {key}")
286
+ sys.exit(1)
287
+ finally:
288
+ client.close()
289
+
290
+ def list(
291
+ self,
292
+ server: Optional[str] = None,
293
+ ):
294
+ """
295
+ 列出服务器上的所有文件
296
+
297
+ Args:
298
+ server: 服务器名称(可选)
299
+
300
+ 示例:
301
+ flaxfile list
302
+ flaxfile list --server prod
303
+
304
+ 注意: 需要服务器支持LIST命令(当前版本暂不支持,待实现)
305
+ """
306
+ print("注意: list功能待实现")
307
+ print("当前服务器端暂不支持列出文件功能")
308
+ print("可以通过服务器端存储目录查看:zmq_streaming_storage/")
309
+
310
+ def version(self):
311
+ """显示版本信息"""
312
+ from . import __version__
313
+ print(f"FlaxFile v{__version__}")
314
+ print("高性能文件传输工具")
315
+ print("基于ZMQ优化的跨网络文件传输系统")
316
+
317
+
318
+ def main():
319
+ """CLI入口"""
320
+ try:
321
+ fire.Fire(FlaxFileCLI)
322
+ except KeyboardInterrupt:
323
+ print("\n\n中断")
324
+ sys.exit(0)
325
+ except Exception as e:
326
+ print(f"错误: {e}")
327
+ sys.exit(1)
328
+
329
+
330
+ if __name__ == "__main__":
331
+ main()