def _cast(self, _input, _output): """ Transforms a pair of input/output into the real slim shoutput. :param _input: Bag :param _output: mixed :return: Bag """ if isenvelope(_output): _output, _flags, _options = _output.unfold() else: _flags, _options = [], {} if len(_flags): # TODO: parse flags to check constraints are respected (like not modified alone, etc.) if F_NOT_MODIFIED in _flags: if self._output_type: return ensure_tuple(_input, cls=self._output_type) return _input if F_INHERIT in _flags: if self._output_type is None: self._output_type = concat_types(self._input_type, self._input_length, self._output_type, len(_output)) _output = _input + ensure_tuple(_output) if not self._output_type: if issubclass(type(_output), tuple): self._output_type = type(_output) return ensure_tuple(_output, cls=self._output_type)
def write(self, *messages): """ Push a message list to this context's input queue. :param mixed value: message """ for message in messages: if isinstance(message, Token): self.input.put(message) elif self._input_type: self.input.put(ensure_tuple(message, cls=self._input_type)) else: self.input.put(ensure_tuple(message))
def split_token(output): """ Split an output into token tuple, real output tuple. :param output: :return: tuple, tuple """ output = ensure_tuple(output) flags, i, len_output, data_allowed = set(), 0, len(output), True while i < len_output and isflag(output[i]): if output[i].must_be_first and i: raise ValueError('{} flag must be first.'.format(output[i])) if i and output[i - 1].must_be_last: raise ValueError('{} flag must be last.'.format(output[i - 1])) if output[i] in flags: raise ValueError('Duplicate flag {}.'.format(output[i])) flags.add(output[i]) data_allowed &= output[i].allows_data i += 1 output = output[i:] if not data_allowed and len(output): raise ValueError( 'Output data provided after a flag that does not allow data.') return flags, output
def setup(self, *context): if self._stack is not None: raise RuntimeError("Cannot setup context currification twice.") self._stack, self._stack_values = list(), list() for processor in resolve_processors(self.wrapped): _processed = processor(self.wrapped, *context, *self.args, **self.kwargs) _append_to_context = next(_processed) self._stack_values.append(_append_to_context) if _append_to_context is not None: self.args += ensure_tuple(_append_to_context) self._stack.append(_processed)
def setup(self, *context): if self._stack is not None: raise RuntimeError('Cannot setup context currification twice.') self._stack, self._stack_values = list(), list() for processor in resolve_processors(self.wrapped): _processed = processor(self.wrapped, *context, *self.args, **self.kwargs) _append_to_context = next(_processed) self._stack_values.append(_append_to_context) if _append_to_context is not None: self.args += ensure_tuple(_append_to_context) self._stack.append(_processed)
def _cast(self, _input, _output): """ Transforms a pair of input/output into the real slim output. :param _input: Bag :param _output: mixed :return: Bag """ tokens, _output = split_token(_output) if NOT_MODIFIED in tokens: return ensure_tuple(_input, cls=(self.output_type or tuple)) if INHERIT in tokens: if self._output_type is None: self._output_type = concat_types(self._input_type, self._input_length, self._output_type, len(_output)) _output = _input + ensure_tuple(_output) return ensure_tuple(_output, cls=(self._output_type or tuple))
def write(self, *messages): """ Push a message list to this context's input queue. :param mixed value: message """ for message in messages: if not isinstance(message, Token): message = ensure_tuple(message, cls=self._input_type, length=self._input_length ) # lgtm [py/call/wrong-named-argument] if self._input_length is None: self._input_length = len(message) self.input.put(message)
def write(self, file, context, *values, fs): context.setdefault('lineno', 0) fields = context.get_input_fields() if not context.lineno: context.writer = self.writer_factory(file) if fields: context.writer(fields) context.lineno += 1 if fields: if len(values) != len(fields): raise ValueError( 'Values length differs from input fields length. Expected: {}. Got: {}. Values: {!r}.' .format(len(fields), len(values), values)) context.writer(values) else: for arg in values: context.writer(ensure_tuple(arg)) return NOT_MODIFIED
def write(self, file, context, *values, fs): context.setdefault("lineno", 0) fields = coalesce(self.fields, context.get_input_fields()) if not context.lineno: context.writer = self.writer_factory(file) if fields and not self.skip_header: context.writer(fields) context.lineno += 1 if fields: if len(values) != len(fields): raise ValueError( "Values length differs from input fields length. Expected: {}. Got: {}. Values: {!r}.".format( len(fields), len(values), values ) ) context.writer(values) else: context.writer(ensure_tuple(values)) return NOT_MODIFIED
def write(self, file, context, *values, fs): context.setdefault('lineno', 0) fields = context.get_input_fields() if not context.lineno: context.writer = self.writer_factory(file) if fields: context.writer(fields) context.lineno += 1 if fields: if len(values) != len(fields): raise ValueError( 'Values length differs from input fields length. Expected: {}. Got: {}. Values: {!r}.'.format( len(fields), len(values), values ) ) context.writer(values) else: for arg in values: context.writer(ensure_tuple(arg)) return NOT_MODIFIED
def test_ensure_tuple(): assert ensure_tuple('a') == ('a', ) assert ensure_tuple(('a', )) == ('a', ) assert ensure_tuple(()) is ()
def test_ensure_tuple(): assert ensure_tuple("a") == ("a", ) assert ensure_tuple(("a", )) == ("a", ) assert ensure_tuple(()) is ()