如何在使用子文件夹模板的cookiecutter模板中创建多个子文件夹

3
我希望为我们的内部项目创建一个模板。
项目的布局包含可变数量的子文件夹。
我想在某个配置文件中指定要创建的子文件夹。 例如,我有以下文件夹结构:
my_project
|
|___ Tables
|  |
|  |__ Table1    <--- The folders I would like to create
|  |__ Table2    <---|
|  |__ Table3    <---|
|
|___ Other_folders

配置文件应该有这样的内容:

配置文件应该有类似这样的内容:

{
  "prj_name": "my_project",
  "tables": ["Table1", "Table2", "Table3"]
}

我尝试创建以下结构:

{{cookiecutter.prj_name}}
|
|___ Tables
|  |
|  |__ {% for table in cookiecutter.tables %}{{table}}{% endfor %}
|
|___ Other_folders

将提到的配置添加到cookiecutter.json中。

我使用了--no-input运行了cookiecutter,结果只有一个子文件夹(Table1)。

还尝试使用以下文件夹名称:{% for table in cookiecutter.tables.keys() %}{{table}}{% endfor %} 使用以下配置:

{
  "prj_name": "my_project",
  "tables": {
    "table1": "table1",
    "table2": "table2",
    "table3": "table3"
  }
}

输出仍是一个单独的子文件夹(Table2Table1Table3)

有什么办法可以实现所需的结构吗?

2个回答

5

最终,我编写了一个使用Cookiecutter包函数的Python脚本,它有两个模板文件夹。

其中一个是用于主项目的模板文件夹,另一个是用于表文件夹的模板文件夹。

此外,该脚本会使用cookiecutter.main.cookiecutter函数循环创建主项目和子文件夹。

我在博客中写了一篇更详细的文章。不确定这里是否允许发布指向个人博客的链接,如果未来有人想查看,请在评论区告诉我。


你好,我很感兴趣看到你的扩展。我也在寻找这个功能。你能发布你的解决方案吗? :) - maryhadalittlelamb
3
谢谢您的关注。这篇文章链接在这里:https://igorbasko01.github.io/cookiecutter/python/2020/02/04/cookiecutter-sub-folders.html。您可以直接跳到底部的解决方案部分。 - Igor Basko

-1

我在这里采用了一种低技术的解决方案。我的用例非常简单,我需要创建的文件夹数量是有限的,所以我的解决方案对我来说是可管理的。你的情况可能不同,但我想在这里提供它,以防它适用于其他人。

我基本上预先填充了我认为我将来需要的目录数量。我只是使用Python的formatrange循环来预填充我需要的任意数量的文件夹。

cookiecutter/mycookiecutter/
├── cookiecutter.json
└── {{cookiecutter.project_name}}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 0\ %}{{cookiecutter.account_info["accounts"][0]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 1\ %}{{cookiecutter.account_info["accounts"][1]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 10\ %}{{cookiecutter.account_info["accounts"][10]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 11\ %}{{cookiecutter.account_info["accounts"][11]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 12\ %}{{cookiecutter.account_info["accounts"][12]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 13\ %}{{cookiecutter.account_info["accounts"][13]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 14\ %}{{cookiecutter.account_info["accounts"][14]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 15\ %}{{cookiecutter.account_info["accounts"][15]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 16\ %}{{cookiecutter.account_info["accounts"][16]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 17\ %}{{cookiecutter.account_info["accounts"][17]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 18\ %}{{cookiecutter.account_info["accounts"][18]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 19\ %}{{cookiecutter.account_info["accounts"][19]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 2\ %}{{cookiecutter.account_info["accounts"][2]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 3\ %}{{cookiecutter.account_info["accounts"][3]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 4\ %}{{cookiecutter.account_info["accounts"][4]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 5\ %}{{cookiecutter.account_info["accounts"][5]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 6\ %}{{cookiecutter.account_info["accounts"][6]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 7\ %}{{cookiecutter.account_info["accounts"][7]}}{%\ endif\ %}
    ├── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 8\ %}{{cookiecutter.account_info["accounts"][8]}}{%\ endif\ %}
    └── {%\ if\ cookiecutter.account_info["accounts"]|length\ >\ 9\ %}{{cookiecutter.account_info["accounts"][9]}}{%\ endif\ %}

cookiecutter.json

{
    "project_name": "test",
    "account_info": {
        "accounts": [
            "root",
            "corp",
            "dns",
            "dev",
            "prod"
        ]
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接