def od(dev=True): #lookups,data = load_od() #check(data,lookups, make_open_data_after_check(lookups)) #lookups['depts'] = {k:v for k,v in lookups['depts'].iteritems() # if 'tables' in v} #for dept in lookups['depts']: # d = lookups['depts'][dept] # d['fin_size'] = sum(x[-1] for x in d['tables']['table4']) # if dept != 'ZGOC': # #lookups['depts'][dept].update(lookups['igoc'][dept]) # if dept in lookups['qfr_links']: # lookups['depts'][dept]['qfr_link'] = lookups['qfr_links'][dept] ##del lookups['igoc'] #del lookups['qfr_links'] ##check_g_and_c(lookups['depts']) #js_data = ";\n".join( # [u'{}={}'.format(k,json.dumps(lookups[k])) # for k in lookups] #)+";\n" #app_js_files = list(my_js_files) #app_js_files += ["od/text.js", # "od/tables.js", # "od/od.js"] #js_app = process_my_js(app_js_files, dev=dev) full_js = '' #full_js = "\n".join([js_data]) full_css = '' #full_css = cssdata #import IPython #IPython.embed() t = lookup.get_template('od.html') with open("../../InfoBase/index-eng.html", 'w') as leshtml: leshtml.write( t.render( full_js=full_js, #js_root = './', full_css=full_css, no_auto_js=True, no_auto_css=True)) t = lookup.get_template('od_fr.html') with open("../../InfoBase/index-fra.html", 'w') as leshtml: leshtml.write( t.render( full_js=full_js, #js_root = './', full_css=full_css, no_auto_js=True, no_auto_css=True))
def __init__(self, gctx): self.gctx = gctx paths = [os.path.join(os.path.dirname(__file__), "templates")] lookup = mako.lookup.TemplateLookup( paths, default_filters=['html_escape'], imports=['from mako.filters import html_escape']) self.templates = {} self.templates[Module] = lookup.get_template("module.mako") self.templates[Function] = lookup.get_template("function.mako") self.templates[Class] = lookup.get_template("class.mako") self.templates["source"] = lookup.get_template("source.mako")
def od(dev=True): #lookups,data = load_od() #check(data,lookups, make_open_data_after_check(lookups)) #lookups['depts'] = {k:v for k,v in lookups['depts'].iteritems() # if 'tables' in v} #for dept in lookups['depts']: # d = lookups['depts'][dept] # d['fin_size'] = sum(x[-1] for x in d['tables']['table4']) # if dept != 'ZGOC': # #lookups['depts'][dept].update(lookups['igoc'][dept]) # if dept in lookups['qfr_links']: # lookups['depts'][dept]['qfr_link'] = lookups['qfr_links'][dept] ##del lookups['igoc'] #del lookups['qfr_links'] ##check_g_and_c(lookups['depts']) #js_data = ";\n".join( # [u'{}={}'.format(k,json.dumps(lookups[k])) # for k in lookups] #)+";\n" #app_js_files = list(my_js_files) #app_js_files += ["od/text.js", # "od/tables.js", # "od/od.js"] #js_app = process_my_js(app_js_files, dev=dev) full_js = ''#full_js = "\n".join([js_data]) full_css = ''#full_css = cssdata #import IPython #IPython.embed() t = lookup.get_template('od.html') with open("../../InfoBase/index-eng.html",'w') as leshtml: leshtml.write(t.render(full_js = full_js, #js_root = './', full_css = full_css, no_auto_js = True, no_auto_css = True)) t = lookup.get_template('od_fr.html') with open("../../InfoBase/index-fra.html",'w') as leshtml: leshtml.write(t.render(full_js = full_js, #js_root = './', full_css = full_css, no_auto_js = True, no_auto_css = True))
def _render_using_plim(filename, localcontext): """Render the template using Plim.""" root_dir = os.path.dirname(os.path.abspath(filename)) template_file = os.path.basename(filename) lookup = mako.lookup.TemplateLookup( directories=[root_dir], input_encoding="utf-8", output_encoding="utf-8", preprocessor=plim.preprocessor, strict_undefined=True, default_filters=["trim"], ) output = lookup.get_template(template_file).render_unicode( **localcontext ) if ( "SLIM_OPTIONS" in self.settings and "PRETTYIFY" in self.settings["SLIM_OPTIONS"] and self.settings["SLIM_OPTIONS"]["PRETTYIFY"] ): output = bs(output, "html.parser").prettify() # prettify the html else: output = minify(output) # minify the html return output
def get_template(tmpl_name, tmpl_dir): import mako.lookup template_dirs = [os.getcwd()] if tmpl_dir is not None: template_dirs.append(tmpl_dir) lookup = mako.lookup.TemplateLookup(directories=template_dirs) return lookup.get_template(tmpl_name)
def lookup_template(template_name): template = TEMPLATES.get(template_name) cache_template = template and APP_ENV != "development" if cache_template: return template template = lookup.get_template(f'{template_name}.haml') TEMPLATES[template_name] = template return template
def render(self, fn=None, **kwargs): if not fn: fn = ('/%s/%s.html' % ('/'.join(self.__module__.split('.')[1:-1]), self.__class__.__name__.lower())).replace( r'//', r'/') kwargs.update({ 'req': self, 'config': config, 'static': self.static_url, 'url_for': self.reverse_url, 'get_messages': self.get_messages, 'xsrf_token': self.xsrf_form_html(), 'csrf_token': self.xsrf_form_html(), 'duty_username': self.duty_username() }) if lookup: tmpl = lookup.get_template(fn) self.finish(tmpl.render(**kwargs)) else: if fn.startswith('/'): fn = '.' + fn if 'page_title' not in kwargs: kwargs['page_title'] = None super(View, self).render(fn, **kwargs)
def __init__(self, errors): location = config.get('mako.directories') module, final = location.split(':') base = os.path.dirname(__import__(module).__file__) directory = base + "/" + final lookup = mako.lookup.TemplateLookup( directories=[directory], output_encoding='utf-8', input_encoding='utf-8', ) template = lookup.get_template('errors.html') try: body = template.render( errors=errors, status=errors.status, request=errors.request, summary=status2summary(errors.status), ) except: log.error(mako.exceptions.text_error_template().render()) raise # This thing inherits from both Exception *and* Response, so.. take the # Response path in the diamond inheritance chain and ignore the # exception side. # That turns this thing into a "real boy" like pinnochio. pyramid.response.Response.__init__(self, body) self.status = errors.status self.content_type = 'text/html'
def render(self): if mako is None: raise Exception("HTML support is unavailable without the python3-mako package installed.") lookup = mako.lookup.TemplateLookup([ "template" ]) template = lookup.get_template("template.html") result = template.render(complaints = self._complaints) return result
def test_dynamic_key_with_imports(self): lookup = TemplateLookup() lookup.put_string( "foo.html", """ <%! callcount = [0] %> <%namespace file="ns.html" import="*"/> <%page cached="True" cache_key="${foo}"/> this is foo <% callcount[0] += 1 %> callcount: ${callcount} """, ) lookup.put_string("ns.html", """""") t = lookup.get_template("foo.html") m = self._install_mock_cache(t) t.render(foo="somekey") t.render(foo="somekey") assert result_lines(t.render(foo="somekey")) == [ "this is foo", "callcount: [1]", ] assert m.kwargs == {}
def render(self, fn=None, **kwargs): if not fn: fn = ('/%s/%s.html' % ( '/'.join(self.__module__.split('.')[1:-1]), self.__class__.__name__.lower() )).replace(r'//', r'/') kwargs.update({ 'req': self, 'config': config, 'static': self.static_url, 'url_for': self.reverse_url, 'get_messages': self.get_messages, 'xsrf_token': self.xsrf_form_html(), 'csrf_token': self.xsrf_form_html(), }) if lookup: tmpl = lookup.get_template(fn) self.finish(tmpl.render(**kwargs)) else: if fn.startswith('/'): fn = '.' + fn if 'page_title' not in kwargs: kwargs['page_title'] = None super(View, self).render(fn, **kwargs)
def mako_template(lookup, templateName): ''' Return a mako template instance. This method checks the mako template in all directories specified by the Mako TemplateLookup `lookup`. ''' return lookup.get_template(templateName)
def generate_interface(name, iface): methods = (m for m in iface.members if type(m) is librpc.Method) properties = (m for m in iface.members if type(m) is librpc.Property) events = () t = lookup.get_template('interface.mako') return t.render(name=name, iface=iface, methods=methods, properties=properties, events=events)
def generate_index(name, typing): entries = typing.types types = (t for t in entries if t.is_builtin) typedefs = (t for t in entries if t.is_typedef) structures = (t for t in entries if t.is_struct or t.is_union or t.is_enum) t = lookup.get_template('index.mako') return t.render(name=name, interfaces=sorted(typing.interfaces, key=lambda t: t.name), types=sorted(types, key=lambda t: t.name), typedefs=sorted(typedefs, key=lambda t: t.name), structs=sorted(structures, key=lambda t: t.name))
def render_template(package, path, **context): key = package, path package_parts = package.split('.') mod = __import__(package) for p in package.split('.')[1:]: mod = getattr(mod, p) mod_dir = os.path.split(mod.__file__)[0] tmpl_dir = os.path.join(mod_dir, 'templates') lookup = mako.lookup.TemplateLookup(directories=[tmpl_dir]) template = lookup.get_template(path) buf = template.render(**context) return buf
def _template(self): """ Get the mako.Template from template or template_file """ template_file = self.myconfig('template_file') if template_file: template_dirs = base.config.parse_conf_array( self.myconfig('template_dirs')) lookup = mako.lookup.TemplateLookup( directories=template_dirs, input_encoding=self.myconfig('input_encoding')) return lookup.get_template(template_file) else: return mako.template.Template(self.myconfig('template'))
def get_template(template_path): import mako.lookup templates_dir = os.path.join(os.path.dirname(__file__), "templates") lookup_dirs = [ templates_dir, os.path.abspath(os.path.join(templates_dir, "..", "..", "..")) ] lookup = mako.lookup.TemplateLookup(directories=lookup_dirs) return lookup.get_template(template_path)
def renderTemplate(templateName): template = lookup.get_template(templateName + '.mako') if templateName.startswith('multiple'): open(expandPath('%s.html' % templateName), 'wt').write(template.render( results=results, plotPathByIndexByName=plotPathByIndexByName, )) else: for resultIndex, result in itertools.izip(itertools.count(1), results): open(expandPath('%s%s.html' % (templateName, resultIndex)), 'wt').write(template.render( result=result, plotPathByName=plotPathByIndexByName[resultIndex], ))
def _init_template(self, template_filters=['unicode', 'h']): # create the template lookup if self.template_file is None: raise Exception("SQLTap Report template file not specified!") # (we need this for extensions inheriting the base template) if self.template_dir is None: self.template_dir = os.path.join(os.path.dirname(__file__), "templates") # mako fixes unicode -> str on py3k lookup = mako.lookup.TemplateLookup(self.template_dir, default_filters=template_filters) self.template = lookup.get_template(self.template_file)
def get_template(template_path): import mako.lookup templates_dir = os.path.join(os.path.dirname(__file__), "templates") lookup_dirs = [ templates_dir, os.path.abspath(os.path.join(templates_dir, "..", "..", "..")) ] lookup = mako.lookup.TemplateLookup(directories=lookup_dirs) try: return lookup.get_template(template_path) except mako.exceptions.TopLevelLookupException as e: raise ValueError(e)
def html_les(dev=True): lookups, data = load_les() check(data, lookups, make_after_check(lookups)) ## filter out departments with no tables lookups['depts'] = { key: val for key, val in lookups['depts'].iteritems() if val.get('tables') } double_check(lookups) add_dept_data(lookups['depts']) js_data = ";\n".join( [u'{}={}'.format(k, json.dumps(lookups[k])) for k in lookups]) + ";\n" app_js_files = list(my_js_files) app_js_files += [ "les/text.js", "od/od.js", "les/tables.js", "les/graph1.js", "les/graph2a.js", "les/graph2b.js", "les/graph2.js", "les/graph3.js", "les/graph4.js", "les/graph5.js", "les/graph6.js", "les/graph7.js", "les/graphIS.js", "table_builder.js", #"les/horizontal.js", ] js_app = process_my_js(app_js_files, dev=dev) full_js = "\n".join([js_libs, js_data, js_app]) full_css = cssdata t = lookup.get_template('les.html') with open("led_tbfd.html", 'w') as leshtml: leshtml.write( t.render( full_js=full_js, #js_root = './', full_css=full_css, no_auto_js=True, no_auto_css=True))
def html_les(dev=True): lookups,data = load_les() check(data,lookups,make_after_check(lookups)) ## filter out departments with no tables lookups['depts'] = {key:val for key,val in lookups['depts'].iteritems() if val.get('tables')} double_check(lookups) add_dept_data(lookups['depts']) js_data = ";\n".join( [u'{}={}'.format(k,json.dumps(lookups[k])) for k in lookups] )+";\n" app_js_files = list(my_js_files) app_js_files += ["les/text.js", "od/od.js", "les/tables.js", "les/graph1.js", "les/graph2a.js", "les/graph2b.js", "les/graph2.js", "les/graph3.js", "les/graph4.js", "les/graph5.js", "les/graph6.js", "les/graph7.js", "les/graphIS.js", "table_builder.js", #"les/horizontal.js", ] js_app = process_my_js(app_js_files, dev=dev) full_js = "\n".join([js_libs, js_data, js_app]) full_css = cssdata t = lookup.get_template('les.html') with open("led_tbfd.html",'w') as leshtml: leshtml.write(t.render(full_js = full_js, #js_root = './', full_css = full_css, no_auto_js = True, no_auto_css = True))
def _get_template(template_file: str) -> mako.template.Template: directories = [ str( pathlib.Path(appdirs.user_config_dir('online-judge-tools')) / 'template'), pkg_resources.resource_filename('onlinejudge_template_resources', 'template'), ] lookup = mako.lookup.TemplateLookup(directories=directories, input_encoding="utf-8", output_encoding="utf-8") path = pathlib.Path(template_file) has_slash = path.name != template_file # If template_file has path separators or any other things characteristic to paths, we use it as a path. This is a similar behavior to searching commands in shell. if has_slash and path.exists(): with open(path, "rb") as fh: lookup.put_string(template_file, fh.read()) template = lookup.get_template(template_file) logger.info('use template file: %s', template.filename) return template
def render(self, fn=None, **kwargs): if not fn: fn = ('/%s/%s.html' % ( '/'.join(self.__module__.split('.')[1:-1]), self.__class__.__name__.lower() )).replace(r'//', r'/') kwargs.update({ 'req': self, 'static': self.static_url, 'url_for': self.reverse_url, 'get_messages': self.get_messages, }) if lookup: tmpl = lookup.get_template(fn) self.finish(tmpl.render(**kwargs)) else: super(View, self).render(self, fn, **kwargs)
def __init__(self, errors, request): """ Initialize the HTML error handler to render an error messgae for human readers. This method sets the Response body to rendered HTML version of the given errors, and the status code to the code specified by errors. Args: errors (cornice.errors.Errors): The errors to be rendered as HTML for users. request (pyramid.util.Request): The current Request. """ location = config.get('mako.directories') module, final = location.split(':') base = os.path.dirname(__import__(module).__file__) directory = base + "/" + final lookup = mako.lookup.TemplateLookup( directories=[directory], output_encoding='utf-8', input_encoding='utf-8', ) template = lookup.get_template('errors.html') try: body = template.render( errors=errors, status=errors.status, request=request, summary=status2summary(errors.status), ) except Exception: log.error(mako.exceptions.text_error_template().render()) raise # This thing inherits from both Exception *and* Response, so.. take the # Response path in the diamond inheritance chain and ignore the # exception side. # That turns this thing into a "real boy" like pinnochio. pyramid.response.Response.__init__(self, body) self.status = errors.status self.content_type = 'text/html'
def render(self, fn=None, **kwargs): if not fn: fn = ('/%s/%s.html' % ('/'.join(self.__module__.split('.')[1:-1]), self.__class__.__name__.lower())).replace( r'//', r'/') kwargs.update({ 'req': self, 'static': self.static_url, 'url_for': self.reverse_url, 'get_messages': self.get_messages, 'xsrf_token': self.xsrf_form_html(), 'csrf_token': self.xsrf_form_html(), }) if lookup: tmpl = lookup.get_template(fn) self.finish(tmpl.render(**kwargs)) else: super(View, self).render(self, fn, **kwargs)
def __init__(self, request): """ Initialize the HTML error handler to render an error message for human readers. This method sets the Response body to rendered HTML version of the given errors, and the status code to the code specified by errors. Args: request (pyramid.request.Request): The current Request. """ location = config.get('mako.directories') directory = get_absolute_path(location) lookup = mako.lookup.TemplateLookup( directories=[directory], output_encoding='utf-8', input_encoding='utf-8', ) template = lookup.get_template('errors.html') errors = request.errors try: body = template.render( errors=errors, status=errors.status, request=request, summary=status2summary(errors.status), ) except Exception: log.error(mako.exceptions.text_error_template().render()) raise # This thing inherits from both Exception *and* Response, so.. take the # Response path in the diamond inheritance chain and ignore the # exception side. # That turns this thing into a "real boy" like pinnochio. pyramid.response.Response.__init__(self, body) self.status = errors.status self.content_type = 'text/html'
def _render_using_plim(filename, localcontext): """Render the template using Plim.""" root_dir = os.path.dirname(os.path.abspath(filename)) template_file = os.path.basename(filename) lookup = mako.lookup.TemplateLookup( directories=[root_dir], input_encoding='utf-8', output_encoding='utf-8', preprocessor=plim.preprocessor, strict_undefined=True, default_filters=['trim']) output = lookup.get_template(template_file).render_unicode( **localcontext) if ('SLIM_OPTIONS' in self.settings and 'PRETTYIFY' in self.settings['SLIM_OPTIONS'] and self.settings['SLIM_OPTIONS']['PRETTYIFY']): output = bs(output, 'html.parser').prettify() # prettify the html else: output = minify(output) # minify the html return output
def generate_dir(dirpath): sourcepath = path.join(source, dirpath) destpath = path.join(dest, dirpath) pages = filter(lambda p: p[0] != "." and not p.endswith(".mako"), os.listdir(sourcepath)) for page in pages: template_path = path.join(dirpath, page) template_sourcepath = path.join(source, template_path) template_destpath = path.join(dest, template_path.replace(".mako.", "")) if path.isdir(template_sourcepath): if not path.exists(template_destpath): os.mkdir(template_destpath) generate_dir(template_path) else: template = lookup.get_template(template_path) print "* Generating " + template_destpath try: html = template.render(dirpath=dirpath, page=page) except: print mako.exceptions.text_error_template().render() else: with open(template_destpath, "w") as f: f.write(html)
def gen_mdu_from_template(mdufilename, **variables): """Generate an MDU file from the template, using passed variables. A template MDU file should reside inside the templates directory. It can contain ${varname} entries, which can be passed from the call site inside the **variables input argument. """ templatedir = 'templates' templatename = 'model.tpl.mdu' lookup = mako.lookup.TemplateLookup(directories=templatedir, module_directory='/tmp/mako_modules') mdutemplate = lookup.get_template(templatename) with open(mdufilename, 'wb') as f: # You can use all the local variables in the templates rendered = mdutemplate.render(**variables) # print rendered print('Writing ' + mdufilename + '...') #print(rendered) f.write(rendered)
def test_dynamic_key_with_imports(self): lookup = TemplateLookup() lookup.put_string("foo.html", """ <%! callcount = [0] %> <%namespace file="ns.html" import="*"/> <%page cached="True" cache_key="${foo}"/> this is foo <% callcount[0] += 1 %> callcount: ${callcount} """) lookup.put_string("ns.html", """""") t = lookup.get_template("foo.html") m = self._install_mock_cache(t) t.render(foo='somekey') t.render(foo='somekey') assert result_lines(t.render(foo='somekey')) == [ "this is foo", "callcount: [1]" ] assert m.kwargs == {}
def generate_dir(dirpath): sourcepath = path.join(source, dirpath) destpath = path.join(dest, dirpath) pages = filter(lambda p: p[0] != "." and not p.endswith(".mako"), os.listdir(sourcepath)) for page in pages: template_path = path.join(dirpath, page) template_sourcepath = path.join(source, template_path) template_destpath = path.join(dest, template_path.replace(".mako.","")) if path.isdir(template_sourcepath): if not path.exists(template_destpath): os.mkdir(template_destpath) generate_dir(template_path) else: template = lookup.get_template(template_path) print "* Generating " + template_destpath try: html = template.render(dirpath=dirpath, page=page) except: print mako.exceptions.text_error_template().render() else: with open(template_destpath, "w") as f: f.write(html)
def make_html(project, docstrings_format, strip_docstrings): if docstrings_format == 'rst': format_doc = format_rst else: format_doc = format_md if strip_docstrings: format_doc = compose(stripper, format_doc) try: lookup = mako.lookup.TemplateLookup(directories=[TEMPLATE_PATH]) template = lookup.get_template('html.mako') return template.render(project=project, format_md=format_md, format_rst=format_rst, format_doc=format_doc) except mako.exceptions.MakoException: tb = mako.exceptions.text_error_template() \ .render() raise FatalError('error loading `html.mako`', tb=tb) except Exception: tb = traceback.format_exc() # Below, we attempt to provide a nice Mako traceback, which includes # relevant Mako templating, but keep a standard formatted traceback in # case anything goes wrong. try: tb = mako.exceptions.text_error_template() \ .render() except Exception: tb = tb raise FatalError('unknown error while rendering HTML', tb=tb)
def _get_template(self, lookup, template_name): return lookup.get_template(template_name, lambda path: self._create_template(lookup, path))
def report(statistics, filename=None, template="report.mako", **kwargs): """ Generate an HTML report of query statistics. :param statistics: An iterable of :class:`QueryStats` objects over which to prepare a report. This is typically a list returned by a call to :func:`collect`. :param filename: If present, additionally write the html report out to a file at the specified path. :param template: The name of the file in the sqltap/templates directory to render for the report. This is mostly intended for extensions to sqltap (like the wsgi extension). :param kwargs: Additional keyword arguments to be passed to the template. Intended for extensions. :return: The generated HTML report. """ class QueryGroup: def __init__(self): self.queries = [] self.stacks = collections.defaultdict(int) self.callers = {} self.max = 0 self.min = sys.maxsize self.sum = 0 self.mean = 0 self.median = 0 def find_user_fn(self, stack): """ rough heuristic to try to figure out what user-defined func in the call stack (i.e. not sqlalchemy) issued the query """ for frame in reversed(stack): # frame[0] is the file path to the module if 'sqlalchemy' not in frame[0]: return frame def add(self, q): if not bool(self.queries): self.text = str(q.text) self.title = q.user_context q.caller = self.find_user_fn(q.stack) self.queries.append(q) self.stacks[q.stack_text] += 1 self.callers[q.stack_text] = self.find_user_fn(q.stack) self.max = max(self.max, q.duration) self.min = min(self.min, q.duration) self.sum = self.sum + q.duration self.mean = self.sum / len(self.queries) def calc_median(self): queries = sorted(self.queries, key=lambda q: q.duration, reverse=True) length = len(queries) if not length % 2: x1 = queries[length // 2].duration x2 = queries[length // 2 - 1].duration self.median = (x1 + x2) / 2 else: self.median = queries[length // 2].duration query_groups = collections.defaultdict(QueryGroup) all_group = QueryGroup() # group together statistics for the same query for qstats in statistics: qstats.stack_text = \ ''.join(traceback.format_list(qstats.stack)).strip() group = query_groups[str(qstats.request_id)] group.add(qstats) all_group.add(qstats) query_groups = sorted(query_groups.values(), key=lambda g: g.sum, reverse=True) # calculate the median for each group for g in query_groups: g.calc_median() # create the template lookup # (we need this for extensions inheriting the base template) tmpl_dir = os.path.join(os.path.dirname(__file__), "templates") lookup = mako.lookup.TemplateLookup(tmpl_dir) # render the template html = lookup.get_template(template).render( query_groups = query_groups, all_group = all_group, name = "SQLTap Profiling Report", **kwargs ) # write it out to a file if you asked for it if filename: with open(filename, 'w') as f: f.write(html) return html
def render(self, filename=None, **kwargs): if not filename: filename = '/%s/%s.html' % ('/'.join(self.__module__.split('.')[1:-1]), self.__class__.__name__.lower()) tmpl = lookup.get_template(filename.replace(r'//', r'/')) self.finish(tmpl.render(req=self, **kwargs))
def instantiate(cls, template_name): base_path = os.path.dirname(__file__) + "/templates/" lookup = mako.lookup.TemplateLookup([ base_path + "testcases", base_path + "blocks" ], strict_undefined = True, input_encoding = "utf-8") template = lookup.get_template(template_name) return cls(template)
def get_rendered_template ( self, template ): return lookup.get_template(template).render( view = self.view, urls = self.urls, )
def report(statistics, filename=None, template="report.mako", **kwargs): """ Generate an HTML report of query statistics. :param statistics: An iterable of :class:`QueryStats` objects over which to prepare a report. This is typically a list returned by a call to :func:`collect`. :param filename: If present, additionally write the html report out to a file at the specified path. :param template: The name of the file in the sqltap/templates directory to render for the report. This is mostly intended for extensions to sqltap (like the wsgi extension). :param kwargs: Additional keyword arguments to be passed to the template. Intended for extensions. :return: The generated HTML report. """ class QueryGroup: def __init__(self): self.queries = [] self.stacks = collections.defaultdict(int) self.callers = {} self.max = 0 self.min = sys.maxsize self.sum = 0 self.mean = 0 self.median = 0 def find_user_fn(self, stack): """ rough heuristic to try to figure out what user-defined func in the call stack (i.e. not sqlalchemy) issued the query """ for frame in reversed(stack): # frame[0] is the file path to the module if 'sqlalchemy' not in frame[0]: return frame def add(self, q): if not bool(self.queries): self.text = str(q.text) self.first_word = self.text.split()[0] self.queries.append(q) self.stacks[q.stack_text] += 1 self.callers[q.stack_text] = self.find_user_fn(q.stack) self.max = max(self.max, q.duration) self.min = min(self.min, q.duration) self.sum = self.sum + q.duration self.mean = self.sum / len(self.queries) def calc_median(self): queries = sorted(self.queries, key=lambda q: q.duration, reverse=True) length = len(queries) if not length % 2: x1 = queries[length // 2].duration x2 = queries[length // 2 - 1].duration self.median = (x1 + x2) / 2 else: self.median = queries[length // 2].duration query_groups = collections.defaultdict(QueryGroup) all_group = QueryGroup() # group together statistics for the same query for qstats in statistics: qstats.stack_text = \ ''.join(traceback.format_list(qstats.stack)).strip() group = query_groups[str(qstats.text)] group.add(qstats) all_group.add(qstats) query_groups = sorted(query_groups.values(), key=lambda g: g.sum, reverse=True) # calculate the median for each group for g in query_groups: g.calc_median() # create the template lookup # (we need this for extensions inheriting the base template) tmpl_dir = os.path.join(os.path.dirname(__file__), "templates") lookup = mako.lookup.TemplateLookup( tmpl_dir, default_filters=['unicode', 'h']) # mako fixes unicode -> str on py3k # render the template html = lookup.get_template(template).render(query_groups=query_groups, all_group=all_group, name="SQLTap Profiling Report", **kwargs) # write it out to a file if you asked for it if filename: with open(filename, 'w') as f: f.write(html) return html
import os import haml import mako.lookup # Build the template lookup. lookup = mako.lookup.TemplateLookup([os.path.dirname(__file__)], preprocessor=haml.preprocessor ) # Retrieve a template. template = lookup.get_template('page.haml') print template.render_unicode()
def vote(self, *args, **kwargs): template = lookup.get_template('index.html') return template.render()
def render(template_name, **namespace): lookup = mako.lookup.TemplateLookup(directories=[TEMPLATE_DIR]) template = lookup.get_template(template_name) s = template.render(PARSED_ARGS=PARSED_ARGS, **namespace) return re.sub("^\s+$", "", s, flags=re.M)
def get_mako_template(template): import mako.lookup dirs = os.path.join(os.path.dirname(__file__), "templates") lookup = mako.lookup.TemplateLookup(directories=[dirs]) return lookup.get_template(template)
import haml import mako.template import mako.lookup import os # Build the template lookup. lookup = mako.lookup.TemplateLookup(["templates"], preprocessor=haml.preprocessor ) # parse all haml files which don't start with _ for filename in os.listdir('./templates'): if filename[0] != '_' and filename.split('.')[-1] == 'haml': template_name = filename.split('.')[0] # Retrieve a template. template = lookup.get_template('%s.haml' % template_name) destination_file = './build/%s.html' % template_name # Render! print "Rendering %s.haml to %s" % (template_name, destination_file) with open(destination_file, 'w') as fh: try: fh.write(template.render()) except: print mako.exceptions.text_error_template().render()
def _get_template(self, lookup, template_name): return lookup.get_template( template_name, lambda path: self._create_template(lookup, path))
def generate_objc(typing): t = lookup.get_template('objc.mako') return t.render( interfaces=sorted(typing.interfaces, key=lambda t: t.name), )
def get_template(template_path): return lookup.get_template(template_path)
def get_template(self, template_name): return lookup.get_template(template_name, factory)
def _load(self, path): dir_path = os.path.dirname(path) file_name = os.path.basename(path) lookup = mako.lookup.TemplateLookup(directories=[dir_path]) return lookup.get_template(file_name)
def get_template(name): return lookup.get_template(name)
def render_template(self, template_name, context): lookup = mako.lookup.TemplateLookup(directories=[ 'lib/edx-platform/lms/templates', 'lib/edx-platform/common/lib/capa/capa/templates']) template = lookup.get_template(template_name) return template.render(**context)
'dataset_trainingPositive': dataset_trainingPositive, 'dataset_trainingNegative': dataset_trainingNegative, 'dataset_trainingTotal': dataset_trainingTotal, 'dataset_testPositive': dataset_testPositive, 'dataset_testNegative': dataset_testNegative, 'dataset_testTotal': dataset_testTotal, 'dataset_windowLength': dataset_windowLength, 'dataset_windowNames': dataset_windowNames, 'dataset_patchNames': dataset_patchNames, 'source_meanShiftCount': source_meanShiftCount, 'source_meanMultispectralPixelShiftValue': source_meanMultispectralPixelShiftValue, }) # Write csvWriter.writerow(row) # Render tables open(expandPath('latex_scan.tex'), 'wt').write(lookup.get_template('latex_scan.mako').render(preparedResults=preparedResults)) if hasLocations: open(expandPath('latex_location.tex'), 'wt').write(lookup.get_template('latex_location.mako').render(preparedResults=preparedResults)) # Render templates print 'Rendering templates...' try: renderTemplate('single_ugly') renderTemplate('single_pretty') # renderTemplate('multiple_ugly') renderTemplate('multiple_pretty') open(expandPath('paths.html'), 'wt').write(lookup.get_template('paths.mako').render(paths=paths)) except: print mako.exceptions.text_error_template().render() # Plot roc curves # pylab.figure() # scan_tprs = [int(x['scan_actualTruePredictedTrue']) / float(x['scan_actualTrue']) for x in preparedResults]