oasislmf.utils.path

Classes

PathCleaner

A callable that generates the absolute path of the given path and checks

Functions

as_path(path, label[, is_dir, preexists, null_is_valid])

Processes the path and returns the absolute path.

empty_dir(dir_fp)

Empties the contents of a directory, but leaves the directory in place.

import_from_string(name)

return the object or module from the path given

get_custom_module(custom_module_path, label)

return the custom module present at the custom_module_path.

setcwd(path)

Module Contents

oasislmf.utils.path.as_path(path, label, is_dir=False, preexists=True, null_is_valid=True)[source]

Processes the path and returns the absolute path.

If the path does not exist and preexists is true an OasisException is raised.

Parameters:
  • path (str) – The path to process

  • label (str) – Human-readable label of the path (used for error reporting)

  • is_dir (bool) – Whether the path is a directory

  • preexists (bool) – Flag whether to raise an error if the path does not exist.

  • null_is_valid (bool) – flag to indicate if None is a valid value

Returns:

The absolute path of the input path

oasislmf.utils.path.empty_dir(dir_fp)[source]

Empties the contents of a directory, but leaves the directory in place.

Parameters:

dir_fp (str) – A pre-existing directory path

class oasislmf.utils.path.PathCleaner(label, preexists=True)[source]

Bases: object

A callable that generates the absolute path of the given path and checks that it exists if indicated as preexisting.

Parameters:
  • label (str) – A user-friendly label for the path (used for error reporting)

  • preexists (bool) – Flag whether to raise an error if the path does not exist.

label[source]
preexists[source]
oasislmf.utils.path.import_from_string(name)[source]

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
oasislmf.utils.path.get_custom_module(custom_module_path, label)[source]

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’

oasislmf.utils.path.setcwd(path)[source]