pelican-quickstart: switch to jinja2

- Use the jinja2 templating language for Makefile, fabfile,
  pelicanconf, publishconf, and develop-server.sh
- Add logic in pelican_quickstart.py
- Let jinja2 handle opening and reading template files
- Remove thus unused functions for string.Template
This commit is contained in:
ix5 2017-01-15 08:47:21 +01:00
commit 6d46bf4257
11 changed files with 465 additions and 420 deletions

View file

@ -6,9 +6,10 @@ import argparse
import codecs
import locale
import os
import string
import sys
from jinja2 import Environment, FileSystemLoader
import pytz
try:
@ -30,6 +31,11 @@ else:
_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"templates")
_jinja_env = Environment(
loader=FileSystemLoader(_TEMPLATES_DIR),
trim_blocks=True,
)
_GITHUB_PAGES_BRANCHES = {
'personal': 'master',
@ -99,20 +105,6 @@ def decoding_strings(f):
return wrapper
def get_template(name, as_encoding='utf-8'):
template = os.path.join(_TEMPLATES_DIR, "{0}.in".format(name))
if not os.path.isfile(template):
raise RuntimeError("Cannot open {0}".format(template))
with codecs.open(template, 'r', as_encoding) as fd:
line = fd.readline()
while line:
yield line
line = fd.readline()
fd.close()
@decoding_strings
def ask(question, answer=str_compat, default=None, length=None):
if answer == str_compat:
@ -273,6 +265,7 @@ needed by Pelican.
if automation:
if ask('Do you want to upload your website using FTP?',
answer=bool, default=False):
CONF['ftp'] = True,
CONF['ftp_host'] = ask('What is the hostname of your FTP server?',
str_compat, CONF['ftp_host'])
CONF['ftp_user'] = ask('What is your username on that server?',
@ -282,6 +275,7 @@ needed by Pelican.
str_compat, CONF['ftp_target_dir'])
if ask('Do you want to upload your website using SSH?',
answer=bool, default=False):
CONF['ssh'] = True,
CONF['ssh_host'] = ask('What is the hostname of your SSH server?',
str_compat, CONF['ssh_host'])
CONF['ssh_port'] = ask('What is the port of your SSH server?',
@ -294,16 +288,19 @@ needed by Pelican.
if ask('Do you want to upload your website using Dropbox?',
answer=bool, default=False):
CONF['dropbox'] = True,
CONF['dropbox_dir'] = ask('Where is your Dropbox directory?',
str_compat, CONF['dropbox_dir'])
if ask('Do you want to upload your website using S3?',
answer=bool, default=False):
CONF['s3'] = True,
CONF['s3_bucket'] = ask('What is the name of your S3 bucket?',
str_compat, CONF['s3_bucket'])
if ask('Do you want to upload your website using '
'Rackspace Cloud Files?', answer=bool, default=False):
CONF['cloudfiles'] = True,
CONF['cloudfiles_username'] = ask('What is your Rackspace '
'Cloud username?', str_compat,
CONF['cloudfiles_username'])
@ -317,6 +314,7 @@ needed by Pelican.
if ask('Do you want to upload your website using GitHub Pages?',
answer=bool, default=False):
CONF['github'] = True,
if ask('Is this your personal page (username.github.io)?',
answer=bool, default=False):
CONF['github_pages_branch'] = \
@ -342,9 +340,8 @@ needed by Pelican.
for key, value in CONF.items():
conf_python[key] = repr(value)
for line in get_template('pelicanconf.py'):
template = string.Template(line)
fd.write(template.safe_substitute(conf_python))
_template = _jinja_env.get_template('pelicanconf.py.jinja2')
fd.write(_template.render(**conf_python))
fd.close()
except OSError as e:
print('Error: {0}'.format(e))
@ -352,9 +349,8 @@ needed by Pelican.
try:
with codecs.open(os.path.join(CONF['basedir'], 'publishconf.py'),
'w', 'utf-8') as fd:
for line in get_template('publishconf.py'):
template = string.Template(line)
fd.write(template.safe_substitute(CONF))
_template = _jinja_env.get_template('publishconf.py.jinja2')
fd.write(_template.render(**CONF))
fd.close()
except OSError as e:
print('Error: {0}'.format(e))
@ -363,25 +359,19 @@ needed by Pelican.
try:
with codecs.open(os.path.join(CONF['basedir'], 'fabfile.py'),
'w', 'utf-8') as fd:
for line in get_template('fabfile.py'):
template = string.Template(line)
fd.write(template.safe_substitute(CONF))
_template = _jinja_env.get_template('fabfile.py.jinja2')
fd.write(_template.render(**CONF))
fd.close()
except OSError as e:
print('Error: {0}'.format(e))
try:
with codecs.open(os.path.join(CONF['basedir'], 'Makefile'),
'w', 'utf-8') as fd:
mkfile_template_name = 'Makefile'
py_v = 'PY?=python'
py_v = 'python'
if six.PY3:
py_v = 'PY?=python3'
template = string.Template(py_v)
fd.write(template.safe_substitute(CONF))
fd.write('\n')
for line in get_template(mkfile_template_name):
template = string.Template(line)
fd.write(template.safe_substitute(CONF))
py_v = 'python3'
_template = _jinja_env.get_template('Makefile.jinja2')
fd.write(_template.render(py_v=py_v, **CONF))
fd.close()
except OSError as e:
print('Error: {0}'.format(e))
@ -396,14 +386,11 @@ needed by Pelican.
with codecs.open(os.path.join(CONF['basedir'],
'develop_server.sh'),
'w', 'utf-8') as fd:
lines = list(get_template('develop_server.sh'))
py_v = 'PY=${PY:-python}\n'
py_v = '${PY:-python}'
if six.PY3:
py_v = 'PY=${PY:-python3}\n'
lines = lines[:4] + [py_v] + lines[4:]
for line in lines:
template = string.Template(line)
fd.write(template.safe_substitute(conf_shell))
py_v = '${PY:-python3}'
_template = _jinja_env.get_template('develop_server.sh.jinja2')
fd.write(_template.render(py_v=py_v, **conf_shell))
fd.close()
# mode 0o755