
import maya.cmds
import sys, os.path

# Locations of commandList file by OS type as returned by maya.cmds.about( os=True )
commandListLocations = {
	'nt' : 'bin',
	'win64' : 'bin',
	'mac' : 'MacOS',
	'linux' : 'lib',
	'linux64' : 'lib'
}

def __makeDummyFunc( command, library ):
	def dummyFunc( *args, **keywords ):
		maya.cmds.dynamicLoad( library )
		maya.cmds.__dict__[command]( *args, **keywords )
	return dummyFunc

def processCommandList():
	"""
	Process the "commandList" file that contains the mappings between command names and the
	libraries in which they are found.  This function will install dummy functions in maya.cmds
	for all commands that are not yet loaded.  The dummy functions will load the required slice
	and then execute the command
	"""

	try:
		# Assume that maya.cmds.about and maya.cmds.internalVar are already registered
		#
		commandListPath = os.path.realpath( os.environ[ 'MAYA_LOCATION' ] )
		platform = maya.cmds.about( os=True )
		commandListPath = os.path.join( commandListPath, commandListLocations[platform], 'commandList' )

		file = open( commandListPath, 'r' )
		for line in file:
			commandName, library = line.split()
			if not commandName in maya.cmds.__dict__:
				maya.cmds.__dict__[commandName] = __makeDummyFunc( commandName, library )
	except:
		sys.stderr.write("Unable to open commandList file for reading")
		raise

