pyasn1

[Python ASN1]: GeneralizedTime pyasn1 implementation


Here is my asn1 grammar representation:-

MiepPullWtdr ::= [0] SEQUENCE
{
    timeStamp                   [8]   GeneralizedTime
}

I need help in implementing the same using pyasn1.

Here is my code snippet:-

from pyasn1.type import univ, namedtype, tag, char, namedval, useful

class MiepPullWtdr(univ.Sequence):
    componentType = namedtype.NamedTypes(namedtype.NamedType('timeStamp', useful.GeneralizedTime().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 8)))

)

miepPullWtdr = MiepPullWtdr()
miepPullWtdr.setComponentByName('timeStamp', '201103081200Z')
print(miepPullWtdr.prettyPrint())

I also tried,

componentType  = namedtype.NamedTypes(namedtype.NamedType('timeStamp',useful.GeneralizedTime().tagSet(tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 8)))

Both doesn't work and gives a weird error regrading Syntax error:-

miepPullWtdr = MiepPullWtdr()
           ^
SyntaxError: invalid syntax

I have checked the commas and brackets. All seem to be correct.

Any help? Thanks


Solution

  • This is your code except for the dangling parenthesis of the componentType fixed. Works for me:

    from pyasn1.type import univ, namedtype, tag, namedval, useful
    
    class MiepPullWtdr(univ.Sequence):
        componentType = namedtype.NamedTypes(
            namedtype.NamedType('timeStamp', useful.GeneralizedTime().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 8)))
        )
    
    miepPullWtdr = MiepPullWtdr()
    miepPullWtdr.setComponentByName('timeStamp', '201103081200Z')
    
    print(miepPullWtdr.prettyPrint())