def callback(self, *args): items = [] if not args: args = [] # Convert args to a file path mod_path = "/modules" search_path = "%s/%s/" % (mod_path, "/".join(args)) # In the case of no args, we need to strip the extra / search_path = search_path.replace("//", "/") try: # Get a list of all directories and files for (dirpath, dirnames, filenames) in os.walk(getBaseDir() + search_path): items.extend(dirnames) items.extend(filenames) break # Break to prevent going deeper except OSError: self.console.error("'%s' is not a valid directory" % search_path) return pattern = re.compile(r".+\.py$") for item in items: # get the absolute path for the item abs_path = os.path.abspath(getBaseDir() + search_path + item) if re.match(pattern, item): if not item == "__init__.py": # Ignore package files item = item.rstrip(".py") self.console.writeln("%s" % (search_path + item)) # Get the absolute path for the item and check if it is a directory elif os.path.isdir(abs_path): self.console.writeln("%s/" % (search_path + item))
def callback(self, *args): if not args: self.console.error("'load' requires at least one argument") return # Convert args to a file path mod_path = "/modules/" + "/".join(args) # In the case of no args, we need to strip the extra / mod_path = mod_path.replace("//", "/") abs_path = getBaseDir() + mod_path + ".py" if not os.path.isfile(abs_path): self.console.error("module '%s' not found" % "/".join(args)) return elif mod_path.split("/")[-1] == "__init__": self.console.error("module '%s' not found" % "/".join(args)) return self.console.set_module(mod_path)
from base import getBaseDir import sys import os import re __all__ = ["commands_list",] _commands = [] """ Look through the commands list folder for commands, then add them to the list of commands available. """ files = os.listdir(getBaseDir() + "/commands/list") pattern = re.compile(r".+\.py$") for file_ in files: if re.match(pattern, file_): if not file_ == "__init__.py": _commands.append(file_[:-3]) commands_list = {} """ Iterate over the list of commands, import the class from the command module, and then add the class to a dictionary entry for the command. """ for command in _commands: module = __import__(("commands.list." + command), fromlist=[command.title()]) klass = getattr(module, command.title()) commands_list[command] = klass