input_file_parser

A module containing the input file parser.

Summary

Classes:

ComposedDatatypesTransformer

Transformer for composed datatypes.

InputDictionary

Input file dictionary.

InputFileParser

Class to parse input files.

InputFileVisitor

Visitor for input files.

PrimitiveTransformer

Transformer for primitive datatypes.

Reference

class ComposedDatatypesTransformer(visit_tokens=False)[source]

Bases: Transformer

Transformer for composed datatypes.

Parameters:

Transformer (Transformer) – Transformer class from lark.

initialize the transformer

Parameters:

visit_tokens (bool, optional) – boolean to visit tokens, by default False

array(
items,
) tuple[list[Any], str, str][source]

Method to transform array values

It transforms a list of items into a list of values, the string “{most_general_type}”, and the line where the token was defined. The most general type is inferred from the list of items. If the list contains a bool and another type, a TypeError is raised. The list of items must contain only primitive types.

Parameters:

items (List[Any]) – items containing the array value

Returns:

tuple containing the array value, the string “{most_general_type}”, and the line where the token was defined.

Return type:

Tuple[List[Any], str, str]

Raises:

TypeError – if the list of items contains a non-primitive type

glob(
items,
) tuple[list[str], str, str][source]

Method to transform glob values

Parameters:

items (List[Any]) – items containing the glob value

Returns:

tuple containing the glob value, the string “glob”, and the line where the token was defined.

Return type:

Tuple[List[str], str, str]

key(items) str[source]

Method to transform key values

Parameters:

items (List[Token]) – items containing the key value

Returns:

key value

Return type:

str

range(
items,
) tuple[Range, str, str][source]

Method to transform range values

Parameters:

items (List[Any]) – items containing the range value

Returns:

tuple containing the range value, the string “range”, and the line where the token was defined.

Return type:

Tuple[Range, str, str]

value(
items,
) tuple[Any, str, str][source]

Method to transform value values

Parameters:

items (List[Any]) – items containing the value value

Returns:

tuple containing the value value, the string “value”, and the line where the token was defined.

Return type:

Tuple[Any, str, str]

primitive_types = ['float', 'int', 'str', 'bool']
class InputDictionary[source]

Bases: object

Input file dictionary.

This is a dictionary-like object that stores the input file keys and values. It is case-insensitive, so that the keys “key”, “Key” and “KEY” are all equivalent. It also stores the type of the value and the line where the key was defined in the input file.

THe InputDictionary is initialized as an empty dictionary.

__eq__(
_InputDictionary__value: object,
) bool[source]

Compare two InputDictionary objects.

Parameters:

__value (object) – The object to compare to.

Returns:

True if the objects are equal, False otherwise.

Return type:

bool

__getitem__(
key: str,
) Any[source]

Get the value of a key.

Parameters:

key (str) – The key to get the value of. It is case-insensitive.

Returns:

The value of the key.

Return type:

Any

Raises:

PQKeyError – If the key is not defined in the input file.

__setitem__(
key: str,
value: tuple[Any, str, str],
) None[source]

Set the value of a key.

Parameters:
  • key (str) – The key to set the value of. It is case-insensitive.

  • value (Tuple[Any, str, str]) – The value to set. It is a tuple containing the value, the type of the value and the line where the key was defined in the input file.

Raises:

PQKeyError – If the key is already defined in the input file.

get_line(key: str) str[source]

Get the line where the key was defined in the input file.

Parameters:

key (str) – The key to get the line of. It is case-insensitive.

Returns:

The line where the key was defined in the input file.

Return type:

str

get_type(key: str) str[source]

Get the type of the value of a key.

Parameters:

key (str) – The key to get the type of. It is case-insensitive.

Returns:

The type of the value of the key.

Return type:

str

get_value(key: str) Any[source]

Get the value of a key.

Parameters:

key (str) – The key to get the value of. It is case-insensitive.

Returns:

The value of the key.

Return type:

