#! /usr/bin/env python

from abjad.cfg.cfg import ABJADPATH
from abjad.tools import iotools
import os
import pprint


def _find_multiline_import_statements():
   total_multiline_import_statements = 0
   for path, subdirectories, files in os.walk(ABJADPATH):
      for f in files:
         found_import_statement_in_file = False
         if f.endswith('.py'):
            full_file_name = os.path.join(path, f)
            fp = file(full_file_name, 'r')
            within_multiline_import, constituent_lines = False, [ ]
            for line in fp:
               if ('import' in line or line.startswith('from')) and line.endswith('\\\n'):
                  if not found_import_statement_in_file:
                     print full_file_name 
                     found_import_statement_in_file = True
                  total_multiline_import_statements += 1
                  constituent_lines.append(line)
                  within_multiline_import = True
               elif within_multiline_import:
                  constituent_lines.append(line)
                  if not line.endswith('\\\n'):   
                     print constituent_lines
                     within_multiline_import, constituent_lines = False, [ ]
         if found_import_statement_in_file:
            print ''
   print 'Total multilline import statements: %s' % total_multiline_import_statements
   print ''


if __name__ == '__main__':
   iotools.clear_terminal()
   print 'Finding multiline import statements ...'
   print ''
   _find_multiline_import_statements()
