Parser¶
- class dictIO.parser.Parser¶
Bases:
objectBase Class for parsers.
Parsers deserialize a string into a SDict. Subclasses of Parser implement parsing of different, specifically formatted strings (see also Formatters).
- counter¶
Shared placeholder counter used while parsing.
- source_file¶
Source file currently being parsed, or None when no file has been assigned.
- __init__() None¶
Initialize parser state and a shared placeholder counter.
- Return type:
None
Methods
__init__()Initialize parser state and a shared placeholder counter.
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(-> TSingleValue)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 | None) – Path used to select a format-specific parser. If None or an unsupported suffix is supplied, use the native parser. Defaults to None.
- Returns:
Parser selected for the source file’s format.
- Return type:
- 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) – String from which quote characters are removed.
all_quotes (bool) – Whether to remove quotes throughout the string instead of only at its edges. Defaults to False.
- Returns:
String with the selected quote characters 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 (M | S) – 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:
M | S
- 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 (str | os.PathLike[str]) – Dictionary file to parse.
target_dict (SDict[K, V] | None) – Dictionary into which parsed content is merged. If None, create a new dictionary for the source file. Defaults to None.
comments (bool) – Whether to preserve comments from the source file. Defaults to True.
- Returns:
Parsed dictionary.
- Raises:
FileNotFoundError – If source_file does not exist.
- Return type:
SDict[K, V]
- parse_key(arg: str) TKey¶
Parse a single key.
Parse the value, then accept only string or integer keys.
- Parameters:
arg (str) – Key value to parse.
- Returns:
Parsed key.
- Raises:
TypeError – If the parsed value is not a string or integer.
- Return type:
- 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) – Text to parse, typically the content read by parse_file.
target_dict (SDict[K, V]) – Dictionary into which parsed content is merged.
comments (bool) – Whether to preserve comments in the parsed content. Defaults to True.
- Returns:
Parsed dictionary content.
- Return type:
SDict[K, V]
- parse_value(arg: str) TSingleValue¶
- parse_value(arg: object) object
Parse a single value.
Convert numeric, boolean, and null string representations to native values. Other strings are returned without conversion.
- Parameters:
arg (object) – Value to parse.
- Returns:
Parsed value, converted when it matches a supported native value.
- Return type:
object
- 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 (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)
- Return type:
None