def __call__(self, parser, namespace, values, option_string=None): if not values: parser.print_help() elif values=='config': print(helputils.format_config(self.app.config_class)) elif values in self.app.default_provider_names: module = importlib.import_module(values) print(helputils.format_providermodule(module)) else: #TODO: This is ugly as hell providermods = self.app.load_providers() rb = ResourceBuilder(self.app.config_class({}), providermods, []) try: providertree = rb.explain_provider(values) except AttributeError: alternatives = ['config', *self.app.default_provider_names] alternatives += [f for mod in providermods for f in get_providers(mod).keys() ] msg = "No help available for %s" % values print(ErrorWithAlternatives(msg, values, alternatives), file=sys.stderr) else: print(helputils.print_providertree(providertree)) parser.exit()
def test_builder(self): extra_args = ( ('time', '10AM') ,) targets = [ Target('english_breakfast', tuple(), extra_args), Target('spam', tuple(), ()), Target('restaurant', tuple(), ()) ] c = Config({'restaurant': "La Patata"}) provider = Provider() builder = ResourceBuilder(targets=targets, providers=provider, input_parser=c) builder.resolve_targets() builder.execute_sequential() namespace = builder.rootns breakfast_key = builder.targets[0].name self.assertEqual(namespace[breakfast_key], "At La Patata. Preparing breakfast with: spam,ham,eggs at 10AM.") rest_key = builder.targets[2].name self.assertEqual(namespace[rest_key], "La Patata")
def test_remove_outer(self): targets = [Target('fruit', (['inner']), ())] c = Config({'apple': True, 'inner':{'orange':False}}) provider = Provider() builder = ResourceBuilder(targets=targets, providers=provider, input_parser=c) builder.resolve_targets() builder.execute_sequential() ns = namespaces.resolve(builder.rootns, ('inner',)) self.assertEqual(ns['fruit'], (None, False))
def test_collect(self): inp = { 'Spain': {'restaurants': [{'restaurant': x} for x in ["La patata", "La boñata"]]}, 'UK': {'restaurants': [{'restaurant':x} for x in ["Whetherspoon","Kings arms"]]}, 'apple': "Golden", } provider = Provider() c = Config(inp) targets = [ Target('score', ('Spain',), ()), Target('score', ('UK',), ()) ] builder = ResourceBuilder(targets=targets, providers=provider, input_parser=c) builder.resolve_targets() builder.execute_parallel()
def test_nested_specs(self): inp = { 'a': {'oranges': 'Valencia'}, 'b': {'oranges': 'Ivrea'}, 'apple': "Golden", } provider = Provider() c = Config(inp) targets = [ Target('sweet_breakfast', tuple('a'), ()), Target('sweet_breakfast', tuple('b'), ()) ] builder = ResourceBuilder(targets=targets, providers=provider, input_parser=c) builder.resolve_targets() builder.execute_sequential()
def test_require_one(self): targets = [Target("fruit", (), ())] c = Config({}) provider = Provider() builder = ResourceBuilder(targets=targets, providers=provider, input_parser=c) with self.assertRaises(ResourceError): builder.resolve_targets() c = Config({"apple": True}) builder = ResourceBuilder(targets=targets, providers=provider, input_parser=c) builder.resolve_targets() builder.execute_sequential() self.assertEqual(builder.rootns["fruit"], (True, None))
def run(self): args = self.args environment = self.environment parallel = args['parallel'] config_file = args['config_yml'] try: with open(config_file) as f: try: c = self.config_class.from_yaml(f, environment=environment) except ConfigError as e: format_rich_error(e) sys.exit(1) except OSError as e: log.error("Could not open configuration file: %s" % e) sys.exit(1) self.environment.init_output() try: actions = c.parse_actions_(c['actions_']) except ConfigError as e: format_rich_error(e) sys.exit(1) except KeyError as e: log.error("A key 'actions_' is needed in the top level of the config file.") sys.exit(1) providers = self.providers rb = ResourceBuilder(c, providers, actions, environment=self.environment) try: rb.resolve_targets() except ConfigError as e: format_rich_error(e) sys.exit(1) except ResourceError as e: with contextlib.redirect_stdout(sys.stderr): log.error("Cannot process a resource:") print(e) sys.exit(1) try: if parallel: rb.execute_parallel() else: rb.execute_sequential() except KeyboardInterrupt: print(colors.t.bold_red("Interrupted by user. Exiting."), file=sys.stderr) exit(1)