Commit 8edd3d5a by Peter Weidenbach

unecassary import removed

parent 14671679
...@@ -2,52 +2,51 @@ import logging ...@@ -2,52 +2,51 @@ import logging
import os import os
import re import re
import sys import sys
from builtins import FileExistsError
from .file_functions import create_dir_for_file from .file_functions import create_dir_for_file
def get_binary_from_file(file_path): def get_binary_from_file(file_path):
""" '''
Fail-safe file read operation. Symbolic links are converted to text files including the link. Fail-safe file read operation. Symbolic links are converted to text files including the link.
Errors are logged. No exception raised. Errors are logged. No exception raised.
:param file_path: Path of the file. Can be absolute or relative to the current directory. :param file_path: Path of the file. Can be absolute or relative to the current directory.
:type file_path: str :type file_path: str
:return: file's binary as bytes; returns empty byte string on error :return: file's binary as bytes; returns empty byte string on error
""" '''
try: try:
if os.path.islink(file_path): if os.path.islink(file_path):
binary = "symbolic link -> {}".format(os.readlink(file_path)) binary = 'symbolic link -> {}'.format(os.readlink(file_path))
else: else:
with open(file_path, 'rb') as f: with open(file_path, 'rb') as f:
binary = f.read() binary = f.read()
except Exception as e: 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'' binary = b''
return binary return binary
def get_string_list_from_file(file_path): def get_string_list_from_file(file_path):
""" '''
Fail-safe file read operation returning a list of text strings. Fail-safe file read operation returning a list of text strings.
Errors are logged. No exception raised. Errors are logged. No exception raised.
:param file_path: Path of the file. Can be absolute or relative to the current directory. :param file_path: Path of the file. Can be absolute or relative to the current directory.
:type file_path: str :type file_path: str
:return: file's content as text string list; returns empty list on error :return: file's content as text string list; returns empty list on error
""" '''
try: try:
raw = get_binary_from_file(file_path) raw = get_binary_from_file(file_path)
string = raw.decode(encoding='utf_8', errors='replace') string = raw.decode(encoding='utf_8', errors='replace')
return string.split("\n") return string.split('\n')
except Exception as e: 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 [] return []
def write_binary_to_file(file_binary, file_path, overwrite=False, file_copy=False): def write_binary_to_file(file_binary, file_path, overwrite=False, file_copy=False):
""" '''
Fail-safe file write operation. Creates directories if needed. Fail-safe file write operation. Creates directories if needed.
Errors are logged. No exception raised. Errors are logged. No exception raised.
...@@ -62,7 +61,7 @@ def write_binary_to_file(file_binary, file_path, overwrite=False, file_copy=Fals ...@@ -62,7 +61,7 @@ def write_binary_to_file(file_binary, file_path, overwrite=False, file_copy=Fals
:type file_copy: bool :type file_copy: bool
:default file_copy: False :default file_copy: False
:return: None :return: None
""" '''
try: try:
create_dir_for_file(file_path) create_dir_for_file(file_path)
if not os.path.exists(file_path) or overwrite: 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 ...@@ -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) new_path = _get_counted_file_path(file_path)
_write_file(new_path, file_binary) _write_file(new_path, file_binary)
except Exception as e: 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): def _write_file(file_path, binary):
...@@ -80,32 +79,32 @@ def _write_file(file_path, binary): ...@@ -80,32 +79,32 @@ def _write_file(file_path, binary):
def _get_counted_file_path(original_path): 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: if tmp is not None:
current_count = int(tmp.group(1)) 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: else:
new_file_path = "{}-1".format(original_path) new_file_path = '{}-1'.format(original_path)
return new_file_path return new_file_path
def delete_file(file_path): def delete_file(file_path):
""" '''
Fail-safe delete file operation. Deletes a file if it exists. Fail-safe delete file operation. Deletes a file if it exists.
Errors are logged. No exception raised. Errors are logged. No exception raised.
:param file_path: Path of the file. Can be absolute or relative to the current directory. :param file_path: Path of the file. Can be absolute or relative to the current directory.
:type file_path: str :type file_path: str
:return: None :return: None
""" '''
try: try:
os.unlink(file_path) os.unlink(file_path)
except Exception as e: 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): def create_symlink(src_path, dst_path):
""" '''
Fail-safe symlink operation. Symlinks a file if dest does not exist. Fail-safe symlink operation. Symlinks a file if dest does not exist.
Errors are logged. No exception raised. Errors are logged. No exception raised.
...@@ -114,18 +113,18 @@ def create_symlink(src_path, dst_path): ...@@ -114,18 +113,18 @@ def create_symlink(src_path, dst_path):
:param dst_path: link location :param dst_path: link location
:type dst_path: str :type dst_path: str
:return: None :return: None
""" '''
try: try:
create_dir_for_file(dst_path) create_dir_for_file(dst_path)
os.symlink(src_path, dst_path) os.symlink(src_path, dst_path)
except FileExistsError as e: 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: 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-_+. '): def get_safe_name(file_name, max_size=200, valid_characters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+. '):
""" '''
removes all problematic characters from a file name removes all problematic characters from a file name
cuts file names if they are too long cuts file names if they are too long
...@@ -138,42 +137,42 @@ def get_safe_name(file_name, max_size=200, valid_characters='abcdefghijklmnopqrs ...@@ -138,42 +137,42 @@ def get_safe_name(file_name, max_size=200, valid_characters='abcdefghijklmnopqrs
:type valid_characters: str :type valid_characters: str
:default valid_characters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+. ' :default valid_characters: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+. '
:return: str :return: str
""" '''
allowed_charachters = set(valid_characters) allowed_charachters = set(valid_characters)
safe_name = filter(lambda x: x in allowed_charachters, file_name) safe_name = filter(lambda x: x in allowed_charachters, file_name)
safe_name = "".join(safe_name) safe_name = ''.join(safe_name)
safe_name = safe_name.replace(" ", "_") safe_name = safe_name.replace(' ', '_')
if len(safe_name) > max_size: if len(safe_name) > max_size:
safe_name = safe_name[0:max_size] safe_name = safe_name[0:max_size]
return safe_name return safe_name
def get_files_in_dir(directory_path): def get_files_in_dir(directory_path):
""" '''
Returns a list with the absolute paths of all files in the directory directory_path Returns a list with the absolute paths of all files in the directory directory_path
:param directory_path: directory including files :param directory_path: directory including files
:type directory_path: str :type directory_path: str
:return: list :return: list
""" '''
result = [] result = []
try: try:
for file_path, _, files in os.walk(directory_path): for file_path, _, files in os.walk(directory_path):
for file_ in files: for file_ in files:
result.append(os.path.abspath(os.path.join(file_path, file_))) result.append(os.path.abspath(os.path.join(file_path, file_)))
except Exception as e: 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 return result
def get_dirs_in_dir(directory_path): 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. 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 :param directory_path: directory including sub-directories
:type directory_path: str :type directory_path: str
:return: list :return: list
""" '''
result = [] result = []
try: try:
dir_content = os.listdir(directory_path) dir_content = os.listdir(directory_path)
...@@ -182,20 +181,20 @@ def get_dirs_in_dir(directory_path): ...@@ -182,20 +181,20 @@ def get_dirs_in_dir(directory_path):
if os.path.isdir(dir_path): if os.path.isdir(dir_path):
result.append(dir_path) result.append(dir_path)
except Exception as e: 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 return result
def get_dir_of_file(file_path): def get_dir_of_file(file_path):
""" '''
Returns absolute path of the directory including file Returns absolute path of the directory including file
:param file_path: Paht of the file :param file_path: Paht of the file
:type: paht-like object :type: paht-like object
:return: string :return: string
""" '''
try: try:
return os.path.dirname(os.path.abspath(file_path)) return os.path.dirname(os.path.abspath(file_path))
except Exception as e: except Exception as e:
logging.error("Could not get directory path: {} {}".format(sys.exc_info()[0].__name__, e)) logging.error('Could not get directory path: {} {}'.format(sys.exc_info()[0].__name__, e))
return "/" return '/'
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