Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common_helper_files
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
common_helper_files
Commits
22f1745e
Unverified
Commit
22f1745e
authored
Dec 10, 2021
by
Jörg Stucke
Committed by
GitHub
Dec 10, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #6 from maringuu/pathlib
Use pathlib
parents
86686b18
3909ba50
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
80 additions
and
113 deletions
+80
-113
__init__.py
common_helper_files/__init__.py
+8
-5
fail_safe_file_operations.py
common_helper_files/fail_safe_file_operations.py
+51
-82
file_functions.py
common_helper_files/file_functions.py
+18
-15
git_functions.py
common_helper_files/git_functions.py
+1
-1
setup.py
setup.py
+1
-1
test_fail_safe_file_operations.py
tests/test_fail_safe_file_operations.py
+1
-9
No files found.
common_helper_files/__init__.py
View file @
22f1745e
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__
=
[
...
...
common_helper_files/fail_safe_file_operations.py
View file @
22f1745e
This diff is collapsed.
Click to expand it.
common_helper_files/file_functions.py
View file @
22f1745e
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 nic
e
ly 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}'
)
common_helper_files/git_functions.py
View file @
22f1745e
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'
)
setup.py
View file @
22f1745e
from
setuptools
import
setup
,
find_packages
VERSION
=
'0.
2.3
'
VERSION
=
'0.
3.0
'
setup
(
name
=
'common_helper_files'
,
...
...
tests/test_fail_safe_file_operations.py
View file @
22f1745e
...
...
@@ -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\n
c'
,
'ab
\n
c'
),
])
def
test_rm_cr
(
input_data
,
expected
):
assert
_rm_cr
(
input_data
)
==
expected
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment