<commit_before>import sys
import mistletoe


def main(args):
    filenames, renderer = _parse(args)
    if filenames:
        convert(filenames, renderer)
    else:
        interactive(renderer)


def convert(filenames, renderer):
    for filename in filenames:
        convert_file(filename, renderer)


def convert_file(filename, renderer):
    """
    Parse a Markdown file and dump the output to stdout.
    """
    try:
        with open(filename, 'r') as fin:
            rendered = mistletoe.markdown(fin, renderer)
            print(rendered, end='')
    except OSError:
        sys.exit('Cannot open file "{}".'.format(filename))


def interactive(renderer):
    """
    Parse user input, dump to stdout, rinse and repeat.
    Python REPL style.
    """
    _import_readline()
    _print_heading(renderer)
    contents = []
    more = False
    while True:
        try:
            prompt, more = ('... ', True) if more else ('>>> ', True)
            contents.append(input(prompt) + '\n')
        except EOFError:
            print('\n' + mistletoe.markdown(contents, renderer), end='')
            more = False
            contents.clear()
        except KeyboardInterrupt:
            print('\nExiting.')
            break


def _parse(args):
    flag = None
    filenames = []
    renderer = mistletoe.HTMLRenderer
    for arg in args:
        if flag == 'renderer':
            renderer = _import(arg)
            flag = None
        elif arg in ('-r', '--renderer'):
            flag = 'renderer'
        else:
            filenames.append(arg)
    return filenames, renderer


def _import(arg):
    import importlib
    *path, cls_name = arg.split('.')
    path = '.'.join(path)
    try:
        module = importlib.import_module(path)
        renderer = getattr(module, cls_name)
    except ImportError:
        sys.exit('Cannot import module "{}".'.format(path))
    except AttributeError:
        sys.exit('Cannot find renderer "{}" from module "{}"'.format(cls_name, path))
    return renderer


def _import_readline():
    try:
        import readline
    except ImportError:
        print('[warning] readline library not available.')


def _print_heading(renderer):
    print('mistletoe [version {}] (interactive)'.format(mistletoe.__version__))
    print('Type Ctrl-D to complete input, or Ctrl-C to exit.')
    if renderer is not mistletoe.HTMLRenderer:
        print('Using renderer: {}'.format(renderer.__name__))
<commit_msg>🐛 fixed _import: unbounded local variable, typo<commit_after>import sys
import mistletoe


def main(args):
    filenames, renderer = _parse(args)
    if filenames:
        convert(filenames, renderer)
    else:
        interactive(renderer)


def convert(filenames, renderer):
    for filename in filenames:
        convert_file(filename, renderer)


def convert_file(filename, renderer):
    """
    Parse a Markdown file and dump the output to stdout.
    """
    try:
        with open(filename, 'r') as fin:
            rendered = mistletoe.markdown(fin, renderer)
            print(rendered, end='')
    except OSError:
        sys.exit('Cannot open file "{}".'.format(filename))


def interactive(renderer):
    """
    Parse user input, dump to stdout, rinse and repeat.
    Python REPL style.
    """
    _import_readline()
    _print_heading(renderer)
    contents = []
    more = False
    while True:
        try:
            prompt, more = ('... ', True) if more else ('>>> ', True)
            contents.append(input(prompt) + '\n')
        except EOFError:
            print('\n' + mistletoe.markdown(contents, renderer), end='')
            more = False
            contents.clear()
        except KeyboardInterrupt:
            print('\nExiting.')
            break


def _parse(args):
    flag = None
    filenames = []
    renderer = mistletoe.HTMLRenderer
    for arg in args:
        if flag == 'renderer':
            renderer = _import(arg)
            flag = None
        elif arg in ('-r', '--renderer'):
            flag = 'renderer'
        else:
            filenames.append(arg)
    return filenames, renderer


def _import(arg):
    import importlib
    *path, cls_name = arg.split('.')
    path = '.'.join(path)
    try:
        module = importlib.import_module(path)
        return getattr(module, cls_name)
    except ImportError:
        sys.exit('Cannot import module "{}".'.format(path))
    except AttributeError:
        sys.exit('Cannot find renderer "{}" from module "{}".'.format(cls_name, path))


def _import_readline():
    try:
        import readline
    except ImportError:
        print('[warning] readline library not available.')


def _print_heading(renderer):
    print('mistletoe [version {}] (interactive)'.format(mistletoe.__version__))
    print('Type Ctrl-D to complete input, or Ctrl-C to exit.')
    if renderer is not mistletoe.HTMLRenderer:
        print('Using renderer: {}'.format(renderer.__name__))

{'fraction_non_alphanumeric': 0.11947893344188887, 'fraction_numerical': 0.0, 'mean_word_length': 2.4006920415224915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2832067', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1478, 'n_words': 392}