def __init__(self, spec, extras=None, actions=None, name=None, value=None, value_func=None, elements=None, default=None): # pylint: disable=too-many-arguments,too-many-branches if isinstance(spec, binary_type): spec = spec.decode(locale.getpreferredencoding()) self._spec = spec self._value = value self._value_func = value_func if extras is None: extras = {} if actions is None: actions = {} if elements is None: elements = {} # Convert extras argument from sequence to mapping. if isinstance(extras, (tuple, list)): mapping = {} for element in extras: if not isinstance(element, ElementBase): self._log.error("Invalid extras item: %s", element) raise TypeError("Invalid extras item: %s" % element) if not element.name: self._log.error("Extras item does not have a name: %s", element) raise TypeError("Extras item does not have a name: %s" % element) if element.name in mapping: self._log.warning("Multiple extras items with the same name: %s", element) mapping[element.name] = element extras = mapping elif not isinstance(extras, dict): self._log.error("Invalid extras argument: %s", extras) raise TypeError("Invalid extras argument: %s" % extras) # Temporary transition code so that both "elements" and "extras" # are supported as keyword arguments. if extras and elements: extras = dict(extras) extras.update(elements) elif elements: extras = elements self._extras = extras try: tree = self._parser.parse(spec) except Exception as e: self._log.error("Exception raised parsing %r: %s", spec, e) raise ParseError("Exception raised parsing %r: %s" % (spec, e)) try: element = CompoundTransformer(self._extras).transform(tree) except Exception as e: self._log.error("Exception raised transforming %r: %s", spec, e) raise ParseError("Exception raised transforming %r: %s" % (spec, e)) Alternative.__init__(self, (element,), name=name, default=default)
def __init__(self, name, choices, extras=None, default=None): # Argument type checking. assert isinstance(name, string_types) or name is None assert isinstance(choices, dict) for k, v in choices.items(): assert isinstance(k, string_types) # Construct children from the given choice keys and values. self._choices = choices self._extras = extras children = [] for k, v in choices.items(): child = Compound(spec=k, value=v, extras=extras) children.append(child) # Initialize super class. Alternative.__init__(self, children=children, name=name, default=default)
def value(self, node): if self._value_func is not None: # Prepare *extras* dict for passing to value_func(). extras = {"_node": node} for name, element in self._extras.items(): extra_node = node.get_child_by_name(name, shallow=True) if extra_node: extras[name] = extra_node.value() elif element.has_default(): extras[name] = element.default try: value = self._value_func(node, extras) except Exception as e: self._log.warning("Exception from value_func: %s", e) raise return value elif self._value is not None: return self._value else: return Alternative.value(self, node)
class _UserDictationSequenceRule(CompoundRule): spec = "<__kaldi_user_dictation_sequence>" extras = [ Repetition(Alternative([ BaseDictation(format=False), ListRef('__kaldi_user_dictation_listref', user_dictation_list), DictListRef('__kaldi_user_dictation_dictlistref', user_dictation_dictlist), ]), min=1, max=16, name="__kaldi_user_dictation_sequence"), ] exported = False def value(self, node): # This method returns the value of the root Repetition: a list of values of # Alternatives, each (dictation or listref or dictlistref) being a string. chunks = node.children[0].value() # Make sure each chunk is a list (of strings), then concat them together. return list( itertools.chain.from_iterable( [chunk] if not isinstance(chunk, list) else chunk for chunk in chunks))
def alternative(self, args): return Alternative(args)