在 coockiecutter 模板中,仅当 choice 变量具有给定值时才添加文件夹

Lyn*_*nda 6 python cookiecutter

我正在创建一个 cookiecutter 模板,并且仅当变量具有给定值时才添加文件夹(及其包含的文件)。例如 cookiecutter.json:

\n
{\n    "project_slug":"project_folder"\n    "i_want_this_folder":['y','n']\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我的模板结构如下所示:

\n
template\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 {{ cookiecutter.project_slug }}\n \xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config.ini\n \xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data\n \xc2\xa0\xc2\xa0 \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 data.csv\n \xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 {% if cookiecutter.i_want_this_folder == 'y' %}my_folder{% endif %}\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 some_files\n\n
Run Code Online (Sandbox Code Playgroud)\n

但是,当运行cookiecutter template并选择“n”时,我收到错误

\n
Error: "~/project_folder" directory already exists\n
Run Code Online (Sandbox Code Playgroud)\n

我的文件夹名称语法正确吗?

\n

Mig*_*ejo 3

我面临同样的问题,可以选择添加或不添加具有不同内容的文件夹(所有文件夹可以同时存在)。该项目的结构如下:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 {{cookiecutter.project_slug}}                 \n\xe2\x94\x82   \xe2\x94\x82\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 folder_1_to_add_or_no \n\xe2\x94\x82   \xe2\x94\x82    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file1.py\n\xe2\x94\x82   \xe2\x94\x82    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file2.py\n\xe2\x94\x82   \xe2\x94\x82    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file3.txt\n\xe2\x94\x82   \xe2\x94\x82\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 folder_2_to_add_or_no \n\xe2\x94\x82   \xe2\x94\x82    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 image.png\n\xe2\x94\x82   \xe2\x94\x82    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data.csv\n\xe2\x94\x82   \xe2\x94\x82    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 file.txt         \n\xe2\x94\x82   \xe2\x94\x82\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 folder_3_to_add_or_no \n\xe2\x94\x82        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 file1.py\n\xe2\x94\x82        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 some_dir \n\xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 hooks \n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 post_gen_project.py \n\xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 cookiecutter.json \n
Run Code Online (Sandbox Code Playgroud)\n

其中cookiecutter.json包含以下内容

\n
{\n  "project_owner": "some-name",\n  "project_slug": "some-project",\n  "add_folder_one": ["yes", "no"],\n  "add_folder_two": ["yes", "no"],\n  "add_folder_three": ["yes", "no"],\n}\n
Run Code Online (Sandbox Code Playgroud)\n

由于每个目录folder_X_to_add_or_no包含不同的文件,技巧是删除那些答案为“否”的文件夹,您可以通过钩子来完成此操作。post_gen_project.py文件里面

\n
# post_gen_project.py\nimport os\nimport shutil\n\nfrom pathlib import Path\n\n# Current path\npath = Path(os.getcwd())\n\n# Source path\nparent_path = path.parent.absolute()\n\ndef remove(filepath):\n    if os.path.isfile(filepath):\n        os.remove(filepath)\n    elif os.path.isdir(filepath):\n        shutil.rmtree(filepath)\n        \nfolders_to_add = [\n    \'folder_one\',\n    \'folder_two\',\n    \'folder_three\'\n] \n\nfor folder in folders_to_add:\n\n    # Check if user wants the folder\n    cookiecutter_var = \'{{cookiecutter.\' + f\'{folder}\' + \'}}\'\n    add_folder = cookiecutter_var == \'yes\'\n    \n    # User does not want folder so remove it\n    if not add_folder:\n\n        folder_path = os.path.join(\n            parent_path, \n            \'{{cookiecutter.project_slug}}\', \n            \'folder\'\n        )\n\n        remove(folder_path)\n
Run Code Online (Sandbox Code Playgroud)\n

现在,用户选择不添加的文件夹将被删除。

\n
Select add_folder_one:\n1 - yes\n2 - no\nChoose from 1, 2 [1]: \n
Run Code Online (Sandbox Code Playgroud)\n

参考

\n

这个答案基于 briancapello 对这个 github问题的回答

\n