file_utils.py

file_utils.py#

Utilities for working with files.

openmdao.utils.file_utils.clean_outputs(obj='.', recurse=False, prompt=True, pattern='*_out', dryrun=False)[source]

Remove output directories created by OpenMDAO.

A directory is determined to be an OpenMDAO output directory if its name ends in _out and it contains the file .openmdao_out.

Parameters:
objProblem or System or Solver or str or Path

The problem or system or solver whose output file should be removed.

recursebool

If True, and if obj is a string or Path, recurse into it finding and removing OpenMDAO output directories along the way. This option is ignored if obj is a Problem, System, or Solver.

promptbool

If True, prompt the user to confirm directories to be removed. This option is ignored if obj is a Problem, System, or Solver.

patternstr

A glob pattern used for matching directories.

dryrunbool

If True, report which directories would be removed without actually removing them.

openmdao.utils.file_utils.files_iter(start_dir='.', dir_includes=None, dir_excludes=(), file_includes=None, file_excludes=(), package_only=False)[source]

Iterate over files (recursively) starting in start_dir.

NOTE : all _includes and *_excludes are applied to *local directory and file names.

Parameters:
start_dirstr

Starting directory.

dir_includesiter of str or None

Glob patterns for directory inclusion. Be careful here because dir names are local, so, for example, if includes=(‘foo’,), then directory ‘foo’ would be included, but any subdirectories of ‘foo’ that were not also named ‘foo’ would not.

dir_excludesiter of str

Glob patterns for directory exclusion.

file_includesiter of str or None

Glob patterns for file inclusion.

file_excludesiter of str

Glob patterns for file exclusion.

package_onlybool

If True, only yield files that are contained in a python package.

Yields:
str

Filenames (full path from start_dir).

openmdao.utils.file_utils.fname2mod_name(fname)[source]

Convert a string to a valid python module name.

Parameters:
fnamestr

The filename to convert.

Returns:
str

A valid module name corresponding to the given filename.

openmdao.utils.file_utils.get_module_path(fpath)[source]

Given a module filename, return its full Python module path.

This includes enclosing packages and is based on existence of __init__.py files.

Parameters:
fpathstr

Pathname of file.

Returns:
str or None

Full module path of the given file. Returns None if the file is not part of a package.

openmdao.utils.file_utils.get_work_dir()[source]

Return either os.getcwd() or the value of the OPENMDAO_WORKDIR environment variable.

Returns:
str

The working directory.

openmdao.utils.file_utils.image2html(imagefile, title='', alt='')[source]

Wrap the given image for display as an html file.

Returns an html syntax string that can be written to a file.

Parameters:
imagefilestr

Name of image file to be displayed.

titlestr

The page title.

altstr

Set the alt text for the image.

Returns:
str

Content string to create an html file.

openmdao.utils.file_utils.package_iter(start_dir='.', dir_includes=None, dir_excludes=(), file_includes=None, file_excludes=())[source]

Iterate over python files in packages (recursively) starting in start_dir.

NOTE : all _includes and *_excludes are applied to *local directory and file names.

Parameters:
start_dirstr

Starting directory.

dir_includesiter of str or None

Glob patterns for directory inclusion. Be careful here because dir names are local, so, for example, if includes=(‘foo’,), then directory ‘foo’ would be included, but any subdirectories of ‘foo’ that were not also named ‘foo’ would not.

dir_excludesiter of str

Glob patterns for directory exclusion.

file_includesiter of str or None

Glob patterns for file inclusion.

file_excludesiter of str

Glob patterns for file exclusion.

Yields:
str

Filenames (full path from start_dir).

openmdao.utils.file_utils.text2html(text, title='', style=None)[source]

Wrap the given text for display as an html file.

Returns an html syntax string that can be written to a file.

Parameters:
textstr

Text to be displayed.

titlestr

Title to display above text.

stylestr or None

If not None, use as the contents of the style block for the enclosing <pre> tag.

Returns:
str

Content string to create an html file.