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
8edd3d5a
Commit
8edd3d5a
authored
7 years ago
by
Peter Weidenbach
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
unecassary import removed
parent
14671679
master
…
2.2
0.3.0
0.2.3
0.2.2
No related merge requests found
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
36 deletions
+35
-36
fail_safe_file_operations.py
common_helper_files/fail_safe_file_operations.py
+35
-36
No files found.
common_helper_files/fail_safe_file_operations.py
View file @
8edd3d5a
...
...
@@ -2,52 +2,51 @@ import logging
import
os
import
re
import
sys
from
builtins
import
FileExistsError
from
.file_functions
import
create_dir_for_file
def
get_binary_from_file
(
file_path
):
"""
'''
Fail-safe file read operation. Symbolic links are converted to text files including the link.
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 binary as bytes; returns empty byte string on error
"""
'''
try
:
if
os
.
path
.
islink
(
file_path
):
binary
=
"symbolic link -> {}"
.
format
(
os
.
readlink
(
file_path
))
binary
=
'symbolic link -> {}'
.
format
(
os
.
readlink
(
file_path
))
else
:
with
open
(
file_path
,
'rb'
)
as
f
:
binary
=
f
.
read
()
except
Exception
as
e
:
logging
.
error
(
"Could not read file: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
logging
.
error
(
'Could not read file: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
binary
=
b
''
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
"
)
return
string
.
split
(
'
\n
'
)
except
Exception
as
e
:
logging
.
error
(
"Could not read file: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
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.
Errors are logged. No exception raised.
...
...
@@ -62,7 +61,7 @@ def write_binary_to_file(file_binary, file_path, overwrite=False, file_copy=Fals
:type file_copy: bool
:default file_copy: False
:return: None
"""
'''
try
:
create_dir_for_file
(
file_path
)
if
not
os
.
path
.
exists
(
file_path
)
or
overwrite
:
...
...
@@ -71,7 +70,7 @@ def write_binary_to_file(file_binary, file_path, overwrite=False, file_copy=Fals
new_path
=
_get_counted_file_path
(
file_path
)
_write_file
(
new_path
,
file_binary
)
except
Exception
as
e
:
logging
.
error
(
"Could not write file: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
logging
.
error
(
'Could not write file: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
def
_write_file
(
file_path
,
binary
):
...
...
@@ -80,32 +79,32 @@ def _write_file(file_path, binary):
def
_get_counted_file_path
(
original_path
):
tmp
=
re
.
search
(
r
"-([0-9]+)\Z"
,
original_path
)
tmp
=
re
.
search
(
r
'-([0-9]+)\Z'
,
original_path
)
if
tmp
is
not
None
:
current_count
=
int
(
tmp
.
group
(
1
))
new_file_path
=
re
.
sub
(
r
"-[0-9]+\Z"
,
"-{}"
.
format
(
current_count
+
1
),
original_path
)
new_file_path
=
re
.
sub
(
r
'-[0-9]+\Z'
,
'-{}'
.
format
(
current_count
+
1
),
original_path
)
else
:
new_file_path
=
"{}-1"
.
format
(
original_path
)
new_file_path
=
'{}-1'
.
format
(
original_path
)
return
new_file_path
def
delete_file
(
file_path
):
"""
'''
Fail-safe delete file operation. Deletes a file if it exists.
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: None
"""
'''
try
:
os
.
unlink
(
file_path
)
except
Exception
as
e
:
logging
.
error
(
"Could not delete file: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
logging
.
error
(
'Could not delete file: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
def
create_symlink
(
src_path
,
dst_path
):
"""
'''
Fail-safe symlink operation. Symlinks a file if dest does not exist.
Errors are logged. No exception raised.
...
...
@@ -114,18 +113,18 @@ def create_symlink(src_path, dst_path):
:param dst_path: link location
:type dst_path: str
:return: None
"""
'''
try
:
create_dir_for_file
(
dst_path
)
os
.
symlink
(
src_path
,
dst_path
)
except
FileExistsError
as
e
:
logging
.
debug
(
"Could not create Link: File exists: {}"
.
format
(
e
))
logging
.
debug
(
'Could not create Link: File exists: {}'
.
format
(
e
))
except
Exception
as
e
:
logging
.
error
(
"Could not create link: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
logging
.
error
(
'Could not create link: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
def
get_safe_name
(
file_name
,
max_size
=
200
,
valid_characters
=
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+. '
):
"""
'''
removes all problematic characters from a file name
cuts file names if they are too long
...
...
@@ -138,42 +137,42 @@ def get_safe_name(file_name, max_size=200, valid_characters='abcdefghijklmnopqrs
:type valid_characters: str
:default valid_characters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+. '
:return: str
"""
'''
allowed_charachters
=
set
(
valid_characters
)
safe_name
=
filter
(
lambda
x
:
x
in
allowed_charachters
,
file_name
)
safe_name
=
""
.
join
(
safe_name
)
safe_name
=
safe_name
.
replace
(
" "
,
"_"
)
safe_name
=
''
.
join
(
safe_name
)
safe_name
=
safe_name
.
replace
(
' '
,
'_'
)
if
len
(
safe_name
)
>
max_size
:
safe_name
=
safe_name
[
0
:
max_size
]
return
safe_name
def
get_files_in_dir
(
directory_path
):
"""
'''
Returns a list with the absolute paths of all files in the directory directory_path
:param directory_path: directory including files
:type directory_path: str
:return: list
"""
'''
result
=
[]
try
:
for
file_path
,
_
,
files
in
os
.
walk
(
directory_path
):
for
file_
in
files
:
result
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
file_path
,
file_
)))
except
Exception
as
e
:
logging
.
error
(
"Could not get files: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
logging
.
error
(
'Could not get files: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
return
result
def
get_dirs_in_dir
(
directory_path
):
"""
'''
Returns a list with the absolute paths of all 1st level sub-directories in the directory directory_path.
:param directory_path: directory including sub-directories
:type directory_path: str
:return: list
"""
'''
result
=
[]
try
:
dir_content
=
os
.
listdir
(
directory_path
)
...
...
@@ -182,20 +181,20 @@ def get_dirs_in_dir(directory_path):
if
os
.
path
.
isdir
(
dir_path
):
result
.
append
(
dir_path
)
except
Exception
as
e
:
logging
.
error
(
"Could not get directories: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
logging
.
error
(
'Could not get directories: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
return
result
def
get_dir_of_file
(
file_path
):
"""
'''
Returns absolute path of the directory including file
:param file_path: Paht of the file
:type: paht-like object
:return: string
"""
'''
try
:
return
os
.
path
.
dirname
(
os
.
path
.
abspath
(
file_path
))
except
Exception
as
e
:
logging
.
error
(
"Could not get directory path: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
return
"/"
logging
.
error
(
'Could not get directory path: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
return
'/'
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