P1sec / pycrate

A Python library to ease the development of encoders and decoders for various protocols and file formats; contains ASN.1 and CSN.1 compilers.
GNU Lesser General Public License v2.1
381 stars 132 forks source link

pycrate_asn1compile.py -g #153

Closed russhousley closed 2 years ago

russhousley commented 2 years ago

Has anyone tried to write a different generator for use with pycrate_asn1compile.py? Just copying the generator from https://github.com/P1sec/pycrate/blob/master/pycrate_asn1c/generator.py, and adjusting the import statements to work for the directory containing the code does not provide a starting point. I get:

$pycrate_asn1compile.py -i foo.asn -o foo -g ./mygen.py /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/pycrate_asn1compile.py, args error: generator file does not contain a _Generator class in module <module 'mygen' from './mygen.pyc'> $

Any hints about getting started would be most welcome.

p1-bmu commented 2 years ago

As you can see in the pycrate_asn1compile.py tool, a custom code generator must have a _Generator subclass with a destination filename passed as init argument. This subclass is then called to work on all ASN.1 modules available into GLOBAL.MOD. This is the case of both PycrateGenerator and JSONDepGraphGenerator in generator.py.

Here is a skeleton for such a generator, if you prefer:

from pycrate_asn1c.generator import _Generator
from pycrate_asn1c.asnproc   import GLOBAL

class MyCodeGenerator(_Generator):

    def __init__(self, dstfile='/tmp/generated.txt'):
        print('Hello, I am a code generator !')
        print('There are %i modules to work on' % len(GLOBAL.MOD))
        # TODO: now generate some output from GLOBAL.MOD

I hope this helps.