BaseDefinition

class ase2sprkkr.common.configuration_definitions.BaseDefinition(name, is_optional=None, condition=None)[source]

This class is a member of definition of configuration, that can be both real (holds a value or values) or just virtual. Virtual definitions do not store values, generates grammar e.g. using other members of the container etc…

Parameters:
  • name (str) – Name of the value/section

  • is_optional (bool or callable) – Whether this element may be omitted from a parsed file. A callable accepts the runtime item and is evaluated after parsing.

  • condition –

    If defined, the condition
    • the condition.parse_condition() is invoked, when given grammar element should be parsed. If it is False, the element is skipped

    • the condition() is invoked, when the elements of the container is listed to hide the inactive members

Class hierarchy

Inheritance diagram of ase2sprkkr.common.configuration_definitions.BaseDefinition


Constructor

__init__(name, is_optional=None, condition=None)[source]
validate_parsed = None

Can be redefined for checks that require knowing which input items were explicitly parsed.

class Repeated(*values)

Supported repetition modes; retained here as part of the public definition API.

class Type(*values)

Storage used for a repeated item.

NO = None
ARRAY = <class 'numpy.ndarray'>
LIST = <class 'list'>
DICT = <class 'dict'>
class Numbering(*values)

Numbering used by an item name.

property has_default
NO = 0
YES = 1
WITH_DEFAULT = 2
property grammar_numbering

Numbering syntax accepted by the parser.

property numbering_condition

Condition controlling output numbering, if there is one.

numbering_for(option)

Return the numbering to use when writing option.

Parameters:

option (Option)

Return type:

ase2sprkkr.common.repetition.Repeated.Numbering

static NUMBERED_IF(condition)

Create a dense repetition numbered when condition(option) is true.

Parameters:

condition (Callable[[Option], bool])

Return type:

RepeatedNumberedIf

classmethod create(value, default)
property is_array
property is_dict
NO = Type.NO
IGNORED = (Type.NO, <class fIgnoredKey>)
REPEATED = (Type.LIST, <class fRepeatedKey>)
ARRAY = (Type.ARRAY, <class fRepeatedKey>)
LIST_SECTION = (Type.LIST, <function Key.NONE>, Numbering.NO, False)
DICT_SECTION = (Type.DICT, <function Key.NONE>, Numbering.NO, False)
NUMBERED = (Type.ARRAY, <class fArrayKey>, Numbering.YES)
DICT = (Type.DICT, <class fDictKey>, Numbering.YES)
DEFAULTDICT = (Type.DICT, <class fDefDictKey>, Numbering.WITH_DEFAULT)
is_option = True

Whether this definition creates a leaf option rather than a container.

is_generated = False

Whether the runtime option computes its value from other values.

is_stored = False

Whether this definition has an explicit representation in input files. Virtual definitions and non-stored generated values are not stored.

_resolve_is_optional(is_optional)[source]

Resolve an unspecified grammar-optional flag for this definition.

is_repeated = Type.NO

By default, the configuration items are not repeated

interactive_names()[source]
property all_names
property real_name
has_name(name, lower_case=False)[source]
allowed(container)[source]
is_optional_for(item)[source]

Evaluate whether the runtime item may be omitted from input.

add_grammar_hook(hook)[source]

Added hooks process the grammar of the option/container. E.g. it is used when the number of readed lines should depend on the value of the option.

remove_grammar_hook(hook)[source]

Remove a grammar hook

grammar(allow_dangerous=False, *, allow_incomplete=False)[source]

Generate grammar with the correct settings of pyparsing global state.

Parameters:
  • allow_dangerous (bool) – Allow dangerous values - i.e. values that do not fulfill the requirements for the given-option value (i.e. a type requirement or other constraints).

  • allow_incomplete (bool) – Allow missing non-optional input items, reporting them as validation issues.

_get_root_definition()[source]

Return the root of the definition tree containing this item.

_finalize()[source]

Finalize the complete definition tree before its first use.

property _grammar

Return the grammar. Descendants can redefine this method e.g. to allow not to generate the grammar at all. This method is implemented by property, that conditionaly returns the real method.

Returns

func: callable Function to generate grammar or None if no grammar is returned

has_grammar()[source]

Returns, whether the definition generates a grammar or not. By default, it just check the self._grammar

_hooked_grammar(allow_dangerous=False, **kwargs)[source]

Generates grammar. Unlike grammar(), it does not change the global pyparsing state to ensure that the generated grammar will handle whitespaces in a propper way. Unlike the _create_grammar() method, which should contain implementation of the grammar creation, this function add the common functionality to the generated grammar (currently, just the grammar hooks)

Parameters:

allow_dangerous (bool)

_add_hooks_to_grammar(grammar)[source]

Add registered grammar hooks to a grammar.

Developer note: the hooks are added even by grammar types, so it can not accept definition as argument, if you need it, use the closure of the hook to “pass” the argument.

added_to_container(container)[source]

Hook called, when the object is assigned to the container (currently from the container constructor)

get_path()[source]
info(*args, **kwargs)[source]
description(*args, **kwargs)[source]
_get_init_args_mapping_for_copy()[source]

Compute the dictionary that defines the attributes to create a copy of this object using the constructor.

Returns:

copy – The returning dictionary has this structure: { name of the argument of the __init__ function : name of the object attribute }

Return type:

Dict

_get_init_args_for_copy(**kwargs)[source]

Compute the dictionary-kwargs to create copy

Return type:

Dict[str, Any]

copy(**kwargs)[source]
create_object(container=None)[source]

Creates Section/Option/…. object (whose properties I define)

is_independent_on_the_predecessor = True
_copy_excluded_args = ['container', 'grammar_hooks']
enrich(option)[source]
property output_definition
accept_value(value)[source]

Return, whether a given value is accepted by this type of object. Accepting does not mean that the value can be validated. It is a base check, that a type of the value is suitable to be used here. If it is not – e.g. single value is assigned to a section – then the container can try to find another object (e.g. contained value of the same name) where the value is accepted.

Return type:

bool