53 lines
988 B
Python
53 lines
988 B
Python
class CmdNotFound(Exception):
|
|
pass
|
|
|
|
class NoApiKey(Exception):
|
|
pass
|
|
|
|
class FeatureMissing(Exception):
|
|
pass
|
|
|
|
class Command:
|
|
FTR_PRFX = "vultron_"
|
|
|
|
def __init__(self, cmd, api_key):
|
|
self.cmd = cmd
|
|
self.uses_api = True
|
|
self.default = "help"
|
|
|
|
self.init()
|
|
|
|
if self.uses_api and api_key is None:
|
|
raise NoApiKey()
|
|
|
|
def init(self):
|
|
pass
|
|
|
|
def find(self, name):
|
|
needle = "{prfx}{name}".format(prfx=self.FTR_PRFX, name=name.lower())
|
|
haystack = dir(self)
|
|
|
|
for legume in haystack:
|
|
if legume.lower().startswith(needle):
|
|
fn = getattr(self, legume)
|
|
|
|
if callable(fn):
|
|
return fn
|
|
else:
|
|
raise FeatureMissing(legume)
|
|
|
|
raise CmdNotFound(name)
|
|
|
|
def vultron_help(self, *args):
|
|
pass
|
|
|
|
def find_cmd(needle):
|
|
needle = needle.lower()
|
|
haystack = Command.__subclasses__()
|
|
|
|
for legume in haystack:
|
|
if legume.__name__.lower().startswith(needle):
|
|
return legume
|
|
|
|
raise CmdNotFound(needle)
|