Bug 1256571 - Add a generic method to get the results of Options and DependsFunctions. r=chmanchester

instead of manually reading the member variable containing the results.
This commit is contained in:
Mike Hommey 2016-04-08 15:23:25 +09:00
Родитель a1c0231f85
Коммит 56bbf297c7
1 изменённых файлов: 15 добавлений и 16 удалений

Просмотреть файл

@ -164,9 +164,7 @@ class ConfigureSandbox(dict):
self._help_option = self.option_impl('--help', self._help_option = self.option_impl('--help',
help='print this message') help='print this message')
self._seen.add(self._help_option) self._seen.add(self._help_option)
# self._option_impl('--help') will have set this if --help was on the if self._value_for(self._help_option):
# command line.
if self._option_values[self._help_option]:
self._help = HelpFormatter(argv[0]) self._help = HelpFormatter(argv[0])
self._help.add(self._help_option) self._help.add(self._help_option)
elif moz_logger: elif moz_logger:
@ -256,15 +254,24 @@ class ConfigureSandbox(dict):
if isinstance(arg, DependsFunction): if isinstance(arg, DependsFunction):
assert arg in self._depends assert arg in self._depends
func, deps = self._depends[arg] func, deps = self._depends[arg]
assert not inspect.isgeneratorfunction(func)
assert func in self._results
if need_help_dependency and self._help_option not in deps: if need_help_dependency and self._help_option not in deps:
raise ConfigureError("Missing @depends for `%s`: '--help'" % raise ConfigureError("Missing @depends for `%s`: '--help'" %
func.__name__) func.__name__)
result = self._results[func] return self._value_for(arg)
return result
return arg return arg
def _value_for(self, obj):
if isinstance(obj, DependsFunction):
assert obj in self._depends
func, deps = self._depends[obj]
assert not inspect.isgeneratorfunction(func)
assert func in self._results
return self._results[func]
elif isinstance(obj, Option):
assert obj in self._option_values
return self._option_values.get(obj)
assert False
def option_impl(self, *args, **kwargs): def option_impl(self, *args, **kwargs):
'''Implementation of option() '''Implementation of option()
This function creates and returns an Option() object, passing it the This function creates and returns an Option() object, passing it the
@ -364,15 +371,7 @@ class ConfigureSandbox(dict):
% (func.__name__, arg.__name__, arg.__name__)) % (func.__name__, arg.__name__, arg.__name__))
if not self._help or with_help: if not self._help or with_help:
resolved_args = [] resolved_args = [self._value_for(d) for d in dependencies]
for arg in dependencies:
if isinstance(arg, Option):
assert arg in self._option_values
resolved_args.append(self._option_values.get(arg))
elif isinstance(arg, DependsFunction):
arg, _ = self._depends[arg]
resolved_args.append(self._results.get(arg))
self._results[func] = func(*resolved_args) self._results[func] = func(*resolved_args)
return dummy return dummy