Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Manages subcommands in a script. | |
| 6 | |
| 7 Each subcommand should look like this: | |
| 8 @usage('[pet name]') | |
| 9 def CMDpet(parser, args): | |
| 10 '''Prints a pet. | |
| 11 | |
| 12 Many people likes pet. This command prints a pet for your pleasure. | |
| 13 ''' | |
| 14 parser.add_option('--color', help='color of your pet') | |
| 15 options, args = parser.parse_args(args) | |
| 16 if len(args) != 1: | |
| 17 parser.error('A pet name is required') | |
| 18 pet = args[0] | |
| 19 if options.color: | |
| 20 print('Nice %s %d' % (options.color, pet)) | |
| 21 else: | |
| 22 print('Nice %s' % pet) | |
| 23 return 0 | |
| 24 | |
| 25 Explanation: | |
| 26 - usage decorator alters the 'usage: %prog' line in the command's help. | |
| 27 - docstring is used to both short help line and long help line. | |
| 28 - parser can be augmented with arguments. | |
| 29 - return the exit code. | |
| 30 - Every function in the specified module with a name starting with 'CMD' will | |
| 31 be a subcommand. | |
| 32 - The module's docstring will be used in the default 'help' page. | |
| 33 - If a command has no docstring, it will not be listed in the 'help' page. | |
| 34 Useful to keep compatibility commands around or aliases. | |
| 35 - If a command is an alias to another one, it won't be documented. E.g.: | |
| 36 CMDoldname = CMDnewcmd | |
| 37 will result in oldname not being documented but supported and redirecting to | |
| 38 newcmd. Make it a real function that calls the old function if you want it | |
| 39 to be documented. | |
| 40 """ | |
| 41 | |
| 42 import difflib | |
| 43 import sys | |
| 44 | |
| 45 | |
| 46 def usage(more): | |
| 47 """Adds a 'usage_more' property to a CMD function.""" | |
| 48 def hook(fn): | |
| 49 fn.usage_more = more | |
| 50 return fn | |
| 51 return hook | |
| 52 | |
| 53 | |
| 54 def CMDhelp(parser, args): | |
| 55 """Prints list of commands or help for a specific command.""" | |
| 56 # This is the default help implementation. It can be disabled or overriden if | |
| 57 # wanted. | |
| 58 if not any(i in ('-h', '--help') for i in args): | |
| 59 args = args + ['--help'] | |
| 60 _, args = parser.parse_args(args) | |
| 61 # Never gets there. | |
| 62 assert False | |
| 63 | |
| 64 | |
| 65 class CommandDispatcher(object): | |
| 66 def __init__(self, module): | |
| 67 """module is the name of the main python module where to look for commands. | |
| 68 | |
| 69 The python builtin variable __name__ MUST be used for |module|. If the | |
| 70 script is executed in the form 'python script.py', __name__ == '__main__' | |
| 71 and sys.modules['script'] doesn't exist. On the other hand if it is unit | |
| 72 tested, __main__ will be the unit test's module so it has to reference to | |
| 73 itself with 'script'. __name__ always match the right value. | |
|
iannucci
2013/08/16 19:53:06
Ah, good call. This works :)
| |
| 74 """ | |
| 75 self.module = sys.modules[module] | |
| 76 | |
| 77 def enumerate_commands(self): | |
| 78 """Returns a dict of command and their handling function. | |
| 79 | |
| 80 The commands must be in the '__main__' modules. To import a command from a | |
| 81 submodule, use: | |
| 82 from mysubcommand import CMDfoo | |
| 83 | |
| 84 Automatically adds 'help' if not already defined. | |
| 85 | |
| 86 A command can be effectively disabled by defining a global variable to None, | |
| 87 e.g.: | |
| 88 CMDhelp = None | |
| 89 """ | |
| 90 cmds = dict( | |
| 91 (fn[3:], getattr(self.module, fn)) | |
| 92 for fn in dir(self.module) if fn.startswith('CMD')) | |
| 93 cmds.setdefault('help', CMDhelp) | |
| 94 return cmds | |
| 95 | |
| 96 def find_nearest_command(self, name): | |
| 97 """Retrieves the function to handle a command. | |
| 98 | |
| 99 It automatically tries to guess the intended command by handling typos or | |
| 100 incomplete names. | |
| 101 """ | |
| 102 commands = self.enumerate_commands() | |
| 103 if name in commands: | |
| 104 return commands[name] | |
| 105 | |
| 106 # An exact match was not found. Try to be smart and look if there's | |
| 107 # something similar. | |
| 108 commands_with_prefix = [c for c in commands if c.startswith(name)] | |
| 109 if len(commands_with_prefix) == 1: | |
| 110 return commands[commands_with_prefix[0]] | |
| 111 | |
| 112 # A #closeenough approximation of levenshtein distance. | |
| 113 def close_enough(a, b): | |
| 114 return difflib.SequenceMatcher(a=a, b=b).ratio() | |
| 115 | |
| 116 hamming_commands = sorted( | |
| 117 ((close_enough(c, name), c) for c in commands), | |
| 118 reverse=True) | |
| 119 if (hamming_commands[0][0] - hamming_commands[1][0]) < 0.3: | |
| 120 # Too ambiguous. | |
| 121 return | |
| 122 | |
| 123 if hamming_commands[0][0] < 0.8: | |
| 124 # Not similar enough. Don't be a fool and run a random command. | |
| 125 return | |
| 126 | |
| 127 return commands[hamming_commands[0][1]] | |
| 128 | |
| 129 def _add_command_usage(self, parser, command): | |
| 130 """Modifies an OptionParser object with the function's documentation.""" | |
| 131 name = command.__name__[3:] | |
| 132 more = getattr(command, 'usage_more', '') | |
| 133 if name == 'help': | |
| 134 name = '<command>' | |
| 135 # Use the module's docstring as the description for the 'help' command if | |
| 136 # available. | |
| 137 parser.description = self.module.__doc__ | |
| 138 else: | |
| 139 # Use the command's docstring if available. | |
| 140 parser.description = command.__doc__ | |
| 141 parser.description = (parser.description or '').strip() | |
| 142 if parser.description: | |
| 143 parser.description += '\n' | |
| 144 parser.set_usage( | |
| 145 'usage: %%prog %s [options]%s' % (name, '' if not more else ' ' + more)) | |
| 146 | |
| 147 @staticmethod | |
| 148 def _create_command_summary(name, command): | |
| 149 """Creates a oneline summary from the command's docstring.""" | |
| 150 if name != command.__name__[3:]: | |
| 151 # Skip aliases. | |
| 152 return '' | |
| 153 doc = command.__doc__ or '' | |
| 154 line = doc.split('\n', 1)[0].rstrip('.') | |
| 155 if not line: | |
| 156 return line | |
| 157 return (line[0].lower() + line[1:]).strip() | |
| 158 | |
| 159 def execute(self, parser, args): | |
| 160 """Dispatches execution to the right command. | |
| 161 | |
| 162 Fallbacks to 'help' if not disabled. | |
| 163 """ | |
| 164 commands = self.enumerate_commands() | |
| 165 length = max(len(c) for c in commands) | |
| 166 | |
| 167 # Lists all the commands in 'help'. | |
| 168 if commands['help']: | |
| 169 docs = sorted( | |
| 170 (name, self._create_command_summary(name, handler)) | |
| 171 for name, handler in commands.iteritems()) | |
| 172 # Skip commands without a docstring. | |
| 173 commands['help'].usage_more = ( | |
| 174 '\n\nCommands are:\n' + '\n'.join( | |
| 175 ' %-*s %s' % (length, name, doc) for name, doc in docs if doc)) | |
| 176 | |
| 177 if args: | |
| 178 if args[0] in ('-h', '--help') and len(args) > 1: | |
| 179 # Inverse the argument order so 'tool --help cmd' is rewritten to | |
| 180 # 'tool cmd --help'. | |
| 181 args = [args[1], args[0]] + args[2:] | |
| 182 command = self.find_nearest_command(args[0]) | |
| 183 if command: | |
| 184 if command.__name__ == 'CMDhelp' and len(args) > 1: | |
| 185 # Inverse the arguments order so 'tool help cmd' is rewritten to | |
| 186 # 'tool cmd --help'. Do it here since we want 'tool hel cmd' to work | |
| 187 # too. | |
| 188 args = [args[1], '--help'] + args[2:] | |
| 189 command = self.find_nearest_command(args[0]) or command | |
| 190 | |
| 191 # "fix" the usage and the description now that we know the subcommand. | |
| 192 self._add_command_usage(parser, command) | |
| 193 return command(parser, args[1:]) | |
| 194 | |
| 195 if commands['help']: | |
| 196 # Not a known command. Default to help. | |
| 197 self._add_command_usage(parser, commands['help']) | |
| 198 return commands['help'](parser, args) | |
| 199 | |
| 200 # Nothing can be done. | |
| 201 return 2 | |
| OLD | NEW |