Source code for ska_sdp_piper.piper.utils.io_utils

from datetime import datetime
from functools import cache
from pathlib import Path

import yaml


[docs] def create_output_dir( output_path, unique_output_subdir=False, prefix_name=None ): """ Creates the root output directory if it doesn't exist already and optionally a timestamped folder inside it to store the pipeline output data products. This function returns the absolute path of the effective output directory. Parameters ---------- output_path: str The root output folder where the timestamped folders are created unique_output_subdir: bool, default = False Whether to create a unique timestamped subdirectory prefix_name: str, optional Optional prefix to add to the timestamped subdirectory name. Ignored if unique_output_subdir is False. Returns ------- str Effective output directory absolute path """ effective_output_path = Path(output_path) if unique_output_subdir: timestamped_folder = ( f"{prefix_name}_{timestamp()}" if prefix_name else timestamp() ) effective_output_path = Path(effective_output_path, timestamped_folder) effective_output_path.mkdir(parents=True, exist_ok=True) return str(effective_output_path.absolute())
[docs] def read_yml(input_path): """ Reads a yaml file as python dictionary Parameters ---------- input_path: str Location of yaml file to read from. Returns ------- dict """ with open(input_path, "r") as input_file: return yaml.safe_load(input_file)
[docs] def write_yml(output_path, config): """ Writes a config to output path as yaml Parameters ---------- output_path: str Location of yaml file to write to. config: dict Data to write """ with open(output_path, "w") as conf_file: yaml.safe_dump(config, conf_file, sort_keys=False)
[docs] @cache def timestamp(cache_breaker=0): """ Creates timestamp with predefined format `%Y-%m-%dT%H:%M:%S` Caches the result between calls to keep the timestamp consistent within a run Parameters ---------- cache_breaker: Any Change value to return a new timestamp Returns ------- (str): Timestamp. """ return datetime.now().strftime("%Y-%m-%dT%H:%M:%S")