libio: I/O helping functions

Lib I/O.

haddock.libs.libio.add_suffix_to_files(files: Iterable[str | Path], suffix: str) Generator[Path, None, None][source]

Add a suffix to file paths.

Yields:

pathlib.Path objects – Exhausts when files exhaust.

haddock.libs.libio.archive_files_ext(path: str | Path, ext: str, compresslevel: int = 9) bool[source]

Archive all files with same extension in folder.

Parameters:
  • path (str or pathlib.Path) – The folder containing the files.

  • ext (str) – The extension of the files.

  • compresslevel (int) – The compression level.

Returns:

boolTrue if files with ext were found and the Zip files created. False if no files with ext were found and, hence, the Zip files was not created.

haddock.libs.libio.check_yaml_duplicated_parameters(yaml_fpath: str) None[source]

Make sure the provided yaml file do not contain duplicated parameters.

Parameters:

yaml_fpath (str) – Path to a yaml file

haddock.libs.libio.clean_suffix(ext: str) str[source]

Remove the preffix dot of an extension if exists.

Parameters:

ext (str) – The extension string.

Examples

>>> clean_suffix('.pdb')
'pdb'
>>> clean_suffix('pdb')
'pdb'
haddock.libs.libio.compress_files_ext(path: str | Path, ext: str, ncores: int = 1, **kwargs: Any) bool[source]

Compress all files with same extension in folder to .gz.

Do not archive the files in TAR, only compress files individually.

Parameters:
  • path (str or pathlib.Path) – The folder containing the files.

  • ext (str) – The extension of the files.

  • **kwargs (anything) – Arguments passed to gzip_files().

Returns:

boolTrue if files with ext were found and the compressed .gz files created.

False if no files with ext were found and, hence, the .gz files were not created.

haddock.libs.libio.dot_suffix(ext: str) str[source]

Add the dot preffix to an extension if missing.

Parameters:

ext (str) – The extension string.

Examples

>>> clean_suffix('.pdb')
'.pdb'
>>> clean_suffix('pdb')
'.pdb'
haddock.libs.libio.file_exists(path: str | ~pathlib.Path, exception: type[Exception] = <class 'ValueError'>, emsg: str = '`path` is not a file or does not exist') Path[source]

Assert if file exist.

Parameters:
  • path (str or pathlib.Path) – The file path.

  • exception (Exception) – The Exception to raise in case path is not file or does not exist.

  • emsg (str) – The error message to give to exception. May accept formatting to pass path.

Returns:

pathlib.Path – The Path representation of the input path if condition is true.

Raises:

Exception – Any exception that pathlib.Path can raise.

haddock.libs.libio.folder_exists(path: str | ~pathlib.Path, exception: type[Exception] = <class 'ValueError'>, emsg: str = 'The folder {!r} does not exist or is not a folder.') Path[source]

Assert if a folder exist.

Parameters:
  • path (str or pathlib.Path) – The path to the folder.

  • exception (Exception) – The Exception to raise in case path is not file or does not exist.

  • emsg (str) – The error message to give to exception. May accept formatting to pass path.

Returns:

pathlib.Path – The Path representation of the input path if condition is true.

Raises:

Exception – Any exception that pathlib.Path can raise.

haddock.libs.libio.get_perm(fname: str | Path) int[source]

Get permissions of file.

haddock.libs.libio.glob_folder(folder: str | Path, ext: str) list[Path][source]

List files with extention ext in folder.

Does NOT perform recursive search.

Parameters:
  • folder (str) – The path to the folder to investigate.

  • ext (str) – The file extention. Can be with or without the dot [.] preffix.

Returns:

list of Path objects – SORTED list of matching results.

haddock.libs.libio.gzip_files(file_: str | Path, block_size: int | None = None, compresslevel: int = 9, remove_original: bool = False) None[source]

Gzip a file.

Parameters:
  • file_ (str or pathlib.Path) – The path to the file to compress.

  • block_size (int) – The block size to treat per cycle. Defaults to 200MB (2*10**8 (2*10**8).

  • compresslevel (int) – The compress level. Defaults to 9.

haddock.libs.libio.make_writeable_recursive(path: str | Path) None[source]

Add writing to a folder, its subfolders and files.

Parameters:

path (str or Path) – The path to add writing permissions.

haddock.libs.libio.open_files_to_lines(*files: str | Path) list[list[str]][source]

Open files to lines.

New-lines are stripped.

Returns:

list of lists of strings – The lines of the files. Input order is maintained.

haddock.libs.libio.pdb_path_exists(pdb_path: Path) tuple[bool, str | None][source]

Check if a pdb path exists.

If not, checks for the existence of a gzipped pdb file and informs the user that the file is gzipped

Parameters:

pdb_path (pathlib.Path) – path to the pdb

Returns:

  • exists (bool) – True if file exists

  • msg (str or None) – the error message

haddock.libs.libio.read_from_yaml(yaml_file: str | Path) dict[Any, Any][source]

Read a YAML file to a dictionary.

Used internally to read HADDOCK3’s default configuration files.

Parameters:

yaml_file (str or Path) – Path to the YAML file.

Returns:

dict – Always returns a dictionary. Returns empty dictionary if yaml_file is empty.

haddock.libs.libio.read_lines(func: Callable[[...], Any]) Callable[[...], Any][source]

Open the file and read lines for the decorated function.

Send to the decorated function the lines of the file in the form of list.

haddock.libs.libio.remove_files_with_ext(folder: str | Path, ext: str) None[source]

Remove files with ext in folder.

Parameters:
  • folder (str) – The path to the folder.

  • ext (str) – The extention of files to delete. Can be with or without the dot . preffix.

haddock.libs.libio.save_lines_to_files(files: Iterable[str | Path], lines: Iterable[Iterable[str]]) None[source]

Save a list of list of lines to files.

The first list of strings in lines will be saved in the first file of files, and so on.

Lines are saved using pathlib.Path.write_text function.

Parameters:
  • files (list) – The list of file names to save.

  • lines (list of lists of str) – A list containing lists of lines that are the file contents. Must be synched with files.

haddock.libs.libio.working_directory(path: str | Path) Generator[None, None, None][source]

Change working directory and returns to previous on exit.

haddock.libs.libio.write_dic_to_file(data_dict: Mapping[Any, Any], output_fname: str | Path, info_header: str = '', sep: str = '\t') None[source]

Create a table from a dictionary.

Parameters:
  • data_dict (dict) – Dictionary to write.

  • output_fname (str or Path) – Name of the output file.

  • info_header (str) – Header to write before the data.

haddock.libs.libio.write_nested_dic_to_file(data_dict: Mapping[Any, Any], output_fname: str | Path, info_header: str = '', sep: str = '\t') None[source]

Create a table from a nested dictionary.

Parameters:
  • data_dict (dict) – Dictionary to write.

  • output_fname (str or Path) – Name of the output file.

Notes

This function is used to write nested dictionaries. {int: {key: value}}, the int key will be discarded.