Pipeline File Import#
This module introduces tools that allow importing Pipeline objects from json/yaml files.
JSONImporter
is a class that imports pipeline from filesget_chatsky_objects()
is a function that provides an index of objects commonly used in a Pipeline definition.
- exception JSONImportError[source]#
Bases:
Exception
An exception for incorrect usage of
JSONImporter
.
- class JSONImporter(custom_dir)[source]#
Bases:
object
Enables pipeline import from file.
Since Pipeline and all its components are already pydantic
BaseModel
, the only purpose of this class is to allow importing and instantiating arbitrary objects.Import is done by replacing strings of certain patterns with corresponding objects. This process is implemented in
resolve_string_reference()
.Instantiating is done by replacing dictionaries where a single key is an imported object with an initialized object where arguments are specified by the dictionary values. This process is implemented in
replace_resolvable_objects()
andparse_args()
.- Parameters:
custom_dir (
Union
[str
,Path
]) – Path to the directory containing custom code available for import under theCUSTOM_DIR_NAMESPACE_PREFIX
.
-
CHATSKY_NAMESPACE_PREFIX:
str
= 'chatsky.'# Prefix that indicates an import from the chatsky library.
This class variable can be changed to allow using a different prefix.
-
CUSTOM_DIR_NAMESPACE_PREFIX:
str
= 'custom.'# Prefix that indicates an import from the custom directory.
This class variable can be changed to allow using a different prefix.
-
EXTERNAL_LIB_NAMESPACE_PREFIX:
str
= 'external:'# Prefix that indicates an import from any library.
This class variable can be changed to allow using a different prefix.
- static is_resolvable(value)[source]#
Check if
value
starts with any of the namespace prefixes:- Return type:
bool
- Returns:
Whether the value should be resolved (starts with a namespace prefix).
- static sys_path_append(path)[source]#
Append
path
tosys.path
before yielding and restoresys.path
to initial state after returning.
- static replace_prefix(string, old_prefix, new_prefix)[source]#
Replace
old_prefix
instring
withnew_prefix
.- Raises:
ValueError – If the
string
does not begin withold_prefix
.- Return type:
str
- Returns:
A new string with a new prefix.
- resolve_string_reference(obj)[source]#
Import an object indicated by
obj
.First,
obj
is pre-processed – prefixes are replaced to allow import:CUSTOM_DIR_NAMESPACE_PREFIX
is replaced{stem}.
where stem is the stem of the custom dir;CHATSKY_NAMESPACE_PREFIX
is replaced withchatsky.
;EXTERNAL_LIB_NAMESPACE_PREFIX
is removed.
Next the resulting string is imported: If the string is
a.b.c.d
, the following is tried in order:from a import b; return b.c.d
from a.b import c; return c.d
from a.b.c import d; return d
For custom dir imports; parent of the custom dir is appended to
sys.path
viasys_path_append()
.- Return type:
Any
- Returns:
An imported object.
- Raises:
ValueError – If
obj
does not begin with any of the prefixes (is notis_resolvable()
).JSONImportError – If a string could not be imported. Includes exceptions raised on every import attempt.
- parse_args(value)[source]#
Parse
value
into args and kwargs:If
value
is a dictionary, it is returned as kwargs;If
value
is a list, it is returned as args;If
value
isNone
, both args and kwargs are empty;If
value
is anything else, it is returned as the only arg.
- Return type:
Tuple
[list
,dict
]- Returns:
A tuple of args and kwargs.
- replace_resolvable_objects(obj)[source]#
Replace any resolvable objects inside
obj
with their resolved versions and initialize any that are the only key of a dictionary.This method iterates over every value inside
obj
(which isJsonValue
). Any string thatis_resolvable()
is replaced with an object return fromresolve_string_reference()
. This is done only once (i.e. if a string is resolved to another resolvable string, that string is not resolved).Any dictionaries that contain only one resolvable key are replaced with a result of
resolve_string_reference(key)(*args, **kwargs)
(the object is initialized) whereargs
andkwargs
is the result ofparse_args()
on the value of the dictionary.- Return type:
Any
- Returns:
A new object with replaced resolvable strings and dictionaries.
- import_pipeline_file(file)[source]#
Import a dictionary from a json/yaml file and replace resolvable objects in it.
- Return type:
dict
- Returns:
A result of
replace_resolvable_objects()
on the dictionary.- Raises:
JSONImportError – If a file does not have a correct file extension.
JSONImportError – If an imported object from file is not a dictionary.
- get_chatsky_objects()[source]#
Return an index of most commonly used
chatsky
objects (in the context of pipeline initialization).- Returns:
A dictionary where keys are names of the objects (e.g.
chatsky.core.Message
) and values are the objects. The items in the dictionary are all the objects from the__init__
files of the following modules:”chatsky.cnd”;
”chatsky.rsp”;
”chatsky.dst”;
”chatsky.proc”;
”chatsky.core”;
”chatsky.core.service”;
”chatsky.slots”;
”chatsky.context_storages”;
”chatsky.messengers”.