文章详情页 您现在的位置是:网站首页>文章详情

工程项目模板工具cookiecutter的使用(二)

图片丢失 Jeyrce.Lu 发表于:2020年4月3日 22:38 分类:【Python 2724次阅读

上一篇已经讲过cookiecutter的基本使用方式了,此篇进一步了解其中的一些细节。

用户配置

如果经常使用cookiecutter的话,可以在利用模板创建项目时指定一个用户配置文件,例如我们可以用这样的命令来创建项目;

cookiecutter --config-file <配置文件> <模板文件>

可以在自己的环境变量中.bashrc设定`COOKIECUTTER_CONFIG`来省掉命令行:

export COOKIECUTTER_CONFIG=/home/jeeyshe/.cookiecutter-config.yaml

一份典型的用户配置: 如果有项目不想使用全局配置,可以用--default-config来跳过

# 默认的上下文键值对
default_context:
    full_name: "Jeeyshe.Lu"
    email: "jeeyshe@gmail.com"
    github_username: "Jeeyshe"
    author: "Jeeyshe"

# 远程仓库克隆时文件存放位置
cookiecutter_dir: "~/.cookiecutters"

# 上下文存储目录, 使用创建项目的重播功能时会用到
replay_dir: "~/.cookiecutter_replay"

# 缩写
abbreviations:
    gh: https://github.com/{0}.git

在Python中调用cookiecutter函数

cookiecutter不仅可以在命令行执行,我们也可以在python代码中调用cookiecutter,我们可以利用cookiecutter的函数实现django-admin, npm类似的功能,例如:

from cookiecutter.main import cookiecutter

if __name__ == '__main__':
    cookiecutter(
        template="https://github.com/audreyr/cookiecutter-pypackage.git",
        extra_context={
            "project_name": "xshop",
        }
    )
  • extra_context 字典类型的参数,其中的key会覆盖掉cookiecutter.json中的上下文

  • 在cookiecutter.json中可以使用自带的时间戳上下文{{cookiecutter.timestamp}}

cookiecutter的模板语法

  • cookiecutter实际上采用的就是Jinja2模板语法,cookiecutter.json中的值也遵循模板语法,但是键不是;

{
  "project_name": "My New Project",
  "project_slug": "{{ cookiecutter.project_name|lower|replace(' ', '-') }}",
  "pkg_name": "{{ cookiecutter.project_slug|replace('-', '') }}"
}
  • 有些模板虽然采用了模板标签但是不需要渲染,可以采用cookiecutter.json中的_copy_without_render来过滤,语法支持shell下的通配符

{
    "project_slug": "sample",
    "_copy_without_render": [
        "*.html",
        "*not_rendered_dir",
        "rendered_dir/not_rendered_file.ini"
    ]
}
  • cookiecutter选择变量的使用,我们在创建项目时某些变量的值可以为用户提供一个可选列表

{
    "license": ["MIT", "BSD-3", "GNU GPL v3.0", "Apache Software License 2.0"]
}

创建项目时我们就会看到让我们选择

Select license:
1 - MIT
2 - BSD-3
3 - GNU GPL v3.0
4 - Apache Software License 2.0
Choose from 1, 2, 3, 4 [1]:

如果我们在用户自定义的默认配置中也提供了license的值,他会自动添加到这个列表的第一项

  • cookiecutter.json中支持字典类型的上下文

{
  "dir_name": "hello",
  "author": "jeeyshe",
  "data": {
    "group_one": [
      "https://www.lujianxin.com",
      "http://xoo.site"
    ],
    "group_two": [
      "https://www.google.com",
      "https://www.yahoo.com"
    ]
  }
}

那么我们在模板中就可以这样来用:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% for group, urls in cookiecutter.data.items() %}
    <span>group</span>
    {% for url in urls %}
        <a href="{{url}}" target="_blank">{{url}}</a>
    {% endfor %}
    <hr>
{% endfor %}

</body>
</html>

除此之外我们还可以在模板中使用{% if xxx%} {% else %}{%endif%}这样的条件判断值,更多的语法支持则需要你自己去学习Jinja2语法了。

cookiecutter的命令行参数

  • --replay: 重播功能,可以使用同一个模板上一次用户输入的值创建项目

  • -V 显示当前cookiecutter的版本

  • --no-input 不使用用户输入,只使用cookiecutter.json中定义的值

  • -c 或者 --checkout <分支> 指定要克隆的分支

  • -v 或者--verbose 打印调试信息

  • -f 或者 --overwrite-if-exists 强制覆盖掉已经存在的项目目录

  • -o 或者 --output-dir 指定创建好的项目路径,默认是当前目录下

  • --config-file 指定用户配置文件

  • --default-config 不使用用户的自定义配置文件

  • --debug-file 用作debug日志记录的文件

  • -h 帮助信息

到此cookiecutter的所有知识都讲解完毕了,文中所有步骤都来自cookiecutter官方文档并且经过我的手动测试,这个工具属于短小精悍类型的,不妨在项目中试试。


版权声明 本文属于本站  原创作品,文章版权归本站及作者所有,请尊重作者的创作成果,转载、引用自觉附上本文永久地址: http://blog.lujianxin.com/x/art/4dwmc3si5gm7

文章评论区

作者名片

图片丢失
  • 作者昵称:Jeyrce.Lu
  • 原创文章:61篇
  • 转载文章:3篇
  • 加入本站:1808天

站点信息

  • 运行天数:1809天
  • 累计访问:164169人次
  • 今日访问:0人次
  • 原创文章:69篇
  • 转载文章:4篇
  • 微信公众号:第一时间获取更新信息