Any

keys() list[str][source]

Get the keys of the dictionary.

Returns:

The keys of the dictionary in a list.

Return type:

List[str]

logger = <CustomLogger PQAnalysis.InputDictionary (INFO)>
class InputFileParser(
filename: str,
input_format: InputFileFormat | str = InputFileFormat.PQANALYSIS,
)[source]

Bases: BaseReader

Class to parse input files.

This parser is based on a lark grammar. It uses the lark parser to parse the input file. For more information have a look at the lark documentation. This input file parser is used for parsing all kind of input files. By selecting the input_format the automatically invokes the corresponding grammar. The input_format can be either PQANALYSIS, PQ or QMCFC. The PQANALYSIS format is used for parsing the input files of the PQAnalysis code. The PQ and QMCFC formats are used for parsing the input files of the PQ and QMCFC codes, respectively.

Parameters:
  • BaseReader (BaseReader) – BaseReader class from PQAnalysis.io

  • filename (str) – The name of the input file.

  • input_format (InputFileFormat | str, optional) – The format of the input file, by default InputFileFormat.PQANALYSIS

parse() InputDictionary[source]

Parse the input file.

It uses the lark parser to parse the input file. It then uses the PrimitiveTransformer and ComposedDatatypesTransformer to transform the tree. Finally, it uses the InputFileVisitor to visit the tree and return the dictionary.

Returns:

InputDictionary – The parsed input file dictionary.

Return type:

InputDictionary

class InputFileVisitor[source]

Bases: Visitor

Visitor for input files.

Parameters:

Visitor – Visitor class from lark.

Initialize the visitor.

It initializes the dictionary and the composed datatypes transformer.

assign(
items: Tree,
) Tree[source]

Parse an assign statement. The assign statement is of the form:

key = value;

where key is a string, and the value is a tuple containing the value, the type of the value and the line where the key was defined.

Parameters:

items (Tree) – The assign statement.

Returns:

The assign statement.

Return type:

Tree

multiline_statement(
items: Tree,
) Tree[source]

Parse a multiline statement. The multiline statement is of the form:

key value1 value2 … END

where key is a string, and the values are a tuple containing the value, the type of the value and the line where the key was defined.

Parameters:

items (Tree) – The multiline statement.

Returns:

The multiline statement.

Return type:

Tree

visit(
tree: Tree,
) InputDictionary[source]

Visit the tree and return the dictionary.

Parameters:

tree (Tree) – The tree to visit.

Returns:

The parsed input file dictionary.

Return type:

InputDictionary

class PrimitiveTransformer(visit_tokens=False)[source]

Bases: Transformer

Transformer for primitive datatypes.

Parameters:

Transformer (Transformer) – Transformer class from lark.

initialize the transformer

Parameters:

visit_tokens (bool, optional) – boolean to visit tokens, by default False

boolean(
items,
) tuple[bool, str, str][source]

Method to transform bool values

Parameters:

items (List[Token]) – items containing the bool value

Returns:

tuple containing the bool value, the string “bool”, and the line where the token was defined.

Return type:

Tuple[bool, str, str]

float(
items,
) tuple[Real, str, str][source]

Method to transform float values

A “float” token is transformed into a float value, the string “float”, and the line where the token was defined.

Parameters:

items (List[Token]) – items containing the float value

Returns:

tuple containing the float value, the string “float”, and the line where the token was defined.

Return type:

Tuple[Real, str, str]

integer(
items,
) tuple[int, str, str][source]

Method to transform int values

Parameters:

items (List[Token]) – items containing the int value

Returns:

tuple containing the int value, the string “int”, and the line where the token was defined.

Return type:

Tuple[Integral, str, str]

word(
items,
) tuple[str, str, str][source]

Method to transform word values

Parameters:

items (List[Token]) – items containing the word value

Returns:

tuple containing the word value, the string “str”, and the line where the token was defined.

Return type:

Tuple[str, str, str]