def load(config_file: Optional[Union[Text, Dict]]) -> List["Policy"]: """Load policy data stored in the specified file.""" from rasa.core.policies.ensemble import PolicyEnsemble if not config_file: raise FileNotFoundException( f"The provided configuration file path does not seem to be valid. " f"The file '{os.path.abspath(config_file)}' could not be found.") config_data = {} if isinstance(config_file, str) and os.path.isfile(config_file): config_data = rasa.shared.utils.io.read_model_configuration( config_file) elif isinstance(config_file, Dict): config_data = config_file return PolicyEnsemble.from_dict(config_data)
def read_file(filename: Union[Text, Path], encoding: Text = DEFAULT_ENCODING) -> Any: """Read text from a file.""" try: with open(filename, encoding=encoding) as f: return f.read() except FileNotFoundError: raise FileNotFoundException( f"Failed to read file, " f"'{os.path.abspath(filename)}' does not exist.") except UnicodeDecodeError: raise FileIOException( f"Failed to read file '{os.path.abspath(filename)}', " f"could not read the file using {encoding} to decode " f"it. Please make sure the file is stored with this " f"encoding.")
def is_key_in_yaml(file_path: Union[Text, Path], *keys: Text) -> bool: """Checks if any of the keys is contained in the root object of the yaml file. Arguments: file_path: path to the yaml file keys: keys to look for Returns: `True` if at least one of the keys is found, `False` otherwise. Raises: FileNotFoundException: if the file cannot be found. """ try: with open(file_path) as file: return any( any(line.lstrip().startswith(f"{key}:") for key in keys) for line in file) except FileNotFoundError: raise FileNotFoundException( f"Failed to read file, " f"'{os.path.abspath(file_path)}' does not exist.")