This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove sync, move files to filehelper, add function download
- Loading branch information
Showing
25 changed files
with
263 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
@brief test log(time=2s) | ||
""" | ||
|
||
import sys, os, unittest | ||
|
||
if "temp_" in os.path.abspath(__file__): | ||
raise ImportError("this file should not be imported in that location: " + os.path.abspath(__file__)) | ||
|
||
try : | ||
import src | ||
except ImportError : | ||
path = os.path.normpath(os.path.abspath( os.path.join( os.path.split(__file__)[0], "..", ".."))) | ||
if path not in sys.path : sys.path.append (path) | ||
import src | ||
|
||
from src.pyquickhelper import download, get_temp_folder, fLOG | ||
|
||
|
||
class TestDownload (unittest.TestCase): | ||
|
||
def test_download(self) : | ||
fLOG (__file__, self._testMethodName, OutputPrint = __name__ == "__main__") | ||
fold = get_temp_folder(__file__,"temp_download") | ||
url = "https://docs.python.org/3.5/library/ftplib.html" | ||
f = download(url, fold) | ||
fLOG(f) | ||
assert os.path.exists(f) | ||
assert f.endswith("ftplib.html") | ||
|
||
|
||
if __name__ == "__main__" : | ||
unittest.main () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
@file | ||
@brief Functions about compressing files. | ||
""" | ||
|
||
import os, re, zipfile, datetime, gzip | ||
|
||
from ..loghelper.flog import noLOG | ||
|
||
def zip_files (filename, fileSet, fLOG = noLOG) : | ||
""" | ||
put all files from an iterator in a zip file | ||
@param filename final zip file | ||
@param fileSet iterator on file to add | ||
@param fLOG logging function | ||
@return number of added files | ||
""" | ||
nb = 0 | ||
a1980 = datetime.datetime(1980,1,1) | ||
with zipfile.ZipFile(filename, 'w') as myzip: | ||
for file in fileSet : | ||
st = os.stat(file) | ||
atime = datetime.datetime.fromtimestamp(st.st_atime) | ||
mtime = datetime.datetime.fromtimestamp(st.st_mtime) | ||
if atime < a1980 or mtime < a1980 : | ||
new_mtime = st.st_mtime + (4*3600) #new modification time | ||
while datetime.datetime.fromtimestamp(new_mtime) < a1980 : | ||
new_mtime += (4*3600) #new modification time | ||
|
||
fLOG("zip_files: changing time timestamp for file ", file) | ||
os.utime(file,(st.st_atime,new_mtime)) | ||
|
||
myzip.write(file) | ||
nb += 1 | ||
return nb | ||
|
||
def gzip_files (filename_gz, fileSet, fLOG = noLOG, filename_zip = None) : | ||
""" | ||
put all files from an iterator in a zip file and then in a gzip file | ||
@param filename_gz final gzip file (double compression, extension should something like .zip.gz) | ||
@param filename_zip temporary zip file (will be removed after the zipping unless it is different from None) | ||
@param fileSet iterator on file to add | ||
@param log log function | ||
@return number of added files | ||
""" | ||
if filename_zip is None : | ||
zipf = filename_gz + ".temp.zip" | ||
else : zipf = filename_zip | ||
nb = zip_files (zipf, fileSet, fLOG = fLOG) | ||
|
||
f = gzip.open(filename_gz, 'wb') | ||
with open(zipf, "rb") as gr : | ||
bb = gr.read(1000000) | ||
while len(bb) > 0 : | ||
f.write(bb) | ||
bb = gr.read(1000000) | ||
f.close() | ||
|
||
if filename_zip is None : | ||
os.remove (zipf) | ||
|
||
return nb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
@file | ||
@brief exceptions | ||
""" | ||
|
||
class FileException(Exception): | ||
""" | ||
exception related to files | ||
""" | ||
pass |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
""" | ||
@file | ||
@brief Gather functions about downloading from internet, ... | ||
""" | ||
|
||
import os, urllib, urllib.request, sys | ||
|
||
from ..loghelper.flog import noLOG, _get_file_url | ||
from .fexceptions import FileException | ||
|
||
def download (url, path_download = ".", outfile = None, fLOG = noLOG) : | ||
""" | ||
Download a file | ||
If url is an url, downloads the file and return the downloaded | ||
If it has already been downloaded, it is not downloaded again | ||
The function raises an exception if the url does not contain ``http://`` or ``https://`` or ``ftp://``. | ||
@param url url | ||
@param path_download download the file here | ||
@param outfile see below | ||
@param fLOG logging function | ||
@return the filename | ||
If *outfile* is None, the function will give a relative name | ||
based on the last part of the url. | ||
If *outfile* is "", the function will remove every weird character. | ||
If *outfile* is not null, the function will use it. It will be relative to | ||
the current folder and not *path_download*. | ||
.. versionadded:: 0.9 | ||
""" | ||
lurl = url.lower() | ||
if "http://" in lurl or "https://" in lurl or "ftp://": | ||
if outfile is None: dest = os.path.join(path_download, os.path.split(url)[-1]) | ||
elif outfile == "" : dest = _get_file_url (url, path_download) | ||
else: dest = outfile | ||
|
||
down = False | ||
nyet = dest + ".notyet" | ||
|
||
if os.path.exists (dest) and not os.path.exists (nyet) : | ||
try : | ||
f1 = urllib.urlopen (url) | ||
down = _first_more_recent (f1, dest) | ||
newdate = down | ||
f1.close () | ||
except IOError as e : | ||
raise FileException("unable to access url: " + url) from e | ||
else : | ||
down = True | ||
newdate = False | ||
|
||
if down : | ||
if newdate : fLOG (" downloading (updated) ", url) | ||
else : fLOG (" downloading ", url) | ||
|
||
if len (url) > 4 and url [-4].lower () in [".txt", ".csv", ".tsv", ".log"] : | ||
fLOG ("creating text file ", dest) | ||
format = "w" | ||
else : | ||
fLOG ("creating binary file ", dest) | ||
format = "wb" | ||
|
||
if os.path.exists (nyet) : | ||
size = os.stat (dest).st_size | ||
fLOG ("resume downloading (stop at", size, ") from ", url) | ||
request = urllib.request.Request(url) | ||
request.add_header("Range", "bytes=%d-" % size) | ||
fu = urllib.request.urlopen (request) | ||
f = open (dest, format.replace ("w", "a")) | ||
else : | ||
fLOG ("downloading ", url) | ||
request = urllib.request.Request(url) | ||
fu = urllib.request.urlopen (url) | ||
f = open (dest, format) | ||
|
||
open (nyet, "w").close () | ||
c = fu.read (2**21) | ||
size = 0 | ||
while len (c) > 0 : | ||
size += len (c) | ||
fLOG(" size", size) | ||
f.write (c) | ||
f.flush () | ||
c = fu.read (2**21) | ||
fLOG ("end downloading") | ||
f.close () | ||
fu.close () | ||
os.remove (nyet) | ||
|
||
url = dest | ||
return url | ||
else: | ||
raise FileException("This url does not seem to be one: " + url) |
Oops, something went wrong.