<commit_before># -*- coding: utf-8 -*-
"""
Parse and translate an EBNF grammar into a Python parser for
the described language.
"""
from __future__ import print_function, division, absolute_import, unicode_literals
import sys
import os
import argparse
from .bootstrap import GrakoGrammarGenerator
from .exceptions import GrakoException

DESCRIPTION = ('Grako (for grammar compiler) takes grammars'
               ' in a variation of EBNF as input, and outputs a memoizing'
               ' PEG parser in Python.'
               )
argparser = argparse.ArgumentParser(prog='grako',
                                    description=DESCRIPTION
                                    )

argparser.add_argument('filename',
                       metavar='grammar',
                       help='The filename of the grammar to gencode( a parser for'
                       )
argparser.add_argument('-m', '--name',
                       nargs=1,
                       metavar='name',
                       help="An optional name for the grammar. It defaults to the basename of the grammar file's name"
                       )
argparser.add_argument('-o', '--outfile',
                       metavar='outfile',
                       help='specify where the output should go (default is stdout)'
                       )
argparser.add_argument('-t', '--trace',
                       help='produce verbose parsing output',
                       action='store_true'
                       )


def genmodel(name, grammar, trace=False, filename=None):
    parser = GrakoGrammarGenerator(name, trace=trace)
    return parser.parse(grammar, filename=filename)


def gencode(name, grammar, trace=False, filename=None):
    model = genmodel(name, grammar, trace=trace, filename=filename)
    return model.render()


def main():
    import logging
    logging.basicConfig(format='%(levelname)s: %(message)s')
    log = logging.getLogger()
    try:
        args = argparser.parse_args()
    except Exception as e:
        print(e)
        sys.exit(1)
    filename = args.filename
    outfile = args.outfile
    name = args.name
    if name is None:
        name = os.path.splitext(os.path.basename(filename))[0]
    if outfile and os.path.isfile(outfile):
        os.unlink(outfile)
    grammar = open(filename, 'r').read()
    try:
        parser = gencode(name, grammar, trace=args.trace, filename=filename)
        if outfile:
            dirname = os.path.dirname(outfile)
            if dirname and not os.path.isdir(dirname):
                os.makedirs(dirname)
            open(outfile, 'w').write(parser)
        else:
            print(parser)
    except GrakoException as e:
        log.error(str(e))

if __name__ == '__main__':
    main()
<commit_msg>Programs expect the package to export all exceptions.<commit_after># -*- coding: utf-8 -*-
"""
Parse and translate an EBNF grammar into a Python parser for
the described language.
"""
from __future__ import print_function, division, absolute_import, unicode_literals
import sys
import os
import argparse
from .bootstrap import GrakoGrammarGenerator
from .exceptions import *

DESCRIPTION = ('Grako (for grammar compiler) takes grammars'
               ' in a variation of EBNF as input, and outputs a memoizing'
               ' PEG parser in Python.'
               )
argparser = argparse.ArgumentParser(prog='grako',
                                    description=DESCRIPTION
                                    )

argparser.add_argument('filename',
                       metavar='grammar',
                       help='The filename of the grammar to gencode( a parser for'
                       )
argparser.add_argument('-m', '--name',
                       nargs=1,
                       metavar='name',
                       help="An optional name for the grammar. It defaults to the basename of the grammar file's name"
                       )
argparser.add_argument('-o', '--outfile',
                       metavar='outfile',
                       help='specify where the output should go (default is stdout)'
                       )
argparser.add_argument('-t', '--trace',
                       help='produce verbose parsing output',
                       action='store_true'
                       )


def genmodel(name, grammar, trace=False, filename=None):
    parser = GrakoGrammarGenerator(name, trace=trace)
    return parser.parse(grammar, filename=filename)


def gencode(name, grammar, trace=False, filename=None):
    model = genmodel(name, grammar, trace=trace, filename=filename)
    return model.render()


def main():
    import logging
    logging.basicConfig(format='%(levelname)s: %(message)s')
    log = logging.getLogger()
    try:
        args = argparser.parse_args()
    except Exception as e:
        print(e)
        sys.exit(1)
    filename = args.filename
    outfile = args.outfile
    name = args.name
    if name is None:
        name = os.path.splitext(os.path.basename(filename))[0]
    if outfile and os.path.isfile(outfile):
        os.unlink(outfile)
    grammar = open(filename, 'r').read()
    try:
        parser = gencode(name, grammar, trace=args.trace, filename=filename)
        if outfile:
            dirname = os.path.dirname(outfile)
            if dirname and not os.path.isdir(dirname):
                os.makedirs(dirname)
            open(outfile, 'w').write(parser)
        else:
            print(parser)
    except GrakoException as e:
        log.error(str(e))

if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.09965573473455336, 'fraction_numerical': 0.0014495379597753216, 'mean_word_length': 2.0147460404150737, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '461668', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1468, 'n_words': 412}