bag.io.file

This module handles file related IO.

Module Contents

Functions

render_yaml(→ Dict[str, Any])

Renders a yaml file as a jinja template.

open_file(→ TextIO)

Opens a file with the correct encoding interface.

read_file(→ str)

Read the given file and return content as string.

readlines_iter(→ Iterable[str])

Iterate over lines in a file.

is_valid_file(→ bool)

Checks if given file is valid by seeing if it exists and optionally contains a string.

read_yaml(→ Any)

Read the given file using YAML.

read_yaml_env(→ Any)

Parse YAML file with environment variable substitution.

read_resource(→ str)

Read the given resource file and return content as string.

write_file(→ None)

Writes the given content to file.

write_yaml(→ None)

Writes the given object to a file using YAML format.

make_temp_dir(→ str)

Create a new temporary directory.

open_temp(→ TextIO)

Opens a new temporary file for writing with unicode interface.

Attributes

yaml

bag.io.file.yaml[source]
bag.io.file.render_yaml(fname: Union[str, pathlib.Path], params: Dict[str, Any]) Dict[str, Any][source]

Renders a yaml file as a jinja template.

Parameters:
  • fname (Union[str, Path]) – the yaml file name.

  • params (Dict[str, Any]) – parameters to be replaced in the yaml template

Returns:

rendered_content – A dictionary with keywords replaced in the yaml file.

Return type:

Dict[str, Any]

bag.io.file.open_file(fname: Union[str, pathlib.Path], mode: str) TextIO[source]

Opens a file with the correct encoding interface.

Use this method if you need to have a file handle.

Parameters:
  • fname (str) – the file name.

  • mode (str) – the mode, either ‘r’, ‘w’, or ‘a’.

Returns:

file_obj – a file objects that reads/writes string with the BAG system encoding.

Return type:

TextIO

bag.io.file.read_file(fname: Union[str, pathlib.Path]) str[source]

Read the given file and return content as string.

Parameters:

fname (Union[str, Path]) – the file name.

Returns:

content – the content as a unicode string.

Return type:

str

bag.io.file.readlines_iter(fname: Union[str, pathlib.Path]) Iterable[str][source]

Iterate over lines in a file.

Parameters:

fname (str) – the file name.

Yields:

line (str) – a line in the file.

bag.io.file.is_valid_file(fname: Union[str, pathlib.Path], ready_str: Optional[str], timeout: float, wait_intvl: float, check_nonempty: bool = False) bool[source]

Checks if given file is valid by seeing if it exists and optionally contains a string.

Parameters:
  • fname (Union[str, Path]) – the file name.

  • ready_str (Optional[str]) – the string to check if file is ready. None if we should only check to see if file exists.

  • timeout (float) – maximum amount of time to wait.

  • wait_intvl (float) – amount of time in between iterations to check if file is ready.

  • check_nonempty (bool) – True to check if file is not empty. Default is False.

Returns:

is_valid – True if file exists and optionally contains ready_str. False if timed out.

Return type:

bool

bag.io.file.read_yaml(fname: Union[str, pathlib.Path]) Any[source]

Read the given file using YAML.

Parameters:

fname (str) – the file name.

Returns:

content – the object returned by YAML.

Return type:

Any

bag.io.file.read_yaml_env(fname: str) Any[source]

Parse YAML file with environment variable substitution.

Parameters:

fname (str) – yaml file name.

Returns:

table – the object returned by YAML.

Return type:

Any

bag.io.file.read_resource(package: str, fname: str) str[source]

Read the given resource file and return content as string.

Parameters:
  • package (str) – the package name.

  • fname (str) – the resource file name.

Returns:

content – the content as a unicode string.

Return type:

str

bag.io.file.write_file(fname: Union[str, pathlib.Path], content: str, append: bool = False, mkdir: bool = True) None[source]

Writes the given content to file.

Parameters:
  • fname (Union[str, Path]) – the file name.

  • content (str) – the unicode string to write to file.

  • append (bool) – True to append instead of overwrite.

  • mkdir (bool) – If True, will create parent directories if they don’t exist.

bag.io.file.write_yaml(fname: Union[str, pathlib.Path], obj: object, mkdir: bool = True) None[source]

Writes the given object to a file using YAML format.

Parameters:
  • fname (Union[str, Path]) – the file name.

  • obj (object) – the object to write.

  • mkdir (bool) – If True, will create parent directories if they don’t exist.

Returns:

content – the object returned by YAML.

Return type:

Any

bag.io.file.make_temp_dir(prefix: str, parent_dir: str = '') str[source]

Create a new temporary directory.

Parameters:
  • prefix (str) – the directory prefix.

  • parent_dir (str) – the parent directory.

Returns:

dir_name – the temporary directory name.

Return type:

str

bag.io.file.open_temp(**kwargs: Any) TextIO[source]

Opens a new temporary file for writing with unicode interface.

Parameters:

**kwargs (Any) – the tempfile keyword arguments. See documentation for tempfile.NamedTemporaryFile().

Returns:

file – the opened file that accepts unicode input.

Return type:

TextIO