summaryrefslogtreecommitdiff
path: root/pyasn1/codec/ber/encoder.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyasn1/codec/ber/encoder.py')
-rw-r--r--pyasn1/codec/ber/encoder.py48
1 files changed, 33 insertions, 15 deletions
diff --git a/pyasn1/codec/ber/encoder.py b/pyasn1/codec/ber/encoder.py
index 778aa86..6b77b70 100644
--- a/pyasn1/codec/ber/encoder.py
+++ b/pyasn1/codec/ber/encoder.py
@@ -17,7 +17,7 @@ from pyasn1.type import tag
from pyasn1.type import univ
from pyasn1.type import useful
-__all__ = ['encode']
+__all__ = ['Encoder', 'encode']
LOG = debug.registerLoggee(__name__, flags=debug.DEBUG_ENCODER)
@@ -706,7 +706,7 @@ class AnyEncoder(OctetStringEncoder):
return value, not options.get('defMode', True), True
-tagMap = {
+TAG_MAP = {
eoo.endOfOctets.tagSet: EndOfOctetsEncoder(),
univ.Boolean.tagSet: BooleanEncoder(),
univ.Integer.tagSet: IntegerEncoder(),
@@ -739,7 +739,7 @@ tagMap = {
}
# Put in ambiguous & non-ambiguous types for faster codec lookup
-typeMap = {
+TYPE_MAP = {
univ.Boolean.typeId: BooleanEncoder(),
univ.Integer.typeId: IntegerEncoder(),
univ.BitString.typeId: BitStringEncoder(),
@@ -774,14 +774,16 @@ typeMap = {
}
-class Encoder(object):
+class SingleItemEncoder(object):
fixedDefLengthMode = None
fixedChunkSize = None
- # noinspection PyDefaultArgument
- def __init__(self, tagMap, typeMap={}):
- self.__tagMap = tagMap
- self.__typeMap = typeMap
+ TAG_MAP = TAG_MAP
+ TYPE_MAP = TYPE_MAP
+
+ def __init__(self, tagMap=None, typeMap=None):
+ self.__tagMap = tagMap or self.TAG_MAP
+ self.__typeMap = typeMap or self.TYPE_MAP
def __call__(self, value, asn1Spec=None, **options):
try:
@@ -795,8 +797,11 @@ class Encoder(object):
'and "asn1Spec" not given' % (value,))
if LOG:
- LOG('encoder called in %sdef mode, chunk size %s for '
- 'type %s, value:\n%s' % (not options.get('defMode', True) and 'in' or '', options.get('maxChunkSize', 0), asn1Spec is None and value.prettyPrintType() or asn1Spec.prettyPrintType(), value))
+ LOG('encoder called in %sdef mode, chunk size %s for type %s, '
+ 'value:\n%s' % (not options.get('defMode', True) and 'in' or '',
+ options.get('maxChunkSize', 0),
+ asn1Spec is None and value.prettyPrintType() or
+ asn1Spec.prettyPrintType(), value))
if self.fixedDefLengthMode is not None:
options.update(defMode=self.fixedDefLengthMode)
@@ -804,12 +809,12 @@ class Encoder(object):
if self.fixedChunkSize is not None:
options.update(maxChunkSize=self.fixedChunkSize)
-
try:
concreteEncoder = self.__typeMap[typeId]
if LOG:
- LOG('using value codec %s chosen by type ID %s' % (concreteEncoder.__class__.__name__, typeId))
+ LOG('using value codec %s chosen by type ID '
+ '%s' % (concreteEncoder.__class__.__name__, typeId))
except KeyError:
if asn1Spec is None:
@@ -827,15 +832,28 @@ class Encoder(object):
raise error.PyAsn1Error('No encoder for %r (%s)' % (value, tagSet))
if LOG:
- LOG('using value codec %s chosen by tagSet %s' % (concreteEncoder.__class__.__name__, tagSet))
+ LOG('using value codec %s chosen by tagSet '
+ '%s' % (concreteEncoder.__class__.__name__, tagSet))
substrate = concreteEncoder.encode(value, asn1Spec, self, **options)
if LOG:
- LOG('codec %s built %s octets of substrate: %s\nencoder completed' % (concreteEncoder, len(substrate), debug.hexdump(substrate)))
+ LOG('codec %s built %s octets of substrate: %s\nencoder '
+ 'completed' % (concreteEncoder, len(substrate),
+ debug.hexdump(substrate)))
return substrate
+
+class Encoder(object):
+ SINGLE_ITEM_ENCODER = SingleItemEncoder
+
+ @classmethod
+ def __call__(cls, pyObject, asn1Spec=None, **options):
+ singleItemEncoder = cls.SINGLE_ITEM_ENCODER()
+ return singleItemEncoder(pyObject, asn1Spec=asn1Spec, **options)
+
+
#: Turns ASN.1 object into BER octet stream.
#:
#: Takes any ASN.1 object (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
@@ -887,4 +905,4 @@ class Encoder(object):
#: >>> encode(seq)
#: b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03'
#:
-encode = Encoder(tagMap, typeMap)
+encode = Encoder()