#!/usr/bin/env python                                                           

import os,sys
import pdb

chromium_path = os.path.abspath(
    os.path.join(os.environ['HOME'], 'chromium', 'src'))
blink_bindings_path = os.path.join(
    chromium_path, 'third_party', 'WebKit', 'Source', 'bindings', 'scripts')
sys.path.insert(0, blink_bindings_path)

from blink_idl_parser import parse_file, BlinkIDLParser


def find_all_files(directory):
    for root, dirs, files in os.walk(directory):
        yield root
        for file in files:
            yield os.path.join(root, file)

def find_idl_files(dir_name):
    idl_file_list = []
    for file in find_all_files(dir_name):
	#import pdb
	#pdb.set_trace()
        if file.endswith('.idl') and not os.path.basename(file) == 'InspectorInstrumentation.idl':
            idl_file_list.append(file)
    #idl_file_list.remove('/usr/local/google/home/shimadaa/chromium/src/third_party/WebKit/Source/core/inspector/InspectorInstrumentation.idl')
    return idl_file_list


def find_interface(node, depth=0):
    list_of_interface = []
    for child in node.GetChildren():
       if child.GetClass().startswith('Interface'):
           import pdb
           pdb.set_trace()
           ilist_of_interface.append(child.GetName())
    return list_of_interface
	


def main(dir_name):
    list_of_all_interface = []
    for idl_file in find_idl_files(dir_name):
        #import pdb
        #pdb.set_trace()
        parser = BlinkIDLParser(debug=False)
        definitions = parse_file(parser, idl_file)
        list_of_all_interface.append(find_interface(definitions))
    return list_of_all_interface
    

if __name__ == '__main__':
   print main(sys.argv[1]) 
    
