]> jfr.im git - irc/rizon/acid.git/blame - pyva/pyva/src/main/python/erepublik/cmd_manager.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / erepublik / cmd_manager.py
CommitLineData
685e346e
A
1ARG_NO = 0x1
2ARG_YES = 0x2
3ARG_OPT = 0x4
4ARG_OFFLINE = 0x8
5ARG_OFFLINE_REQ = 0x10
6
7class CommandManager(object):
8 def __init__(self):
9 self.prefix = self.get_prefix()
10 self.invalid = self.get_invalid()
11 self.commands = self.get_commands()
12 self.generate_help()
13
14 def get_prefix(self):
15 return ''
16
17 def get_invalid(self):
18 return ''
19
20 def get_commands(self):
21 return {}
22
23 def get_command(self, command):
24 command = command.lower()
25
26 if not command.startswith(self.prefix):
27 return None
28
29 command = command[len(self.prefix):]
30
31 if not command in self.commands:
32 return None
33
34 command = self.commands[command]
35
36 if not isinstance(command, basestring):
37 return command
38
39 command = command.lower()
40
41 if not command in self.commands:
42 return None
43
44 return self.commands[command]
45
46 def get_help(self, command = None):
47 if command == None:
48 return self.help
49 else:
50 if command.startswith(self.prefix):
51 command = command[len(self.prefix):]
52
53 if command in self.help_command:
54 return self.help_command[command]
55
56 return None
57
58 def add_help_command(self, command):
59 cmd = self.commands[command]
60
61 if isinstance(cmd, basestring):
62 cmd = self.commands[cmd]
63
64 message = []
65
66 cmd_type = cmd[1]
67 cmd_desc = cmd[2]
68 cmd_args = cmd[3]
69
70 message.append('@b%s%s: %s' % (self.prefix, command, cmd_desc))
71 message.append(' ')
72
73 msg = 'Usage: @b%s%s@b' % (self.prefix, command)
74
75 if len(cmd_args) > 0:
76 msg += ' ' + ' '.join(['[%s%s]' % (cmd_arg[1], (' ' + cmd_arg[0]) if ('action' not in cmd_arg[3] or cmd_arg[3]['action'] != 'store_true') else '') for cmd_arg in cmd_args])
77
78 if len(cmd) > 4:
79 argument_name = cmd[4]
80 else:
81 argument_name = 'argument'
82
83 if cmd_type & ARG_YES:
84 msg += ' %s' % argument_name
85 elif cmd_type & ARG_OPT:
86 msg += ' [%s]' % argument_name
87
88 message.append(msg)
89 message.append('')
90
91 longest = 0
92
93 for cmd_arg in cmd_args:
94 longest = len(cmd_arg[0]) if len(cmd_arg[0]) > longest else longest
95
96 for cmd_arg in cmd_args:
97 message.append('@b--%-*s (%s)@b %s' % (longest + 1, cmd_arg[0], cmd_arg[1], cmd_arg[2]))
98
99 self.help_command[command] = message
100
101
102 def generate_help(self):
103 self.help = []
104
105 self.help.append('@bCommands@b (type @b%shelp command name@b for detailed information):' % self.prefix)
106 self.help.append(' ')
107
108 longest = 0
109 alias_dict = {}
110 commands = {}
111
112 for cmd in self.commands:
113 if isinstance(self.commands[cmd], basestring):
114 orig = self.commands[cmd]
115
116 if orig in alias_dict:
117 alias_dict[orig].append(cmd)
118 else:
119 alias_dict[orig] = [cmd]
120 else:
121 if not cmd in alias_dict:
122 alias_dict[cmd] = []
123
124 for key in alias_dict:
125 cur = key + ('' if len(alias_dict[key]) == 0 else (' (' + ', '.join(alias_dict[key]) + ')'))
126 longest = len(cur) if len(cur) > longest else longest
127 commands[cur] = self.commands[key][2]
128
129 for cmd in sorted(commands):
130 self.help.append('@b%-*s@b %s' % (longest + 1, cmd, commands[cmd]))
131
132 self.help_command = {}
133
134 for command in self.commands:
135 self.add_help_command(command)