当前位置: 首页>>代码示例>>Python>>正文


Python Template.blocks["vhostm_hosts"]方法代码示例

本文整理汇总了Python中jinja2.Template.blocks["vhostm_hosts"]方法的典型用法代码示例。如果您正苦于以下问题:Python Template.blocks["vhostm_hosts"]方法的具体用法?Python Template.blocks["vhostm_hosts"]怎么用?Python Template.blocks["vhostm_hosts"]使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jinja2.Template的用法示例。


在下文中一共展示了Template.blocks["vhostm_hosts"]方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: vhostm_gen

# 需要导入模块: from jinja2 import Template [as 别名]
# 或者: from jinja2.Template import blocks["vhostm_hosts"] [as 别名]
def vhostm_gen(config):
    try:
        with open(config.vhosts_file, "r") as f:
            vhosts = json.load(f)
    except:
        vhosts = {"vhosts": []}

    hosts_file = ""
    try:
        with open(config.hosts_file) as f:
            hosts_file = f.read()
    except FileNotFoundError:
        pass

    hosts = ""
    for vhost_str in vhosts["vhosts"]:
        vhost = Vhost.read(vhost_str)

        hosts += "{}\t{}\n".format(vhost.address, vhost.domain)

        # Write nginx config file
        with open(join(config.nginx_conf_dir, vhost.domain), "w+") as f:
            template = Template(config.nginx_template)
            f.write(template.render(domain=vhost.domain,
                                    port=vhost.port,
                                    address=vhost.address,
                                    static_root=vhost.static_root))

    with open(config.hosts_file, "w+") as f:
        if "#{%block vhostm_hosts%}\n" not in hosts_file:
            hosts_file += "\n#{%block vhostm_hosts%}\n#{%endblock%}\n"

        template = Template(hosts_file)

        def swap_vhostm_hosts(*args, **kwargs):
            yield "\n#{%block vhostm_hosts%}\n" + hosts + "#{%endblock%}\n"

        template.blocks["vhostm_hosts"] = swap_vhostm_hosts
        hosts_file = template.render()
        f.write(hosts_file)

    assert(subprocess.call(["nginx", "-t"]) == 0)

    assert(subprocess.call(["service", "nginx", "reload"]) == 0)
开发者ID:bysslord,项目名称:vhostm,代码行数:46,代码来源:vhostm.py


注:本文中的jinja2.Template.blocks["vhostm_hosts"]方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。