Skip to content

configure_camply

Camply Configuration Script

check_dot_camply_file() #

Check to see if the .camply file already exists

Return the file existence status

Returns:

Type Description
bool
Source code in camply/utils/configure_camply.py
def check_dot_camply_file() -> bool:
    """
    Check to see if the `.camply` file already exists

    Return the file existence status

    Returns
    -------
    bool
    """
    if isfile(FileConfig.DOT_CAMPLY_FILE) is True:
        logger.info(
            "Skipping configuration. `.camply` file already exists: "
            f"{FileConfig.DOT_CAMPLY_FILE}"
        )
        return True
    else:
        return False

double_check(message) #

Double check if a step should be taken within CLI

Parameters:

Name Type Description Default
message str

Message to log in interactive shell

required
Source code in camply/utils/configure_camply.py
def double_check(message: str) -> bool:
    """
    Double check if a step should be taken within CLI

    Parameters
    ----------
    message: str
        Message to log in interactive shell
    """
    first_confirmation = Confirm.ask(prompt=message)
    if first_confirmation is True:
        second_confirmation = Confirm.ask(prompt="Are you sure?")
        return second_confirmation
    else:
        logging.info("Okay, skipping")
        return False

generate_configuration() #

Generate the Camply Configuration Config

Returns:

Type Description
OrderedDict

Dict of configuration values

Source code in camply/utils/configure_camply.py
def generate_configuration() -> OrderedDict:
    """
    Generate the Camply Configuration Config

    Returns
    -------
    OrderedDict
        Dict of configuration values
    """
    config_dict = FileConfig.DOT_CAMPLY_FIELDS.copy()
    for field, field_dict in config_dict.items():
        default_value = field_dict["default"]
        field_note = field_dict["notes"]
        if field_note is not None:
            rich.print(
                f"[bold blue]{field}:[/bold blue] "
                f"[bold green]{field_note}[/bold green]"
            )
        message = f"Enter value for [bold blue]{field}[/bold blue]"
        if default_value != "":
            message += f" (default: `[bold purple]{default_value}[/bold purple]`)"
        logged_input = Prompt.ask(prompt=message)
        config_value = logged_input if logged_input != "" else default_value
        config_dict[field] = config_value
    return config_dict

generate_dot_camply_file() #

Perform the larger Dot Camply File Generation

Source code in camply/utils/configure_camply.py
def generate_dot_camply_file():
    """
    Perform the larger Dot Camply File Generation
    """
    logger.info("Running camply configuration.")
    logger.info(
        "This process generates a configuration file "
        "(https://github.com/juftin/camply/blob/main/docs/examples/example.camply)"
    )
    logger.info("Do not include quotes around values")
    logger.info(
        "To skip a configuration field or keep it as default, just press <Enter>."
    )
    sleep(1.5)
    if isfile(FileConfig.DOT_CAMPLY_FILE):
        logger.warning(
            f".camply file already exists on this machine: {FileConfig.DOT_CAMPLY_FILE}"
        )
        overwrite = double_check(
            "Would you like to overwrite your "
            "[bold yellow].camply[/bold yellow] "
            "configuration file?"
        )
        if overwrite is False:
            sys.exit(0)
    config = generate_configuration()
    if double_check(
        "Are you ready to publish this to a file at "
        f"[bold yellow]{FileConfig.DOT_CAMPLY_FILE}[/bold yellow]"
    ):
        write_config_to_file(config_dict=config)
        logger.info(f"`.camply` file written to machine: {FileConfig.DOT_CAMPLY_FILE}")

write_config_to_file(config_dict) #

Write the Configuration Object to a file

Parameters:

Name Type Description Default
config_dict OrderedDict

Configuration Object

required
Source code in camply/utils/configure_camply.py
def write_config_to_file(config_dict: OrderedDict) -> None:
    """
    Write the Configuration Object to a file

    Parameters
    ----------
    config_dict : OrderedDict
        Configuration Object
    """
    string_list = [
        "# CAMPLY CONFIGURATION FILE. ",
        "# SEE https://github.com/juftin/camply/blob/main/docs/examples/example.camply",
        "",
    ]
    for config_key, config_value in config_dict.items():
        string_list.append(f'{config_key}="{config_value}"')
    string_list.append("")
    with open(FileConfig.DOT_CAMPLY_FILE, "w") as file_object:
        file_object.write("\n".join(string_list))
        file_object.seek(0)