#!/usr/bin/python3

import sys, project, inspect


def main(arg):

	if len(arg) < 1 : raise project.error.ModuleNameNotSpecifiedException(project)
	module = getattr(project, arg[0], None)
	if module == None or not inspect.ismodule(module): raise project.error.ModuleDoesNotExistException(arg[0], project)

	if len(arg) < 2 : raise project.error.ActionNameNotSpecifiedException(module, arg[0])
	action = getattr(module, arg[1], None)
	if action == None or not inspect.isfunction(action): raise project.error.ActionDoesNotExistException(arg[1], module, arg[0])

	spec = inspect.getargspec(action)
	m = (0 if spec[0] == None else len(spec[0])) - (0 if spec[3] == None else len(spec[3]))
	n = len(arg) - 2
	if n < m : raise project.error.TooFewArgumentsForActionException(n, spec, arg[1], arg[0])
	if spec[1] == None and n > len(spec[0]) : raise project.error.TooManyArgumentsForActionException(n, spec, arg[1], arg[0])
	action(*arg[2:])


if __name__ == '__main__':

	try : main(sys.argv[1:])
	except project.error.MainException as e : print(e)

#!/usr/bin/python3

import sys, project, inspect


def main(inp):

	action, arguments = parse(inp)
	action(*arguments)


def parse(inp):
	# Determine module
	if len(inp) < 1:
		raise project.error.ModuleNameNotSpecifiedException(project)

	module = getattr(project, inp[0], None)
	if module == None or not inspect.ismodule(module):
		raise project.error.ModuleDoesNotExistException(inp[0], project)

	# Determine action
	if len(inp) < 2:
		raise project.error.ActionNameNotSpecifiedException(module, inp[0])

	action = getattr(module, inp[1], None)
	if action == None or not inspect.isfunction(action):
		raise project.error.ActionDoesNotExistException(inp[1], module, inp[0])

	# Check action arguments
	spec = inspect.getargspec(action)
	m = (0 if spec[0] == None else len(spec[0])) - (0 if spec[3] == None else len(spec[3]))
	n = len(inp) - 2

	if n < m:
		raise project.error.TooFewArgumentsForActionException(n, spec, inp[1], inp[0])

	if spec[1] == None and n > len(spec[0]):
		raise project.error.TooManyArgumentsForActionException(n, spec, inp[1], inp[0])

	# Done
	return action, inp[2:]


if __name__ == '__main__':

	try : main(sys.argv[1:])
	except project.error.MainException as e : print(e)

clean main