bag.io.file
This module handles file related IO.
Module Contents
Functions
|
Renders a yaml file as a jinja template. |
|
Opens a file with the correct encoding interface. |
|
Read the given file and return content as string. |
|
Iterate over lines in a file. |
|
Checks if given file is valid by seeing if it exists and optionally contains a string. |
|
Read the given file using YAML. |
|
Parse YAML file with environment variable substitution. |
|
Read the given resource file and return content as string. |
|
Writes the given content to file. |
|
Writes the given object to a file using YAML format. |
|
Create a new temporary directory. |
|
Opens a new temporary file for writing with unicode interface. |
Attributes
- 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.
- 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.
- bag.io.file.read_file(fname: Union[str, pathlib.Path]) str [source]
Read the given file and return content as string.
- 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:
- 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.
- 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.
- 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.
- bag.io.file.make_temp_dir(prefix: str, parent_dir: str = '') str [source]
Create a new temporary directory.
- 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