-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More major improvements into Temp_Folder, Temp_Zip classes
Added new class Temp_Zip_In_Memory
- Loading branch information
Showing
8 changed files
with
197 additions
and
27 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
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,57 @@ | ||
from osbot_utils.testing.Temp_Folder import Temp_Folder | ||
from osbot_utils.utils.Files import is_file, is_folder, files_recursive, filter_parent_folder, temp_file | ||
from osbot_utils.utils.Zip import zip_files_to_bytes | ||
|
||
|
||
class Temp_Zip_In_Memory: | ||
|
||
def __init__(self, targets=None): | ||
self.targets = targets or [] | ||
self.root_path = None | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, type, value, traceback): | ||
pass | ||
|
||
def add_file(self, file): | ||
if is_file(file): | ||
self.targets.append(file) | ||
return self | ||
|
||
def add_folder(self, folder): | ||
if type(folder) is Temp_Folder: | ||
folder = folder.path() | ||
if is_folder(folder): | ||
self.targets.append(folder) | ||
return self | ||
|
||
def all_source_files(self): | ||
all_files = [] | ||
for target in self.targets: | ||
if is_file(target): | ||
all_files.append(target) | ||
elif is_folder(target): | ||
all_files.extend(files_recursive(target)) | ||
return all_files | ||
|
||
def create_zip_file(self, target_zip_file=None): | ||
if target_zip_file is None: | ||
target_zip_file = temp_file(extension='.zip') | ||
with open(target_zip_file, 'wb') as f: | ||
f.write(self.zip_bytes()) | ||
return target_zip_file | ||
|
||
def set_root_path(self, root_path): | ||
if type(root_path) is Temp_Folder: | ||
root_path = root_path.path() | ||
self.root_path = root_path | ||
return self | ||
|
||
def zip_bytes(self): | ||
return self.zip_buffer().getvalue() | ||
|
||
def zip_buffer(self): | ||
target_files = self.all_source_files() | ||
return zip_files_to_bytes(target_files, root_path=self.root_path) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from unittest import TestCase | ||
|
||
from osbot_utils.utils.Files import file_exists, file_delete, file_extension | ||
|
||
from osbot_utils.utils.Dev import pprint | ||
|
||
from osbot_utils.testing.Temp_Folder import Temp_Folder | ||
from osbot_utils.testing.Temp_Zip_In_Memory import Temp_Zip_In_Memory | ||
from osbot_utils.utils.Zip import zip_files_to_bytes, zip_file_list | ||
|
||
|
||
class test_Temp_Zip_In_Memory(TestCase): | ||
|
||
def test__with__default_params(self): | ||
with Temp_Zip_In_Memory() as _: | ||
assert _.targets == [] | ||
|
||
def test_all_source_files(self): | ||
with Temp_Folder() as temp_folder: | ||
max_total_files = 30 | ||
temp_folder.add_temp_files_and_folders(max_total_files=max_total_files) | ||
assert len(temp_folder.files()) == max_total_files | ||
with Temp_Zip_In_Memory() as temp_zip_in_memory: | ||
temp_zip_in_memory.add_folder(temp_folder) | ||
target_files_to_zip = temp_zip_in_memory.all_source_files() | ||
|
||
assert temp_folder.files(show_parent_folder=True) == target_files_to_zip | ||
assert len(temp_zip_in_memory.zip_bytes()) > 11000 | ||
|
||
def test_create_zip_file(self): | ||
with Temp_Folder(temp_files_to_add=3) as temp_folder: | ||
with Temp_Zip_In_Memory() as _: | ||
_.add_folder(temp_folder) | ||
_.set_root_path(temp_folder) | ||
target_zip_file = _.create_zip_file() # save in memory zip into disk | ||
assert file_exists(target_zip_file) # make sure it exists | ||
assert file_extension(target_zip_file) == '.zip' # make sure it has the right extension | ||
assert temp_folder.files() == zip_file_list(target_zip_file) # confirm that all files inside temp_folder are inside the in memory zip file | ||
|
||
assert file_delete(target_zip_file) is True | ||
|