# Module with tools related to data validation.
#
# Author: Fernando García Gutiérrez
# Email: ga.gu.fernando.concat@gmail.com
#
import os
import inspect
from collections.abc import Iterable
def _checkExists(path: str, must_exists: bool, file: bool):
""" Check if a given file/path exists. """
checkInputType('file', file, [bool])
path_type = 'File' if file else 'Path'
checkMultiInputTypes(
(path_type.lower(), path, [str]),
('must_exists', must_exists, [bool]))
if must_exists:
if not os.path.exists(path):
raise FileNotFoundError('{} "{}" not found.'.format(path_type, os.path.abspath(path)))
else:
if os.path.exists(path):
raise FileExistsError('{} "{}" already exists.'.format(path_type, os.path.abspath(path)))
[docs]def fileExists(file: str, must_exists: bool):
""" Function that checks if a given file exists or not exists"""
_checkExists(file, must_exists, file=True)
[docs]def pathExists(path: str, must_exists: bool):
""" Function that checks if a given path exists. """
_checkExists(path, must_exists, file=False)
[docs]def checkCallable(input_obj_name: str, obj: callable):
""" Function used to check if a given object is callable. """
if not callable(obj):
raise NotImplementedError('"{}" is not callable.'.format(input_obj_name))
[docs]def checkIterable(input_obj_name: str, obj):
""" Function used to check if a given object is an iterable. """
if not isinstance(obj, Iterable):
raise NotImplementedError('"{}" is not an iterable.'.format(input_obj_name))
[docs]def checkClass(input_obj_name: str, obj):
""" Function used to check if a given object is a class. """
if not inspect.isclass(obj):
raise TypeError('"{}" is not a class.'.format(input_obj_name))