本文整理汇总了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)