This makes call sites simpler since they won't have to deal with
missing keys.
Details
Details
- Reviewers
durin42 - Group Reviewers
hg-reviewers
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
durin42 |
hg-reviewers |
This makes call sites simpler since they won't have to deal with
missing keys.
Lint Skipped |
Unit Tests Skipped |
# command. | # command. | ||||
extra = set(self._args) - set(args) | extra = set(self._args) - set(args) | ||||
if extra: | if extra: | ||||
raise error.WireprotoCommandError( | raise error.WireprotoCommandError( | ||||
'unsupported argument to command: %s' % | 'unsupported argument to command: %s' % | ||||
', '.join(sorted(extra))) | ', '.join(sorted(extra))) | ||||
# And look for required arguments that are missing. | # And look for required arguments that are missing. | ||||
missing = {a for a in args if args[a].get('required')} - set(self._args) | missing = {a for a in args if args[a]['required']} - set(self._args) | ||||
if missing: | if missing: | ||||
raise error.WireprotoCommandError( | raise error.WireprotoCommandError( | ||||
'missing required arguments: %s' % ', '.join(sorted(missing))) | 'missing required arguments: %s' % ', '.join(sorted(missing))) | ||||
# Now derive the arguments to pass to the command, taking into | # Now derive the arguments to pass to the command, taking into | ||||
# account the arguments specified by the client. | # account the arguments specified by the client. | ||||
data = {} | data = {} | ||||
for k, meta in sorted(args.items()): | for k, meta in sorted(args.items()): | ||||
# This argument wasn't passed by the client. | # This argument wasn't passed by the client. | ||||
if k not in self._args: | if k not in self._args: | ||||
data[k] = meta.get('default', lambda: None)() | data[k] = meta['default']() | ||||
continue | continue | ||||
v = self._args[k] | v = self._args[k] | ||||
# Sets may be expressed as lists. Silently normalize. | # Sets may be expressed as lists. Silently normalize. | ||||
if meta['type'] == 'set' and isinstance(v, list): | if meta['type'] == 'set' and isinstance(v, list): | ||||
v = set(v) | v = set(v) | ||||
the argument name. Values are dicts with the following keys: | the argument name. Values are dicts with the following keys: | ||||
``type`` | ``type`` | ||||
The argument data type. Must be one of the following string | The argument data type. Must be one of the following string | ||||
literals: ``bytes``, ``int``, ``list``, ``dict``, ``set``, | literals: ``bytes``, ``int``, ``list``, ``dict``, ``set``, | ||||
or ``bool``. | or ``bool``. | ||||
``default`` | ``default`` | ||||
A callable returning the default value for this argument. | A callable returning the default value for this argument. If not | ||||
specified, ``None`` will be the default value. | |||||
``required`` | ``required`` | ||||
Bool indicating whether the argument is required. | Bool indicating whether the argument is required. | ||||
``example`` | ``example`` | ||||
An example value for this argument. | An example value for this argument. | ||||
``permission`` defines the permission type needed to run this command. | ``permission`` defines the permission type needed to run this command. | ||||
raise error.ProgrammingError('%s argument for command %s has ' | raise error.ProgrammingError('%s argument for command %s has ' | ||||
'illegal type: %s' % (arg, name, | 'illegal type: %s' % (arg, name, | ||||
meta['type'])) | meta['type'])) | ||||
if 'example' not in meta: | if 'example' not in meta: | ||||
raise error.ProgrammingError('%s argument for command %s does not ' | raise error.ProgrammingError('%s argument for command %s does not ' | ||||
'declare example field' % (arg, name)) | 'declare example field' % (arg, name)) | ||||
meta.setdefault('default', lambda: None) | |||||
meta.setdefault('required', False) | |||||
def register(func): | def register(func): | ||||
if name in COMMANDS: | if name in COMMANDS: | ||||
raise error.ProgrammingError('%s command already registered ' | raise error.ProgrammingError('%s command already registered ' | ||||
'for version 2' % name) | 'for version 2' % name) | ||||
COMMANDS[name] = wireprototypes.commandentry( | COMMANDS[name] = wireprototypes.commandentry( | ||||
func, args=args, transports=transports, permission=permission) | func, args=args, transports=transports, permission=permission) | ||||