EOX GitLab Instance

Skip to content
Snippets Groups Projects
Commit 9d36c491 authored by Fabian Schindler's avatar Fabian Schindler
Browse files

Adding pycache files to gitignore

parent 37eb8d37
No related branches found
No related tags found
No related merge requests found
/data /data
*.pyc
**/__pycache__
\ No newline at end of file
from os import PathLike from os import PathLike
import os.path import os.path
import io
from typing import List, Union, BinaryIO from typing import List, Union, BinaryIO
import tarfile import tarfile
import zipfile import zipfile
...@@ -35,12 +36,12 @@ def is_tarfile(archive_file: Union[PathLike, BinaryIO]) -> bool: ...@@ -35,12 +36,12 @@ def is_tarfile(archive_file: Union[PathLike, BinaryIO]) -> bool:
def open_tarfile(archive_file: Union[PathLike, BinaryIO]) -> tarfile.TarFile: def open_tarfile(archive_file: Union[PathLike, BinaryIO]) -> tarfile.TarFile:
""" Open a TAR file from either a path or a file object. """ Open a TAR file from either a path or a file object.
""" """
if isinstance(archive_file, BinaryIO): if isinstance(archive_file, (BinaryIO, io.BufferedReader)):
return tarfile.open(fileobj=archive_file) return tarfile.open(fileobj=archive_file)
return tarfile.open(archive_file) return tarfile.open(archive_file)
def unpack_files(archive_path: Union[PathLike, BinaryIO] , target_dir: PathLike, glob=None, filenames=None, recursive=False) -> List[PathLike]: def unpack_files(archive_path: Union[PathLike, BinaryIO], target_dir: PathLike, glob=None, filenames=None, recursive=False) -> List[PathLike]:
""" Unpacks the contents of the specified ZIP or TAR archive to the """ Unpacks the contents of the specified ZIP or TAR archive to the
given target directory. Optionally, only a given list of filenames given target directory. Optionally, only a given list of filenames
will be extracted. will be extracted.
...@@ -49,6 +50,7 @@ def unpack_files(archive_path: Union[PathLike, BinaryIO] , target_dir: PathLike, ...@@ -49,6 +50,7 @@ def unpack_files(archive_path: Union[PathLike, BinaryIO] , target_dir: PathLike,
""" """
iszip = False iszip = False
istar = False istar = False
# open the archive and extract a list of filenames # open the archive and extract a list of filenames
if is_tarfile(archive_path): if is_tarfile(archive_path):
archive = open_tarfile(archive_path) archive = open_tarfile(archive_path)
...@@ -94,21 +96,27 @@ def unpack_files(archive_path: Union[PathLike, BinaryIO] , target_dir: PathLike, ...@@ -94,21 +96,27 @@ def unpack_files(archive_path: Union[PathLike, BinaryIO] , target_dir: PathLike,
for extension in ARCHIVE_EXTENSIONS: for extension in ARCHIVE_EXTENSIONS:
sub_archives = filter_filenames(all_filenames, '*.%s' % extension) sub_archives = filter_filenames(all_filenames, '*.%s' % extension)
for sub_archive in sub_archives: for sub_archive in sub_archives:
sub_archive_filename = os.path.join(
os.path.dirname(archive_path),
os.path.basename(sub_archive),
)
if istar: if istar:
sub_archive_file = archive.extractfile( archive.extract(
archive.getmember(sub_archive) archive.getmember(sub_archive)
) )
os.rename(sub_archive, sub_archive_filename)
if iszip: if iszip:
sub_archive_file = archive.open(sub_archive) archive.extract(sub_archive)
os.rename(sub_archive, sub_archive_filename)
sub_filenames = unpack_files( sub_filenames = unpack_files(
sub_archive_file, sub_archive_filename,
os.path.join(target_dir, sub_archive), os.path.join(target_dir, sub_archive),
glob, glob,
filenames, filenames,
recursive recursive,
) )
extracted_filenames.extend(sub_filenames) extracted_filenames.extend(sub_filenames)
# return a list of files extracted # return a list of files extracted
return extracted_filenames return extracted_filenames
\ No newline at end of file
...@@ -2,5 +2,5 @@ import os ...@@ -2,5 +2,5 @@ import os
from os.path import splitext from os.path import splitext
def replace_ext(filename: os.PathLike, new_ext: str) -> os.PathLike: def replace_ext(filename: os.PathLike, new_ext: str, force_dot: bool=True) -> os.PathLike:
return splitext(filename)[0] + ('' if new_ext.startswith('.') else '.') + new_ext return splitext(filename)[0] + ('' if new_ext.startswith('.') or not force_dot else '.') + new_ext
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment