Exemple #1
0
def launcher(func):
    """Option decorator fer the commands using a launcher"""
    opts = [
        option("--launcher-command",
               help=("Extra arguments to prepend to the simulation command."
                     " Ignored if --launcher is given."),
               type=LauncherCommandType(),
               group="launcher"),
        option(
            "-l",
            "--launcher",
            help=("Name of the launcher to prepend to the simulation command."
                  " It overrides --launcher-command."
                  " See the launchers commands for more information"),
            type=LauncherType(),
            group="launcher")
    ]
    for opt in reversed(opts):
        func = opt(func)
    return func
Exemple #2
0
 def decorator(func):
     fields_type = click.Choice(choices) if choices else None
     opts = [
         option('--field',
                'fields',
                multiple=True,
                type=fields_type,
                default=default,
                help="Only display the following fields in the output",
                callback=callback)
     ]
     for opt in reversed(opts):
         func = opt(func)
     return func
Exemple #3
0
 def decorator(func):
     # not sure why, but python can't access the default value with a closure in a statement of this kind
     #     default = default
     # so we have to use another name
     actual_default = config.get_settings('values').get(
         config_name, {}).get('value') or default or 'simple'
     opts = [
         option('--format',
                default=actual_default,
                help='Table format',
                type=get_tabulate_formats()),
     ]
     for opt in reversed(opts):
         func = opt(func)
     return func
Exemple #4
0
    def decorator(f):
        if settings_name not in settings_stores:
            settings_stores[settings_name] = settings_cls()
        settings_store = settings_stores[settings_name]
        settings_store.recipe = None

        def compute_settings(with_explicit=True):
            settings_store.all_settings = {
                profile.name: profile.get_settings(settings_name)
                for profile in config.all_enabled_profiles
                if with_explicit or not profile.explicit
            }

        def setup_settings(ctx):
            if ctx is not None and hasattr(ctx, "click_project_profile"):
                profile_name = ctx.click_project_profile
            else:
                profile_name = default_profile
            if ctx is not None and hasattr(ctx, "click_project_recipe"):
                recipe = ctx.click_project_recipe
            else:
                recipe = "main"
            if recipe == "main":
                recipe = None
            if profile_name == "context":
                compute_settings(False)
                s1, s2 = merge_settings(
                    config.iter_settings(
                        recurse=True,
                        only_this_recipe=recipe,
                    ))
                profile = config.local_profile or config.global_profile
                if recipe:
                    profile = profile.get_recipe(recipe)
                    for r in config.get_enabled_recipes_by_short_name(recipe):
                        settings_store.all_settings[r.name] = r.get_settings(
                            settings_name)
                else:
                    compute_settings(True)
                settings_store.readprofile = profile_name
            else:
                compute_settings(False)
                profile = config.get_profile(profile_name)
                profile = profile.get_recipe(recipe) if recipe else profile
                settings_store.readprofile = profile_name
                settings_store.all_settings[
                    profile_name] = profile.get_settings(settings_name)
                s1, s2 = merge_settings(
                    config.load_settings_from_profile(
                        profile,
                        recurse=True,
                        only_this_recipe=recipe,
                    ))
                for recipe in config.filter_enabled_profiles(profile.recipes):
                    settings_store.all_settings[
                        recipe.name] = recipe.get_settings(settings_name)

            readonly = s1 if override else s2
            readonly = readonly.get(settings_name, {})

            settings_store.writeprofile = profile.name
            settings_store.writeprofilename = profile.friendly_name
            settings_store.profile = profile
            settings_store.readonly = readonly
            settings_store.writable = profile.get_settings(settings_name)
            settings_store.write = profile.write_settings

        def recipe_callback(ctx, attr, value):
            if value is not None:
                ctx.click_project_recipe = value
                setup_settings(ctx)
            return value

        class RecipeType(ParameterType):
            @property
            def choices(self):
                return [r.short_name for r in config.all_recipes] + ["main"]

            def complete(self, ctx, incomplete):
                return [
                    candidate for candidate in self.choices
                    if startswith(candidate, incomplete)
                ]

        def profile_name_to_commandline_name(name):
            return name.replace("/", "-")

        def commandline_name_to_profile_name(name):
            return name.replace("-", "/")

        def profile_callback(ctx, attr, value):
            if value:
                ctx.click_project_profile = commandline_name_to_profile_name(
                    value)
                setup_settings(ctx)
            return value

        for profile in [
                profile_name_to_commandline_name(profile.name)
                for profile in config.root_profiles
        ]:
            f = flag('--{}'.format(profile),
                     "profile",
                     flag_value=profile,
                     help="Consider only the {} profile".format(profile),
                     callback=profile_callback)(f)
        f = flag('--context',
                 "profile",
                 flag_value="context",
                 help="Guess the profile",
                 callback=profile_callback)(f)
        f = option('--recipe',
                   type=RecipeType(),
                   callback=recipe_callback,
                   help="Use this recipe")(f)

        setup_settings(None)

        @functools.wraps(f)
        def wrapped(*args, **kwargs):
            setattr(config, settings_name, settings_store)
            del kwargs["recipe"]
            del kwargs["profile"]
            LOGGER.debug("Will use the settings at profile {}".format(
                settings_store.readprofile))
            return f(*args, **kwargs)

        wrapped.inherited_params = ["recipe", "profile"]
        return wrapped
