-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_transfer_client.py
More file actions
305 lines (189 loc) · 7.07 KB
/
file_transfer_client.py
File metadata and controls
305 lines (189 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -*- coding: utf-8 -*-
"""
Created on Mon May 23 20:17:46 2016
@author: john
==============================================================================
License
==============================================================================
Copyright 2016 John Bainbridge
This file is part of PacketComms.
PacketComms is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
PacketComms is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PacketComms. If not, see <http://www.gnu.org/licenses/>.
File Transfer client side
=================================
This is the client side library for the File transfer manager
"""
#==============================================================================
#%% Imports
#==============================================================================
# Standard library
import os,sys
import collections
BASEPATH, dummy = os.path.split(os.path.abspath(__file__))
sys.path.append(BASEPATH)
# Third party libraries
# My libraries
import packetcommslib as pcl
import mainfilemanager
#==============================================================================
#%% Constants
#==============================================================================
COMMAND_LABEL = 'command'
#==============================================================================
#%% Class definitions
#==============================================================================
class FileTransferClient(pcl.PacketClient):
"""
Client for File transfer
Example usage
---------------
Setup client
>>> ftc = FileTransferClient(hostIP,master_folder)
Add file to master storage
>>> ftc.addFile(filename)
Add directory to master storage
>>> ftc.addDir(dir_path)
Add new master folder TODO : this may only be required when starting client
>>> ftc.addMasterDir(master_folder_name,folder_path)
Commit all to master folder
>>> ftc.commit(folder_path)
Delete file or folder from master folder
>>> ftc.delete(folder_or_file_path))
Find file in master folder
>>> ftc.findFile(filename))
"""
def __init__(self,serverIP,master_folder):
"""
Initialise FileTransfer client
Inputs
-------
serverIP : str
IP or hostname of server
master_folder_name : str
Name of master folder on the server repository. This is where all
the files are copied to.
"""
# Initialise the base class
super(FileTransferClient, self).__init__(serverIP)
# Set the master folder
self.masterFolder = master_folder
# Create/Get a local file manager database
self.localFileManager = mainfilemanager.Files()
def addFile(self,filename):
"""
Add a file to the storage repository in the the specified master folder.
Inputs
--------
master_folder_name : str
Name of master folder to store file in
filename : str
path/filename to local file that is to be uploaded to repository
"""
# Check if file exists in master storage
# if not then return
pcl.makeTextPacket('command:')
# Check last time the file was modified
# Send file to master storage
pass
def addDir(self,dir_path):
"""
Inputs
--------
dir_path : str
path to local directory that is to be uploaded to repository
"""
pass
def addMasterDir(self,folder_path):
"""
Inputs
--------
folder_path : str
path to local directory that is to be uploaded to repository as a
new master folder
"""
pass
def commit(self,folder_path):
"""
Inputs
--------
folder_path : str
path to local directory that is to be uploaded to repository
TODO : may not need this if there is a local file manager
"""
pass
def delete(self,folder_or_file_path):
"""
Inputs
--------
filename : str
path or filename that is to be deleted from the repository
"""
pass
def findFile(self,filename):
"""
Inputs
--------
filename : str
filename of file in the storage repository that is to be searched
for.
Can be a regular expression
"""
# Make a command packet
# -----------------------
reply = self.cmd('findfile',filename)
return reply=='True'
def getFileInfo(self,filename):
"""
Get information about a file in the remote repository
Inputs
--------
filename : str
path/filename to remote file
Output
----------
file_info : namedtuple
File info with fields:
size_bytes : size of file in bytes
last_modified : date
"""
pass
def xxxx(self,master_folder_name,XX):
"""
Inputs
--------
master_folder_name : str
Name of master folder to store file in
filename : str
path/filename to local file that is to be uploaded to repository
"""
pass
def cmd(self,command,parameters):
"""
Send a command to the server
Inputs
----------
command : str
Command name
parameters : list of str
list of parameters, they must all be strings
"""
if isinstance(parameters,str):
parameters = [parameters]
# Check everything is a string
assert all([isinstance(s,str) for s in parameters]), "FileTransferClient/cmd: All parameters must be strings"
# Make the command packet
cmd_packet = pcl.makeTextPacket('command',[command]+parameters)
# Send to server
reply = self.send(cmd_packet)
return reply
#==============================================================================
#%% Functions
#==============================================================================