Skip to content

Commit

Permalink
Breaking changes to Zip utils. Fixed naming of methods that was confu…
Browse files Browse the repository at this point in the history
…sing the actions to create zips and to consume zip files
  • Loading branch information
DinisCruz committed Jun 17, 2024
1 parent 2266393 commit e624b9f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 39 deletions.
4 changes: 2 additions & 2 deletions osbot_utils/testing/Temp_Zip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from osbot_utils.testing.Temp_Folder import Temp_Folder
from osbot_utils.utils.Files import Files, is_folder, file_exists, file_name, file_move_to_folder, file_move, \
file_move_to
from osbot_utils.utils.Zip import zip_folder, zip_file_list
from osbot_utils.utils.Zip import zip_folder, zip_file__list


class Temp_Zip():
Expand Down Expand Up @@ -35,7 +35,7 @@ def path(self):
return self.zip_file

def files(self):
return zip_file_list(self.zip_file)
return zip_file__list(self.zip_file)

def print_path(self):
print()
Expand Down
8 changes: 4 additions & 4 deletions osbot_utils/testing/Temp_Zip_In_Memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from osbot_utils.testing.Temp_File import Temp_File
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, zip_bytes_file_list, zip_bytes_add_file, zip_bytes_get_file
from osbot_utils.utils.Zip import zip_files_to_bytes, zip_bytes__file_list, zip_bytes__add_file, zip_bytes__get_file


class Temp_Zip_In_Memory:
Expand Down Expand Up @@ -76,14 +76,14 @@ def zip_bytes(self):
for items in self.targets_as_content:
file_path = items.get('file_path')
file_contents = items.get('file_contents')
zip_bytes = zip_bytes_add_file(zip_bytes, file_path, file_contents)
zip_bytes = zip_bytes__add_file(zip_bytes, file_path, file_contents)
return zip_bytes

def zip_bytes_file_content(self, file_path):
return zip_bytes_get_file(self.zip_bytes(), file_path)
return zip_bytes__get_file(self.zip_bytes(), file_path)

def zip_bytes_files(self):
return zip_bytes_file_list(self.zip_bytes())
return zip_bytes__file_list(self.zip_bytes())

def zip_buffer(self):
targets = self.target_files_with_root_folder()
Expand Down
58 changes: 34 additions & 24 deletions osbot_utils/utils/Zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

from osbot_utils.utils.Files import temp_folder, folder_files, temp_file, is_file, file_copy, file_move


def gz_tar_bytes_file_list(gz_bytes):
# actions on gz_tar_bytes
def gz_tar_bytes__file_list(gz_bytes):
gz_buffer_from_bytes = io.BytesIO(gz_bytes)
with gzip.GzipFile(fileobj=gz_buffer_from_bytes, mode='rb') as gz:
decompressed_data = gz.read()
tar_buffer_from_bytes = io.BytesIO(decompressed_data) # Assuming the decompressed data is a tag file, process it
with tarfile.open(fileobj=tar_buffer_from_bytes, mode='r:') as tar:
return sorted(tar.getnames())

def gz_tar_bytes_get_file(gz_bytes, tar_file_path):
def gz_tar_bytes__get_file(gz_bytes, tar_file_path):
gz_buffer_from_bytes = io.BytesIO(gz_bytes)
with gzip.GzipFile(fileobj=gz_buffer_from_bytes, mode='rb') as gz:
decompressed_data = gz.read()
Expand All @@ -29,20 +29,18 @@ def gz_tar_bytes_get_file(gz_bytes, tar_file_path):
else:
raise FileNotFoundError(f"The file {tar_file_path} was not found in the tar archive.")

def gz_zip_bytes_file_list(gz_bytes):
# actions on gz_zip_bytes

