Skip to content

Commit

Permalink
implemented basic caching system for downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonstreete committed Dec 10, 2017
1 parent af85aed commit 07cdb94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 8 additions & 6 deletions cfs_manager/file_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
except FileNotFoundError:
dirs = ''

def download_move(filename, abs_file_path, new_file_location):
if '.' in new_file_location: #If it has a file extension
shutil.copyfile(abs_file_path, new_file_location)
else: #If it's a folder
shutil.copytree(abs_file_path, new_file_location)
print("'"+filename.replace('.zip', '')+"'", "was downloaded.")

def download_cleanup(decorated):
"""Cleans up after downloads by putting them in the right directory and removing unneeded files/directories
Expand All @@ -21,13 +28,8 @@ def download_cleanup(decorated):
Deletes this download location as cleanup."""
def wrapper(self, filename, destination_directory):
abs_file_path, new_file_location = decorated(self, filename, destination_directory)
if '.' in new_file_location:
shutil.copyfile(abs_file_path, new_file_location)
else:
shutil.copytree(abs_file_path, new_file_location)
download_move(filename, abs_file_path, new_file_location)
shutil.rmtree(os.path.split(abs_file_path)[0]) #Removes the swap directory where the file was extracted

print("'"+filename.replace('.zip', '')+"'", "was downloaded.")
return new_file_location
return wrapper

Expand Down
15 changes: 14 additions & 1 deletion cfs_manager/manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os, shutil
from file_systems import CloudFileSystem, PCloud_FS, GDrive_FS, DBox_FS, _Box_FS
from file_systems import download_move

fs_classes = [['pCloud', PCloud_FS], ['Google Drive', GDrive_FS], ['Dropbox', DBox_FS], ['Box (no drop)', _Box_FS]]

Expand Down Expand Up @@ -137,6 +138,7 @@ def file_size_display(size, precision):
class Main_FS(CloudFileSystem):
"""The top-level file system abstraction which utilizes all other FSs"""
def __init__(self):
self.cache = {}
file_systems = start()
files = get_all_files(file_systems)
size = 0
Expand Down Expand Up @@ -225,7 +227,18 @@ def get_cloud(self, value, field='filename'):
return sys

def download_file(self, filename, destination_directory):
return self.get_cloud(filename+'.zip').download_file(filename+'.zip', destination_directory)
if filename in self.cache:
#If the file/folder was already downloaded, just copy the existing file/folder to the new destination
try:
new_file_location = os.path.join(destination_directory, filename)
download_move(filename, self.cache[filename], new_file_location)
except FileNotFoundError: #If the file has been deleted from that location since then
new_file_location = self.get_cloud(filename+'.zip').download_file(filename+'.zip', destination_directory)
self.cache[filename] = new_file_location
else:
new_file_location = self.get_cloud(filename+'.zip').download_file(filename+'.zip', destination_directory)
self.cache[filename] = new_file_location
return new_file_location

def inspect_file(self, filename):
for f in self.files:
Expand Down

0 comments on commit 07cdb94

Please sign in to comment.