MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / encodeValue

Method encodeValue

pager_lib/pyasn1/codec/ber/encoder.py:559–674  ·  view source on GitHub ↗
(self, value, asn1Spec, encodeFun, **options)

Source from the content-addressed store, hash-verified

557 # TODO: handling three flavors of input is too much -- split over codecs
558
559 def encodeValue(self, value, asn1Spec, encodeFun, **options):
560
561 substrate = b''
562
563 omitEmptyOptionals = options.get(
564 'omitEmptyOptionals', self.omitEmptyOptionals)
565
566 if LOG:
567 LOG('%sencoding empty OPTIONAL components' % (
568 omitEmptyOptionals and 'not ' or ''))
569
570 if asn1Spec is None:
571 # instance of ASN.1 schema
572 inconsistency = value.isInconsistent
573 if inconsistency:
574 raise error.PyAsn1Error(
575 f"ASN.1 object {value.__class__.__name__} is inconsistent")
576
577 namedTypes = value.componentType
578
579 for idx, component in enumerate(value.values()):
580 if namedTypes:
581 namedType = namedTypes[idx]
582
583 if namedType.isOptional and not component.isValue:
584 if LOG:
585 LOG('not encoding OPTIONAL component %r' % (namedType,))
586 continue
587
588 if namedType.isDefaulted and component == namedType.asn1Object:
589 if LOG:
590 LOG('not encoding DEFAULT component %r' % (namedType,))
591 continue
592
593 if omitEmptyOptionals:
594 options.update(ifNotEmpty=namedType.isOptional)
595
596 # wrap open type blob if needed
597 if namedTypes and namedType.openType:
598
599 wrapType = namedType.asn1Object
600
601 if wrapType.typeId in (
602 univ.SetOf.typeId, univ.SequenceOf.typeId):
603
604 substrate += encodeFun(
605 component, asn1Spec,
606 **dict(options, wrapType=wrapType.componentType))
607
608 else:
609 chunk = encodeFun(component, asn1Spec, **options)
610
611 if wrapType.isSameTypeWith(component):
612 substrate += chunk
613
614 else:
615 substrate += encodeFun(chunk, wrapType, **options)
616

Callers

nothing calls this directly

Calls 4

isSameTypeWithMethod · 0.80
getMethod · 0.45
valuesMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected