def show(name, opts=None, insert_at=None, mode="load"): """Show the commands that would result from loading module given by `name` Parameters ---------- name : string_like Module name, full name, or file path insert_at : int Load the module as the `insert_at`th module. Raises ------ ModuleNotFoundError """ # Execute the module module = pymod.modulepath.get(name) if module is None: raise ModuleNotFoundError(name, mp=pymod.modulepath) # Set the command line options if opts: module.opts = opts # Now execute it pymod.mc.execmodule(module, pymod.modes.show) # and show it sys.stderr.write(pymod.mc.cur_module_command_his.getvalue())
def whatis(name): """Display 'whatis' message for the module given by `name`""" module = pymod.modulepath.get(name) if module is None: raise ModuleNotFoundError(name, mp=pymod.modulepath) pymod.mc.load_partial(module, mode=pymod.modes.whatis) return module.format_whatis()
def unload(name, tolerant=False, caller="command_line"): """Unload the module given by `name`""" module = pymod.modulepath.get(name) if module is None: raise ModuleNotFoundError(name) loaded_modules = pymod.mc.get_loaded_modules() for loaded in loaded_modules: if loaded.name == name: break elif loaded.fullname == name: break else: tty.warn("Module {0} is not loaded".format(name)) return if pymod.environ.get(pymod.names.loaded_collection): # pragma: no cover collection = pymod.environ.get(pymod.names.loaded_collection) tty.debug("Unloading {0} on top of loaded collection {1}. " "Removing the collection name from the environment".format( module.fullname, collection)) pymod.environ.unset(pymod.names.loaded_collection) unload_impl(loaded, caller) return loaded
def info(names): for name in names: modules = pymod.modulepath.candidates(name) if not modules: raise ModuleNotFoundError(name) for module in modules: s = "@B{Module:} @*{%s}\n" % module.fullname s += " @C{Name:} %s\n" % module.name if module.version: # pragma: no cover s += " @C{Version:} %s\n" % module.version if module.family: # pragma: no cover s += " @C{Family:} %s\n" % module.family s += " @C{Loaded:} %s\n" % module.is_loaded s += " @C{Filename:} %s\n" % module.filename s += " @C{Modulepath:} %s" % module.modulepath unlocked_by = module.unlocked_by() if unlocked_by: # pragma: no cover s += " @C{Unlocked by:} %s\n" for m in unlocked_by: s += " %s\n" % m.fullname unlocks = module.unlocks() if unlocks: # pragma: no cover s += " @C{Unlocks:} %s\n" for dirname in unlocks: s += " %s\n" % dirname sys.stderr.write(colorize(s) + "\n")
def help(modulename): """Display 'help' message for the module given by `modulename`""" module = pymod.modulepath.get(modulename) if module is None: raise ModuleNotFoundError(modulename, mp=pymod.modulepath) pymod.mc.load_partial(module, mode=pymod.modes.help) return module.format_help()
def load_first(module, mode, *names): """Load the first of modules in `names` Arguments: module (Module): The module being executed mode (Mode): The mode of execution names (tuple of str): Names of modules to load Returns: loaded (Module): The loaded module Notes: - In load mode, loads the first available module in `names` and returns it. In \ unload mode, the first loaded module in `names` is unloaded. - If no available modules are found in `names`, an error occurs - If the last of `names` is None, no error is thrown if no available \ modules are found in `names` Examples: Consider the module ``baz`` .. code-block:: python load_first('spam', 'eggs') On loading module ``baz``, the first available module of ``spam`` or ``eggs`` is loaded. .. code-block:: console $ module ls No loaded modules $ module load baz $ module ls Currently loaded modules 1) eggs 2) baz The module ``eggs`` was loaded because ``spam`` was not available. """ pymod.modes.assert_known_mode(mode) for name in names: if name is None: continue try: if mode == pymod.modes.unload: # We are in unload mode and the module was requested to be # loaded. So, we reverse the action and unload it return pymod.mc.unload(name, caller="load_first") else: return pymod.mc.load(name, caller="load_first") except ModuleNotFoundError: continue if names and name is None: return raise ModuleNotFoundError(",".join(names))
def find(names): for name in names: s = None candidates = pymod.modulepath.candidates(name) if not candidates: raise ModuleNotFoundError(name) for module in candidates: s = "@*{%s}\n @C{%s}" % (module.fullname, module.filename) sys.stderr.write(colorize(s) + "\n")
def reload(name): """Reload the module given by `modulename`""" module = pymod.modulepath.get(name) if module is None: raise ModuleNotFoundError(name, pymod.modulepath) if not module.is_loaded: tty.warn("{0} is not loaded!".format(module.fullname)) return assert module.is_loaded pymod.mc.swap_impl(module, module, maintain_state=True, caller="reload") return module
def save(args): target = pymod.modulepath.get(args.target) if target is None: raise ModuleNotFoundError(args.target) pymod.alias.save(target, args.alias_name)
def load(name, opts=None, insert_at=None, caller="command_line"): """Load the module given by `name` This is a higher level interface to `load_impl` that gets the actual module object from `name` Parameters ---------- name : string_like Module name, full name, or file path opts : dict (Optional) options to send to module insert_at : int Load the module as the `insert_at`th module. caller : str Who is calling. If modulefile, the reference count will be incremented if the module is already loaded. Returns ------- module : Module If the `name` was loaded (or is already loaded), return its module. Raises ------ ModuleNotFoundError """ tty.verbose("Loading {0}".format(name)) # Execute the module module = pymod.modulepath.get(name, use_file_modulepath=True) # caller=="command_line") if module is None: if caller == "command_line": collection = pymod.collection.get(name) if collection is not None: return pymod.mc.collection.restore_impl(name, collection) raise ModuleNotFoundError(name, mp=pymod.modulepath) # Set the command line options if opts: module.opts = opts if module.is_loaded: if caller == "modulefile": pymod.mc.increment_refcount(module) else: tty.warn( "{0} is already loaded, use 'module reload' to reload".format( module.fullname ) ) return module if pymod.environ.get(pymod.names.loaded_collection): # pragma: no cover collection = pymod.environ.get(pymod.names.loaded_collection) tty.debug( "Loading {0} on top of loaded collection {1}. " "Removing the collection name from the environment".format( module.fullname, collection ) ) pymod.environ.unset(pymod.names.loaded_collection) if insert_at is not None: load_inserted_impl(module, insert_at) else: load_impl(module) return module