#! /usr/bin/env python
from __future__ import print_function
import os
from abjad.tools import systemtools


def find_import_as_statements():
    total_modules_with_import_as_statements = 0
    total_import_as_statements = 0
    for directory, subdirectory_names, file_names in os.walk('.'):
        for file_name in file_names:
            found_import_as_statement = False
            if file_name.endswith('.py') and \
                not file_name == 'autodoc.py' and \
                not file_name == '_write_parser_syntax_skeleton.py':
                file_path = os.path.join(directory, file_name)
                with open(file_path, 'r') as file_pointer:
                    for line in file_pointer:
                        if (
                            'import' in line and
                            ' as ' in line and
                            'command =' not in line
                            ):
                            if not found_import_as_statement:
                                print(file_path)
                                found_import_as_statement = True
                                total_modules_with_import_as_statements += 1
                            print(line.strip())
                            total_import_as_statements += 1
            if found_import_as_statement:
                print()

    total = total_modules_with_import_as_statements
    print('Modules with import-as statements:   {}'.format(total))

    total = total_import_as_statements
    print('Total import-as statements:          {}'.format(total))


if __name__ == '__main__':
    systemtools.IOManager.clear_terminal()
    print('Finding import-as statements ...')
    print()
    find_import_as_statements()
    print()
