#! /usr/bin/env python

from abjad.tools import iotools
import os


def _fix_test_case_names( ):
   total_test_modules = 0
   total_test_cases = 0
   total_nonmatching_names = 0
   for path, subdirectories, files in os.walk('.'):
      test_modules = [ ]
      for f in files:
         if f.startswith('test_') and f.endswith('.py'):
            test_modules.append(f)
      total_test_modules += len(test_modules)
      for test_module in test_modules:
         result = _process_test_module(test_module, path)
         total_test_cases_in_module, total_nonmatching_names_in_module = result
         total_test_cases += total_test_cases_in_module
         total_nonmatching_names += total_nonmatching_names_in_module
   print '%s total test modules.' % total_test_modules
   print '%s total test cases.' % total_test_cases
   if total_nonmatching_names == 1:
      suffix = ''
   else:
      suffix = 's'
   print '%s total nonmatching test case name%s.' % (total_nonmatching_names, suffix)


def _process_test_module(test_module, path):
   total_nonmatching_names_in_current_module = 0
   test_cases_in_current_module = 0
   full_module_name = os.path.join(path, test_module)
   short_module_name = test_module[:-3]
   desired_test_case_prefix = short_module_name
   #print 'desired: %s' % desired_test_case_prefix
   fp = file(full_module_name, 'r')
   new_lines = [ ]
   lines = fp.readlines( )
   for line in lines:
      if line.startswith('def test') or line.startswith('#def test'):
         test_cases_in_current_module += 1
   
         ### TODO: replace this transform of test_case_name with a single regex ###
         test_case_name = line.strip( )
         if test_case_name.startswith('#'):
            test_case_name = test_case_name[1:]
         test_case_name = test_case_name.split('def ')[1]
         test_case_name = test_case_name.split('(')[0]

         ### TODO: relace this transform of actual_test_case_name with a single regex ###
         actual_test_case_prefix = line.strip( )
         if actual_test_case_prefix.startswith('#'):
            actual_test_case_prefix = actual_test_case_prefix[1:]
         actual_test_case_prefix = actual_test_case_prefix.split('def ')[1]
         actual_test_case_prefix = actual_test_case_prefix.split('(')[0]
         actual_test_case_prefix = actual_test_case_prefix[:-3]

         if not actual_test_case_prefix == desired_test_case_prefix:
            total_nonmatching_names_in_current_module += 1
            print 'NONMATCHING in %s' % test_module
            new_line = line.replace(actual_test_case_prefix, desired_test_case_prefix)
            print 'OLD LINE:  %s' % line.strip('\n')
            print 'NEW LINE:  %s' % new_line.strip('\n')
            #choice = raw_input('Make replacement [y/n]? ')
            choice = 'y'
            if choice.lower( ) == 'y':
               new_lines.append(new_line)
            else:
               new_lines.append(line)
            print ''
         else:
            new_lines.append(line)
      else:
         new_lines.append(line)
   fp.close( )
   fp = open(full_module_name, 'w')
   fp.writelines(new_lines)
   fp.close( )
   return test_cases_in_current_module, total_nonmatching_names_in_current_module
      

if __name__ == '__main__':
   iotools.clear_terminal( )
   _fix_test_case_names( )
   print ''
