Add support for templates

To create the URLs for all dashboard files inside dashboards use:

python gerrit_dash_creator/cmd/creator.py dashboards/*

Change-Id: I922c4e0606ae168c5c9b353d550629b4f52a8aff
This commit is contained in:
Christian Berendt 2014-08-08 16:28:59 +02:00
parent 18b5b7ecb6
commit b7cc43aaf4
5 changed files with 90 additions and 9 deletions

View File

@ -18,6 +18,9 @@ import os.path
import sys
import urllib
import jinja2
import pkg_resources
import six
from six.moves import configparser
@ -43,9 +46,9 @@ def generate_dashboard_url(dashboard):
except configparser.NoOptionError:
baseurl = 'https://review.openstack.org/#/dashboard/?'
base = baseurl
base += urllib.urlencode({'title': title, 'foreach': foreach})
base += '&'
url = baseurl
url += urllib.urlencode({'title': title, 'foreach': foreach})
url += '&'
for section in dashboard.sections():
if not section.startswith('section'):
continue
@ -57,8 +60,8 @@ def generate_dashboard_url(dashboard):
title = section[9:-1]
encoded = urllib.urlencode({title: query})
base += "&%s" % encoded
return base
url += "&%s" % encoded
return url
def get_options():
@ -69,6 +72,14 @@ def get_options():
parser.add_argument('dashboard_files', nargs='+',
metavar='dashboard_file',
help='Dashboard definition file to create URL from')
parser.add_argument('--template', default='single.txt',
help='Name of template')
parser.add_argument('--template-directory',
default=pkg_resources.resource_filename(
__name__, "templates"
), help='Directory to scan for template files')
parser.add_argument('--template-file', default=None,
help='Location of a specific template file')
return parser.parse_args()
@ -79,11 +90,48 @@ def read_dashboard_file(fname):
return dashboard
def load_template(template_file=None, template_directory=None,
template_name=None):
"""Load the specified template."""
if template_file:
template_name = os.path.basename(template_file)
template_directory = os.path.dirname(os.path.abspath(template_file))
try:
loader = jinja2.FileSystemLoader(template_directory)
environment = jinja2.Environment(loader=loader)
template = environment.get_template(template_name)
except (jinja2.exceptions.TemplateError, IOError) as e:
print("error: opening template '%s' failed: %s" %
(template_name, e.__class__.__name__))
return
return template
def get_configuration(dashboard):
"""Returns the configuration of a dashboard as string."""
configuration = six.StringIO()
dashboard.write(configuration)
result = configuration.getvalue()
configuration.close()
return result
def main():
"""Entrypoint."""
result = 0
opts = get_options()
template = load_template(
template_file=opts.template_file,
template_directory=opts.template_directory,
template_name=opts.template
)
if not template:
return 1
for dashboard_file in opts.dashboard_files:
if (not os.path.isfile(dashboard_file) or
not os.access(dashboard_file, os.R_OK)):
@ -108,9 +156,14 @@ def main():
result = 1
continue
print("\nGenerated URL for the Gerrit dashboard '%s':\n" %
dashboard_file)
print(url)
variables = {
'title': dashboard.get('dashboard', 'title') or None,
'description': dashboard.get('dashboard', 'description') or None,
'url': url,
'filename': dashboard_file,
'configuration': get_configuration(dashboard)
}
print(template.render(variables))
return result

View File

@ -1,3 +1,4 @@
argparse
jinja2
pbr>=0.6,!=0.7,<1.0
six>=1.7.0

View File

@ -27,7 +27,8 @@ setup-hooks =
packages =
gerrit_dash_creator
data_files =
share/gerrit-dash-creator = dashboards/*
share/gerrit-dash-creator/dashboards = dashboards/*
share/gerrit-dash-creator/templates = templates/*
[entry_points]
console_scripts =

22
templates/single.rst Normal file
View File

@ -0,0 +1,22 @@
`{{ title }}`_
==========================
{% if description %}
Description::
{{ description }}
{%- endif %}
URL::
{{ url }}
{% if configuration %}
Configuration::
{% for line in configuration.splitlines() %}
{{ line }}
{%- endfor %}
{% endif %}
.. _{{ title }}: {{ url }}

4
templates/single.txt Normal file
View File

@ -0,0 +1,4 @@
Generated URL for the Gerrit dashboard '{{ title }}':
{{ url }}