def render(self, ctx=None): ''' :param ctx: rendering context in which the method was called :rtype: `Bits` :return: rendered value of the container ''' self._initialize() render_count = 1 if ctx is None: ctx = RenderContext() if self._need_second_pass: render_count = 2 ctx.push(self) if self.is_default(): self._current_rendered = self._default_rendered else: if self.offset is None: self.offset = 0 for i in range(render_count): offset = self.offset rendered = BitArray() for field in self._fields: field.set_offset(offset) frendered = field.render(ctx) if not isinstance(frendered, Bits): raise KittyException('the field %s:%s was rendered to type %s, you should probably wrap it with appropriate encoder' % ( field.get_name(), type(field), type(frendered))) rendered.append(frendered) offset += len(frendered) self.set_current_value(rendered) ctx.pop() return self._current_rendered
def get_rendered_fields(self, ctx=None): ''' :param ctx: rendering context in which the method was called :return: ordered list of the fields that will be rendered ''' if ctx is None: ctx = RenderContext() ctx.push(self) current = self._fields[self._field_idx] res = current.get_rendered_fields(ctx) ctx.pop() return res
def get_rendered_fields(self, ctx=None): ''' :param ctx: rendering context in which the method was called :return: ordered list of the fields that will be rendered ''' res = [] if ctx is None: ctx = RenderContext() if self._evaluate_condition(ctx): ctx.push(self) res = super(Conditional, self).get_rendered_fields(ctx) ctx.pop() return res
def get_rendered_fields(self, ctx=None): ''' :param ctx: rendering context in which the method was called :return: ordered list of the fields that will be rendered ''' if ctx is None: ctx = RenderContext() ctx.push(self) result = [] for f in self._fields: if len(f.render(ctx)): result.append(f) ctx.pop() return result
def render(self, ctx=None): ''' :param ctx: rendering context in which the method was called :rtype: `Bits` :return: rendered value of the container ''' self._initialize() if ctx is None: ctx = RenderContext() ctx.push(self) self.set_offset(self._offset, ctx) if self.is_default(): self._current_rendered = self._default_rendered ctx.pop() return self._default_rendered rendered = BitArray() offset = 0 for field in self._fields: frendered = field.render(ctx) if not isinstance(frendered, Bits): raise KittyException( 'the field %s:%s was rendered to type %s, you should probably wrap it with appropriate encoder' % (field.get_name(), type(field), type(frendered))) rendered.append(frendered) offset += len(frendered) self.set_current_value(rendered) ctx.pop() return self._current_rendered
def render(self, ctx=None): ''' Render only the mutated field (or the first one if not in mutation) :param ctx: rendering context in which the method was called :rtype: `Bits` :return: rendered value of the container ''' if ctx is None: ctx = RenderContext() ctx.push(self) self._initialize() rendered = self._fields[self._field_idx].render(ctx) self.set_current_value(rendered) ctx.pop() return self._current_rendered
def set_offset(self, offset, ctx=None): ''' Set the offset of a field :param offset: offset to set :param ctx: rendering context in which the method was called ''' if ctx is None: ctx = RenderContext() if offset is None: if self._enclosing is None: offset = 0 else: self._enclosing.set_offset(offset, ctx) offset = self._offset self._offset = offset ctx.push(self) res = self._fields[self._field_idx].set_offset(offset, ctx) ctx.pop() return res
def render(self, ctx=None): ''' Render the current value into a :class:`bitstring.Bits` object :rtype: :class:`bitstring.Bits` :return: the rendered field ''' self._initialize() if ctx is None: ctx = RenderContext() # # if we are called from within render, return a dummy object... # if self in ctx: self._current_rendered = self._in_render_value() else: ctx.push(self) self._rendered_field = self._field.render(ctx) self._render() ctx.pop() return self._current_rendered
def render(self, ctx=None): ''' Only render if condition applies :param ctx: rendering context in which the method was called :rtype: `Bits` :return: rendered value of the container ''' if ctx is None: ctx = RenderContext() self._initialize() if self in ctx: self._current_rendered = self._in_render_value() else: ctx.push(self) if self._evaluate_condition(ctx): super(Conditional, self).render(ctx) else: self.set_current_value(empty_bits) ctx.pop() return self._current_rendered
def render(self, ctx=None): ''' Render only a single field (see class docstring for more details) :param ctx: rendering context in which the method was called :rtype: `Bits` :return: rendered value of the container ''' if ctx is None: ctx = RenderContext() ctx.push(self) self._initialize() offset = self.offset if self.offset else 0 if self._mutating(): field_idx = self._field_idx else: key_field = self.resolve_field(self._key_field) key_from_key_field = key_field._current_value if key_from_key_field in self._keys: field_idx = self._keys.index(key_from_key_field) else: field_idx = 0 # default value self._fields[field_idx].set_offset(offset) rendered = self._fields[field_idx].render(ctx) self.set_current_value(rendered) ctx.pop() return self._current_rendered
def _calculate(self, field): ''' We want to avoid trouble, so if the field is not enclosed by any other field, we just return 0. ''' encloser = field.enclosing if encloser: rendered = encloser.get_rendered_fields(RenderContext(self)) if field not in rendered: value = len(rendered) else: value = rendered.index(field) else: value = 0 return value
def render(self, ctx=None): ''' Render the current value into a :class:`bitstring.Bits` object :rtype: :class:`bitstring.Bits` :return: the rendered field ''' self._initialize() if ctx is None: ctx = RenderContext() # # if we are called from within render, return a dummy object... # if self in ctx: self._current_rendered = self._in_render_value() else: ctx.push(self) if self.dependency_type == Calculated.VALUE_BASED: self._rendered_field = self._field.render(ctx) self._render() ctx.pop() return self._current_rendered
def _calculate(self, field): return len(field.get_rendered_fields(RenderContext(self)))