|
33 | 33 | # ============================================================================= |
34 | 34 | @public |
35 | 35 | class SubCommandManager(OrderedDict, AutoUnload): |
36 | | - '''''' |
| 36 | + '''Class used for executing sub-commands for the given console command''' |
37 | 37 |
|
38 | 38 | def __init__(self, command, description='', prefix=''): |
39 | | - '''''' |
| 39 | + '''Called on instance initialization''' |
40 | 40 |
|
41 | | - # |
| 41 | + # Re-call OrderedDict's __init__ to properly setup the object |
42 | 42 | super(SubCommandManager, self).__init__() |
43 | 43 |
|
44 | 44 | # Does the class have a proper manager object assigned? |
@@ -79,225 +79,233 @@ def __init__(self, command, description='', prefix=''): |
79 | 79 | self.command, self.call_command, description, 0) |
80 | 80 |
|
81 | 81 | def _unload_instance(self): |
82 | | - '''''' |
| 82 | + '''Unregisters commands when the instance is unloaded''' |
83 | 83 | ServerCommandManager.unregister_commands( |
84 | 84 | self.command, self.call_command) |
85 | 85 |
|
86 | 86 | def call_command(self, CICommand): |
87 | | - '''''' |
| 87 | + '''Gets the provided sub-command and executes accordingly''' |
88 | 88 |
|
89 | | - # |
| 89 | + # Get the argument string |
90 | 90 | arg_string = CICommand.get_arg_string() |
91 | | - # |
| 91 | + |
| 92 | + # Use try/except to split the argument string, |
| 93 | + # if it contains more than one argument |
92 | 94 | try: |
93 | 95 |
|
94 | | - # |
| 96 | + # Split the argument string to get the sub-command |
95 | 97 | command, args = arg_string.split(maxsplit=1) |
96 | 98 |
|
| 99 | + # Were there not enough arguments to split? |
97 | 100 | except ValueError: |
98 | 101 |
|
99 | | - # |
| 102 | + # Set the command to the entire string |
100 | 103 | command = arg_string.strip() |
101 | 104 |
|
102 | | - # |
| 105 | + # Set args to nothing |
103 | 106 | args = '' |
104 | 107 |
|
105 | | - # |
| 108 | + # Split the remaining args |
106 | 109 | args = args.split() |
107 | 110 |
|
108 | | - # |
| 111 | + # Make the sub-command lower-case |
109 | 112 | command = command.lower() |
110 | 113 |
|
111 | | - # |
| 114 | + # Is the sub-command in the dictionary? |
112 | 115 | if not command in self: |
113 | 116 |
|
114 | | - # |
| 117 | + # Was a sub-command given? |
115 | 118 | if command: |
116 | 119 |
|
117 | | - # |
| 120 | + # Print a message about the invalid sub-command |
118 | 121 | message = self.prefix + _plugin_strings[ |
119 | 122 | 'Invalid Command'].get_string( |
120 | 123 | command=self.command, subcommand=command) |
121 | 124 |
|
122 | | - # |
| 125 | + # Was no sub-command given? |
123 | 126 | else: |
124 | 127 |
|
125 | | - # |
| 128 | + # Print a message about the missing sub-command |
126 | 129 | message = self.prefix + _plugin_strings[ |
127 | 130 | 'No Command'].get_string(command=self.command) |
128 | 131 |
|
| 132 | + # Print the help text for the console command |
129 | 133 | self.print_help(message) |
130 | 134 |
|
131 | | - # |
| 135 | + # No need to go further |
132 | 136 | return |
133 | 137 |
|
134 | | - # |
| 138 | + # Does the given sub-command's callable allow for arguments? |
135 | 139 | if hasattr(self[command], 'args'): |
136 | 140 |
|
137 | | - # |
| 141 | + # Get the number of required arguments for the callable |
138 | 142 | required = len([ |
139 | 143 | x for x in self[command].args if x.startswith('<')]) |
140 | 144 |
|
141 | | - # |
| 145 | + # Get the number of arguments provided |
142 | 146 | given = len(args) |
143 | 147 |
|
144 | | - # |
| 148 | + # Were enough arguments provided? |
145 | 149 | if given < required: |
146 | 150 |
|
147 | | - # |
| 151 | + # Print a message about the invalid number of arguments given |
148 | 152 | self.logger.log_message( |
149 | 153 | self.prefix + _plugin_strings['Invalid Arguments' |
150 | 154 | ].get_string(command=self.command, subcommand=command) + |
151 | 155 | ' '.join(self[command].args)) |
152 | 156 |
|
153 | | - # |
| 157 | + # No need to go further |
154 | 158 | return |
155 | 159 |
|
156 | | - # |
| 160 | + # Are all of the arguments required? |
157 | 161 | if required == len(self[command].args): |
158 | 162 |
|
159 | | - # |
| 163 | + # Were the correct number of arguments given? |
160 | 164 | if given != required: |
161 | 165 |
|
162 | | - # |
| 166 | + # Print a message about the invalid |
| 167 | + # number of arguments given |
163 | 168 | self.logger.log_message( |
164 | 169 | self.prefix + _plugin_strings['Invalid Arguments' |
165 | 170 | ].get_string(command=self.command, subcommand=command |
166 | 171 | ) + ' '.join(self[command].args)) |
167 | 172 |
|
168 | | - # |
| 173 | + # No need to go further |
169 | 174 | return |
170 | 175 |
|
171 | | - # |
| 176 | + # Call the sub-command's callable with the given arguments |
172 | 177 | self[command](*args) |
173 | 178 |
|
174 | | - # |
| 179 | + # No need to go further |
175 | 180 | return |
176 | 181 |
|
177 | | - # |
| 182 | + # Does the current item have its own call_command method? |
178 | 183 | if hasattr(self[command], 'call_command'): |
179 | 184 |
|
180 | | - # |
| 185 | + # Call the instance's call_command method with the arguments |
181 | 186 | self[command].call_command(args) |
182 | 187 |
|
183 | | - # |
| 188 | + # No need to go further |
184 | 189 | return |
185 | 190 |
|
186 | | - # |
| 191 | + # Call the callable without the arguments |
187 | 192 | self[command]() |
188 | 193 |
|
189 | 194 | def print_help(self, message=''): |
190 | | - '''''' |
| 195 | + '''Prints all sub-commands for the console command''' |
191 | 196 |
|
192 | | - # |
| 197 | + # Add a header message |
193 | 198 | message += '\n' + self.prefix + _plugin_strings[ |
194 | 199 | 'Help'].get_string(command=self.command) + '\n' + '=' * 78 |
195 | 200 |
|
196 | | - # |
| 201 | + # Loop through all registered sub-commands |
197 | 202 | for item in self: |
198 | 203 |
|
199 | | - # |
| 204 | + # Set the text |
200 | 205 | text = str(item) |
201 | 206 |
|
202 | | - # |
| 207 | + # Does the current sub-command have its own print_help method? |
203 | 208 | if hasattr(self[item], 'print_help'): |
204 | 209 |
|
205 | | - # |
206 | | - message += '\n' + self[item]._get_help_text() |
| 210 | + # Get the instance's help text |
| 211 | + message += '\n' + self[item].get_help_text() |
207 | 212 |
|
208 | | - # |
| 213 | + # Continue to the next item |
209 | 214 | continue |
210 | 215 |
|
211 | | - # |
| 216 | + # Does the current command have any arguments? |
212 | 217 | if hasattr(self[item], 'args'): |
213 | 218 |
|
214 | | - # |
| 219 | + # Add the arguments to the text |
215 | 220 | text += ' ' + ' '.join(self[item].args) |
216 | 221 |
|
217 | | - # |
| 222 | + # Add a message for the current command |
218 | 223 | message += '\n' + text + self[ |
219 | 224 | item].__doc__.rjust(78 - len(text)) |
220 | 225 |
|
221 | | - # |
| 226 | + # Send the message |
222 | 227 | self.logger.log_message(message + '\n' + '=' * 78) |
223 | 228 |
|
224 | 229 | def load_plugin(self, plugin_name): |
225 | | - '''''' |
| 230 | + '''Loads a plugin by name''' |
226 | 231 |
|
227 | | - # |
| 232 | + # Is the plugin already loaded? |
228 | 233 | if plugin_name in self.manager: |
229 | 234 |
|
230 | | - # |
| 235 | + # Send a message that the plugin is already loaded |
231 | 236 | self.logger.log_message(self.prefix + _plugin_strings[ |
232 | 237 | 'Already Loaded'].get_string(plugin=plugin_name)) |
233 | 238 |
|
234 | | - # |
| 239 | + # No need to go further |
235 | 240 | return |
236 | 241 |
|
237 | | - # |
| 242 | + # Load the plugin and get its instance |
238 | 243 | plugin = self.manager[plugin_name] |
239 | 244 |
|
240 | | - # |
| 245 | + # Was the plugin unable to be loaded? |
241 | 246 | if plugin is None: |
242 | 247 |
|
243 | | - # |
| 248 | + # Send a message that the plugin was not loaded |
244 | 249 | self.logger.log_message(self.prefix + _plugin_strings[ |
245 | 250 | 'Unable to Load'].get_string(plugin=plugin_name)) |
246 | 251 |
|
247 | | - # |
| 252 | + # No need to go further |
248 | 253 | return |
249 | 254 |
|
250 | | - # |
| 255 | + # Send a message that the plugin was loaded |
251 | 256 | self.logger.log_message(self.prefix + _plugin_strings[ |
252 | 257 | 'Successful Load'].get_string(plugin=plugin_name)) |
253 | 258 |
|
| 259 | + # Set the method's required arguments |
254 | 260 | load_plugin.args = ['<plugin>'] |
255 | 261 |
|
256 | 262 | def unload_plugin(self, plugin_name): |
257 | | - '''''' |
| 263 | + '''Unloads a plugin by name''' |
258 | 264 |
|
259 | | - # |
| 265 | + # Is the plugin loaded? |
260 | 266 | if not plugin_name in self.manager: |
261 | 267 |
|
262 | | - # |
| 268 | + # Send a message that the plugin is not loaded |
263 | 269 | self.logger.log_message(self.prefix + _plugin_strings[ |
264 | 270 | 'Not Loaded'].get_string(plugin=plugin_name)) |
265 | 271 |
|
266 | | - # |
| 272 | + # No need to go further |
267 | 273 | return |
268 | 274 |
|
269 | | - # |
| 275 | + # Unload the plugin |
270 | 276 | del self.manager[plugin_name] |
271 | 277 |
|
272 | | - # |
| 278 | + # Send a message that the plugin was unloaded |
273 | 279 | self.logger.log_message(self.prefix + _plugin_strings[ |
274 | 280 | 'Successful Unload'].get_string(plugin=plugin_name)) |
275 | 281 |
|
| 282 | + # Set the method's required arguments |
276 | 283 | unload_plugin.args = ['<plugin>'] |
277 | 284 |
|
278 | 285 | def reload_plugin(self, plugin_name): |
279 | | - '''''' |
| 286 | + '''Reloads a plugin by name''' |
280 | 287 |
|
281 | | - # |
| 288 | + # Unload the plugin |
282 | 289 | self.unload_plugin(plugin_name) |
283 | 290 |
|
284 | | - # |
| 291 | + # Load the plugin |
285 | 292 | self.load_plugin(plugin_name) |
286 | 293 |
|
| 294 | + # Set the method's required arguments |
287 | 295 | reload_plugin.args = ['<plugin>'] |
288 | 296 |
|
289 | 297 | def print_plugins(self): |
290 | | - '''''' |
| 298 | + '''Prints all currently loaded plugins''' |
291 | 299 |
|
292 | | - # |
| 300 | + # Get the header message |
293 | 301 | message = self.prefix + _plugin_strings[ |
294 | 302 | 'Plugins'].get_string() + '\n' + '=' * 61 + '\n\n\t' |
295 | 303 |
|
296 | | - # |
| 304 | + # Add all loaded plugins to the message |
297 | 305 | message += '\n\t'.join(self.manager) |
298 | 306 |
|
299 | | - # |
| 307 | + # Add a breaker at the end of the message |
300 | 308 | message += '\n' + '=' * 61 |
301 | 309 |
|
302 | | - # |
| 310 | + # Send the message |
303 | 311 | self.logger.log_message(message) |
0 commit comments