Beispiel #1
0
def main():
    arg_parser = argparse.ArgumentParser(
        description='Generate Python classes from an ASN.1 definition file.'
        'Output to stdout by default.')
    arg_parser.add_argument('file', help='the ASN.1 file to process')
    arg_parser.add_argument('--split',
                            action='store_true',
                            help='output multiple modules to separate files')
    args = arg_parser.parse_args()

    with open(args.file, 'r') as data:
        asn1def = data.read()

    parse_tree = parser.parse_asn1(asn1def)

    modules = build_semantic_model(parse_tree)
    if len(modules) > 1 and not args.split:
        print('WARNING: More than one module generated to the same stream.',
              file=sys.stderr)

    output_file = sys.stdout
    for module in modules:
        try:
            if args.split:
                output_file = open(_sanitize_module(module.name) + '.py', 'w')
            print(pygen.auto_generated_header(args.file, __version__),
                  file=output_file)
            generate_pyasn1(module, output_file, modules)
        finally:
            if output_file != sys.stdout:
                output_file.close()

    return 0
Beispiel #2
0
def main():
    arg_parser = argparse.ArgumentParser(description='Generate Python classes from an ASN.1 definition file. Output to stdout by default.')
    arg_parser.add_argument('file', metavar='file', type=argparse.FileType('r'),
                            help='the ASN.1 file to process')
    arg_parser.add_argument('--split', action='store_true',
                            help='output multiple modules to separate files')
    args = arg_parser.parse_args()
    asn1def = args.file.read()

    parse_tree = parser.parse_asn1(asn1def)

    modules = build_semantic_model(parse_tree)
    if len(modules) > 1 and not args.split:
        print('WARNING: More than one module generated to the same stream.', file=sys.stderr)

    output_file = sys.stdout
    for module in modules:
        try:
            if args.split:
                output_file = open(_sanitize_module(module.name) + '.py', 'w')
            print(pygen.auto_generated_header(), file=output_file)
            generate_pyasn1(module, output_file, modules)
        finally:
            if output_file != sys.stdout:
                output_file.close()

    return 0
Beispiel #3
0
def main(args):
    with open(args.file, 'r') as data:
        asn1def = data.read()

    parse_tree = parser.parse_asn1(asn1def)

    modules = build_semantic_model(parse_tree)
    if len(modules) > 1 and not args.split:
        print('WARNING: More than one module generated to the same stream.',
              file=sys.stderr)

    header = pygen.auto_generated_header(args.file, __version__)
    if not args.split:
        # Print header once and then reset so we don't emit it for every module
        print(header, file=sys.stdout)
        header = None

    for module in modules:
        if args.split:
            outfile = _sanitize_module(module.name) + '.py'
        else:
            outfile = '-'

        with _maybe_open(outfile) as output_file:
            generate_pyasn1(module, output_file, modules, header=header)

    return 0
Beispiel #4
0
def main(args):
    with open(args[0]) as f:
        asn1def = f.read()

    parse_tree = parser.parse_asn1(asn1def)

    modules = build_semantic_model(parse_tree)
    if len(modules) > 1:
        print('WARNING: More than one module generated to the same stream.', file=sys.stderr)

    for module in modules:
        print(pygen.auto_generated_header())
        generate_pyasn1(module, sys.stdout)

    return 0
Beispiel #5
0
def main(args):
    with open(args[0]) as f:
        asn1def = f.read()

    parse_tree = parser.parse_asn1(asn1def)

    modules = build_semantic_model(parse_tree)
    if len(modules) > 1:
        print('WARNING: More than one module generated to the same stream.',
              file=sys.stderr)

    for module in modules:
        print(pygen.auto_generated_header())
        generate_pyasn1(module, sys.stdout)

    return 0
Beispiel #6
0
def main():
    args = parse_args()
    with open(args.file) as f:
        asn1def = f.read()

    parse_tree = parser.parse_asn1(asn1def)
    if args.parse:
        parser.print_parse_tree(parse_tree)
        return 0

    modules = sema.build_semantic_model(parse_tree)
    if args.sema:
        for module in modules:
            print(module)
        return 0

    if args.gen:
        for module in modules:
            print(pygen.auto_generated_header(args.file, __version__))
            pyasn1gen.generate_pyasn1(module, sys.stdout, modules)

    return 0
Beispiel #7
0
def main():
    args = parse_args()
    with open(args.file) as f:
        asn1def = f.read()

    parse_tree = parser.parse_asn1(asn1def)
    if args.parse:
        parser.print_parse_tree(parse_tree)
        return 0

    modules = sema.build_semantic_model(parse_tree)
    if args.sema:
        for module in modules:
            print(module)
        return 0

    if args.gen:
        for module in modules:
            print(pygen.auto_generated_header(args.file, __version__))
            pyasn1gen.generate_pyasn1(module, sys.stdout, modules)

    return 0
Beispiel #8
0
def main(args):
    with open(args.file, 'r') as data:
        asn1def = data.read()

    parse_tree = parser.parse_asn1(asn1def)

    modules = build_semantic_model(parse_tree)
    if len(modules) > 1 and not args.split:
        print('WARNING: More than one module generated to the same stream.', file=sys.stderr)

    header = pygen.auto_generated_header(args.file, __version__)
    if args.include_asn1:
        header += 'ASN1_SOURCES = {}'
        header += os.linesep

    if not args.split:
        # Print header once and then reset so we don't emit it for every module
        print(header, file=sys.stdout)
        header = None

    for module in modules:
        if args.split:
            outfile = _sanitize_module(module.name) + '.py'
        else:
            outfile = '-'

        if args.include_asn1:
            footer = 'ASN1_SOURCES[%r] = %s' % (module.name,
                                                pygen.format_longstring(str(module)))
            footer += os.linesep
        else:
            footer = None

        with _maybe_open(outfile) as output_file:
            generate_pyasn1(module, output_file, modules, header=header, footer=footer)

    return 0
Beispiel #9
0
def generate_code_to_file(input_name, module, modules, file):
    print(pygen.auto_generated_header(input_name, __version__), file=file)
    pyasn1gen.generate_pyasn1(module, file, modules)