Пример #1
0
def compile(
    configuration_file: str,
    output_file: str,
    check: bool,
    options: Sequence[Tuple[str, str]],
):

    from choixe.configurations import XConfig
    import rich

    try:
        cfg = XConfig(filename=configuration_file)
    except Exception as e:
        rich.print(f"[red]Invalid configuration file: {e}[/red]")
        import sys

        sys.exit(1)

    placeholders = cfg.available_placeholders()
    for key, value in options:
        if key in placeholders:
            placeholder = placeholders[key]
            cfg.deep_set(key, placeholder.cast(value))

    if check:
        cfg.check_available_placeholders(close_app=True)

    cfg.save_to(output_file)
Пример #2
0
    def prompt(cls, xconfig: XConfig, close_app: bool = True) -> XConfig:
        """Prompts the placeholders of an xconfig and fill them
        with the user answers, it returns a copy of the original xconfig

        :param xconfig: the xconfig
        :type xconfig: XConfig
        :param close_app: if True the app will be closed if all the placeholder are not filled, defaults to True
        :type close_app: bool, optional
        :return: the filled xconfig
        :rtype: XConfig
        """

        xconfig = xconfig.copy()
        unique_phs = cls.unique_placeholders_with_order(
            xconfig.available_placeholders()
        )
        # user interaction
        questions = [cls.placeholder_to_question(x) for x in unique_phs]
        answers = cls._system_prompt(questions)
        # replaces placeholders with inquirer answers
        xconfig.replace_variables_map(answers)
        xconfig.check_available_placeholders(close_app=close_app)
        return xconfig
Пример #3
0
from choixe.configurations import XConfig
import rich

cfg = XConfig(filename="cfg.yml")

# Checks for placeholders before replace
rich.print("\nBefore Replace", cfg.to_dict())
cfg.check_available_placeholders(close_app=False)

# Iterate Placeholders
placeholders = cfg.available_placeholders()
for key, p in placeholders.items():
    # Replace default value if any
    if p.default_value is not None:
        cfg.replace_variable(
            p.name, p.default_value
        )  # Replace automatically Casts for CFG

# Checks for placeholders after replace
rich.print("\nAfter Replace", cfg.to_dict())

# Checks for placeholders after replace
cfg.deep_parse()
rich.print("\nAfter Second Deep Parse", cfg.to_dict())