Exemple #5
0
    def decorator(f):
        if settings_name not in settings_stores:
            settings_stores[settings_name] = settings_cls()
        settings_store = settings_stores[settings_name]
        settings_store.recipe = None

        def compute_settings(context=True):
            settings_store.all_settings = {
                "global/preset":
                config.global_context_settings.get(settings_name, {}),
                "local/preset":
                config.local_context_settings.get(settings_name, {}),
                "env":
                config.env_settings.get(settings_name, {}),
                "commandline":
                config.command_line_settings.get(settings_name, {}),
            }
            if context:
                settings_store.all_settings.update({
                    "global":
                    config.global_profile.get_settings(settings_name),
                    "workgroup":
                    config.workgroup_profile
                    and config.workgroup_profile.get_settings(settings_name),
                    "local":
                    config.local_profile
                    and config.local_profile.get_settings(settings_name),
                })
                for recipe in config.all_recipes:
                    settings_store.all_settings[
                        recipe.name] = recipe.get_settings(settings_name)
                if config.local_profile:
                    name = config.local_profile.name
                    settings_store.all_settings[
                        name] = config.local_profile.get_settings(
                            settings_name)
            for key, value in settings_store.all_settings.copy().items():
                if value is None:
                    del settings_store.all_settings[key]

        def setup_settings(ctx):
            if ctx is not None and hasattr(ctx, "click_project_level"):
                level = ctx.click_project_level
            else:
                level = default_level
            if ctx is not None and hasattr(ctx, "click_project_recipe"):
                recipe = ctx.click_project_recipe
            else:
                recipe = "main"
            if recipe == "main":
                recipe = None
            if level == "context":
                compute_settings(False)
                s1, s2 = merge_settings(
                    config.iter_settings(
                        profiles_only=True,
                        with_recipes=True,
                        recipe_short_name=recipe,
                    ))
                profile = config.local_profile or config.global_profile
                if recipe:
                    profile = profile.get_recipe(recipe)
                    for r in config.iter_recipes(recipe):
                        settings_store.all_settings[r.name] = r.get_settings(
                            settings_name)
                else:
                    compute_settings(True)
                settings_store.readlevel = level
            else:
                compute_settings(False)
                profile = config.profiles_per_level[level]
                profile = profile.get_recipe(recipe) if recipe else profile
                settings_store.readlevel = profile.name
                settings_store.all_settings[
                    profile.name] = profile.get_settings(settings_name)
                s1, s2 = merge_settings(
                    config.load_settings_from_profile(
                        profile,
                        with_recipes=True,
                        recipe_short_name=recipe,
                    ))
                for recipe in config.filter_enabled_recipes(profile.recipes):
                    settings_store.all_settings[
                        recipe.name] = recipe.get_settings(settings_name)

            readonly = s1 if override else s2
            readonly = readonly.get(settings_name, {})

            settings_store.writelevel = profile.name
            settings_store.writelevelname = profile.friendly_name
            settings_store.profile = profile
            settings_store.readonly = readonly
            settings_store.writable = profile.get_settings(settings_name)
            settings_store.write = profile.write_settings

        def level_callback(ctx, attr, value):
            if value:
                ctx.click_project_level = value
                setup_settings(ctx)
            return value

        def recipe_callback(ctx, attr, value):
            if value is not None:
                ctx.click_project_recipe = value
                setup_settings(ctx)
            return value

        class RecipeType(ParameterType):
            @property
            def choices(self):
                return [r.short_name for r in config.all_recipes] + ["main"]

            def complete(self, ctx, incomplete):
                return [
                    candidate for candidate in self.choices
                    if startswith(candidate, incomplete)
                ]

        for level in config.levels:
            f = flag('--{}'.format(level),
                     "level",
                     flag_value=level,
                     help="Consider only the {} level".format(level),
                     callback=level_callback)(f)
        f = flag('--context',
                 "level",
                 flag_value="context",
                 help="Guess the level",
                 callback=level_callback)(f)
        f = option('--recipe',
                   type=RecipeType(),
                   callback=recipe_callback,
                   help="Use this recipe")(f)

        setup_settings(None)

        @functools.wraps(f)
        def wrapped(*args, **kwargs):
            setattr(config, settings_name, settings_store)
            del kwargs["recipe"]
            del kwargs["level"]
            LOGGER.debug("Will use the settings at level {}".format(
                settings_store.readlevel))
            return f(*args, **kwargs)

        wrapped.inherited_params = ["recipe", "level"]
        return wrapped