def gz_zip_bytes__file_list(gz_bytes):
gz_buffer_from_bytes = io.BytesIO(gz_bytes)
with gzip.GzipFile(fileobj=gz_buffer_from_bytes, mode='rb') as gz:
decompressed_data = gz.read()
zip_buffer_from_bytes = io.BytesIO(decompressed_data) # Assuming the decompressed data is a zip file, process it
with zipfile.ZipFile(zip_buffer_from_bytes, 'r') as zf:
return sorted(zf.namelist())

def unzip_file(zip_file, target_folder=None, format='zip'):
target_folder = target_folder or temp_folder()
shutil.unpack_archive(zip_file, extract_dir=target_folder, format=format)
return target_folder

def zip_bytes_add_file(zip_bytes, zip_file_path, file_contents):
# actions on zipped bytes
def zip_bytes__add_file(zip_bytes, zip_file_path, file_contents):
if type(file_contents) is str:
file_contents = file_contents.encode('utf-8')
elif type(file_contents) is not bytes:
Expand All @@ -53,24 +51,38 @@ def zip_bytes_add_file(zip_bytes, zip_file_path, file_contents):

return zip_buffer.getvalue()

def zip_bytes_get_file(zip_bytes, zip_file_path):
def zip_bytes__get_file(zip_bytes, zip_file_path):
zip_buffer = io.BytesIO(zip_bytes)
with zipfile.ZipFile(zip_buffer, 'r') as zf:
return zf.read(zip_file_path)

def zip_bytes_extract_to_folder(zip_bytes, target_folder=None):
def zip_bytes__extract_to_folder(zip_bytes, target_folder=None):
target_folder = target_folder or temp_folder() # Use the provided target folder or create a temporary one
zip_buffer = io.BytesIO(zip_bytes) # Create a BytesIO buffer from the zip bytes
with zipfile.ZipFile(zip_buffer, 'r') as zf: # Open the zip file from the buffer
zf.extractall(target_folder) # Extract all files to the target folder
return target_folder # Return the path of the target folder


def zip_bytes_file_list(zip_bytes):
def zip_bytes__file_list(zip_bytes):
zip_buffer_from_bytes = io.BytesIO(zip_bytes)
with zipfile.ZipFile(zip_buffer_from_bytes, 'r') as zf:
return sorted(zf.namelist())

# actions on zipped file

def zip_file__list(path):
if is_file(path):
with zipfile.ZipFile(path) as zip_file:
return sorted(zip_file.namelist())
return []

def zip_file__unzip(zip_file, target_folder=None, format='zip'):
target_folder = target_folder or temp_folder()
shutil.unpack_archive(zip_file, extract_dir=target_folder, format=format)
return target_folder

# zip creation actions

def zip_bytes_to_file(zip_bytes, target_file=None):
if target_file is None:
target_file = temp_file(extension='.zip')
Expand Down Expand Up @@ -103,7 +115,6 @@ def zip_folder_to_file (root_dir, target_file):
zip_file = zip_folder(root_dir)
return file_move(zip_file, target_file)


def zip_folder_to_bytes(root_dir): # todo add unit test
zip_buffer = io.BytesIO() # Create a BytesIO buffer to hold the zipped file
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zf: # Create a ZipFile object with the buffer as the target
Expand All @@ -115,12 +126,6 @@ def zip_folder_to_bytes(root_dir): # todo add unit test
zip_buffer.seek(0) # Reset buffer position
return zip_buffer

def zip_file_list(path):
if is_file(path):
with zipfile.ZipFile(path) as zip_file:
return sorted(zip_file.namelist())
return []

def zip_files(base_folder, file_pattern="*.*", target_file=None):
base_folder = abspath(base_folder)
file_list = folder_files(base_folder, file_pattern)
Expand All @@ -136,6 +141,11 @@ def zip_files(base_folder, file_pattern="*.*", target_file=None):


# extra function's mappings
file_unzip = unzip_file
folder_zip = zip_folder
zip_bytes_unzip_to_folder = zip_bytes_extract_to_folder

file_unzip = zip_file__unzip
folder_zip = zip_folder

unzip_file = zip_file__unzip

