How can I update my README.md file in a cookiecutter project with python?

63 views Asked by At

I am trying to automatically update my README.md file in my cookiecutter project after it has been created. The project structure in the README.md should be adapted according to the features selected by the user (cookiecutter deletes some folders). However, all lines of the project organization are skipped and not a single line of the section is displayed in the README.

The folder structure in the README when using all features is as follows:

    ├── assets             <- Folder for storing assets like images 
    ├── data               <- Folder for storing your data 
    ├── docs               <- A default Sphinx project; see sphinx-doc.org for details
    ├── models             <- Trained and serialized models, model predictions, or model summaries
    ├── notebooks          <- Jupyter notebooks
    |
    ├── src                <- Source code for use in this project
    │   ├── data           <- Scripts to download or generate data
    │   ├── features       <- Scripts to turn raw data into features for modeling
    │   ├── models         <- Scripts to train models and then use trained models to make
    │   │                     predictions
    │   ├── pages          <- Contains your application views
    │   ├── style          <- Contains all style related code 
    │   ├── utils          <- This folder is for storing all utility functions, such as auth, 
    |   |                     theme, handleApiError, etc.
    │   ├── visualization  <- Scripts to create visualizations 
    |   └── widgets        <- Contains custom widgets 
    │
    ├── .env                        <- File for storing passwords
    ├── .gitignore                  <- Specifies intentionally untracked files to ignore
    ├── .pre-commit.config.yaml     <- Configuration file for the pre-commits
    ├── poetry.lock                 <- Autogenerated file for handling dependencies
    ├── pyproject.toml              <- Configuration of dependencies and project variables e.g. version
    └── README.md                   <- The top-level README for developers using this project.

And with my post_gen_project I am trying to remove all lines containing folders that were removed. Here is a MVE of my post_gen_project.py:

import os
import sys

PROJECT_DIRECTORY = os.path.realpath(os.path.curdir)

def update_readme():
    lines = []
    gui_indicators = [
        "Folder for storing assets like images",
        "Contains your application views",
        "Contains all style related code",
        "Contains custom widgets",
    ]
    data_science_indicators = [
        "Trained and serialized models, model predictions, or model summaries",
        "Scripts to turn raw data into features for modeling",
        "Scripts to train models and then use trained models to make",
        "Scripts to create visualizations",
    ]

    # read file
    with open(os.path.join(PROJECT_DIRECTORY, "README.md"), "r") as file:
        lines = file.readlines()

    # Write file
    with open(os.path.join(PROJECT_DIRECTORY, "README.md"), "w") as file:
        for line in lines:
            if (
                "{{ cookiecutter.use_pre_commits }}" == "false"
                and "Configuration file for the pre-commits" in line
                or "{{ cookiecutter.use_sphinx_documentation }}" == "false"
                and "A default Sphinx project; see sphinx-doc.org for details" in line
                or "{{ cookiecutter.include_gui_structure }}" == "false"
                and any(indicator in line for indicator in gui_indicators)
                or "{{ cookiecutter.include_data_science_structure }}" == "false"
                and any(indicator in line for indicator in data_science_indicators)
            ):
                pass  # skip line
            else:
                file.write(line)


if __name__ == "__main__":
    update_readme()

And my cookiecutter.json:

{
    "project_name": "example project",
    "project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_') }}",
    "project_description": "short description",
    "version": "0.1.0",
    "author": "Name",
    "email": "",
    "include_gui_structure": false,
    "include_data_science_structure": false,
    "use_pre_commits": true,
    "use_sphinx_documentation": true,
    "repo_url": "https://github.com/user/example_repo.git",
    "branch_name": "main"
    }

I also tried to check for "False", but could not make it work. Any suggestion?

0

There are 0 answers