Source code for railgun.website.renders

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# @file: railgun/website/renders.py
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This file is released under BSD 2-clause license.

"""This module provides the strategies to render various objects into html."""

from flask import render_template

from railgun.common.lazy_i18n import GetTextString
from .context import app


[docs]class PartialScoreRender(object): """Base class to render a :class:`~railgun.common.hw.HwPartialScore`."""
[docs] def render(self, partial): """Render the given partial score object. Derived classes should override this to implement the renderer. :param partial: The partial score object. :type partial: :class:`~railgun.common.hw.HwPartialScore` """ raise NotImplementedError()
@staticmethod
[docs] def getRender(typeName): """Get the renderer for :class:`~railgun.common.hw.HwPartialScore` with given type name. :param typeName: The type name of :class:`~railgun.common.hw.HwPartialScore`. :type typeName: :class:`str` """ return { 'default': DefaultPartialScoreRender, 'CoverageScorer': CoveragePartialScoreRender, 'ObjSchemaScorer': ObjSchemaPartialScoreRender, 'InputClassScorer': InputDataPartialScoreRender, 'BoundaryValueScorer': InputDataPartialScoreRender, }.get(typeName, DefaultPartialScoreRender)()
[docs]class DefaultPartialScoreRender(PartialScoreRender): """A default :class:`~railgun.common.hw.HwPartialScore` renderer that wraps each line of the detail message into a pair of `pre` tags. :template: renders/PartialScore.default.html """ def render(self, partial): return render_template( 'renders/PartialScore.default.html', partial=partial )
[docs]class CoveragePartialScoreRender(PartialScoreRender): """A default :class:`~railgun.common.hw.HwPartialScore` renderer that formats the partial score generated by :class:`~pyhost.scorer.CoverageScorer`. :template: renders/PartialScore.coverage.html """ def line_class(self, line): return { '*': 'warning', '-': 'danger', '+': 'success', }.get(line[:1], None) def format_file(self, detail): ret = {} text = unicode(detail).split(u'\n') # first line is the summary of file ret['file'] = text[0] # second line is the delimeter ret['lines'] = [ (self.line_class(line), line[2:]) for line in text[2:] ] return ret def extract_total(self, first): if not first: return None txt = unicode(first) lines = txt.split('\n') headers = [s.strip() for s in lines[2].split(u',')] files = [[s.strip() for s in v.split(u',')] for v in lines[4:-2]] total = [s.strip() for s in lines[-1].split(u',')] return headers, files, total def render(self, partial): # Format each file result if partial.detail: # Whether there exists the total coverage report? # this special check is for compatibility with older versions. first = partial.detail[0] first_title = str(first.text) if isinstance(first, GetTextString) \ else str(first) if first_title.startswith('Coverage Results:'): raw_detail = partial.detail[1:] else: raw_detail = partial.detail first = None # Make the detail reports first_extract = self.extract_total(first) if first_extract: total_report = { 'headers': first_extract[0], 'files': first_extract[1], 'total': first_extract[2], } else: total_report = None detail = [self.format_file(d) for d in raw_detail] else: detail = None total_report = None return render_template('renders/PartialScore.coverage.html', detail=detail, total=total_report)
[docs]class ObjSchemaPartialScoreRender(PartialScoreRender): """A default :class:`~railgun.common.hw.HwPartialScore` renderer that formats the partial score generated by :class:`~pyhost.scorer.ObjSchemaScorer`. :template: renders/PartialScore.coverage.html """ def render(self, partial): return render_template( 'renders/PartialScore.objschema.html', partial=partial )
[docs]class InputDataPartialScoreRender(PartialScoreRender): """A default :class:`~railgun.common.hw.HwPartialScore` renderer that formats the partial score generated by :class:`~pyhost.scorer.InputDataScorer`. :template: renders/PartialScore.inputdata.html """ def _parse_detail(self, detail): ret = [] for d in detail: if isinstance(d, GetTextString): s = d.text else: s = str(d) err = s.startswith('NOT COVERED') ret.append((err, d)) return ret def render(self, partial): return render_template( 'renders/PartialScore.inputdata.html', detail=self._parse_detail(partial.detail) )
[docs]def renderPartialScore(partial): """Shorcut to finding a suitable renderer for given partial score, and to get the rendered html text. :param partial: The partial score object. :type partial: :class:`~railgun.common.hw.HwPartialScore` """ return PartialScoreRender.getRender(partial.typeName).render(partial) # inject renderPartialScore into template context
@app.context_processor def __inject_renderPartialScore(): return dict(renderPartialScore=renderPartialScore)