dictIO.parser module#

class dictIO.parser.Parser#

Bases: object

Base Class for parsers.

Parsers deserialize a string into a CppDict. Subclasses of Parser implement parsing of different, specifically formatted strings (see also Formatters).

classmethod get_parser(source_file: Path | None = None)#

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:

Parser

parse_file(source_file: str | PathLike[str], target_dict: CppDict | None = None, comments: bool = True) CppDict#

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 (CppDict, 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:

CppDict

Raises:

FileNotFoundError – if source_file does not exist

parse_string(string: str, target_dict: CppDict, comments: bool = True) CppDict#

Parse a string and deserialize it into a CppDict.

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 (CppDict) – 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:

CppDict

parse_type(arg: Any) Any#

Parse a single value.

Parses a single value and casts it to its native type (str, int, float, boolean or None).

Parameters:

arg (Any) – the value to be parsed

Returns:

the value casted to its native type (str, int, float, boolean or None)

Return type:

Any

parse_types(arg: MutableMapping[Any, Any] | MutableSequence[Any])#

Parse multiple values.

Parses all values inside a dict or list and casts them to its native types (str, int, float, boolean 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[Any, Any], MutableSequence[Any]]) – the dict or list containing the values to be parsed and casted to its native types (str, int, float, boolean or None)

static remove_quotes_from_string(arg: str, all: 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 (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: MutableMapping[Any, Any] | MutableSequence[Any]) MutableMapping[Any, Any] | MutableSequence[Any]#

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 (Union[MutableMapping[Any, Any], MutableSequence[Any]]) – 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:

Union[MutableMapping[Any, Any], MutableSequence[Any]]

class dictIO.parser.CppParser#

Bases: Parser

Parser to deserialize a string in dictIO dict file format into a CppDict.

parse_string(string: str, target_dict: CppDict, comments: bool = True) CppDict#

Parse a string in dictIO dict file format and deserialize it into a CppDict.

Parameters:
  • string (str) – the string to be parsed (i.e. the content of the file that had been read using parse_file())

  • target_dict (CppDict) – 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:

CppDict

class dictIO.parser.FoamParser#

Bases: CppParser

Parser to deserialize a string in OpenFOAM dictionary format into a CppDict.

parse_string(string: str, target_dict: CppDict, comments: bool = True) CppDict#

Parse a string in OpenFOAM dictionary format and deserialize it into a CppDict.

Parameters:
  • string (str) – the string to be parsed (i.e. the content of the file that had been read using parse_file())

  • target_dict (CppDict) – 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:

CppDict

class dictIO.parser.JsonParser#

Bases: Parser

Parser to deserialize a string in JSON dictionary format into a CppDict.

parse_string(string: str, target_dict: CppDict, comments: bool = True) CppDict#

Parse a string in JSON dictionary format and deserialize it into a CppDict.

Parameters:
  • string (str) – the string to be parsed (i.e. the content of the file that had been read using parse_file())

  • target_dict (CppDict) – 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:

CppDict

class dictIO.parser.XmlParser(add_node_numbering: bool = True)#

Bases: Parser

Parser to deserialize a string in XML format into a CppDict.

parse_string(string: str, target_dict: CppDict, comments: bool = True) CppDict#

Parse a string in XML format and deserialize it into a CppDict.

Parameters:
  • string (str) – the string to be parsed (i.e. the content of the file that had been read using parse_file())

  • target_dict (CppDict) – 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:

CppDict