Unverified Commit 22f1745e by Jörg Stucke Committed by GitHub

Merge pull request #6 from maringuu/pathlib

Use pathlib
parents 86686b18 3909ba50
from .fail_safe_file_operations import (
get_binary_from_file, get_string_list_from_file, write_binary_to_file, get_safe_name, delete_file, get_files_in_dir,
get_dirs_in_dir, create_symlink, get_dir_of_file, safe_rglob
)
from .file_functions import read_in_chunks, get_directory_for_filename, create_dir_for_file, human_readable_file_size
from .fail_safe_file_operations import (create_symlink, delete_file,
get_binary_from_file, get_dir_of_file,
get_dirs_in_dir, get_files_in_dir,
get_safe_name,
get_string_list_from_file, safe_rglob,
write_binary_to_file)
from .file_functions import (create_dir_for_file, get_directory_for_filename,
human_readable_file_size, read_in_chunks)
from .git_functions import get_version_string_from_git
__all__ = [
......
import os
import io
from pathlib import Path
from typing import Type, Union
import bitmath
def read_in_chunks(file_object, chunk_size=1024):
def read_in_chunks(file_object: Type[io.BufferedReader], chunk_size=1024) -> bytes:
'''
Helper function to read large file objects iteratively in smaller chunks. Can be used like this::
......@@ -12,7 +15,6 @@ def read_in_chunks(file_object, chunk_size=1024):
:param file_object: The file object from which the chunk data is read. Must be a subclass of ``io.BufferedReader``.
:param chunk_size: Number of bytes to read per chunk.
:type chunk_size: int
:return: Returns a generator to iterate over all chunks, see above for usage.
'''
while True:
......@@ -22,35 +24,36 @@ def read_in_chunks(file_object, chunk_size=1024):
yield data
def get_directory_for_filename(filename):
def get_directory_for_filename(filename: Union[str, Path]) -> str:
'''
Convenience function which returns the absolute path to the directory that contains the given file name.
:param filename: Path of the file. Can be absolute or relative to the current directory.
:type filename: str
:return: Absolute path of the directory
.. deprecated::
You should use pathlib instead of this function.
'''
return os.path.dirname(os.path.abspath(filename))
return str(Path(filename).resolve().parent)
def create_dir_for_file(file_path):
def create_dir_for_file(file_path: Union[str, Path]) -> None:
'''
Creates all directories of file path. File path may include the file as well.
:param file_path: Path of the file. Can be absolute or relative to the current directory.
:type file_path: str
:return: None
.. deprecated::
You should use pathlib instead of this function.
'''
directory = os.path.dirname(os.path.abspath(file_path))
os.makedirs(directory, exist_ok=True)
Path(file_path).resolve().parent.mkdir(parents=True, exist_ok=True)
def human_readable_file_size(size_in_bytes):
def human_readable_file_size(size_in_bytes: int) -> str:
'''
Returns a nicly human readable file size
Returns a nicely human readable file size
:param size_in_bytes: Size in Bytes
:type size_in_bytes: int
:return: str
'''
return bitmath.Byte(bytes=size_in_bytes).best_prefix().format('{value:.2f} {unit}')
import subprocess
def get_version_string_from_git(directory_name):
def get_version_string_from_git(directory_name: str) -> str:
return subprocess.check_output(['git', 'describe', '--always'], cwd=directory_name).strip().decode('utf-8')
from setuptools import setup, find_packages
VERSION = '0.2.3'
VERSION = '0.3.0'
setup(
name='common_helper_files',
......
......@@ -8,7 +8,7 @@ from common_helper_files import (
create_symlink, delete_file, get_safe_name, get_binary_from_file, get_dir_of_file, get_directory_for_filename,
get_dirs_in_dir, get_files_in_dir, get_string_list_from_file, safe_rglob, write_binary_to_file
)
from common_helper_files.fail_safe_file_operations import _get_counted_file_path, _rm_cr
from common_helper_files.fail_safe_file_operations import _get_counted_file_path
TEST_DATA_DIR = Path(__file__).absolute().parent / 'data'
EMPTY_FOLDER = TEST_DATA_DIR / 'empty_folder'
......@@ -188,11 +188,3 @@ def test_safe_rglob_empty_dir():
assert EMPTY_FOLDER.exists()
result = safe_rglob(EMPTY_FOLDER)
assert len(list(result)) == 0
@pytest.mark.parametrize('input_data, expected', [
('abc', 'abc'),
('ab\r\nc', 'ab\nc'),
])
def test_rm_cr(input_data, expected):
assert _rm_cr(input_data) == expected
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment