Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _write_endpoint_config_to_yaml(path: Path, data: Dict[Text, Any]) -> Path:
endpoints_path = path / "endpoints.yml"
# write endpoints config to file
io_utils.write_yaml_file(data, endpoints_path)
return endpoints_path
@pytest.fixture(scope="module")
def loop():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop = rasa.utils.io.enable_async_loop_debugging(loop)
yield loop
loop.close()
async def test_dump_and_restore_as_json(default_agent, tmpdir_factory):
trackers = await default_agent.load_data(DEFAULT_STORIES_FILE)
for tracker in trackers:
out_path = tmpdir_factory.mktemp("tracker").join("dumped_tracker.json")
dumped = tracker.current_state(EventVerbosity.AFTER_RESTART)
rasa.utils.io.dump_obj_as_json_to_file(out_path.strpath, dumped)
restored_tracker = restore.load_tracker_from_json(
out_path.strpath, default_agent.domain
)
assert restored_tracker == tracker
os.remove("domain.yml")
run_in_default_project(
"train",
"-c",
"config.yml",
"--data",
"data",
"--out",
"train_models_no_domain",
"--fixed-model-name",
"nlu-model-only",
)
assert os.path.exists("train_models_no_domain")
files = io_utils.list_files("train_models_no_domain")
assert len(files) == 1
trained_model_path = "train_models_no_domain/nlu-model-only.tar.gz"
unpacked = model.unpack_model(trained_model_path)
metadata_path = os.path.join(unpacked, "nlu", "metadata.json")
assert os.path.exists(metadata_path)
def test_get_valid_config(parameters):
import rasa.utils.io
config_path = None
if parameters["config_data"] is not None:
config_path = os.path.join(tempfile.mkdtemp(), "config.yml")
rasa.utils.io.write_yaml_file(parameters["config_data"], config_path)
default_config_path = None
if parameters["default_config"] is not None:
default_config_path = os.path.join(tempfile.mkdtemp(), "default-config.yml")
rasa.utils.io.write_yaml_file(parameters["default_config"], default_config_path)
if parameters["error"]:
with pytest.raises(SystemExit):
_get_valid_config(config_path, parameters["mandatory_keys"])
else:
config_path = _get_valid_config(
config_path, parameters["mandatory_keys"], default_config_path
)
config_data = rasa.utils.io.read_yaml_file(config_path)
for k in parameters["mandatory_keys"]:
assert k in config_data
def persist(self, path: Text) -> None:
self.featurizer.persist(path)
memorized_file = os.path.join(path, "memorized_turns.json")
data = {
"priority": self.priority,
"max_history": self.max_history,
"lookup": self.lookup,
}
rasa.utils.io.create_directory_for_file(memorized_file)
rasa.utils.io.dump_obj_as_json_to_file(memorized_file, data)
def _prepare_credentials_for_rasa_x(
credentials_path: Optional[Text], rasa_x_url: Optional[Text] = None
) -> Text:
credentials_path = cli_utils.get_validated_path(
credentials_path, "credentials", DEFAULT_CREDENTIALS_PATH, True
)
if credentials_path:
credentials = io_utils.read_config_file(credentials_path)
else:
credentials = {}
# this makes sure the Rasa X is properly configured no matter what
if rasa_x_url:
credentials["rasa"] = {"url": rasa_x_url}
dumped_credentials = yaml.dump(credentials, default_flow_style=False)
tmp_credentials = io_utils.create_temporary_file(dumped_credentials, "yml")
return tmp_credentials
@staticmethod
def load_from_config(
config_path: Text,
domain_path: Optional[Text] = None,
training_data_paths: Optional[List[Text]] = None,
) -> "TrainingDataImporter":
"""Loads a ``TrainingDataImporter`` instance from a configuration file."""
config = io_utils.read_config_file(config_path)
return TrainingDataImporter.load_from_dict(
config, config_path, domain_path, training_data_paths
)
def __init__(
self,
config_file: Optional[Text] = None,
domain_path: Optional[Text] = None,
training_data_paths: Optional[Union[List[Text], Text]] = None,
):
if config_file and os.path.exists(config_file):
self.config = io_utils.read_config_file(config_file)
else:
self.config = {}
self._domain_path = domain_path
self.story_files, self.nlu_files = data.get_core_nlu_files(training_data_paths)
@staticmethod
def _read_examples_js(fn: Text, language: Text, fformat: Text) -> Any:
"""Infer and load the example file based on the root
filename and root format."""
if fformat == DIALOGFLOW_INTENT:
examples_type = "usersays"
else:
examples_type = "entries"
examples_fn_ending = f"_{examples_type}_{language}.json"
examples_fn = fn.replace(".json", examples_fn_ending)
if os.path.isfile(examples_fn):
return rasa.utils.io.read_json_file(examples_fn)
else:
return None