zip_bytes__unzip_to_folder = zip_bytes__extract_to_folder
zip_list_files = zip_file__list
6 changes: 3 additions & 3 deletions tests/unit/testing/test_Temp_Zip_In_Memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from osbot_utils.utils.Files import file_exists, file_delete, file_extension, file_contents
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_file_list, zip_bytes_to_file, zip_bytes_get_file
from osbot_utils.utils.Zip import zip_file__list, zip_bytes_to_file, zip_bytes__get_file


class test_Temp_Zip_In_Memory(TestCase):
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_zip_bytes_file(self):
assert path_test_file in unzipped_folder.files()
assert file_contents(path_test_file) == new_file_contents

file_contents_from_bytes = zip_bytes_get_file(_.zip_bytes(), new_file_name)
file_contents_from_bytes = zip_bytes__get_file(_.zip_bytes(), new_file_name)
assert file_contents_from_bytes.decode() == new_file_contents

def test_create_zip_file(self):
Expand All @@ -96,7 +96,7 @@ def test_create_zip_file(self):
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 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 # delete the zip file

5 changes: 5 additions & 0 deletions tests/unit/utils/test_Env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from os.path import abspath
from unittest import TestCase

import pytest

from osbot_utils.testing.Temp_Folder import Temp_Folder

from osbot_utils.utils.Dev import pprint
Expand Down Expand Up @@ -57,6 +59,9 @@ def test_env_vars_list(self):
assert list_contains_list(env_vars_list(), ['PATH', 'HOME', 'PWD']) is True

def test_find_dotenv_file(self):
if find_dotenv_file(parent_folder(path='.', use_full_path=True)) is not None:
pytest.skip("this test can only be run when there are no .env files in the parent folders of the current folder")

assert find_dotenv_file() == file_full_path(test_Env.temp_env_file) # we should find the temp .env that was added to the current test folder
assert find_dotenv_file(parent_folder(path='.', use_full_path=True)) is None # there should be no .env paths anywere in the current parent path folders
with Temp_Folder() as folder_a:
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/utils/test_Files.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
file_contents_sha256, create_folder_in_parent, sub_folders, safe_file_name, files_find, is_file, is_folder, \
temp_filename, current_folder, folder_delete, all_parent_folders
from osbot_utils.utils.Misc import random_bytes, random_string, remove, bytes_md5, str_to_bytes, bytes_sha256
from osbot_utils.utils.Zip import zip_files, zip_file_list, unzip_file
from osbot_utils.utils.Zip import zip_files, zip_file__list, unzip_file


class test_Files(TestCase):
Expand Down Expand Up @@ -174,9 +174,9 @@ def test_folder_copy(self):
assert(len(folder_files(target_b)) == 2)

zipped_files = zip_files(target_a)
assert zip_file_list(zipped_files) == sorted([remove(file_a, folder_a + '/'),
remove(file_b, folder_a + '/'),
remove(file_c, folder_a + '/')])
assert zip_file__list(zipped_files) == sorted([remove(file_a, folder_a + '/'),
remove(file_b, folder_a + '/'),
remove(file_c, folder_a + '/')])

path_pattern = f'{folder_a}/**/*.*'
assert len(files_find(path_pattern)) == 8
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/utils/test_Zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from osbot_utils.utils.Files import file_contents, folder_files, temp_folder_with_temp_file, file_exists, \
file_extension, folder_exists, file_name
from osbot_utils.utils.Zip import zip_file_list, unzip_file, folder_zip
from osbot_utils.utils.Zip import zip_file__list, unzip_file, folder_zip


class test_Zip(TestCase):
Expand All @@ -29,7 +29,7 @@ def test_folder_zip(self):

assert file_contents(source_files[0]) == file_contents(target_files[0])

assert zip_file_list(zip_file) == ['temp_file.txt']
assert zip_file__list(zip_file) == ['temp_file.txt']

def test_zip_file_list(self):
with Temp_Folder() as temp_folder:
Expand Down

0 comments on commit e624b9f

Please sign in to comment.