import argparse

from pandac.PandaModules import *


parser = argparse.ArgumentParser()
parser.add_argument('--output', '-o', default='DCClassImports.py',
                    help='The filename of the generated Python module.')
parser.add_argument('filenames', nargs='+', default=['otp.dc', 'toon.dc'],
                    help='The DC class files to be included in the generated Python module.')
args = parser.parse_args()

dcFile = DCFile()
for filename in args.filenames:
    dcFile.read(Filename.fromOsSpecific(filename))

dcImports = {}
for n in xrange(dcFile.getNumImportModules()):
    moduleName = dcFile.getImportModule(n)[:].split('/', 1)[0]
    if moduleName not in dcImports:
        dcImports[moduleName] = []
    importSymbols = []
    for i in xrange(dcFile.getNumImportSymbols(n)):
        symbolName = dcFile.getImportSymbol(n, i).split('/', 1)[0]
        importSymbols.append(symbolName)
    dcImports[moduleName].extend(importSymbols)

data = '''\
# This file was generated by the parse_dclass.py utility.
from pandac.PandaModules import *


hashVal = %r


''' % dcFile.getHash()

for moduleName, importSymbols in dcImports.items():
    data += 'from %s import %s\n' % (moduleName, ', '.join(importSymbols))

data += '''

dcImports = locals().copy()
'''

print 'Writing %s...' % args.output
with open(args.output, 'w') as f:
    f.write(data)

print 'Done writing %s.' % args.output