diff --git a/gerrit_dash_creator/cmd/creator.py b/gerrit_dash_creator/cmd/creator.py index cf5da55..1dae34c 100755 --- a/gerrit_dash_creator/cmd/creator.py +++ b/gerrit_dash_creator/cmd/creator.py @@ -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 diff --git a/requirements.txt b/requirements.txt index e5ffcbb..e99b8a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ argparse +jinja2 pbr>=0.6,!=0.7,<1.0 six>=1.7.0 diff --git a/setup.cfg b/setup.cfg index cec16e9..7e2276c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 = diff --git a/templates/single.rst b/templates/single.rst new file mode 100644 index 0000000..939db21 --- /dev/null +++ b/templates/single.rst @@ -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 }} diff --git a/templates/single.txt b/templates/single.txt new file mode 100644 index 0000000..6695ae1 --- /dev/null +++ b/templates/single.txt @@ -0,0 +1,4 @@ +Generated URL for the Gerrit dashboard '{{ title }}': + +{{ url }} +