Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief provides some functionalities to upload file to a website
5.. deprecated:: 0.8
6"""
7from ftplib import FTP
8import os
9from pyquickhelper.loghelper import fLOG
12class pyhomeFTP (FTP):
13 """
14 Old version to upload file on a FTP web site
15 """
16 errorNoDirectory = "Can't change directory"
18 def __init__(self, site, login, password):
19 """
20 constructor
21 """
22 FTP.__init__(self, site, login, password)
24 def privatelogin(self):
25 fLOG("connecting")
26 FTP.login(self)
28 def RunCommand(self, command, *args):
29 try:
30 t = command(self, *args)
31 if command == FTP.pwd or command == FTP.dir:
32 return t
33 elif command != FTP.cwd:
34 fLOG(" ** run ", str(command), str(args))
35 return True
36 except Exception as e:
37 if pyhomeFTP.errorNoDirectory in str(e):
38 raise e
39 fLOG(e)
40 fLOG(" ** run exc ", str(command), str(args))
41 self.privatelogin()
42 command(self, *args)
43 fLOG(" ** run ", str(command), str(args))
44 return False
46 def printlist(self):
47 return self.RunCommand(FTP.retrlines, 'LIST')
49 def close(self):
50 fLOG("disconnecting")
51 FTP.quit(self)
53 def mkd(self, path): # pylint: disable=W0237
54 return self.RunCommand(FTP.mkd, path)
56 def cwd(self, path, create=False):
57 try:
58 self.RunCommand(FTP.cwd, path)
59 except Exception as e:
60 if create and pyhomeFTP.errorNoDirectory in str(e):
61 fLOG("** creating directory ", path)
62 self.mkd(path)
63 self.cwd(path, create)
64 else:
65 raise e
67 def pwd(self):
68 return self.RunCommand(FTP.pwd)
70 def dir(self, path='.'):
71 return self.RunCommand(FTP.dir, path)
73 def transfer(self, file, to, debug=False):
74 """
75 transfers a file
76 @param file file
77 @param to destination
78 @param debug if True, displays more information
79 @return status
80 """
81 path = to.split("/")
82 path = [_ for _ in path if len(_) > 0]
83 temp = os.path.split(file)[-1]
84 fLOG("-- upload ", temp, "to", to)
85 if debug:
86 fLOG(" -- path", path)
87 fLOG(" -- pwd", self.pwd())
88 for p in path:
89 if debug:
90 fLOG(" -- cwd", p)
91 self.cwd(p, True)
92 if debug:
93 fLOG(" -- transferring", file)
94 with open(file, "rb") as f:
95 r = self.RunCommand(FTP.storbinary, 'STOR ' + temp, f)
96 for p in path:
97 if debug:
98 fLOG(" -- cwd", "..")
99 self.cwd("..")
100 return r