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
cdea1cb8
Commit
cdea1cb8
authored
7 years ago
by
Peter Weidenbach
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get list of lines from text file functionality added
parent
8720d5a7
master
…
2.2
0.3.0
0.2.3
0.2.2
0.2
0.1.9
0.1.8
0.1.7
0.1.6
No related merge requests found
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
4 deletions
+34
-4
__init__.py
common_helper_files/__init__.py
+3
-2
fail_safe_file_operations.py
common_helper_files/fail_safe_file_operations.py
+18
-0
multiline_test.txt
tests/data/multiline_test.txt
+6
-0
test_fail_safe_file_operations.py
tests/test_fail_safe_file_operations.py
+7
-2
No files found.
common_helper_files/__init__.py
View file @
cdea1cb8
from
.file_functions
import
read_in_chunks
,
get_directory_for_filename
,
create_dir_for_file
,
human_readable_file_size
from
.git_functions
import
get_version_string_from_git
from
.hash_functions
import
md5sum
from
.fail_safe_file_operations
import
get_binary_from_file
,
write_binary_to_file
,
get_safe_name
,
delete_file
,
get_files_in_dir
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
from
.config_functions
import
update_config_from_env
__version__
=
'0.1.
5
'
__version__
=
'0.1.
6
'
__all__
=
[
'get_directory_for_filename'
,
...
...
@@ -14,6 +14,7 @@ __all__ = [
'get_version_string_from_git'
,
'md5sum'
,
'get_binary_from_file'
,
'get_string_list_from_file'
,
'write_binary_to_file'
,
'get_safe_name'
,
'delete_file'
,
...
...
This diff is collapsed.
Click to expand it.
common_helper_files/fail_safe_file_operations.py
View file @
cdea1cb8
...
...
@@ -27,6 +27,24 @@ def get_binary_from_file(file_path):
return
binary
def
get_string_list_from_file
(
file_path
):
"""
Fail-safe file read operation returning a list of text strings.
Errors are logged. No exception raised.
:param file_path: Path of the file. Can be absolute or relative to the current directory.
:type file_path: str
:return: file's content as text string list; returns empty list on error
"""
try
:
raw
=
get_binary_from_file
(
file_path
)
string
=
raw
.
decode
(
encoding
=
'utf_8'
,
errors
=
'replace'
)
return
string
.
split
(
"
\n
"
)
except
Exception
as
e
:
logging
.
error
(
"Could not read file: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
return
[]
def
write_binary_to_file
(
file_binary
,
file_path
,
overwrite
=
False
,
file_copy
=
False
):
"""
Fail-safe file write operation. Creates directories if needed.
...
...
This diff is collapsed.
Click to expand it.
tests/data/multiline_test.txt
0 → 100644
View file @
cdea1cb8
first line
second line
thÿrd line
first line
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tests/test_fail_safe_file_operations.py
View file @
cdea1cb8
...
...
@@ -4,7 +4,7 @@ from tempfile import TemporaryDirectory
from
common_helper_files.fail_safe_file_operations
import
get_binary_from_file
,
\
write_binary_to_file
,
delete_file
,
get_safe_name
,
_get_counted_file_path
,
\
get_files_in_dir
get_files_in_dir
,
get_string_list_from_file
from
common_helper_files.file_functions
import
get_directory_for_filename
...
...
@@ -29,6 +29,11 @@ class Test_FailSafeFileOperations(unittest.TestCase):
file_binary
=
get_binary_from_file
(
none_existing_file_path
)
self
.
assertEqual
(
file_binary
,
b
''
,
"content not correct"
)
def
test_fail_safe_read_file_string_list
(
self
):
test_file_path
=
os
.
path
.
join
(
self
.
get_directory_of_current_file
(),
"data"
,
"multiline_test.txt"
)
lines
=
get_string_list_from_file
(
test_file_path
)
self
.
assertEqual
(
lines
,
[
'first line'
,
'second line'
,
'th
\ufffd
rd line'
,
''
,
'first line'
],
"lines not correct"
)
def
test_fail_safe_write_file
(
self
):
file_path
=
os
.
path
.
join
(
self
.
tmp_dir
.
name
,
"test_folder"
,
"test_file"
)
write_binary_to_file
(
b
'this is a test'
,
file_path
)
...
...
@@ -76,7 +81,7 @@ class Test_FailSafeFileOperations(unittest.TestCase):
result
=
get_files_in_dir
(
test_dir_path
)
self
.
assertIn
(
os
.
path
.
join
(
test_dir_path
,
"read_test"
),
result
,
"file in root folder not found"
)
self
.
assertIn
(
os
.
path
.
join
(
test_dir_path
,
"test_folder/generic_test_file"
),
result
,
"file in sub folder not found"
)
self
.
assertEqual
(
len
(
result
),
2
,
"number of found files not correct"
)
self
.
assertEqual
(
len
(
result
),
3
,
"number of found files not correct"
)
def
test_get_files_in_dir_error
(
self
):
result
=
get_files_in_dir
(
"/none_existing/dir"
)
...
...
This diff is collapsed.
Click to expand it.
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