]> jfr.im git - irc/rizon/acid.git/blob - pyva/src/main/python/moo/cmd_manager.py
Import acidictive 4 and pyva plugin
[irc/rizon/acid.git] / pyva / src / main / python / moo / cmd_manager.py
1 class CommandManager(object):
2 def __init__(self):
3 self.prefix = self.get_prefix()
4 #self.invalid = self.get_invalid()
5 self.commands = self.get_commands()
6 self.generate_help()
7
8 def get_command(self, command):
9 command = command.lower()
10
11 if not command.startswith(self.prefix):
12 return None
13
14 command = command[len(self.prefix):]
15
16 if not command in self.commands:
17 return None
18
19 command = self.commands[command]
20
21 if not isinstance(command, basestring):
22 return command
23
24 command = command.lower()
25
26 if not command in self.commands:
27 return None
28
29 return self.commands[command]
30
31 def get_commands(self):
32 return {}
33
34 def generate_help(self):
35 self.help = []
36
37 # self.help.append('@bCommands@b (type @b%shelp command name@b for detailed information):' % self.prefix)
38 # self.help.append(' ')
39
40 longest = 0
41 alias_dict = {}
42 commands = {}
43
44 for cmd in self.commands:
45 if isinstance(self.commands[cmd], basestring):
46 orig = self.commands[cmd]
47
48 if orig in alias_dict:
49 alias_dict[orig].append(cmd)
50 else:
51 alias_dict[orig] = [cmd]
52 else:
53 if not cmd in alias_dict:
54 alias_dict[cmd] = []
55
56 for key in alias_dict:
57 cur = key + ('' if len(alias_dict[key]) == 0 else (' (' + ', '.join(alias_dict[key]) + ')'))
58 longest = len(cur) if len(cur) > longest else longest
59 commands[cur] = self.commands[key][1]
60
61 for cmd in sorted(commands):
62 self.help.append('@b%-*s@b %s' % (longest + 1, cmd, commands[cmd]))