How to use the asn1tools.codecs.gser.Type function in asn1tools

To help you get started, we’ve selected a few asn1tools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
return 'ObjectDescriptor({})'.format(self.name)


class UTCTime(Type):

    def __init__(self, name):
        super(UTCTime, self).__init__(name, 'UTCTime')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(utc_time_from_datetime(data))

    def __repr__(self):
        return 'UTCTime({})'.format(self.name)


class GeneralizedTime(Type):

    def __init__(self, name):
        super(GeneralizedTime, self).__init__(name, 'GeneralizedTime')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(generalized_time_from_datetime(data))

    def __repr__(self):
        return 'GeneralizedTime({})'.format(self.name)


class Date(Type):

    def __init__(self, name):
        super(Date, self).__init__(name, 'DATE')
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
super(TimeOfDay, self).__init__(name, 'TIME-OF-DAY')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(str(data))


class DateTime(Type):

    def __init__(self, name):
        super(DateTime, self).__init__(name, 'DATE-TIME')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(str(data).replace(' ', 'T'))


class Any(Type):

    def __init__(self, name):
        super(Any, self).__init__(name, 'ANY')

    def encode(self, data, _separator, _indent):
        data = binascii.hexlify(data).decode('ascii').upper()

        return "'{}'H".format(data)

    def __repr__(self):
        return 'Any({})'.format(self.name)


class Recursive(Type, compiler.Recursive):

    def __init__(self, name, type_name, module_name):
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
return 'GeneralString({})'.format(self.name)


class BMPString(Type):

    def __init__(self, name):
        super(BMPString, self).__init__(name, 'BMPString')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(data)

    def __repr__(self):
        return 'BMPString({})'.format(self.name)


class GraphicString(Type):

    def __init__(self, name):
        super(GraphicString, self).__init__(name, 'GraphicString')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(data)

    def __repr__(self):
        return 'GraphicString({})'.format(self.name)


class UniversalString(Type):

    def __init__(self, name):
        super(UniversalString, self).__init__(name, 'UniversalString')
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
return u'"{}"'.format(generalized_time_from_datetime(data))

    def __repr__(self):
        return 'GeneralizedTime({})'.format(self.name)


class Date(Type):

    def __init__(self, name):
        super(Date, self).__init__(name, 'DATE')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(str(data))


class TimeOfDay(Type):

    def __init__(self, name):
        super(TimeOfDay, self).__init__(name, 'TIME-OF-DAY')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(str(data))


class DateTime(Type):

    def __init__(self, name):
        super(DateTime, self).__init__(name, 'DATE-TIME')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(str(data).replace(' ', 'T'))
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
class Set(MembersType):

    def __init__(self, name, members):
        super(Set, self).__init__(name, members, 'SET')


class SetOf(ArrayType):

    def __init__(self, name, element_type):
        super(SetOf, self).__init__(name,
                                    'SET OF',
                                    element_type)


class Choice(Type):

    def __init__(self, name, members):
        super(Choice, self).__init__(name, 'CHOICE')
        self.members = members
        self.name_to_member = {member.name: member for member in self.members}

    def format_names(self):
        return format_or(sorted([member.name for member in self.members]))

    def encode(self, data, separator, indent):
        try:
            member = self.name_to_member[data[0]]
        except KeyError:
            raise EncodeError(
                "Expected choice {}, but got '{}'.".format(
                    self.format_names(),
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
return 'UTCTime({})'.format(self.name)


class GeneralizedTime(Type):

    def __init__(self, name):
        super(GeneralizedTime, self).__init__(name, 'GeneralizedTime')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(generalized_time_from_datetime(data))

    def __repr__(self):
        return 'GeneralizedTime({})'.format(self.name)


class Date(Type):

    def __init__(self, name):
        super(Date, self).__init__(name, 'DATE')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(str(data))


class TimeOfDay(Type):

    def __init__(self, name):
        super(TimeOfDay, self).__init__(name, 'TIME-OF-DAY')

    def encode(self, data, _separator, _indent):
        return u'"{}"'.format(str(data))
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
return 'Real({})'.format(self.name)


class Null(Type):

    def __init__(self, name):
        super(Null, self).__init__(name, 'NULL')

    def encode(self, _data, _separator, _indent):
        return 'NULL'

    def __repr__(self):
        return 'Null({})'.format(self.name)


class BitString(Type):

    def __init__(self, name):
        super(BitString, self).__init__(name, 'BIT STRING')

    def encode(self, data, _separator, _indent):
        encoded = int(binascii.hexlify(data[0]), 16)
        encoded |= (0x80 << (8 * len(data[0])))

        return "'{}'B".format(bin(encoded)[10:10 + data[1]]).upper()

    def __repr__(self):
        return 'BitString({})'.format(self.name)


class OctetString(Type):
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
self.element_type)


class Boolean(Type):

    def __init__(self, name):
        super(Boolean, self).__init__(name, 'BOOLEAN')

    def encode(self, data, _separator, _indent):
        return 'TRUE' if data else 'FALSE'

    def __repr__(self):
        return 'Boolean({})'.format(self.name)


class Integer(Type):

    def __init__(self, name):
        super(Integer, self).__init__(name, 'INTEGER')

    def encode(self, data, _separator, _indent):
        return str(data)

    def __repr__(self):
        return 'Integer({})'.format(self.name)


class Real(Type):

    def __init__(self, name):
        super(Real, self).__init__(name, 'REAL')
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
return 'Boolean({})'.format(self.name)


class Integer(Type):

    def __init__(self, name):
        super(Integer, self).__init__(name, 'INTEGER')

    def encode(self, data, _separator, _indent):
        return str(data)

    def __repr__(self):
        return 'Integer({})'.format(self.name)


class Real(Type):

    def __init__(self, name):
        super(Real, self).__init__(name, 'REAL')

    def encode(self, data, _separator, _indent):
        if data == float('inf'):
            data = 'PLUS-INFINITY'
        elif data == float('-inf'):
            data = 'MINUS-INFINITY'
        elif math.isnan(data):
            raise EncodeError('Cannot encode floating point number NaN.')
        elif data == 0.0:
            data = '0'
        else:
            data = '{}E0'.format(data)
github eerimoq / asn1tools / asn1tools / codecs / gser.py View on Github external
self.__class__.__name__,
                        name,
                        data))

        encoded_members = ','.join(encoded_members)

        return separator.join(['{' + encoded_members, '}'])

    def __repr__(self):
        return '{}({}, [{}])'.format(
            self.__class__.__name__,
            self.name,
            ', '.join([repr(member) for member in self.members]))


class ArrayType(Type):

    def __init__(self, name, type_name, element_type):
        super(ArrayType, self).__init__(name, type_name)
        self.element_type = element_type

    def encode(self, data, separator, indent):
        encoded_elements = []
        element_separator = separator + ' ' * indent

        for entry in data:
            encoded_element = self.element_type.encode(entry,
                                                       element_separator,
                                                       indent)
            encoded_element = u'{}{}'.format(element_separator,
                                             encoded_element)
            encoded_elements.append(encoded_element)