module commands

def cmd_module(args):
    '''module commands'''
    usage = "usage: module <list|load|reload|unload>"
    if len(args) < 1:
        print(usage)
        return
    if args[0] == "list":
        for (m,pm) in mpstate.modules:
            print("%s: %s" % (m.name, m.description))
    elif args[0] == "load":
        if len(args) < 2:
            print("usage: module load <name>")
            return
        (modname, kwargs) = generate_kwargs(args[1])
        try:
            load_module(modname, **kwargs)
        except TypeError as ex:
            print(ex)
            print("%s module does not support keyword arguments"% modname)
            return
    elif args[0] == "reload":
        if len(args) < 2:
            print("usage: module reload <name>")
            return
        (modname, kwargs) = generate_kwargs(args[1])
        pmodule = None
        for (m,pm) in mpstate.modules:
            if m.name == modname:
                pmodule = pm
        if pmodule is None:
            print("Module %s not loaded" % modname)
            return
        if unload_module(modname):
            import zipimport
            try:
                reload(pmodule)
            except ImportError:
                clear_zipimport_cache()
                reload(pmodule)
            try:
                if load_module(modname, quiet=True, **kwargs):
                    print("Reloaded module %s" % modname)
            except TypeError:
                print("%s module does not support keyword arguments" % modname)
    elif args[0] == "unload":
        if len(args) < 2:
            print("usage: module unload <name>")
            return
        modname = os.path.basename(args[1])
        unload_module(modname)
    else:
        print(usage)