Source code for ase2sprkkr.common.configuration_definitions

"""
Configuration definitions are classes, that desribes
the syntax of a configuration file, or its parts
(sections or configuration options)

They are able both to parse a file, which results in an
instance of (an instance of :py:class:`ase2sprkkr.common.Configuration`,
e.g. an :py:class:`Option<ase2sprkkr.common.options.Option>` or
:py:class:`Section<ase2sprkkr.common.configuration_containers.Section>`
), or write such object to a file.
"""

import pyparsing as pp
from enum import Enum
import inspect
from typing import Callable, Dict, Iterable, Optional, Union, Any
import itertools
from . import backward_compatibility  # NOQA
import re

from .options import Dummy, DummyStub
from .decorators import cached_class_property, cached_property
from .grammar import as_delimiter, generate_grammar, Suppress, TokenConverter
from .grammar_types.basic import Separator, KeywordSeparator
from .repetition import Repeated, RepeatedItemGrammar
from .warnings import DataValidityWarning, ValidationResult


Validator = Callable[[Any, Any, str], Any]
_NO_GENERATED_KEY = object()


[docs] class _TreeState(Enum): UNFINALIZED = 0 FINALIZING = 1 FINALIZED = 2
[docs] def _definition_write_condition_always_true(_): return True
[docs] def _validate_warning_condition(item: Any, _container: Any, _why: str) -> Any: """Run the legacy one-value warning condition as an ordinary validator.""" condition = item._definition.warning_condition if condition is None: return None value = item() if item._definition.is_option else item result = condition(value) if result is None or isinstance(result, ValidationResult): return result return DataValidityWarning(result)
[docs] class NotAllowed: """Object of this class can be returned as a result of a :func:ConfigurationDefinition.condition"""
[docs] def __init__(self, msg): self.msg = msg
def __str__(self): return self.msg def __bool__(self): """When returned, the value is NOT ALLOWED""" return False
[docs] class BaseDefinition: """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 """ validate_parsed = None """Can be redefined for checks that require knowing which input items were explicitly parsed.""" Repeated = Repeated """Supported repetition modes; retained here as part of the public definition API.""" 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. """ # #
[docs] def _resolve_is_optional(self, is_optional): """Resolve an unspecified grammar-optional flag for this definition.""" return False if is_optional is None else is_optional
[docs] def __init__(self, name, is_optional=None, condition=None): self.name = name self.name_lcase = name.lower() self.is_optional = self._resolve_is_optional(is_optional) self.condition = condition self.grammar_hooks = [] self.condition = condition self.container = None self._tree_state = _TreeState.UNFINALIZED
is_repeated = Repeated.NO """ By default, the configuration items are not repeated """
[docs] def interactive_names(self): for i in self.all_names: yield re.sub(r"[()]", "", re.sub(r"[-\s.]", "_", i))
@property def all_names(self): yield self.name @property def real_name(self): return self.name
[docs] def has_name(self, name, lower_case=False): return name == (self.name_lcase if lower_case else self.name)
[docs] def allowed(self, container): if not self.condition: return True return self.condition(self, container)
[docs] def is_optional_for(self, item): """Evaluate whether the runtime item may be omitted from input.""" optional = self.is_optional return bool(optional(item)) if callable(optional) else bool(optional)
[docs] def add_grammar_hook(self, hook): """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. """ self.grammar_hooks.append(hook)
[docs] def remove_grammar_hook(self, hook): """Remove a grammar hook""" self.grammar_hooks.remove(hook)
[docs] def grammar(self, allow_dangerous: bool = False, *, allow_incomplete: bool = False): """Generate grammar with the correct settings of pyparsing global state. Parameters ---------- allow_dangerous 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 Allow missing non-optional input items, reporting them as validation issues. """ self._finalize() with generate_grammar(): return self._grammar and self._grammar( allow_dangerous, allow_incomplete=allow_incomplete )
[docs] def _get_root_definition(self): """Return the root of the definition tree containing this item.""" root = self while root.container is not None: root = root.container return root
[docs] def _finalize(self): """Finalize the complete definition tree before its first use.""" root = self._get_root_definition() if root._tree_state is not _TreeState.UNFINALIZED: return root._tree_state = _TreeState.FINALIZING try: root.added_to_container(None) except Exception: root._tree_state = _TreeState.UNFINALIZED raise else: root._tree_state = _TreeState.FINALIZED
@property def _grammar(self): """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 """ return self._hooked_grammar
[docs] def has_grammar(self): """Returns, whether the definition generates a grammar or not. By default, it just check the self._grammar""" return self._grammar
[docs] def _hooked_grammar(self, allow_dangerous: bool = False, **kwargs): """Generates grammar. Unlike :meth:`grammar`, it does not change the global pyparsing state to ensure that the generated grammar will handle whitespaces in a propper way. Unlike the :meth:`_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) """ out = self._create_grammar(allow_dangerous, **kwargs) return self._add_hooks_to_grammar(out)
[docs] def _add_hooks_to_grammar(self, grammar): """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. """ if self.grammar_hooks: for i in self.grammar_hooks: hooked = i(grammar) if hooked is not None: grammar = hooked return grammar
[docs] def added_to_container(self, container): """Hook called, when the object is assigned to the container (currently from the container constructor)""" self.container = container
[docs] def get_path(self): out = self.name if self.container and self.container.container: return self.container.get_path() + "." + out return out
[docs] def info(self, *args, **kwargs): return "This object is not intended for a direct use."
[docs] def description(self, *args, **kwargs): return "This object is not intended for a direct use."
[docs] def _get_init_args_mapping_for_copy(self) -> Dict[str, str]: """ Compute the dictionary that defines the attributes to create a copy of this object using the constructor. Returns ------- copy: Dict The returning dictionary has this structure: { name of the argument of the __init__ function : name of the object attribute } """ if "_copy_args" not in self.__class__.__dict__: args = inspect.getfullargspec(self.__class__.__init__).args[1:] self.__class__._copy_args = { value: "_" + value if "_" + value in self.__dict__ else value for value in args if value not in self._copy_excluded_args } return self.__class__._copy_args
[docs] def _get_init_args_for_copy(self, **kwargs) -> Dict[str, Any]: """Compute the dictionary-kwargs to create copy""" out = {k: getattr(self, v) for k, v in self._get_init_args_mapping_for_copy().items()} out.update(kwargs) return out
[docs] def copy(self, **kwargs): arguments = self._get_init_args_for_copy(**kwargs) return self.__class__(**arguments)
[docs] def create_object(self, container=None): """Creates Section/Option/.... object (whose properties I define)""" self._finalize() return self.result_class(self, container)
is_independent_on_the_predecessor = True _copy_excluded_args = ["container", "grammar_hooks"]
[docs] def enrich(self, option): return option
@property def output_definition(self): return self.__dict__.get("_output_definition", self) @output_definition.setter def output_definition(self, od): """Set a special object for writing""" self.__dict__["_output_definition"] = od
[docs] def accept_value(self, value) -> bool: """ 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 True
[docs] class RealItemDefinition(BaseDefinition): """A base class for a configuration definition, either of an option, or of a container. The definition determine the type of value(s) in the configuration/problem-definition file, the format of the values, the way how it/they is/are read etc... """ result_class = None """ Parsing of the data results in an instance of this class. To be redefined in descendants. """ is_stored = True """Real values and containers are present in input files by default.""" name_in_grammar = True """ Is the name of the value/section present in the file? When False, the option/section name is not written to the file at all. The value(s) of the option/section is/are then expected to be located in the file just after the previous one. By default, all options and sections… are named (in the configuration file). However, the attribute can be redefined in instantiated objects and/or descendant classes to change the behavior. """ info_in_data_description = False
[docs] def __init__( self, name, written_name=None, is_optional=None, is_hidden=False, is_expert=False, experimental=False, name_in_grammar=None, name_format=None, info=None, description=None, name_regex=None, condition=None, write_condition=None, result_class=None, is_repeated=False, repeated_delimiter=None, repeated_count=None, repeated_with_name=None, warning_condition: Optional[Callable[[Any], Any]] = None, validators: Optional[Union[Validator, Iterable[Validator]]] = (), plot=None, indent=None ): """ Parameters ---------- name: str | Tuple(str) Name of the value/section written_name: str or None Name to write to the input file. Default None means use the name. is_optional: bool or callable If True, the section/value may be omitted from input. A callable accepts its runtime item and is evaluated after parsing. is_hidden: boolean Hidden values are not offered to a user, usually they are set by another object (and so a direct setting of their values has no sense) is_expert: boolean Expert values/sections are not required and they are somewhat hidden from the user experimental: bool Whether this value or section is experimental (metadata only). name_in_grammar: boolean or None If False, there the name of the variable is not printed in the configuration file. The variable is recognized by its position. If None, the default class value is used name_format: str or None The way how the name is written info: str A short help message for the value/section. It will be the perex for description. description: str The additional informations for the users. write_condition If defined, write the value, only if write_condition(the option) is True. condition If defined, the condition - the condition.parse_condition() is invoked, when a 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 result_class Redefine the class that holds data for this option/section. is_repeated: bool or string The section can be repeated. If a non-empty string is given, the values are divided by the string. repeated_delimiter: string The delimiter used between repeated instances of self, if not repeated_with_name repeated_count: string or int or callable The number of repetitions. If string, the repetition is given from the parsed value at that exact path; ``..`` selects the parent section (for example ``..NPAN``). If callable, the function is called on the values of the local options named by its parameters. repeated_with_name: bool If True, the whole value-name pair is repeated warning_condition A one-value warning condition executed through the validator pipeline. validators A callable or an iterable of callables accepting the runtime item, its containing runtime section, and the validation reason (``set``, ``parse`` or ``save``). Validators return a :class:`DataValidityWarning`, :class:`DataValidityError`, an iterable of such issues, or None. indent Vytiskne daný string před názvem """ if isinstance(name, (tuple, list)): self.all_names = name name = name[0] else: self.all_names = (name,) if written_name: self.all_names = ( *self.all_names, written_name ) super().__init__(name, is_optional, condition) self.written_name = written_name or self.name """ The name of the option/section """ self.name_regex = name_regex self.is_expert = is_expert self.experimental = experimental self.is_hidden = is_hidden self.write_condition = write_condition or _definition_write_condition_always_true if name_in_grammar is not None: self.__dict__['name_in_grammar'] = name_in_grammar self._info = info """ A short help text describing the content for the users. """ self._description = description """ A longer help text describing the content for the users. """ if result_class: self.result_class = result_class self.warning_condition = warning_condition if validators is None: self.validators = () elif callable(validators): self.validators = (validators,) else: self.validators = tuple(validators) if not all(callable(validator) for validator in self.validators): raise TypeError("validators must be callable") if indent: if name_format is not None: raise ValueError("You can not specify both name_format and indent") name_format= indent + "{}" self.name_format = name_format self.plot = plot self.is_repeated = self.Repeated.create(is_repeated, self.default_repeated_type) if self.is_repeated.is_numbered and repeated_with_name and not self.name_in_grammar: raise ValueError("Repeated numbered items have to have its name in the grammar") self._repeated_delimiter = ( as_delimiter(repeated_delimiter) if repeated_delimiter is not None else None ) self.repeated_count = RepeatedItemGrammar.create(is_repeated, repeated_count) self.repeated_with_name = repeated_with_name
default_repeated_type = Repeated.REPEATED all_names = None # remove the property @property def repeated_delimiter(self): if self._repeated_delimiter is not None: return self._repeated_delimiter if self.repeated_with_name and self.container: return self.container.delimiter return pp.Empty().set_name("")
[docs] def added_to_container(self, container): """Hook called when this item is assigned to a parent container. Used to attach grammar hooks for dynamic repeated-count behavior. """ self.repeated_count.apply_hooks(container) super().added_to_container(container)
[docs] @cached_property def lower_case_names(self): return [i.lower() for i in self.all_names]
[docs] def enrich(self, option): if self.plot: if not isinstance(self.plot, tuple): plot = (self.plot, lambda self,**kwargs: 1) else: plot = self.plot option.plot = lambda **kwargs: plot[0](option, **kwargs) option.number_of_plots = lambda **kwargs: plot[1](option, **kwargs) option.plot.__doc__ = " Plot the data." option.number_of_plots.__doc__ = " Return the number of plots to draw."
[docs] def option_attribute_index(self, name): """Return an index exposed as ``option.<name>``, or ``None``.""" return None
@property def formated_name(self): name = self.written_name if self.name_format: return self.name_format.format(name) return name
[docs] def has_name(self, name, lower_case=False): return name in (self.lower_case_names if lower_case else self.all_names)
[docs] def run_validators(self, item: Any, container: Any, why: str) -> None: """Run validators for runtime ``item`` in ``container`` during ``why``.""" for validator in itertools.chain(self.intrinsic_validators, self.validators): ValidationResult.emit(validator(item, container, why))
[docs] def get_generated(self, option: Any, key: Any = _NO_GENERATED_KEY) -> Any: """Invoke a generated getter using its section-oriented API.""" if key is _NO_GENERATED_KEY: return self.getter(option._container) return self.getter(option._container, key)
[docs] def stage_generated( self, option: Any, transaction: Any, value: Any, key: Any = _NO_GENERATED_KEY, ) -> None: """Invoke a generated setter using its section-oriented API.""" if key is _NO_GENERATED_KEY: self.setter(option._container, value) else: self.setter(option._container, value, key)
intrinsic_validators = (_validate_warning_condition,)
[docs] def all_names_in_grammar(self): if not self.name_in_grammar: return yield from self.all_names
[docs] def allow_duplication(self): """Can be the item repeated in the output file""" return False
@property def is_independent_on_the_predecessor(self): """Some value have to be positioned just after their predecessor in the output. """ return self.name_in_grammar
[docs] def info(self, generic: bool = True) -> str: """Return short help string. Parameters ---------- generic If the definition has no help specified and generic is True, return a (not saying much) generic help string """ out = self._info if not out: if generic: out = self._generic_info() else: out = "" return out
_description_indentation = " " """ Nested levels of description will be indented using this 'prefix' """
[docs] def description(self, verbose: bool = True, show_hidden=False, prefix: str = ""): """ Parameters ---------- verbose: bool If true, add also detailed documentation of all included items (e.g. members of a container) show_hidden: bool If ``False`` (default), do not show descriptions of hidden members. prefix The string, with with each line will begin (commonly the spaces for the indentation). """ out = [prefix + self.info().replace("\n", "\n" + prefix), prefix] # out.append(f"{prefix}Data description\n" # f"{prefix}----------------") out.append(self.data_description(verbose, show_hidden, prefix)) if self._description: out.append("\n") out.append(prefix + self._description.replace("\n", "\n" + prefix)) return "\n".join(out)
[docs] def _grammar_of_name(self): """ Return grammar for the name (and possible alternative names etc.) """ numbering = self.is_repeated.grammar_numbering if self.name_regex: reg = pp.Regex(self.name_regex) if self.do_not_skip_whitespaces_before_name: reg.leave_whitespace() names = [pp.Regex(self.name_regex)] else: names = self.all_names_in_grammar() keyword = pp.CaselessLiteral if numbering else pp.CaselessKeyword if self.do_not_skip_whitespaces_before_name: names = [keyword(i).leave_whitespace() for i in names] else: names = [keyword(i) for i in names] if len(names) > 1: name = pp.Or(names) if self.do_not_skip_whitespaces_before_name: name = name.leave_whitespace() else: name = names[0] if self.is_repeated: if numbering: idx = pp.Word(pp.nums) if numbering.has_default: name += pp.Optional(idx, default="def") else: name += idx name += pp.WordEnd(pp.alphanums + "_") name.set_parse_action(lambda x: self.is_repeated.key_type(self.name, *x.asList()[1:])) return name
[docs] def _tuple_with_my_name(self, expr, delimiter=None, has_value: bool = True, name_in_grammar=None): """Create the grammar returning tuple (self.name, <result of the expr>) Parameters ---------- expr Pyparsing expresion for the value of the option/section delimiter Pyparsing expression for the name-value delimiter has_value If False, do not add the parsed value to the results. This can be used e.g. for separators (see :class:`ase2sprkkr.common.grammar_types.Separator`) etc. """ from ase2sprkkr.common.value_definitions import ValueDefinition if self.name_in_grammar if name_in_grammar is None else name_in_grammar: name = self._grammar_of_name() if delimiter: name += delimiter name.suppress() out = name - expr if has_value: out.add_parse_action(lambda x: tuple(x) ) else: out = Suppress(out) else: if has_value: out = TokenConverter(expr) def tupled(x): v = x[0] if isinstance(v, pp.ParseResults): v = v.asList() return (self.is_repeated.key_type(self.name), v) out.add_parse_action(tupled) else: out = Suppress(out) return out
do_not_skip_whitespaces_before_name = False _copy_excluded_args = BaseDefinition._copy_excluded_args + ["expert", "indent"]
[docs] class VirtualDefinition(BaseDefinition): """Base class for a definition, that do not have value, just control the flow of the parsing""" is_hidden = True counter = 1 item_type = "control item"
[docs] def create_object(self, container=None): return Dummy(self, container)
[docs] def __init__(self, name=None, template=None, is_optional=False, condition=None): if not name: if not template: template = self.__class__.__name__.upper() name = f"_{template}_{VirtualDefinition.counter}" VirtualDefinition.counter += 1 super().__init__(name, is_optional, condition)
[docs] def all_names_in_grammar(self): return iter(())
[docs] class ControlDefinition(VirtualDefinition): """Control definitions has no grammar, they just modify the other items of the container""" _grammar = None """ This object does not generate a grammar """
[docs] class Stub(VirtualDefinition): """Item that allows to reuse existing item on the other place e.g. in another branch of Switch"""
[docs] def __init__(self, item, name=None, is_optional=False, condition=None): super().__init__(name=None, template=f"STUB_FOR_{name or item}", is_optional=is_optional, condition=condition) self.item = item self.condition = None
[docs] def create_object(self, container=None): return DummyStub(self, container)
[docs] def _save_to_file(self, file, value, always=False, name_in_grammar=None, delimiter=""): item = value._container[self.item] if not always and not self.allowed(value._container): return False return item._save_to_file(file, always=True, name_in_grammar=name_in_grammar, delimiter=delimiter)
[docs] def _create_grammar(self, allow_dangerous=False, *, allow_incomplete=False, **kwargs): item = self.container[self.item] out = item._grammar(allow_dangerous, allow_incomplete=allow_incomplete) return pp.Forward() << out
@property def real_name(self): return self.item
[docs] class Ignored: """Output definition for an ignored option. Output definition can override the standard definition and set a special way how the item is read/writen: such option is not readed/writed at all... or is readed/writed by an another option, see :meth:`gather` """
[docs] @cached_class_property def singleton(cls): return cls()
_grammar = None
[docs] def has_grammar(self): return False
[docs] def _save_to_file(self, file, value, always=False, name_in_grammar=None, delimiter=""): return False
[docs] class Gather: """ Output definition for the element of grammar, that reads besides himself other grammar elements, such that their names goes first and then the values go. See gather."""
[docs] def __init__(self, *items, name_delimiter=" ", value_delimiter="\t", value_delimiter_grammar=""): if len(items) == 0: raise ValueError("Gather requires at least one value") self.items = items self.name_delimiter = name_delimiter self.value_delimiter = value_delimiter self.value_delimiter_grammar = value_delimiter_grammar
[docs] def _grammar(self, allow_dangerous=False, *, allow_incomplete=False, **kwargs): names = [i._grammar_of_name() for i in self.items if i.name_in_grammar] ln = len(names) delimiter = bool(names) def values(): nonlocal delimiter for i in self.items: yield i._grammar( allow_dangerous, allow_incomplete=allow_incomplete, name_value_delimiter=delimiter, name_in_grammar=False, original=True, ) delimiter = self.value_delimiter_grammar names.extend(values()) out = pp.And(names) def discard_names(x): x = x.asList() return x[ln:] out.set_parse_action(discard_names) return out
[docs] def _save_to_file(self, file, value, always=False, name_in_grammar=None, delimiter=""): if name_in_grammar is not False: names = self.name_delimiter.join(i.formated_name for i in self.items if i.name_in_grammar) else: names = None if names: value._definition.write_name(file, names, delimiter) delimiter = str(self.items[0].delimiter) else: delimiter = "" def write(i): val, write = i._written_value() if write: if not i._definition.write_value(file, i, val, delimiter): raise NotImplementedError("Gathered values have to be always written") else: raise NotImplementedError("Gathered values have to be always written") write(value) delimiter = self.value_delimiter for i in self.items[1:]: write(value._container[i.name]) return True
[docs] def gather(first, *members): """Modify the given option definitions, that they appears in the output file in the form:: <NAME> <NAME 2> <NAME 3> ... = <VALUE 1> <VALUE 2> <VALUE 3> ... """ first.output_definition = Gather(first, *members) for i in members: i.output_definition = Ignored.singleton return (first,) + members
[docs] def switch(item, values, condition=lambda x: x, name=None): switch = Switch(item, values, condition, name) return (switch,) + tuple(switch.all_values())
[docs] def _if_defined_helper(value): return None if value is None else True
[docs] def if_defined(item, values, not_values=[], name=None): if name is None: name = "IF_{item}" return switch(item, {None: not_values, True: values}, _if_defined_helper, name)
[docs] def if_not_defined(item, values, not_values=[], name=None): if name is None: name = "IF_{item}" return if_defined(item, not_values, values, name)
[docs] class Switch(ControlDefinition): """Items of this class can control, which elements of grammar will be active and which not""" create_object = None with generate_grammar(): empty = pp.Empty() empty.set_name("<DISABLED>")
[docs] def __init__(self, item, values, condition=lambda x: x, name=None, template=None): """ Parameters ---------- item The name of Option, whose value determine the active elements values Dictionary, with the possible values of the item in the keys and the active elements in the values. example:: V('TYPE', Keyword('SCALAR', 'COMPLEX'), Switch( {'SCALAR' : V('SCALAR' : int), 'COMPLEX': V('COMPLEX', complex) } desribes both the following files:: TYPE = SCALAR SCALAR = 1 and:: TYPE=COMPLEX COMPLEX=1e5 7e5 name Not needed to be supplied, it can be autogenerated. """ used = set() self.item = item self.fn = condition def create(n): nonlocal used if n.name in used: return Stub(n.name, is_optional=n.is_optional, condition=self) else: used.add(n.name) return n def convert(v): if isinstance(v, dict): return {i: create(v) for i, v in v.items()} elif isinstance(v, str): # strings are "symlinks", will be resolved in the second run return None elif not isinstance(v, (tuple, list)): return {v.name: create(v)} else: return {i.name: create(i) for i in v} self.values = {k: convert(v) for k, v in values.items()} # resolve the "symlinks" for i in self.values: if self.values[i] is None: self.values[i] = self.values[values[i]] self.container = None # self.copied = False if template is None and name is None: name = f"_SWITCH_{item}" super().__init__(name, template)
[docs] def copy(self): raise NotImplementedError
def __call__(self, definition, data): return definition in self.items_for(self.switch_value(data))
[docs] def items_for(self, value): return self.values[value].values()
[docs] def all_values(self): return itertools.chain.from_iterable((i.values() for i in self.values.values()))
[docs] def prepare_grammar(self, definition, grammar): """Childs will have either the original grammar, or empty - set by _choose method. So replace the grammar by the forward. """ if not self.container.force_order: return grammar f = pp.Forward() f <<= self.empty f.set_name(f"<IF* ? THEN {str(grammar)}") self._grammars[definition.name] = (grammar, f) return f
[docs] def item_hook(self, grammar): grammars = self._grammars = {} def choose(value): def ok(original, forward): forward <<= original forward.set_name(f"<IF True THEN {str(original)}>") return True def not_ok(original, forward): forward <<= self.empty forward.set_name(f"<IF False THEN {str(original)}>") return False for k, items in self.values.items(): fn = ok if k == value else not_ok for kk, i in items.items(): tpl = grammars.get(i.name, None) if tpl: fn(tpl[0], tpl[1]) elif i.output_definition.has_grammar(): raise KeyError(f"In Switch, the item {i.name} for case {value} was not prepared") if not self.container.force_order: return grammar return grammar.add_parse_action(lambda x: choose(x[0][1]) and x)
[docs] def remove_from_container(self): if self.container: source = self.container[self.item] if self.item_hook in source.grammar_hooks: source.remove_grammar_hook(self.item_hook) for i in self.values.values(): for j in i.values(): j.condition = None
[docs] def added_to_container(self, container): self.remove_from_container() if container: # copying is not implemented yet # if self.copied: # self.copied = False # self.values = { k:{ n : container[n] for n in v } for k,v in self.values } container[self.item].add_grammar_hook(self.item_hook) for i in self.values.values(): for j in i.values(): j.condition = self super().added_to_container(container)
def __del__(self): self.remove_from_container()
[docs] def switch_value(self, container): return self.fn(container.get(self.item))
@property def condition_name(self): return self.item
[docs] class SeparatorDefinition(VirtualDefinition): """Basic class for separators""" def __repr__(self): return "<SEPARATOR>"
[docs] def __init__(self, separator_type=None, length=None, write_condition=None): super().__init__(template="SEPARATOR") self.write_condition = write_condition if separator_type is not None: if length is not None: separator_type = Separator(char=separator_type, length=length) if isinstance(separator_type, str): separator_type = KeywordSeparator(separator_type) self.separator_type = separator_type
[docs] def _create_grammar(self, allow_dangerous=False, *, allow_incomplete=False): return pp.Suppress(self.separator_type.grammar())
[docs] def _save_to_file(self, file, value, always=False, name_in_grammar=None, delimiter=""): if self.write_condition and not self.write_condition(value): return False if not always: if self.condition and self.condition(value): return False if delimiter: file.write(str(delimiter)) self.separator_type.write(file, None) return True