bluemira.base.file

File I/O functions and some path operations

Attributes

BM_ROOT

SUB_DIRS

Classes

FileManager

A class for managing file operations.

Functions

_get_relpath(→ str)

get_bluemira_root(→ str)

Get the bluemira root install folder.

try_get_bluemira_private_data_root(→ str | None)

Get the bluemira-private-data root install folder.

get_bluemira_path(→ str)

Get a bluemira path of a module subfolder. Defaults to root folder.

try_get_bluemira_path(→ str | None)

Try to get the bluemira path of a module subfolder.

make_bluemira_path(→ str)

Create a new folder in the path, provided one does not already exist.

force_file_extension(→ str)

If the file path does not have one of the valid extensions, append the first

get_files_by_ext(→ list[pathlib.Path])

Get filenames of files in folder with the specified extension.

file_name_maker(→ str)

Ensure the file name is acceptable.

working_dir(directory)

Change working directory

Module Contents

bluemira.base.file.BM_ROOT = '!BM_ROOT!'
bluemira.base.file.SUB_DIRS = ['equilibria', 'neutronics', 'systems_code', 'CAD', 'plots', 'geometry']
bluemira.base.file._get_relpath(folder: str, subfolder: str) str
Parameters:
  • folder (str)

  • subfolder (str)

Return type:

str

bluemira.base.file.get_bluemira_root() str

Get the bluemira root install folder.

Returns:

‘/home/user/code/bluemira’

Return type:

The full path to the bluemira root folder, e.g.

bluemira.base.file.try_get_bluemira_private_data_root() str | None

Get the bluemira-private-data root install folder.

Returns:

‘/home/user/code/bluemira-private-data’

Return type:

The full path to the bluemira root folder, e.g.

Notes

Normal users will not have access to bluemira-private-data; it will be used exclusively for tests which require private data and files.

bluemira.base.file.get_bluemira_path(path: str = '', subfolder: str = 'bluemira') str

Get a bluemira path of a module subfolder. Defaults to root folder.

Parameters:
  • path (str) – The desired path from which to create a full path

  • subfolder (str) – The subfolder (from the bluemira root) in which to create a path Defaults to the source code folder, but can be e.g. ‘tests’, or ‘data’

Return type:

The full path to the desired path in the subfolder specified

bluemira.base.file.try_get_bluemira_path(path: str = '', subfolder: str = 'bluemira', *, allow_missing: bool = True) str | None

Try to get the bluemira path of a module subfolder.

If the path doesn’t exist then optionally carry on regardless or raise an error.

Parameters:
  • path (str) – The desired path from which to create a full path

  • subfolder (str) – The subfolder (from the bluemira root) in which to create a path Defaults to the source code folder, but can be e.g. ‘tests’, or ‘data’

  • allow_missing (bool) – Whether or not to raise an error if the path does not exist

Returns:

  • The full path to the desired path in the subfolder specified, or None if the

  • requested path doesn’t exist.

Raises:

ValueError – If the requested path doesn’t exist and the allow_missing flag is False.

Return type:

str | None

bluemira.base.file.make_bluemira_path(path: str = '', subfolder: str = 'bluemira') str

Create a new folder in the path, provided one does not already exist.

Parameters:
  • path (str) – The desired path from which to create a full path

  • subfolder (str) – The subfolder (from the bluemira root) in which to create a path Defaults to the source code folder, but can be e.g. ‘tests’, or ‘data’

Returns:

The full path to the desired path in the subfolder

Return type:

str

bluemira.base.file.force_file_extension(file_path: str, valid_extensions: str | list[str]) str

If the file path does not have one of the valid extensions, append the first valid one

Parameters:
  • file_path (str) – path to file

  • valid_extensions (str | list[str]) – collection of valid extensions

Return type:

File path

bluemira.base.file.get_files_by_ext(folder: str, extension: str) list[pathlib.Path]

Get filenames of files in folder with the specified extension.

Parameters:
  • folder (str) – The full path directory in which to look for files

  • extension (str) – The extension of the desired file-type

Returns:

The list of full path filenames found in the folder

Return type:

list[pathlib.Path]

bluemira.base.file.file_name_maker(filename: str | os.PathLike, *, lowercase: bool = False) str

Ensure the file name is acceptable.

Parameters:
  • filename (str | os.PathLike) – Full filename or path

  • lowercase (bool) – Whether or not to force lowercase filenames

Return type:

Full filename or path, corrected

bluemira.base.file.working_dir(directory: str)

Change working directory

Parameters:

directory (str)

class bluemira.base.file.FileManager(reactor_name: str, reference_data_root: str = 'data/bluemira', generated_data_root: str = 'data/bluemira')

A class for managing file operations.

Parameters:
  • reactor_name (str)

  • reference_data_root (str)

  • generated_data_root (str)

_reactor_name: str
_reference_data_root: str
_generated_data_root: str
reference_data_dirs: dict[str, str]
generated_data_dirs: dict[str, str]
property reactor_name

Gets the reactor name for this instance.

property generated_data_root: str

Gets the generated data root directory for this instance.

Return type:

str

property reference_data_root: str

Get the reference data root directory for this instance.

Return type:

str

replace_bm_root(keyword: str = BM_ROOT)

Replace the keyword in input paths with path to local bluemira installation.

Parameters:

keyword (str)

_verify_reference_data_root()

Check that the reference data root defined in this instance is a valid directory.

Raises:

ValueError – If the reference data root for this instance is not a valid directory.

make_reactor_folder(subfolder: str) dict[str, str]

Initialise a data storage folder tree.

Parameters:

subfolder (str) – The subfolder of the bluemira directory in which to add the data structure

Return type:

The dictionary of subfolder names to full paths (useful shorthand)

set_reference_data_paths()

Generate the reference data paths for this instance, based on the reactor name.

create_reference_data_paths()

Generate the reference data paths for this instance, based on the reactor name.

Also builds the relevant directory structure.

create_generated_data_paths()

Generate the generated data paths for this instance, based on the reactor name.

Also builds the relevant directory structure.

build_dirs(*, create_reference_data_paths: bool = False)

Create the directory structures for this instance and sets the path references.

Parameters:

create_reference_data_paths (bool)

get_path(sub_dir_name: str, path: str | os.PathLike, *, make_dir: bool = False) str

Get a path within the generated data sub-sdirectories.

If the path does not exist then it will optionally be created as a directory.

Parameters:
  • sub_dir_name (str) – The name of the sub-directory to create the path under. Must be one of the names in bluemira.base.file.SUB_DIRS.

  • path (str | os.PathLike) – The path to create under the sub-directory.

  • make_dir (bool) – Optionally create a directory at the path, by default False.

Return type:

The path within the data sub-directories.