modulitiz-mini 2.6.0__py311-none-any.whl → 2.6.1__py311-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.
- modulitiz_mini/ModuloRsa.py +84 -84
- modulitiz_mini/django/ModuloDjango.py +20 -20
- modulitiz_mini/django/MyMiddleware.py +10 -10
- modulitiz_mini/django/settings.py +132 -132
- modulitiz_mini/files/pdf/AbstractPdf.py +9 -9
- modulitiz_mini/files/pdf/ReadPdf.py +27 -27
- modulitiz_mini/files/pdf/WritePdf.py +14 -14
- modulitiz_mini/rete/ModuloTor.py +46 -46
- modulitiz_mini/rete/ftp/FtpReturnBean.py +9 -9
- modulitiz_mini/rete/ftp/ModuloFtp.py +212 -212
- {modulitiz_mini-2.6.0.dist-info → modulitiz_mini-2.6.1.dist-info}/LICENSE +20 -20
- {modulitiz_mini-2.6.0.dist-info → modulitiz_mini-2.6.1.dist-info}/METADATA +50 -50
- modulitiz_mini-2.6.1.dist-info/RECORD +15 -0
- modulitiz_mini-2.6.0.dist-info/RECORD +0 -15
- {modulitiz_mini-2.6.0.dist-info → modulitiz_mini-2.6.1.dist-info}/WHEEL +0 -0
- {modulitiz_mini-2.6.0.dist-info → modulitiz_mini-2.6.1.dist-info}/top_level.txt +0 -0
modulitiz_mini/rete/ModuloTor.py
CHANGED
@@ -1,46 +1,46 @@
|
|
1
|
-
import time
|
2
|
-
|
3
|
-
from modulitiz_micro.ModuloStringhe import ModuloStringhe
|
4
|
-
from modulitiz_micro.eccezioni.EccezioneRuntime import EccezioneRuntime
|
5
|
-
from modulitiz_micro.files.ModuloFiles import ModuloFiles
|
6
|
-
from modulitiz_micro.rete.ModuloNetworking import ModuloNetworking
|
7
|
-
from modulitiz_micro.sistema.ModuloSystem import ModuloSystem
|
8
|
-
|
9
|
-
|
10
|
-
class ModuloTor(object):
|
11
|
-
COMMAND="tor"
|
12
|
-
HTTP_PORT=9050
|
13
|
-
MAX_RETRIES_CHECK_PORT_OPEN=100
|
14
|
-
|
15
|
-
def __init__(self,dirTor:str):
|
16
|
-
self.dirTor=dirTor
|
17
|
-
self.pid=0
|
18
|
-
|
19
|
-
def start(self)->int:
|
20
|
-
cmd=ModuloFiles.pathJoin(self.dirTor, self.COMMAND)
|
21
|
-
# avvio e aspetto che il servizio si avvii
|
22
|
-
for riga in ModuloSystem.systemCallYieldOutput(cmd, None):
|
23
|
-
if ModuloStringhe.contains(riga,"100%"):
|
24
|
-
break
|
25
|
-
# controllo che la porta sia stata aperta
|
26
|
-
maxRetries=self.MAX_RETRIES_CHECK_PORT_OPEN
|
27
|
-
while maxRetries>0:
|
28
|
-
if ModuloNetworking.isHttpPortOpen(None, self.HTTP_PORT):
|
29
|
-
maxRetries=-1
|
30
|
-
else:
|
31
|
-
maxRetries-=1
|
32
|
-
time.sleep(0.1)
|
33
|
-
if maxRetries==0:
|
34
|
-
raise EccezioneRuntime("Tor non ha la porta aperta.")
|
35
|
-
# ricavo il pid
|
36
|
-
pid=ModuloSystem.findPidByName(self.COMMAND)
|
37
|
-
return pid
|
38
|
-
|
39
|
-
def stop(self):
|
40
|
-
if self.pid==0:
|
41
|
-
return
|
42
|
-
ModuloSystem.sendCtrlcProcess(self.pid)
|
43
|
-
|
44
|
-
def restart(self):
|
45
|
-
self.stop()
|
46
|
-
self.start()
|
1
|
+
import time
|
2
|
+
|
3
|
+
from modulitiz_micro.ModuloStringhe import ModuloStringhe
|
4
|
+
from modulitiz_micro.eccezioni.EccezioneRuntime import EccezioneRuntime
|
5
|
+
from modulitiz_micro.files.ModuloFiles import ModuloFiles
|
6
|
+
from modulitiz_micro.rete.ModuloNetworking import ModuloNetworking
|
7
|
+
from modulitiz_micro.sistema.ModuloSystem import ModuloSystem
|
8
|
+
|
9
|
+
|
10
|
+
class ModuloTor(object):
|
11
|
+
COMMAND="tor"
|
12
|
+
HTTP_PORT=9050
|
13
|
+
MAX_RETRIES_CHECK_PORT_OPEN=100
|
14
|
+
|
15
|
+
def __init__(self,dirTor:str):
|
16
|
+
self.dirTor=dirTor
|
17
|
+
self.pid=0
|
18
|
+
|
19
|
+
def start(self)->int:
|
20
|
+
cmd=ModuloFiles.pathJoin(self.dirTor, self.COMMAND)
|
21
|
+
# avvio e aspetto che il servizio si avvii
|
22
|
+
for riga in ModuloSystem.systemCallYieldOutput(cmd, None):
|
23
|
+
if ModuloStringhe.contains(riga,"100%"):
|
24
|
+
break
|
25
|
+
# controllo che la porta sia stata aperta
|
26
|
+
maxRetries=self.MAX_RETRIES_CHECK_PORT_OPEN
|
27
|
+
while maxRetries>0:
|
28
|
+
if ModuloNetworking.isHttpPortOpen(None, self.HTTP_PORT):
|
29
|
+
maxRetries=-1
|
30
|
+
else:
|
31
|
+
maxRetries-=1
|
32
|
+
time.sleep(0.1)
|
33
|
+
if maxRetries==0:
|
34
|
+
raise EccezioneRuntime("Tor non ha la porta aperta.")
|
35
|
+
# ricavo il pid
|
36
|
+
pid=ModuloSystem.findPidByName(self.COMMAND)
|
37
|
+
return pid
|
38
|
+
|
39
|
+
def stop(self):
|
40
|
+
if self.pid==0:
|
41
|
+
return
|
42
|
+
ModuloSystem.sendCtrlcProcess(self.pid)
|
43
|
+
|
44
|
+
def restart(self):
|
45
|
+
self.stop()
|
46
|
+
self.start()
|
@@ -1,9 +1,9 @@
|
|
1
|
-
class FtpReturnBean(object):
|
2
|
-
|
3
|
-
def __init__(self,percorso_remoto,nomefile,isFile,countFiles,countDirs):
|
4
|
-
self.percorso_remoto=percorso_remoto
|
5
|
-
self.nomefile=nomefile
|
6
|
-
self.isFile=isFile
|
7
|
-
self.countFiles=countFiles
|
8
|
-
self.countDirs=countDirs
|
9
|
-
|
1
|
+
class FtpReturnBean(object):
|
2
|
+
|
3
|
+
def __init__(self,percorso_remoto,nomefile,isFile,countFiles,countDirs):
|
4
|
+
self.percorso_remoto=percorso_remoto
|
5
|
+
self.nomefile=nomefile
|
6
|
+
self.isFile=isFile
|
7
|
+
self.countFiles=countFiles
|
8
|
+
self.countDirs=countDirs
|
9
|
+
|
@@ -1,212 +1,212 @@
|
|
1
|
-
import ftplib
|
2
|
-
import os
|
3
|
-
|
4
|
-
from modulitiz_micro.ModuloListe import ModuloListe
|
5
|
-
from modulitiz_micro.ModuloStringhe import ModuloStringhe
|
6
|
-
from modulitiz_micro.files.ModuloFiles import ModuloFiles
|
7
|
-
from modulitiz_mini.rete.ftp.FtpReturnBean import FtpReturnBean
|
8
|
-
|
9
|
-
|
10
|
-
class ModuloFtp(object):
|
11
|
-
DEFAULT_FTP_PORT=ftplib.FTP_PORT
|
12
|
-
|
13
|
-
def __init__(self,host,username,password):
|
14
|
-
self.host=host
|
15
|
-
self.porta=self.DEFAULT_FTP_PORT
|
16
|
-
self.username=username
|
17
|
-
self.password=password
|
18
|
-
self.conn=None
|
19
|
-
self.msgBenvenuto=None
|
20
|
-
|
21
|
-
def connect(self):
|
22
|
-
conn=ftplib.FTP(encoding=ModuloStringhe.CODIFICA_LATIN1)
|
23
|
-
try:
|
24
|
-
conn.connect(self.host,self.porta)
|
25
|
-
self.msgBenvenuto=conn.welcome
|
26
|
-
except UnicodeDecodeError as udError:
|
27
|
-
self.msgBenvenuto=udError.object.decode(ModuloStringhe.CODIFICA_ASCII,'ignore')
|
28
|
-
conn.login(self.username,self.password)
|
29
|
-
self.conn=conn
|
30
|
-
|
31
|
-
def uploadCartella(self,root_locale,rootRemota,excludeFiles,excludeDirs,min_byte_size,max_byte_size):
|
32
|
-
root_locale=ModuloFiles.normalizzaPercorsoLocale(root_locale)
|
33
|
-
rootRemota=ModuloFiles.normalizzaPercorsoRemoto(rootRemota)
|
34
|
-
countFiles=
|
35
|
-
|
36
|
-
for percorsoLocaleRel,percorsoLocaleAbs,folders,nomefiles in ModuloFiles.walk(root_locale,excludeFiles,excludeDirs,min_byte_size,max_byte_size):
|
37
|
-
percorso_remoto_abs=ModuloFiles.normalizzaPercorsoRemoto(ModuloFiles.pathJoin(rootRemota,percorsoLocaleRel))
|
38
|
-
# carico i file contenuti nella cartella corrente
|
39
|
-
for nomefile in nomefiles:
|
40
|
-
self.uploadFile(percorsoLocaleAbs,percorso_remoto_abs,nomefile,False)
|
41
|
-
countFiles+=1
|
42
|
-
yield FtpReturnBean(percorso_remoto_abs,nomefile,True,countFiles,
|
43
|
-
# upload folders
|
44
|
-
for cartella in folders:
|
45
|
-
cartella=ModuloFiles.normalizzaPercorsoRemoto(ModuloFiles.pathJoin(percorso_remoto_abs,cartella))
|
46
|
-
try:
|
47
|
-
self.conn.mkd(cartella)
|
48
|
-
except Exception:
|
49
|
-
pass
|
50
|
-
|
51
|
-
yield FtpReturnBean(percorso_remoto_abs,cartella,False,countFiles,
|
52
|
-
|
53
|
-
def uploadFile(self,percorso_locale,percorso_remoto,nomefile,renameIfExist)->str:
|
54
|
-
nomefile_locale=ModuloFiles.pathJoin(percorso_locale,nomefile)
|
55
|
-
nomefile_remoto=ModuloFiles.normalizzaPercorsoRemoto(ModuloFiles.pathJoin(percorso_remoto,nomefile))
|
56
|
-
if renameIfExist is True:
|
57
|
-
# se il file esiste gia' sul server gli aggiungo il timestamp
|
58
|
-
if self.isFile(nomefile_remoto):
|
59
|
-
nomefile_remoto=ModuloStringhe.aggiungiTimestamp(nomefile_remoto)
|
60
|
-
# carico il file
|
61
|
-
with open(nomefile_locale,'rb') as fp:
|
62
|
-
self.conn.storbinary("STOR "+nomefile_remoto,fp)
|
63
|
-
return nomefile_remoto
|
64
|
-
|
65
|
-
def downloadCartella(self,percorso_remoto,percorso_locale,excludeFiles,excludeDirs):
|
66
|
-
percorso_remoto=ModuloFiles.normalizzaPercorsoRemoto(percorso_remoto)
|
67
|
-
return self.__downloadCartella(percorso_remoto,percorso_locale,excludeFiles,excludeDirs)
|
68
|
-
|
69
|
-
def downloadFile(self,nomefile_server,nomefile_locale):
|
70
|
-
nomefile_server=ModuloFiles.normalizzaPercorsoRemoto(nomefile_server)
|
71
|
-
nomefile_locale=ModuloFiles.normalizzaPercorsoLocale(nomefile_locale)
|
72
|
-
# creo le cartelle locali
|
73
|
-
cartella_locale=os.path.dirname(nomefile_locale)
|
74
|
-
os.makedirs(cartella_locale,exist_ok=True)
|
75
|
-
# scarico il file
|
76
|
-
with open(nomefile_locale,"wb") as fp:
|
77
|
-
self.conn.retrbinary("RETR "+nomefile_server,fp.write)
|
78
|
-
|
79
|
-
def eliminaCartella(self,percorso_remoto,excludeFiles,excludeDirs):
|
80
|
-
percorso_remoto=ModuloFiles.normalizzaPercorsoRemoto(percorso_remoto)
|
81
|
-
return self.__eliminaCartella(percorso_remoto,'.',excludeFiles,excludeDirs)
|
82
|
-
|
83
|
-
def listaContenutoCartella(self,percorso_remoto):
|
84
|
-
elementi = []
|
85
|
-
if self.isFile(percorso_remoto) is True:
|
86
|
-
return elementi
|
87
|
-
if percorso_remoto.startswith(('.','/')) is False:
|
88
|
-
percorso_remoto="./"+percorso_remoto
|
89
|
-
cmd = "NLST -a "+percorso_remoto
|
90
|
-
try:
|
91
|
-
self.conn.retrlines(cmd, elementi.append)
|
92
|
-
elementi.sort()
|
93
|
-
# elimino . e ..
|
94
|
-
elementi=elementi[2:]
|
95
|
-
except ftplib.error_perm:
|
96
|
-
pass
|
97
|
-
return elementi
|
98
|
-
|
99
|
-
def mkdirs(self,percorso_remoto):
|
100
|
-
percorso_remoto=ModuloFiles.normalizzaPercorsoRemoto(percorso_remoto)
|
101
|
-
dirs=ModuloListe.eliminaElementiVuoti(percorso_remoto.split("/"))
|
102
|
-
percorso_corrente=""
|
103
|
-
for cartella in dirs:
|
104
|
-
percorso_corrente=ModuloFiles.normalizzaPercorsoRemoto(ModuloFiles.pathJoin(percorso_corrente,cartella))
|
105
|
-
try:
|
106
|
-
self.conn.mkd(percorso_corrente)
|
107
|
-
except Exception:
|
108
|
-
pass
|
109
|
-
|
110
|
-
def chiudi(self):
|
111
|
-
"""
|
112
|
-
chiude la connessione
|
113
|
-
"""
|
114
|
-
if self.conn is None:
|
115
|
-
return
|
116
|
-
try:
|
117
|
-
self.conn.quit()
|
118
|
-
except Exception:
|
119
|
-
self.conn.close()
|
120
|
-
self.conn=None
|
121
|
-
|
122
|
-
|
123
|
-
def getFileSize(self,nomefile):
|
124
|
-
try:
|
125
|
-
self.conn.voidcmd('TYPE I')
|
126
|
-
size=self.conn.size(nomefile)
|
127
|
-
return size
|
128
|
-
except Exception:
|
129
|
-
return
|
130
|
-
|
131
|
-
def isFile(self,elemento):
|
132
|
-
"""
|
133
|
-
controlla se un oggetto e' un file o una cartella
|
134
|
-
"""
|
135
|
-
return self.getFileSize(elemento) is not None
|
136
|
-
|
137
|
-
def goParentDir(self):
|
138
|
-
# bisogna per forza usare 2 punti, se c'e' anche lo slash finale non funziona
|
139
|
-
self.conn.cwd("..")
|
140
|
-
|
141
|
-
'''
|
142
|
-
FUNZIONI PRIVATE
|
143
|
-
'''
|
144
|
-
|
145
|
-
def __downloadCartella(self,percorso_remoto,percorso_locale,excludeFiles,excludeDirs,
|
146
|
-
countFiles:int=0,
|
147
|
-
"""
|
148
|
-
funzione ricorsiva
|
149
|
-
"""
|
150
|
-
# ciclo ogni elemento
|
151
|
-
elementi=self.listaContenutoCartella(percorso_remoto)
|
152
|
-
for elemento in elementi:
|
153
|
-
elemento_rel_path=elemento
|
154
|
-
if elemento_rel_path.startswith("/"):
|
155
|
-
elemento_rel_path=elemento_rel_path[1:]
|
156
|
-
elemento_remoto=ModuloFiles.normalizzaPercorsoRemoto(elemento)
|
157
|
-
elemento_locale=ModuloFiles.pathJoin(percorso_locale,elemento_rel_path)
|
158
|
-
# controllo se l'elemento e' un file o una cartella
|
159
|
-
if self.isFile(elemento_remoto):
|
160
|
-
os.makedirs(os.path.dirname(elemento_locale),exist_ok=True)
|
161
|
-
if elemento_remoto not in excludeFiles:
|
162
|
-
self.downloadFile(elemento_remoto,elemento_locale)
|
163
|
-
countFiles+=1
|
164
|
-
yield FtpReturnBean(percorso_remoto,elemento,True,countFiles,
|
165
|
-
else:
|
166
|
-
|
167
|
-
# creo la cartella
|
168
|
-
if ModuloListe.stringContainsCollection(elemento_remoto,excludeDirs) is True:
|
169
|
-
yield FtpReturnBean(percorso_remoto,elemento,True,countFiles,
|
170
|
-
break
|
171
|
-
os.makedirs(elemento_locale,exist_ok=True)
|
172
|
-
yield FtpReturnBean(percorso_remoto,elemento,False,countFiles,
|
173
|
-
# entro ed elaboro la sottocartella
|
174
|
-
for bean in self.__downloadCartella(elemento_remoto,percorso_locale,excludeFiles,excludeDirs,
|
175
|
-
countFiles,
|
176
|
-
countFiles=bean.countFiles
|
177
|
-
|
178
|
-
yield bean
|
179
|
-
|
180
|
-
def __eliminaCartella(self,rootRemota,percorsoRemotoRel,excludeFiles,excludeDirs):
|
181
|
-
"""
|
182
|
-
funzione ricorsiva
|
183
|
-
"""
|
184
|
-
# ciclo ogni elemento
|
185
|
-
countFiles=
|
186
|
-
elementi=self.listaContenutoCartella(percorsoRemotoRel)
|
187
|
-
for elemento in elementi:
|
188
|
-
elemento_remoto_rel=elemento
|
189
|
-
elemento_remoto_abs=ModuloFiles.pathJoin("/",elemento)
|
190
|
-
# controllo se e' un file o una cartella
|
191
|
-
if self.isFile(elemento_remoto_abs):
|
192
|
-
if ModuloListe.collectionContainsString(excludeDirs,percorsoRemotoRel) is False and elemento_remoto_rel not in excludeFiles:
|
193
|
-
self.conn.delete(elemento_remoto_abs)
|
194
|
-
countFiles+=1
|
195
|
-
yield FtpReturnBean(percorsoRemotoRel,elemento,True,countFiles,
|
196
|
-
else:
|
197
|
-
# entro ed elaboro la sottocartella
|
198
|
-
if ModuloListe.collectionContainsString(excludeDirs,elemento_remoto_rel) is False:
|
199
|
-
countFilesSubDir=
|
200
|
-
for bean in self.__eliminaCartella(rootRemota,elemento_remoto_rel,excludeFiles,excludeDirs):
|
201
|
-
countFilesSubDir=bean.countFiles+countFiles
|
202
|
-
|
203
|
-
bean.countFiles=countFilesSubDir
|
204
|
-
bean.
|
205
|
-
yield bean
|
206
|
-
countFiles=countFilesSubDir
|
207
|
-
|
208
|
-
# cancello la cartella dopo aver cancellato i file al suo interno
|
209
|
-
if ModuloListe.collectionContainsString(excludeDirs,elemento_remoto_rel) is False:
|
210
|
-
self.conn.rmd(elemento_remoto_abs)
|
211
|
-
|
212
|
-
yield FtpReturnBean(percorsoRemotoRel,elemento,False,countFiles,
|
1
|
+
import ftplib
|
2
|
+
import os
|
3
|
+
|
4
|
+
from modulitiz_micro.ModuloListe import ModuloListe
|
5
|
+
from modulitiz_micro.ModuloStringhe import ModuloStringhe
|
6
|
+
from modulitiz_micro.files.ModuloFiles import ModuloFiles
|
7
|
+
from modulitiz_mini.rete.ftp.FtpReturnBean import FtpReturnBean
|
8
|
+
|
9
|
+
|
10
|
+
class ModuloFtp(object):
|
11
|
+
DEFAULT_FTP_PORT=ftplib.FTP_PORT
|
12
|
+
|
13
|
+
def __init__(self,host,username,password):
|
14
|
+
self.host=host
|
15
|
+
self.porta=self.DEFAULT_FTP_PORT
|
16
|
+
self.username=username
|
17
|
+
self.password=password
|
18
|
+
self.conn=None
|
19
|
+
self.msgBenvenuto=None
|
20
|
+
|
21
|
+
def connect(self):
|
22
|
+
conn=ftplib.FTP(encoding=ModuloStringhe.CODIFICA_LATIN1)
|
23
|
+
try:
|
24
|
+
conn.connect(self.host,self.porta)
|
25
|
+
self.msgBenvenuto=conn.welcome
|
26
|
+
except UnicodeDecodeError as udError:
|
27
|
+
self.msgBenvenuto=udError.object.decode(ModuloStringhe.CODIFICA_ASCII,'ignore')
|
28
|
+
conn.login(self.username,self.password)
|
29
|
+
self.conn=conn
|
30
|
+
|
31
|
+
def uploadCartella(self,root_locale,rootRemota,excludeFiles,excludeDirs,min_byte_size,max_byte_size):
|
32
|
+
root_locale=ModuloFiles.normalizzaPercorsoLocale(root_locale)
|
33
|
+
rootRemota=ModuloFiles.normalizzaPercorsoRemoto(rootRemota)
|
34
|
+
countFiles=countDirs=0
|
35
|
+
|
36
|
+
for percorsoLocaleRel,percorsoLocaleAbs,folders,nomefiles in ModuloFiles.walk(root_locale,excludeFiles,excludeDirs,min_byte_size,max_byte_size):
|
37
|
+
percorso_remoto_abs=ModuloFiles.normalizzaPercorsoRemoto(ModuloFiles.pathJoin(rootRemota,percorsoLocaleRel))
|
38
|
+
# carico i file contenuti nella cartella corrente
|
39
|
+
for nomefile in nomefiles:
|
40
|
+
self.uploadFile(percorsoLocaleAbs,percorso_remoto_abs,nomefile,False)
|
41
|
+
countFiles+=1
|
42
|
+
yield FtpReturnBean(percorso_remoto_abs,nomefile,True,countFiles,countDirs)
|
43
|
+
# upload folders
|
44
|
+
for cartella in folders:
|
45
|
+
cartella=ModuloFiles.normalizzaPercorsoRemoto(ModuloFiles.pathJoin(percorso_remoto_abs,cartella))
|
46
|
+
try:
|
47
|
+
self.conn.mkd(cartella)
|
48
|
+
except Exception:
|
49
|
+
pass
|
50
|
+
countDirs+=1
|
51
|
+
yield FtpReturnBean(percorso_remoto_abs,cartella,False,countFiles,countDirs)
|
52
|
+
|
53
|
+
def uploadFile(self,percorso_locale,percorso_remoto,nomefile,renameIfExist)->str:
|
54
|
+
nomefile_locale=ModuloFiles.pathJoin(percorso_locale,nomefile)
|
55
|
+
nomefile_remoto=ModuloFiles.normalizzaPercorsoRemoto(ModuloFiles.pathJoin(percorso_remoto,nomefile))
|
56
|
+
if renameIfExist is True:
|
57
|
+
# se il file esiste gia' sul server gli aggiungo il timestamp
|
58
|
+
if self.isFile(nomefile_remoto):
|
59
|
+
nomefile_remoto=ModuloStringhe.aggiungiTimestamp(nomefile_remoto)
|
60
|
+
# carico il file
|
61
|
+
with open(nomefile_locale,'rb') as fp:
|
62
|
+
self.conn.storbinary("STOR "+nomefile_remoto,fp)
|
63
|
+
return nomefile_remoto
|
64
|
+
|
65
|
+
def downloadCartella(self,percorso_remoto,percorso_locale,excludeFiles,excludeDirs):
|
66
|
+
percorso_remoto=ModuloFiles.normalizzaPercorsoRemoto(percorso_remoto)
|
67
|
+
return self.__downloadCartella(percorso_remoto,percorso_locale,excludeFiles,excludeDirs)
|
68
|
+
|
69
|
+
def downloadFile(self,nomefile_server,nomefile_locale):
|
70
|
+
nomefile_server=ModuloFiles.normalizzaPercorsoRemoto(nomefile_server)
|
71
|
+
nomefile_locale=ModuloFiles.normalizzaPercorsoLocale(nomefile_locale)
|
72
|
+
# creo le cartelle locali
|
73
|
+
cartella_locale=os.path.dirname(nomefile_locale)
|
74
|
+
os.makedirs(cartella_locale,exist_ok=True)
|
75
|
+
# scarico il file
|
76
|
+
with open(nomefile_locale,"wb") as fp:
|
77
|
+
self.conn.retrbinary("RETR "+nomefile_server,fp.write)
|
78
|
+
|
79
|
+
def eliminaCartella(self,percorso_remoto,excludeFiles,excludeDirs):
|
80
|
+
percorso_remoto=ModuloFiles.normalizzaPercorsoRemoto(percorso_remoto)
|
81
|
+
return self.__eliminaCartella(percorso_remoto,'.',excludeFiles,excludeDirs)
|
82
|
+
|
83
|
+
def listaContenutoCartella(self,percorso_remoto):
|
84
|
+
elementi = []
|
85
|
+
if self.isFile(percorso_remoto) is True:
|
86
|
+
return elementi
|
87
|
+
if percorso_remoto.startswith(('.','/')) is False:
|
88
|
+
percorso_remoto="./"+percorso_remoto
|
89
|
+
cmd = "NLST -a "+percorso_remoto
|
90
|
+
try:
|
91
|
+
self.conn.retrlines(cmd, elementi.append)
|
92
|
+
elementi.sort()
|
93
|
+
# elimino . e ..
|
94
|
+
elementi=elementi[2:]
|
95
|
+
except ftplib.error_perm:
|
96
|
+
pass
|
97
|
+
return elementi
|
98
|
+
|
99
|
+
def mkdirs(self,percorso_remoto):
|
100
|
+
percorso_remoto=ModuloFiles.normalizzaPercorsoRemoto(percorso_remoto)
|
101
|
+
dirs=ModuloListe.eliminaElementiVuoti(percorso_remoto.split("/"))
|
102
|
+
percorso_corrente=""
|
103
|
+
for cartella in dirs:
|
104
|
+
percorso_corrente=ModuloFiles.normalizzaPercorsoRemoto(ModuloFiles.pathJoin(percorso_corrente,cartella))
|
105
|
+
try:
|
106
|
+
self.conn.mkd(percorso_corrente)
|
107
|
+
except Exception:
|
108
|
+
pass
|
109
|
+
|
110
|
+
def chiudi(self):
|
111
|
+
"""
|
112
|
+
chiude la connessione
|
113
|
+
"""
|
114
|
+
if self.conn is None:
|
115
|
+
return
|
116
|
+
try:
|
117
|
+
self.conn.quit()
|
118
|
+
except Exception:
|
119
|
+
self.conn.close()
|
120
|
+
self.conn=None
|
121
|
+
|
122
|
+
|
123
|
+
def getFileSize(self,nomefile):
|
124
|
+
try:
|
125
|
+
self.conn.voidcmd('TYPE I')
|
126
|
+
size=self.conn.size(nomefile)
|
127
|
+
return size
|
128
|
+
except Exception:
|
129
|
+
return
|
130
|
+
|
131
|
+
def isFile(self,elemento):
|
132
|
+
"""
|
133
|
+
controlla se un oggetto e' un file o una cartella
|
134
|
+
"""
|
135
|
+
return self.getFileSize(elemento) is not None
|
136
|
+
|
137
|
+
def goParentDir(self):
|
138
|
+
# bisogna per forza usare 2 punti, se c'e' anche lo slash finale non funziona
|
139
|
+
self.conn.cwd("..")
|
140
|
+
|
141
|
+
'''
|
142
|
+
FUNZIONI PRIVATE
|
143
|
+
'''
|
144
|
+
|
145
|
+
def __downloadCartella(self,percorso_remoto,percorso_locale,excludeFiles,excludeDirs,
|
146
|
+
countFiles:int=0,countDirs:int=1):
|
147
|
+
"""
|
148
|
+
funzione ricorsiva
|
149
|
+
"""
|
150
|
+
# ciclo ogni elemento
|
151
|
+
elementi=self.listaContenutoCartella(percorso_remoto)
|
152
|
+
for elemento in elementi:
|
153
|
+
elemento_rel_path=elemento
|
154
|
+
if elemento_rel_path.startswith("/"):
|
155
|
+
elemento_rel_path=elemento_rel_path[1:]
|
156
|
+
elemento_remoto=ModuloFiles.normalizzaPercorsoRemoto(elemento)
|
157
|
+
elemento_locale=ModuloFiles.pathJoin(percorso_locale,elemento_rel_path)
|
158
|
+
# controllo se l'elemento e' un file o una cartella
|
159
|
+
if self.isFile(elemento_remoto):
|
160
|
+
os.makedirs(os.path.dirname(elemento_locale),exist_ok=True)
|
161
|
+
if elemento_remoto not in excludeFiles:
|
162
|
+
self.downloadFile(elemento_remoto,elemento_locale)
|
163
|
+
countFiles+=1
|
164
|
+
yield FtpReturnBean(percorso_remoto,elemento,True,countFiles,countDirs)
|
165
|
+
else:
|
166
|
+
countDirs+=1
|
167
|
+
# creo la cartella
|
168
|
+
if ModuloListe.stringContainsCollection(elemento_remoto,excludeDirs) is True:
|
169
|
+
yield FtpReturnBean(percorso_remoto,elemento,True,countFiles,countDirs)
|
170
|
+
break
|
171
|
+
os.makedirs(elemento_locale,exist_ok=True)
|
172
|
+
yield FtpReturnBean(percorso_remoto,elemento,False,countFiles,countDirs)
|
173
|
+
# entro ed elaboro la sottocartella
|
174
|
+
for bean in self.__downloadCartella(elemento_remoto,percorso_locale,excludeFiles,excludeDirs,
|
175
|
+
countFiles,countDirs):
|
176
|
+
countFiles=bean.countFiles
|
177
|
+
countDirs=bean.countDirs
|
178
|
+
yield bean
|
179
|
+
|
180
|
+
def __eliminaCartella(self,rootRemota,percorsoRemotoRel,excludeFiles,excludeDirs):
|
181
|
+
"""
|
182
|
+
funzione ricorsiva
|
183
|
+
"""
|
184
|
+
# ciclo ogni elemento
|
185
|
+
countFiles=countDirs=0
|
186
|
+
elementi=self.listaContenutoCartella(percorsoRemotoRel)
|
187
|
+
for elemento in elementi:
|
188
|
+
elemento_remoto_rel=elemento
|
189
|
+
elemento_remoto_abs=ModuloFiles.pathJoin("/",elemento)
|
190
|
+
# controllo se e' un file o una cartella
|
191
|
+
if self.isFile(elemento_remoto_abs):
|
192
|
+
if ModuloListe.collectionContainsString(excludeDirs,percorsoRemotoRel) is False and elemento_remoto_rel not in excludeFiles:
|
193
|
+
self.conn.delete(elemento_remoto_abs)
|
194
|
+
countFiles+=1
|
195
|
+
yield FtpReturnBean(percorsoRemotoRel,elemento,True,countFiles,countDirs)
|
196
|
+
else:
|
197
|
+
# entro ed elaboro la sottocartella
|
198
|
+
if ModuloListe.collectionContainsString(excludeDirs,elemento_remoto_rel) is False:
|
199
|
+
countFilesSubDir=countDirsSubDir=0
|
200
|
+
for bean in self.__eliminaCartella(rootRemota,elemento_remoto_rel,excludeFiles,excludeDirs):
|
201
|
+
countFilesSubDir=bean.countFiles+countFiles
|
202
|
+
countDirsSubDir=bean.countDirs+countDirs
|
203
|
+
bean.countFiles=countFilesSubDir
|
204
|
+
bean.countDirs=countDirsSubDir
|
205
|
+
yield bean
|
206
|
+
countFiles=countFilesSubDir
|
207
|
+
countDirs=countDirsSubDir
|
208
|
+
# cancello la cartella dopo aver cancellato i file al suo interno
|
209
|
+
if ModuloListe.collectionContainsString(excludeDirs,elemento_remoto_rel) is False:
|
210
|
+
self.conn.rmd(elemento_remoto_abs)
|
211
|
+
countDirs+=1
|
212
|
+
yield FtpReturnBean(percorsoRemotoRel,elemento,False,countFiles,countDirs)
|
@@ -1,21 +1,21 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2023-2040 tiz
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023-2040 tiz
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
21
|
SOFTWARE.
|