atomicshop 3.4.2__py3-none-any.whl → 3.5.0__py3-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.

Potentially problematic release.


This version of atomicshop might be problematic. Click here for more details.

@@ -1,353 +0,0 @@
1
- import os
2
- import time
3
- import zipfile
4
- from io import BytesIO
5
- from typing import Union, Literal
6
-
7
- from .. import filesystem, print_api
8
-
9
-
10
- def is_zip_zipfile(file_object: Union[str, bytes]) -> bool:
11
- """
12
- Function checks if the file is a zip file.
13
- :param file_object: can be two types:
14
- string, full path to the file.
15
- bytes or BytesIO, the bytes of the file.
16
- :return: boolean.
17
- """
18
-
19
- try:
20
- if isinstance(file_object, bytes):
21
- with BytesIO(file_object) as file_object:
22
- with zipfile.ZipFile(file_object) as zip_object:
23
- zip_object.testzip()
24
- return True
25
- elif isinstance(file_object, str):
26
- with zipfile.ZipFile(file_object) as zip_object:
27
- zip_object.testzip()
28
- return True
29
- except zipfile.BadZipFile:
30
- return False
31
-
32
-
33
- def is_zip_magic_number(file_path: str) -> bool:
34
- """
35
- Function checks if the file is a zip file using magic number.
36
- :param file_path: string, full path to the file.
37
- :return: boolean.
38
-
39
- 50 4B 03 04: This is the most common signature, found at the beginning of a ZIP file.
40
- It signifies the start of a file within the ZIP archive and is present in almost all ZIP files.
41
- Each file within the ZIP archive starts with this signature.
42
- 50 4B 05 06: This is the end of central directory record signature.
43
- It's found at the end of a ZIP file and is essential for identifying the structure of the ZIP archive,
44
- especially in cases where the file is split or is a multipart archive.
45
- 50 4B 07 08: This signature is used for spanned ZIP archives (also known as split or multi-volume ZIP archives).
46
- It's found in the end of central directory locator for ZIP files that are split across multiple volumes.
47
- """
48
-
49
- with open(file_path, 'rb') as file:
50
- # Read the first 4 bytes of the file
51
- signature = file.read(4)
52
-
53
- # Check if the signature matches any of the ZIP signatures
54
- return signature in [b'PK\x03\x04', b'PK\x05\x06', b'PK\x07\x08']
55
-
56
-
57
- def extract_archive_with_zipfile(
58
- archive_path: str,
59
- extract_directory: str = None,
60
- files_without_directories: bool = False,
61
- remove_first_directory: bool = False,
62
- print_kwargs: dict = None
63
- ) -> str:
64
- """
65
- Function will extract the archive using standard library 'zipfile'.
66
- This method preserves original date and time of the files inside the archive.
67
-
68
- :param archive_path: string, full path to archived file.
69
- :param extract_directory: string, full path to directory that the files will be extracted to.
70
- If not specified, the files will be extracted to the same directory as the archived file, using the file name
71
- without extension as the directory name.
72
- :param files_without_directories: boolean, default 'False'.
73
- 'True': All the files in the archive will be extracted without subdirectories hierarchy.
74
- Meaning, that if there are duplicate file names, the latest file with the same file name will overwrite
75
- all the rest of the files with the same name.
76
- 'False': Subdirectory hierarchy will be preserved as it is currently in the archived file.
77
- :param remove_first_directory: boolean, default is 'False'.
78
- 'True': all the files will be extracted without first directory in the hierarchy.
79
- Example: package_some_name_1.1.1_build/subdir1/file.exe
80
- Will be extracted as: subdir/file.exe
81
- :param print_kwargs: dict, kwargs for print_api.
82
-
83
- :return: string, full path to directory that the files were extracted to.
84
- """
85
-
86
- if print_kwargs is None:
87
- print_kwargs = dict()
88
-
89
- # If 'extract_directory' is not specified, extract to the same directory as the archived file.
90
- if extract_directory is None:
91
- extract_directory = (
92
- filesystem.get_file_directory(archive_path) + os.sep +
93
- filesystem.get_file_name_without_extension(archive_path))
94
-
95
- print_api.print_api(f'Extracting to directory: {extract_directory}', **print_kwargs)
96
-
97
- # initiating the archived file path as 'zipfile.ZipFile' object.
98
- with zipfile.ZipFile(archive_path) as zip_object:
99
- # '.infolist()' method of the object contains all the directories and files that are in the archive including
100
- # information about each one, like date and time of archiving.
101
- for zip_info in zip_object.infolist():
102
- # '.filename' attribute of the 'infolist()' method is relative path to each directory and file.
103
- # If 'filename' ends with '/' it is a directory (it doesn't matter if it is windows or *nix)
104
- # If so, skip current iteration.
105
- if zip_info.filename[-1] == '/':
106
- continue
107
-
108
- if files_without_directories:
109
- # Put into 'filename' the string that contains only the filename without subdirectories.
110
- zip_info.filename = os.path.basename(zip_info.filename)
111
- elif remove_first_directory:
112
- # Cut the first directory from the filename.
113
- zip_info.filename = zip_info.filename.split('/', maxsplit=1)[1]
114
-
115
- print_api.print_api(f'Extracting: {zip_info.filename}', **print_kwargs)
116
-
117
- # Extract current file from the archive using 'zip_info' of the current file with 'filename' that we
118
- # updated under specified parameters to specified directory.
119
- zip_object.extract(zip_info, extract_directory)
120
-
121
- # === Change the date and time of extracted file from current time to the time specified in 'zip_info'.
122
- # Get full path to extracted file.
123
- extracted_file_path: str = extract_directory + os.sep + zip_info.filename
124
- # Create needed datetime object with original archived datetime from 'zip_info.date_time'.
125
- date_time = time.mktime(zip_info.date_time + (0, 0, -1))
126
- # Using 'os' library, changed the datetime of the file to the object created in previous step.
127
- os.utime(extracted_file_path, (date_time, date_time))
128
- print_api.print_api('Extraction done.', color="green", **print_kwargs)
129
-
130
- return extract_directory
131
-
132
-
133
- def get_file_list_from_zip(file_path: str) -> list:
134
- """
135
- Function returns the list of file names and their relative directories inside the zip file.
136
- :param file_path: string, full path to the zip file.
137
- :return: list of strings.
138
- """
139
-
140
- with zipfile.ZipFile(file_path, 'r') as zip_object:
141
- return zip_object.namelist()
142
-
143
-
144
- def archive_directory(
145
- directory_path: str,
146
- compression: Literal[
147
- 'store',
148
- 'deflate',
149
- 'bzip2',
150
- 'lzma'] = 'deflate',
151
- include_root_directory: bool = True,
152
- remove_original: bool = False
153
- ) -> str:
154
- """
155
- Function archives the directory.
156
- :param directory_path: string, full path to the directory.
157
- :param compression: string, default is 'deflate'.
158
- 'store': No compression.
159
- 'deflate': Standard ZIP compression.
160
- 'bzip2': BZIP2 compression.
161
- Provides better compression than Deflate but is typically slower. This method might not be supported by
162
- all ZIP utilities.
163
- 'lzma': LZMA compression.
164
- high compression ratios but is also slower compared to Deflate. This method is less commonly used and
165
- may not be supported by all ZIP utilities.
166
- :param include_root_directory: boolean, default is 'True'.
167
- 'True': The root directory will be included in the archive.
168
- 'False': The root directory will not be included in the archive.
169
- True is usually the case in most archiving utilities.
170
- :param remove_original: boolean, default is 'False'. If 'True', the original directory will be removed.
171
- :return: string, full path to the archived file.
172
- """
173
-
174
- if compression == 'store':
175
- compression_method = zipfile.ZIP_STORED
176
- elif compression == 'deflate':
177
- compression_method = zipfile.ZIP_DEFLATED
178
- elif compression == 'bzip2':
179
- compression_method = zipfile.ZIP_BZIP2
180
- elif compression == 'lzma':
181
- compression_method = zipfile.ZIP_LZMA
182
- else:
183
- raise ValueError(f"Unsupported compression method: {compression}")
184
-
185
- archive_path: str = directory_path + '.zip'
186
- with zipfile.ZipFile(archive_path, 'w', compression_method) as zip_object:
187
- for root, _, files in os.walk(directory_path):
188
- for file in files:
189
- file_path = os.path.join(root, file)
190
-
191
- # If including the root directory, use the relative path from the parent directory of the root
192
- if include_root_directory:
193
- arcname = os.path.relpath(file_path, os.path.dirname(directory_path))
194
- else:
195
- arcname = os.path.relpath(file_path, directory_path)
196
-
197
- zip_object.write(file_path, arcname)
198
-
199
- if remove_original:
200
- filesystem.remove_directory(directory_path)
201
-
202
- return archive_path
203
-
204
-
205
- # def search_file_in_zip(
206
- # file_path: str = None,
207
- # file_bytes: bytes = None,
208
- # file_names_to_search: list[str] = None,
209
- # case_sensitive: bool = True,
210
- # return_first_only: bool = False,
211
- # return_empty_list_per_file_name: bool = False,
212
- # recursive: bool = False,
213
- # callback_functions: list = None,
214
- # extract_file_to_path: str = None
215
- # ) -> dict[str, list[bytes]]:
216
- # """
217
- # Function searches for the file names inside the zip file and returns a dictionary where the keys are the
218
- # names of the callback functions and the values are lists of found file bytes.
219
- # :param file_path: string, full path to the zip file.
220
- # :param file_bytes: bytes, the bytes of the zip file.
221
- # :param file_names_to_search: list of strings, the names of the files to search.
222
- # :param case_sensitive: boolean, default is 'True'. Determines if file name search should be case sensitive.
223
- # :param return_first_only: boolean, default is 'False'. Return only the first found file for each file name.
224
- # :param return_empty_list_per_file_name: boolean, default is 'False'.
225
- # True: Return empty list for each file name that wasn't found.
226
- # False: Don't return empty list for each file name that wasn't found.
227
- # :param recursive: boolean, default is 'False'. If True, search for file names recursively in nested zip files.
228
- # :param callback_functions: list of callables, default is None. Each function takes a file name and should return a
229
- # boolean that will tell the main function if this file is 'found' or not.
230
- # :param extract_file_to_path: string, full path to the directory where the found files should be extracted.
231
- # :return: dictionary of lists of bytes.
232
- # """
233
- #
234
- # def get_unique_filename(directory, filename):
235
- # """
236
- # Generates a unique filename by appending a number if the file already exists.
237
- # """
238
- # name, ext = os.path.splitext(filename)
239
- # counter = 1
240
- # unique_filename = filename
241
- # while os.path.exists(os.path.join(directory, unique_filename)):
242
- # unique_filename = f"{name}_{counter}{ext}"
243
- # counter += 1
244
- # return unique_filename
245
- #
246
- # def is_zip_file(file, zip_obj):
247
- # try:
248
- # with zip_obj.open(file) as file_data:
249
- # with zipfile.ZipFile(BytesIO(file_data.read())) as zip_file:
250
- # if zip_file.testzip() is None: # No errors found
251
- # return True
252
- # except zipfile.BadZipFile:
253
- # return False
254
- # return False
255
- #
256
- # def match_file_name(target, current):
257
- # if case_sensitive:
258
- # return current.endswith(target)
259
- # else:
260
- # return current.lower().endswith(target.lower())
261
- #
262
- # def search_in_zip(zip_obj, file_names, results, found_set):
263
- # for item in zip_obj.infolist():
264
- # if item.filename.endswith('/'): # Skip directories
265
- # continue
266
- # is_nested_zip = recursive and is_zip_file(item.filename, zip_obj)
267
- #
268
- # with zip_obj.open(item) as file_data:
269
- # archived_file_bytes = file_data.read()
270
- #
271
- # # This is needed to know if the file should be extracted to directory or not.
272
- # should_extract = False
273
- #
274
- # name_matched = False
275
- # if file_names is not None:
276
- # name_matched = any(match_file_name(file_name, item.filename) for file_name in file_names)
277
- # if name_matched:
278
- # should_extract = True
279
- #
280
- # callback_matched = False
281
- # if callback_functions:
282
- # for callback in callback_functions:
283
- # callback_result = callback(archived_file_bytes)
284
- # if callback_result:
285
- # callback_matched = True
286
- # # Initialize key for callback function name if not present
287
- # if callback.__name__ not in results:
288
- # results[callback.__name__] = []
289
- # file_info = {
290
- # 'bytes': archived_file_bytes,
291
- # 'name': item.filename,
292
- # 'size': item.file_size,
293
- # 'modified_time': item.date_time
294
- # }
295
- # results[callback.__name__].append(file_info)
296
- # if return_first_only:
297
- # found_set.add(item.filename)
298
- #
299
- # should_extract = True
300
- # break # Stop checking other callbacks if one has found it
301
- #
302
- # if should_extract and extract_file_to_path:
303
- # unique_filename = get_unique_filename(extract_file_to_path, os.path.basename(item.filename))
304
- # with open(os.path.join(extract_file_to_path, unique_filename), 'wb') as f:
305
- # f.write(archived_file_bytes)
306
- #
307
- # if not callback_matched:
308
- # if is_nested_zip:
309
- # # If the file is a nested ZIP and hasn't matched a callback, search recursively
310
- # nested_zip_bytes = BytesIO(archived_file_bytes)
311
- # with zipfile.ZipFile(nested_zip_bytes) as nested_zip:
312
- # search_in_zip(nested_zip, file_names, results, found_set)
313
- # elif name_matched:
314
- # # Handle name match when no callbacks are provided or no callback matched
315
- # if item.filename not in results:
316
- # results[item.filename] = []
317
- # file_info = {
318
- # 'bytes': archived_file_bytes,
319
- # 'name': item.filename,
320
- # 'size': item.file_size,
321
- # 'modified_time': item.date_time
322
- # }
323
- # results[item.filename].append(file_info)
324
- # if return_first_only:
325
- # found_set.add(item.filename) # Mark as found
326
- #
327
- # if file_names is not None and len(found_set) == len(file_names):
328
- # return # All files found, stop searching
329
- #
330
- # if file_names_to_search is None and callback_functions is None:
331
- # raise ValueError("Either file_names_to_search or callback_functions must be provided.")
332
- #
333
- # # Initialize results dictionary.
334
- # if callback_functions:
335
- # results = {callback.__name__: [] for callback in callback_functions}
336
- # else:
337
- # results = {}
338
- #
339
- # found_set = set()
340
- # if file_bytes is not None:
341
- # with zipfile.ZipFile(BytesIO(file_bytes), 'r') as zip_ref:
342
- # search_in_zip(zip_ref, file_names_to_search, results, found_set)
343
- # elif file_path is not None:
344
- # with zipfile.ZipFile(file_path, 'r') as zip_ref:
345
- # search_in_zip(zip_ref, file_names_to_search, results, found_set)
346
- # else:
347
- # raise ValueError("Either file_path or file_bytes must be provided.")
348
- #
349
- # if not return_empty_list_per_file_name:
350
- # # Filter out keys with empty lists
351
- # results = {key: value for key, value in results.items() if value}
352
- #
353
- # return results
atomicshop/file_types.py DELETED
@@ -1,24 +0,0 @@
1
- from typing import Union
2
-
3
- import magic
4
-
5
-
6
- def get_mime_type(file_object: Union[str, bytes]):
7
- """
8
- Determine the MIME type of the given input.
9
- The input can be a file path (string) or a bytes object.
10
-
11
- :param file_object: File path as a string or bytes object.
12
- :return: MIME type as a string.
13
- """
14
- mime = magic.Magic(mime=True)
15
-
16
- # Check if input is a file path (str) or bytes
17
- if isinstance(file_object, str):
18
- # Assuming input_data is a file path
19
- return mime.from_file(file_object)
20
- elif isinstance(file_object, bytes):
21
- # Assuming input_data is bytes
22
- return mime.from_buffer(file_object)
23
- else:
24
- raise TypeError("Input must be a file path (str) or bytes object.")