oasislmf.utils.path =================== .. py:module:: oasislmf.utils.path Classes ------- .. autoapisummary:: oasislmf.utils.path.PathCleaner Functions --------- .. autoapisummary:: oasislmf.utils.path.as_path oasislmf.utils.path.empty_dir oasislmf.utils.path.import_from_string oasislmf.utils.path.get_custom_module oasislmf.utils.path.setcwd Module Contents --------------- .. py:function:: as_path(path, label, is_dir=False, preexists=True, null_is_valid=True) Processes the path and returns the absolute path. If the path does not exist and ``preexists`` is true an ``OasisException`` is raised. :param path: The path to process :type path: str :param label: Human-readable label of the path (used for error reporting) :type label: str :param is_dir: Whether the path is a directory :type is_dir: bool :param preexists: Flag whether to raise an error if the path does not exist. :type preexists: bool :param null_is_valid: flag to indicate if None is a valid value :type null_is_valid: bool :return: The absolute path of the input path .. py:function:: empty_dir(dir_fp) Empties the contents of a directory, but leaves the directory in place. :param dir_fp: A pre-existing directory path :type dir_fp: str .. py:class:: PathCleaner(label, preexists=True) Bases: :py:obj:`object` A callable that generates the absolute path of the given path and checks that it exists if indicated as preexisting. :param label: A user-friendly label for the path (used for error reporting) :type label: str :param preexists: Flag whether to raise an error if the path does not exist. :type preexists: bool .. py:attribute:: label .. py:attribute:: preexists .. py:function:: import_from_string(name) return the object or module from the path given >>> import os.path >>> mod = import_from_string('os.path') >>> os.path is mod True >>> from os.path import isabs >>> cls = import_from_string('os.path.isabs') >>> isabs is cls True .. py:function:: get_custom_module(custom_module_path, label) return the custom module present at the custom_module_path. the try loop allow for the custom module to work even if it depends on other module of its package by testing recursively for the presence of __init__.py file (ex: this module "path" is using from .exceptions import OasisException so it can only be imported as part of the utils package => sys.path.insert(0, path_to_utils);importlib.import_module('utils.path')) >>> mod = get_custom_module(__file__, "test module") >>> mod.__name__.rsplit('.', 1)[-1] 'path' .. py:function:: setcwd(path)