#! /usr/bin/env python

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


def _find_misnamed_private_modules():
   tools_path = os.path.join(ABJADPATH, 'tools')
   offending_definition_lines = [ ]
   for path, subdirectories, files in os.walk(tools_path):
      for f in files:
         if f.endswith('.py'):
            if not f.startswith(('test', '_')):
               full_name = os.path.join(path, f)
               for line in file(full_name, 'r').readlines():
                  if line.startswith('def '):
                     if line.startswith('def _'):
                        function_name = ' '.join(line.strip().split()[1:])[:-1]
                        function_name = function_name.split('(')[0]
                        module_name = f[:-3]
                        if function_name[1:] == module_name:
                           medium_name = os.sep.join(full_name.split(os.sep)[-2:])
                           token = '%s in %s' % (function_name, medium_name)
                           offending_definition_lines.append(token)
   print ''
   #pprint.pprint(offending_definition_lines)
   #print ''
   total_offending_definitions = len(offending_definition_lines)
   print 'Total tools defined as private in public modules: %s' % total_offending_definitions
   print ''
      

if __name__ == '__main__':
   iotools.clear_terminal()
   print 'Finding misnamed private modules ...'
   _find_misnamed_private_modules()
