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:
parent
18b5b7ecb6
commit
b7cc43aaf4
|
@ -18,6 +18,9 @@ import os.path
|
||||||
import sys
|
import sys
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
|
import jinja2
|
||||||
|
import pkg_resources
|
||||||
|
import six
|
||||||
from six.moves import configparser
|
from six.moves import configparser
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,9 +46,9 @@ def generate_dashboard_url(dashboard):
|
||||||
except configparser.NoOptionError:
|
except configparser.NoOptionError:
|
||||||
baseurl = 'https://review.openstack.org/#/dashboard/?'
|
baseurl = 'https://review.openstack.org/#/dashboard/?'
|
||||||
|
|
||||||
base = baseurl
|
url = baseurl
|
||||||
base += urllib.urlencode({'title': title, 'foreach': foreach})
|
url += urllib.urlencode({'title': title, 'foreach': foreach})
|
||||||
base += '&'
|
url += '&'
|
||||||
for section in dashboard.sections():
|
for section in dashboard.sections():
|
||||||
if not section.startswith('section'):
|
if not section.startswith('section'):
|
||||||
continue
|
continue
|
||||||
|
@ -57,8 +60,8 @@ def generate_dashboard_url(dashboard):
|
||||||
|
|
||||||
title = section[9:-1]
|
title = section[9:-1]
|
||||||
encoded = urllib.urlencode({title: query})
|
encoded = urllib.urlencode({title: query})
|
||||||
base += "&%s" % encoded
|
url += "&%s" % encoded
|
||||||
return base
|
return url
|
||||||
|
|
||||||
|
|
||||||
def get_options():
|
def get_options():
|
||||||
|
@ -69,6 +72,14 @@ def get_options():
|
||||||
parser.add_argument('dashboard_files', nargs='+',
|
parser.add_argument('dashboard_files', nargs='+',
|
||||||
metavar='dashboard_file',
|
metavar='dashboard_file',
|
||||||
help='Dashboard definition file to create URL from')
|
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()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,11 +90,48 @@ def read_dashboard_file(fname):
|
||||||
return dashboard
|
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():
|
def main():
|
||||||
"""Entrypoint."""
|
"""Entrypoint."""
|
||||||
result = 0
|
result = 0
|
||||||
opts = get_options()
|
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:
|
for dashboard_file in opts.dashboard_files:
|
||||||
if (not os.path.isfile(dashboard_file) or
|
if (not os.path.isfile(dashboard_file) or
|
||||||
not os.access(dashboard_file, os.R_OK)):
|
not os.access(dashboard_file, os.R_OK)):
|
||||||
|
@ -108,9 +156,14 @@ def main():
|
||||||
result = 1
|
result = 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print("\nGenerated URL for the Gerrit dashboard '%s':\n" %
|
variables = {
|
||||||
dashboard_file)
|
'title': dashboard.get('dashboard', 'title') or None,
|
||||||
print(url)
|
'description': dashboard.get('dashboard', 'description') or None,
|
||||||
|
'url': url,
|
||||||
|
'filename': dashboard_file,
|
||||||
|
'configuration': get_configuration(dashboard)
|
||||||
|
}
|
||||||
|
print(template.render(variables))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
argparse
|
argparse
|
||||||
|
jinja2
|
||||||
pbr>=0.6,!=0.7,<1.0
|
pbr>=0.6,!=0.7,<1.0
|
||||||
six>=1.7.0
|
six>=1.7.0
|
||||||
|
|
|
@ -27,7 +27,8 @@ setup-hooks =
|
||||||
packages =
|
packages =
|
||||||
gerrit_dash_creator
|
gerrit_dash_creator
|
||||||
data_files =
|
data_files =
|
||||||
share/gerrit-dash-creator = dashboards/*
|
share/gerrit-dash-creator/dashboards = dashboards/*
|
||||||
|
share/gerrit-dash-creator/templates = templates/*
|
||||||
|
|
||||||
[entry_points]
|
[entry_points]
|
||||||
console_scripts =
|
console_scripts =
|
||||||
|
|
|
@ -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 }}
|
|
@ -0,0 +1,4 @@
|
||||||
|
Generated URL for the Gerrit dashboard '{{ title }}':
|
||||||
|
|
||||||
|
{{ url }}
|
||||||
|
|
Loading…
Reference in New Issue