Parser¶
- class dictIO.parser.Parser¶
Bases:
object
Base Class for parsers.
Parsers deserialize a string into a SDict. Subclasses of Parser implement parsing of different, specifically formatted strings (see also Formatters).
- __init__() None ¶
Methods
__init__
()get_parser
([source_file])Return a Parser instance matching the type of the source file to be parsed (factory method).
parse_file
(source_file[, target_dict, comments])Parse a file and deserialize it into a dict.
parse_key
(arg)Parse a single key.
parse_string
(string, target_dict, *[, comments])Parse a string and deserialize it into a SDict.
parse_value
(arg)Parse a single value.
parse_values
(arg)Parse multiple values.
remove_quotes_from_string
(arg, *[, all_quotes])Remove quotes from a string.
Remove quotes from multiple strings.
- classmethod get_parser(source_file: Path | None = None) Parser ¶
Return a Parser instance matching the type of the source file to be parsed (factory method).
- Parameters:
source_file (Path, optional) – name of the source file to be parsed, by default None
- Returns:
specific Parser instance matching the source file type to be parsed
- Return type:
- parse_file(source_file: str | os.PathLike[str], target_dict: SDict[K, V] | None = None, *, comments: bool = True) SDict[K, V] ¶
Parse a file and deserialize it into a dict.
- Parameters:
source_file (Union[str, os.PathLike[str]]) – name of the dict file to be parsed
target_dict (SDict[K, V], optional) – the target dict the parsed dict file shall be merged into, by default None
comments (bool, optional) – reads comments from source file, by default True
- Returns:
the parsed dict
- Return type:
SDict[K, V]
- Raises:
FileNotFoundError – if source_file does not exist
- parse_key(arg: str) Hashable ¶
Parse a single key.
Parses a single key and casts it to its native type (TKey = str | int).
- Parameters:
arg (str) – the value to be parsed
- Returns:
the value casted to its native type (TKey = str | int)
- Return type:
TKey
- parse_string(string: str, target_dict: SDict[K, V], *, comments: bool = True) SDict[K, V] ¶
Parse a string and deserialize it into a SDict.
Note: Override this method when implementing a specific Parser.
- Parameters:
string (str) – the string to be parsed (i.e. the content of the file that had been read using parse_file())
target_dict (SDict[K, V]) – the target dict the parsed dict file shall be merged into
comments (bool, optional) – reads comments, by default True
- Returns:
the parsed dict
- Return type:
SDict[K, V]
- parse_value(arg: Any) str | int | float | bool | None ¶
Parse a single value.
Parses a single value and casts it to its native type (TSingleValue = str | int | float | bool | None).
- Parameters:
arg (TValue) – the value to be parsed
- Returns:
the value casted to its native type (TSingleValue = str | int | float | bool | None)
- Return type:
TSingleValue
- parse_values(arg: MutableMapping[K, V] | MutableSequence[V]) None ¶
Parse multiple values.
Parses all values inside a dict or list and casts them to its native types (str, int, float, bool or None). The function traverses the passed in dict or list recursively so that all values in also nested dicts and lists are parsed.
- Parameters:
arg (Union[MutableMapping[K, V], MutableSequence[V]]) – the dict or list containing the values to be parsed and casted to its native types (str, int, float, bool or None)
- static remove_quotes_from_string(arg: str, *, all_quotes: bool = False) str ¶
Remove quotes from a string.
Removes quotes (single and double quotes) from the string object passed in.
- Parameters:
arg (str) – the string with quotes
all_quotes (bool, optional) – if true, all quotes inside the string will be removed (not only leading and trailing quotes), by default False
- Returns:
the string with quotes being removed
- Return type:
str
- static remove_quotes_from_strings(arg: M | S) M | S ¶
Remove quotes from multiple strings.
Removes quotes (single and double quotes) from all string objects inside a dict or list. The function traverses the passed in dict or list recursively so that all strings in also nested dicts and lists are processed.
- Parameters:
arg (MutableMapping[K, V] | MutableSequence[V]) – the dict or list containing strings the quotes in which shall be removed
- Returns:
the original dict or list, yet with quotes in all strings being removed
- Return type:
MutableMapping[K, V] | MutableSequence[V]