def _load_cmd(self, cmd_name, cmd_ref): """Because some cmds are stored in the cache as either a class or as a path to the .py file, this method is needed to disambiguate. Given the following arguments: cmd_name: The name of the cmd. cmd_ref: Either the OttoCmd object or the path to it's file. This method will always return the corresponding OttoCmd class.""" # Base cmds are already loaded if isOttoCmd(cmd_ref): return cmd_ref # Otherwise, import and return OttoCmd subclass try: cmd_module = imp.load_source( cmd_name, cmd_ref ) cmd_class = getattr(cmd_module, cmd_name.capitalize(), None) if not isOttoCmd(cmd_class): msg = "'%s' could not be loaded from %s" % (cmd_name, cmd_ref) bail(msg) else: return cmd_class except SyntaxError as e: errmsg = "Syntax error found:\nFile:%s (%s, %s)" % e.args[1][:3] bail(errmsg) except Exception as e: raise e
def test_load_cmd(self): # Make sure _load_cmd will always do nothing to a base cmd for cmd_name, cmd_ref in BASE_CMDS.iteritems(): result = self.target._load_cmd(cmd_name, cmd_ref) self.assertTrue(isOttoCmd(result)) # Make sure all refs for installed packs are paths... self.target.load_pack(PACK_NAME, PACK_DIR) test_pack = self.target.cmds_by_pack[PACK_NAME] for cmd_name, cmd_ref in test_pack.iteritems(): self.assertTrue(isfile(cmd_ref)) # ... and that they return from _load_cmd as an OttoCmd result = self.target._load_cmd(cmd_name, cmd_ref) self.assertTrue(isOttoCmd(result))