8
8
9
9
import grpc
10
10
from compress import compress_file , decompress
11
+ from i18n import Text
11
12
import rpc_service_pb2
12
13
import rpc_service_pb2_grpc
13
14
from threading import Thread
@@ -26,6 +27,9 @@ def __init__(self, *args, **kwargs) -> None:
26
27
self ._get_filepath = kwargs ["get_filepath" ]
27
28
self ._logger = kwargs ["logger" ]
28
29
30
+ def set_text_source (self , text : Text ):
31
+ self ._text = text
32
+
29
33
def Ping (self , request , context ):
30
34
print (request )
31
35
return rpc_service_pb2 .PingReply (msg = "pong" )
@@ -53,34 +57,34 @@ def FileStatus(self, request, context):
53
57
54
58
def UploadFile (self , it , context ):
55
59
filepath = self ._get_filepath ()
56
- self ._logger (f"正在接收文件 { os .path .basename (filepath )} " )
60
+ self ._logger (f"{ self . _text . receiving_file } { os .path .basename (filepath )} " )
57
61
zip_filepath = "temp.zip"
58
62
with open (zip_filepath , "wb" ) as fd :
59
63
for i in it :
60
64
fd .write (i .data )
61
- self ._logger (f"接收文件成功 " )
62
- self ._logger (f"正在解压 " )
65
+ self ._logger (f"{ self . _text . receive_file_successful } " )
66
+ self ._logger (f"{ self . _text . decompressing } " )
63
67
decompress (zip_filepath , os .path .dirname (filepath ))
64
- self ._logger (f"解压成功 " )
68
+ self ._logger (f"{ self . _text . decompress_success } " )
65
69
return rpc_service_pb2 .UploadFileReply (status = True )
66
70
67
71
def DownloadFile (self , request , context ):
68
72
filepath = self ._get_filepath ()
69
- self ._logger (f"正在压缩 { os .path .basename (filepath )} " )
73
+ self ._logger (f"{ self . _text . compressing } { os .path .basename (filepath )} " )
70
74
zip_filepath = "temp.zip"
71
75
compress_file (filepath , zip_filepath )
72
- self ._logger (f"压缩成功 " )
73
- self ._logger (f"发送文件 { os .path .basename (filepath )} " )
76
+ self ._logger (f"{ self . _text . compress_done } " )
77
+ self ._logger (f"{ self . _text . sending_file } { os .path .basename (filepath )} " )
74
78
with open (zip_filepath , "rb" ) as fd :
75
79
while True :
76
80
data = fd .read (BUF_SIZE )
77
81
if len (data ) <= 0 :
78
- self ._logger (f"发送文件成功 " )
82
+ self ._logger (f"{ self . _text . send_success } " )
79
83
return
80
84
yield rpc_service_pb2 .DownloadFileReply (data = data )
81
85
82
86
def DownloadDirectory (self , request , context ):
83
- self ._logger ("正在发送文件到远端 " )
87
+ self ._logger (f" { self . _text . sending_file_to_remote } " )
84
88
directory_path = self ._get_filepath ()
85
89
with zipfile .ZipFile ("temp.zip" , "w" , zipfile .ZIP_DEFLATED ) as zip :
86
90
for file in request .files :
@@ -92,21 +96,21 @@ def DownloadDirectory(self, request, context):
92
96
while True :
93
97
data = fd .read (BUF_SIZE )
94
98
if len (data ) <= 0 :
95
- self ._logger (f"发送文件成功 " )
99
+ self ._logger (f"{ self . _text . send_success } " )
96
100
return
97
101
yield rpc_service_pb2 .DownloadDirectoryReply (data = data )
98
102
99
103
def ReceiveDirectory (self , request , context ):
100
104
dir_path = self ._get_filepath ()
101
- self ._logger (f"同步文件到 { dir_path } 中" )
105
+ self ._logger (self . _text . syncing_file_to . format ( dir_path ) )
102
106
zip_filepath = "temp.zip"
103
107
with open (zip_filepath , "wb" ) as fd :
104
108
for i in request :
105
109
fd .write (i .data )
106
- self ._logger ("正在解压 " )
110
+ self ._logger (f" { self . _text . decompressing } " )
107
111
decompress ("temp.zip" , dir_path )
108
- self ._logger ("解压成功 " )
109
- self ._logger ("同步成功 " )
112
+ self ._logger (f" { self . _text . decompress_success } " )
113
+ self ._logger (f" { self . _text . sync_success } " )
110
114
return rpc_service_pb2 .ReceiveDirectoryReply ()
111
115
112
116
def DirectoryStatus (self , request , context ):
0 commit comments