|
@@ -1,6 +1,5 @@
|
|
import logging
|
|
import logging
|
|
import os
|
|
import os
|
|
-import shutil
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig()
|
|
logging.basicConfig()
|
|
@@ -8,10 +7,8 @@ logger = logging.getLogger("kalliope")
|
|
|
|
|
|
|
|
|
|
class FileManager:
|
|
class FileManager:
|
|
-
|
|
|
|
"""
|
|
"""
|
|
-
|
|
|
|
- Usefull Class to manage Files
|
|
|
|
|
|
+ Class used to manage Files
|
|
"""
|
|
"""
|
|
def __init__(self):
|
|
def __init__(self):
|
|
pass
|
|
pass
|
|
@@ -19,9 +16,9 @@ class FileManager:
|
|
@staticmethod
|
|
@staticmethod
|
|
def create_directory(cache_path):
|
|
def create_directory(cache_path):
|
|
"""
|
|
"""
|
|
- Create a directory at the provided `cache_path`
|
|
|
|
- :param cache_path: the path of the directory to create
|
|
|
|
- :type cache_path: String
|
|
|
|
|
|
+ Create a directory at the provided `cache_path`
|
|
|
|
+ :param cache_path: the path of the directory to create
|
|
|
|
+ :type cache_path: str
|
|
"""
|
|
"""
|
|
if not os.path.exists(cache_path):
|
|
if not os.path.exists(cache_path):
|
|
os.makedirs(cache_path)
|
|
os.makedirs(cache_path)
|
|
@@ -29,11 +26,13 @@ class FileManager:
|
|
@staticmethod
|
|
@staticmethod
|
|
def write_in_file(file_path, content):
|
|
def write_in_file(file_path, content):
|
|
"""
|
|
"""
|
|
- Write contents into a file
|
|
|
|
- :param file_path: the path of the file to write on
|
|
|
|
- :param content: the contents to write in the file
|
|
|
|
|
|
+ Write contents into a file
|
|
|
|
+ :param file_path: the path of the file to write on
|
|
|
|
+ :type file_path: str
|
|
|
|
+ :param content: the contents to write in the file
|
|
|
|
+ :type content: str
|
|
|
|
|
|
- .. raises:: IOError
|
|
|
|
|
|
+ .. raises:: IOError
|
|
"""
|
|
"""
|
|
try:
|
|
try:
|
|
with open(file_path, "wb") as file_open:
|
|
with open(file_path, "wb") as file_open:
|
|
@@ -43,36 +42,30 @@ class FileManager:
|
|
except IOError as e:
|
|
except IOError as e:
|
|
logger.error("I/O error(%s): %s", e.errno, e.strerror)
|
|
logger.error("I/O error(%s): %s", e.errno, e.strerror)
|
|
|
|
|
|
- @staticmethod
|
|
|
|
- def wipe_cache(cache_path):
|
|
|
|
- shutil.rmtree(cache_path)
|
|
|
|
-
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
def file_is_empty(file_path):
|
|
def file_is_empty(file_path):
|
|
"""
|
|
"""
|
|
- Check if the file is empty
|
|
|
|
- :param file_path: the path of the file
|
|
|
|
- :return: True if the file is empty, False otherwise
|
|
|
|
|
|
+ Check if the file is empty
|
|
|
|
+ :param file_path: the path of the file
|
|
|
|
+ :return: True if the file is empty, False otherwise
|
|
"""
|
|
"""
|
|
return os.path.getsize(file_path) == 0
|
|
return os.path.getsize(file_path) == 0
|
|
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
def remove_file(file_path):
|
|
def remove_file(file_path):
|
|
"""
|
|
"""
|
|
-
|
|
|
|
- Remove the file locate at the provided `file_path`
|
|
|
|
- :param file_path:
|
|
|
|
- :return: True if the file has been removed succefully, False otherwise
|
|
|
|
|
|
+ Remove the file locate at the provided `file_path`
|
|
|
|
+ :param file_path:
|
|
|
|
+ :return: True if the file has been removed successfully, False otherwise
|
|
"""
|
|
"""
|
|
if os.path.exists(file_path):
|
|
if os.path.exists(file_path):
|
|
return os.remove(file_path)
|
|
return os.remove(file_path)
|
|
|
|
|
|
-
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
def is_path_creatable(pathname):
|
|
def is_path_creatable(pathname):
|
|
"""
|
|
"""
|
|
- `True` if the current user has sufficient permissions to create the passed
|
|
|
|
- pathname; `False` otherwise.
|
|
|
|
|
|
+ `True` if the current user has sufficient permissions to create the passed
|
|
|
|
+ pathname; `False` otherwise.
|
|
"""
|
|
"""
|
|
dirname = os.path.dirname(pathname) or os.getcwd()
|
|
dirname = os.path.dirname(pathname) or os.getcwd()
|
|
return os.access(dirname, os.W_OK)
|
|
return os.access(dirname, os.W_OK)
|
|
@@ -80,12 +73,12 @@ class FileManager:
|
|
@staticmethod
|
|
@staticmethod
|
|
def is_path_exists_or_creatable(pathname):
|
|
def is_path_exists_or_creatable(pathname):
|
|
"""
|
|
"""
|
|
- `True` if the passed pathname is a valid pathname for the current OS _and_
|
|
|
|
- either currently exists or is hypothetically creatable; `False` otherwise.
|
|
|
|
|
|
+ `True` if the passed pathname is a valid pathname for the current OS _and_
|
|
|
|
+ either currently exists or is hypothetically creatable; `False` otherwise.
|
|
|
|
|
|
- This function is guaranteed to _never_ raise exceptions.
|
|
|
|
|
|
+ This function is guaranteed to _never_ raise exceptions.
|
|
|
|
|
|
- .. raises:: OSError
|
|
|
|
|
|
+ .. raises:: OSError
|
|
"""
|
|
"""
|
|
try:
|
|
try:
|
|
return os.path.exists(pathname) or FileManager.is_path_creatable(pathname)
|
|
return os.path.exists(pathname) or FileManager.is_path_creatable(pathname)
|