Exemplo n.º 1
0
 def index(self):
     self.msg = []
     try:
         ar_jcfg = xte_auto_routing.get()
         group_jcfg = info.getGroupJcfg()
         tmp = controller.lookup.get_template("auto_routing.mako")
         if ar_jcfg[0]:  # load successfully
             return tmp.render(
                 arargs=ar_jcfg[1],
                 ipgroup_args=group_jcfg[0],
                 servicegroup_args=group_jcfg[1],
                 fqdn_args=group_jcfg[2],
                 msg_list=self.msg,
             )
         else:
             self.addMsg(str(ar_jcfg[1]))
             return tmp.render(
                 arargs={},
                 ipgroup_args=group_jcfg[0],
                 servicegroup_args=group_jcfg[1],
                 fqdn_args=group_jcfg[2],
                 msg_list=self.msg,
             )
     except:
         exceptions.html_error_template().render()
Exemplo n.º 2
0
    def test_html_error_template(self):
        """test the html_error_template"""
        code = """
% i = 0
"""
        try:
            template = Template(code)
            template.render_unicode()
        except exceptions.CompileException, ce:
            html_error = exceptions.html_error_template().render_unicode()
            assert ("CompileException: Fragment 'i = 0' is not a partial "
                    "control statement") in html_error
            assert '<style>' in html_error
            assert '</style>' in html_error
            html_error_stripped = html_error.strip()
            assert html_error_stripped.startswith('<html>')
            assert html_error_stripped.endswith('</html>')

            not_full = exceptions.html_error_template().\
                                    render_unicode(full=False)
            assert '<html>' not in not_full
            assert '</html>' not in not_full
            assert '<style>' in not_full
            assert '</style>' in not_full

            no_css = exceptions.html_error_template().\
                                    render_unicode(css=False)
            assert '<style>' not in no_css
            assert '</style>' not in no_css
Exemplo n.º 3
0
	def index(self):
		self.msg = []
		try:
			tmp = controller.lookup.get_template("stat_bandwidth_utilization.mako")
			return tmp.render(msg_list = self.msg)
		except:
			exceptions.html_error_template().render()
Exemplo n.º 4
0
 def get_error_html(self, status_code, exception, **kwargs):
     if hasattr(exception, 'code'):
         self.set_status(exception.code)
         if exception.code == 500:
             return exceptions.html_error_template().render()
         return render_template(str(exception.code))
     return exceptions.html_error_template().render()
Exemplo n.º 5
0
	def index(self):
		self.msg = []
		try:
			nat_jcfg = xte_nat.get()
			group_jcfg = info.getGroupJcfg()
			summary_jcfg = xte_summary.get()
			if not summary_jcfg[0]:
				summary_jcfg[1] = {};
			tmp = controller.lookup.get_template("nat.mako")
			if nat_jcfg[0]: # load successfully
				return tmp.render(nat_args = nat_jcfg[1],
					ipgroup_args = group_jcfg[0],
					servicegroup_args = group_jcfg[1],
					fqdn_args = group_jcfg[2],
					summary_args = summary_jcfg[1],
					msg_list = self.msg)
			else:
				self.addMsg(str(nat_jcfg[1]))
				return tmp.render(nat_args = {},
					ipgroup_args = group_jcfg[0],
 					servicegroup_args = group_jcfg[1],
					fqdn_args = group_jcfg[2],
					summary_args = summary_jcfg[1],
					msg_list = self.msg)
		except:
			exceptions.html_error_template().render()
Exemplo n.º 6
0
    def __call__(self, filename,
                 directories=None,
                 module_directory=None,
                 collection_size=None):

        if not directories:
            directories = cherrypy.tools.mako.directories

        if not collection_size and not cherrypy.tools.mako.collection_size:
            collection_size = -1
        elif not collection_size and cherrypy.tools.mako.collection_size:
            collection_size = cherrypy.tools.mako.collection_size
        # Find the appropriate template lookup.
        key = (tuple(directories), module_directory)
        try:
            lookup = self.lookups[key]
        except KeyError:
            lookup = TemplateLookup(directories=directories,
                                    module_directory=module_directory,
                                    collection_size=collection_size,
                                    )
            self.lookups[key] = lookup
        cherrypy.request.lookup = lookup

        # Replace the current handler.
        try:
            cherrypy.request.template = t = lookup.get_template(filename)
            cherrypy.request.handler = MakoHandler(t, cherrypy.request.handler)
        except cherrypy.HTTPRedirect:
            raise
        except:
            print exceptions.html_error_template().render()
Exemplo n.º 7
0
    def test_html_error_template(self):
        """test the html_error_template"""
        code = """
% i = 0
"""
        try:
            template = Template(code)
            template.render_unicode()
            assert False
        except exceptions.CompileException:
            html_error = exceptions.html_error_template().render_unicode()
            assert ("CompileException: Fragment &#39;i = 0&#39; is not "
                    "a partial control statement at line: 2 char: 1") in html_error
            assert '<style>' in html_error
            html_error_stripped = html_error.strip()
            assert html_error_stripped.startswith('<html>')
            assert html_error_stripped.endswith('</html>')

            not_full = exceptions.html_error_template().\
                                    render_unicode(full=False)
            assert '<html>' not in not_full
            assert '<style>' in not_full

            no_css = exceptions.html_error_template().\
                                    render_unicode(css=False)
            assert '<style>' not in no_css
        else:
            assert False, ("This function should trigger a CompileException, "
                           "but didn't")
Exemplo n.º 8
0
 def __call__(self):
     try:
         env = globals().copy()
         env.update(self.next_handler())
         return self.template.render(**env)
     except cherrypy.HTTPRedirect:
         raise
     except:
         print exceptions.html_error_template().render()
Exemplo n.º 9
0
	def get_error_html(self, status_code, exception, **kwargs):
		if hasattr(exception, 'code'):
			self.set_status(exception.code)
			if exception.code == 500:
				return exceptions.html_error_template().render()
			#除了code 500以外,所有的错误都会根据/template/{code}.html进行渲染
			#因此需要在/templates/下面定义对应错误信息的默认页面
			return self.render_template("error/" + str(exception.code))
		return exceptions.html_error_template().render()
Exemplo n.º 10
0
def index():
	agent = os.environ['HTTP_USER_AGENT']
	cpt=agent.count('Firefox')
	cpt=cpt + agent.count('Chrome')
	mytemplate = tplookup.get_template('gestcodir-dev.mako')	
	try:
		print mytemplate.render_unicode(logon=True).encode('utf-8', 'replace')
	except:
		print exceptions.html_error_template().render()
Exemplo n.º 11
0
	def index(self):
		try:
			cherrypy.session['iCurrentCategory'] = 0
			cherrypy.session['iCurrentPage'] = 1
			mytemplate = controller.lookup.get_template("interface.mako")
			return mytemplate.render(iCurrentCategory = cherrypy.session['iCurrentCategory'],
				iCurrentPage = cherrypy.session['iCurrentPage'],
				iUserLanguage = cherrypy.session['iUserLanguage'])
		except:
			exceptions.html_error_template().render()
Exemplo n.º 12
0
	def index(self):
		try:
			view_jcfg = xte_viewlog.get()
			tmp = controller.lookup.get_template("view.mako")
			if view_jcfg[0]:
				return tmp.render(viewargs = view_jcfg[1])
			else:
				return tmp.render(viewargs = {})
		except:
			exceptions.html_error_template().render()
Exemplo n.º 13
0
 def index(self):
     self.msg = []
     try:
         arp_table_jcfg = xte_arp_table.get()
         tmp = controller.lookup.get_template("arp_table.mako")
         if arp_table_jcfg[0]:  # load successfully
             return tmp.render(arptable_args=arp_table_jcfg[1], msg_list=self.msg)
         else:
             self.addMsg(str(arp_table_jcfg[1]))
             return tmp.render(arptable_args={}, msg_list=self.msg)
     except:
         exceptions.html_error_template().render()
Exemplo n.º 14
0
 def index(self):
     self.msg = []
     try:
         fqdn_jcfg = xte_fqdn.get()
         tmp = controller.lookup.get_template("fqdn.mako")
         if fqdn_jcfg[0]:  # load successfully
             return tmp.render(fqdnargs=fqdn_jcfg[1], msg_list=self.msg)
         else:
             self.addMsg(str(fqdn_jcfg[1]))
             return tmp.render(fqdnargs={}, msg_list=self.msg)
     except:
         exceptions.html_error_template().render()
Exemplo n.º 15
0
	def index(self):
		self.msg = []
		try:
			servicegroup_jcfg = xte_service_group.get()
			tmp = controller.lookup.get_template("service_group.mako")
			if servicegroup_jcfg[0]: # load successfully
				return tmp.render(servicegroup_args = servicegroup_jcfg[1], msg_list = self.msg)
			else:
				self.addMsg(str(servicegroup_jcfg[1]))
				return tmp.render(servicegroup_args = {}, msg_list = self.msg)
		except:
			exceptions.html_error_template().render()
Exemplo n.º 16
0
Arquivo: tm.py Projeto: mkuai/pycloud
 def get(self, name, style='html'):
     key = "%s.%s" % (name.lower(), style)
     template = self.templates.get(key)
     if not template:
         try:
             template = config['pylons.app_globals'].mako_lookup.get_template(key)
             if template:
                 self.templates[key] = template
         except TemplateLookupException:
             print exceptions.html_error_template().render()
     if not template:
         raise AttributeError('template for %s not found' % name)
     return template
Exemplo n.º 17
0
	def index(self):
		self.msg = []
		try:
			network_jcfg = xte_network.get()
			usb_jcfg = xte_network.getusb()
			tmp = controller.lookup.get_template("network.mako")
			if network_jcfg[0]: # load successfully
				return tmp.render(network_args = network_jcfg[1], usb_args = usb_jcfg[1], msg_list = self.msg)
			else:
				self.addMsg(str(network_jcfg[1]))
				return tmp.render(network_args = {}, usb_args = {}, msg_list = self.msg)
		except:
			exceptions.html_error_template().render()
Exemplo n.º 18
0
	def index(self):
		self.msg = []
		try:
			ipgroup_jcfg = xte_ip_group.get()
			fqdn_jcfg = info.getGroupJcfg()[2]
			tmp = controller.lookup.get_template("ip_group.mako")
			if ipgroup_jcfg[0]: # load successfully
				return tmp.render(ipgroup_args = ipgroup_jcfg[1], fqdn_args = fqdn_jcfg,
					msg_list = self.msg)
			else:
				self.addMsg(str(ipgroup_jcfg[1]))
				return tmp.render(ipgroup_args = {}, fqdn_args = [], msg_list = self.msg)
		except:
			exceptions.html_error_template().render()
Exemplo n.º 19
0
def render_template(path, params=dict()):
    ''' Render a template for the given parameters 
        path:      Template absolute path
        params:    Rendering parameters
    '''
    
    full_path, file_name = get_template_path_info(path)
    
    lookup = TemplateLookup(directories=[full_path], filesystem_checks=FILE_CHECKS, module_directory=TEMPLATE_CACHE_DIR)
    template = lookup.get_template(os.path.sep + file_name)
    
    ctx_params = dict(params)
    ctx_params['include_template'] = render_template
    ctx_params['config'] = Config
    ctx_params['session'] = session
    ctx_params['helpers'] = helpers
    
    buf = StringIO()
    ctx = Context(buf, **ctx_params)
    try: 
        template.render_context(ctx)
        return buf.getvalue()
    except:
        return exceptions.html_error_template().render()
    finally:
        if buf: buf.close()
Exemplo n.º 20
0
 def MAKOIFY(request, *args, **kwargs):
     res = func(request, *args, **kwargs)
     if not res:
         res = {}
     if type(res) == dict:
         # use current_app/mako_templates/ as first search path.
         # the assumption is that the function's module is under current_app/
         d = os.path.join(
               os.path.dirname(sys.modules[func.__module__].__file__),
               "mako_templates/")
         lookup = TemplateLookup(directories=[d, 'mako_templates/'])
         res['request'] = request
         try:
             t = lookup.get_template(template)
             return HTTPMethod(t.render(**res))
         except:
             #if settings.DEBUG:
             if True:
                 # display Mako's debug page on template error
                 return HTTPMethod(exceptions.html_error_template()\
                                     .render(), status=500)
             raise
     else:
         # if not a dictionary or empty value, return literal result
         return res
Exemplo n.º 21
0
    def toggle_webapp(self, action=None):
        # Set up a generic error catching variable for this page.
        error = ''

        if action == 'activate':
            status = 'active'
            action = 'activated'
        else:
            status = 'disabled'
            action = 'deactivated'

        query = "UPDATE webapps SET status=? WHERE name=?;"
        template = template = (status, self.app, )
        database, cursor = _utils.execute_query(self.servicedb, query, template=template)
        database.commit()
        cursor.close()

        # Render the HTML page and send it to the browser.
        try:
            page = self.templatelookup.get_template("/services/toggled.html")
            return page.render(title = "Byzantium Node Services",
                               purpose_of_page = "Service toggled.",
                               error = error, app = self.app,
                               action = action)
        except:
            return exceptions.html_error_template().render()
Exemplo n.º 22
0
    def toggle_webapp(self, action=None):
        # Set up a generic error catching variable for this page.
        error = ''

        # Set up a connection to the services.sqlite database.
        database = sqlite3.connect(self.servicedb)
        cursor = database.cursor()

        if action == 'activate':
            status = 'active'
            action = 'activated'
        else:
            status = 'disabled'
            action = 'deactivated'

        # Update the database with the new status.
        template = (status, self.app, )
        cursor.execute("UPDATE webapps SET status=? WHERE name=?;", template)
        database.commit()
        cursor.close()

        # Render the HTML page and send it to the browser.
        try:
            page = templatelookup.get_template("/services/toggled.html")
            return page.render(title = "Byzantium Node Services",
                               purpose_of_page = "Service toggled.",
                               error = error, app = self.app,
                               action = action)
        except:
            return exceptions.html_error_template().render()
Exemplo n.º 23
0
 def test_py_unicode_error_html_error_template(self):
     try:
         raise RuntimeError(u'日本')
     except:
         html_error = exceptions.html_error_template().render()
         assert 'RuntimeError: &#x65E5;&#x672C;' in html_error
         assert "RuntimeError(u'&#x65E5;&#x672C;')" in html_error
Exemplo n.º 24
0
def serve(environ, start_response):
    """serves requests using the WSGI callable interface."""
    fieldstorage = cgi.FieldStorage(
            fp = environ['wsgi.input'],
            environ = environ,
            keep_blank_values = True
    )
    d = dict([(k, getfield(fieldstorage[k])) for k in fieldstorage])

    uri = environ.get('PATH_INFO', '/')
    if not uri:
        uri = '/index.html'
    else:
        uri = re.sub(r'^/$', '/index.html', uri)

    if re.match(r'.*\.html$', uri):
        try:
            template = lookup.get_template(uri)
            start_response("200 OK", [('Content-type','text/html')])
            return [template.render(**d)]
        except exceptions.TopLevelLookupException:
            start_response("404 Not Found", [])
            return ["Cant find template '%s'" % uri]
        except:
            if error_style == 'text':
                start_response("200 OK", [('Content-type','text/plain')])
                return [exceptions.text_error_template().render()]
            else:
                start_response("200 OK", [('Content-type','text/html')])
                return [exceptions.html_error_template().render()]
    else:
        u = re.sub(r'^\/+', '', uri)
        filename = os.path.join(root, u)
        start_response("200 OK", [('Content-type',guess_type(uri))])
        return [file(filename).read()]
Exemplo n.º 25
0
    def render(self, vars=None, force=True):
        """Renders the template to a string, if already rendered will not render
        again unless force is set to True.

        :param force: If True then will render the template again, if False \
        will check first if its already rendered.
        """
        self.template_vars.update_recursive({
            '__inherit__': self.template_inherit,
            '__sections__': self.template_sections,
            '__format__': self.template_format,
            '__layout__': self.template_layout,
        })

        self.template_vars.update_recursive(vars or {})
        if not force and self.rendered is not None:
            return self.rendered
        else:
            try:
                output = self.template.render_unicode(**self.template_vars)
            except Exception, e:
                if self.config.get('debug'):
                    output = exceptions.html_error_template().render()
                else:
                    logger.error(exceptions.text_error_template().render())
                    raise Exception(e)
Exemplo n.º 26
0
    def admin(self, context, request):
        """
        Render a page from the admin directory and write it back.
        """
        if request.method == "GET":
            # Read the page_name from the request.
            page_name = request.matchdict.get("page_name", "index")
            object_name = request.matchdict.get("object_name", "")

            if object_name and page_name.endswith("s"):
                page_name = page_name[:-1]
            if page_name.find(".") < 0:
                page_name += ".html"

            # Check that the file exists.
            filename = os.path.join(os.path.dirname(__file__), "admin", page_name)
            if not (os.path.exists(filename)):
                return Response(status=404)

            # Render it.
            lookup_path = os.path.join(os.path.dirname(__file__), "admin", "include")
            lookup = TemplateLookup(directories=[lookup_path])
            template = Template(filename=filename, lookup=lookup)
            kwargs = {"object": object_name, "user": "******", "loggedin": authenticated_userid(request) != None}
            try:
                page_data = template.render(**kwargs)
            except:
                page_data = exceptions.html_error_template().render()

            return Response(body=page_data)
        else:
            return Response(status=403)
Exemplo n.º 27
0
    def GET(self, **kwargs):
        """responds to data GET requests
        Args:
            None yet
        Returns:
            str: table.html
        """
        logger.info('/data GET request')
        kwargs = self.fixInput(kwargs)
        logger.debug('GET kwargs: %s' % kwargs)
        data = self.pipe.human.getTable(**kwargs)

        kwargs = {'Title': 'Human Expression Table',
                  'data': data,
                  'filters': self.fixFilters(kwargs),
                  'columnNames': app.settings.getColumnNames('human')}
        kwargs = self.mako_args(kwargs)

        tmpl = self.lookup.get_template("table.html")

        # logger.debug('kwargs sent to mako for data table: %s' %
        #    pprint.pformat(kwargs))
        try:
            return tmpl.render(**kwargs)
        except:
            return exceptions.html_error_template().render()
Exemplo n.º 28
0
    def test_utf8_html_error_template(self):
        """test the html_error_template with a Template containing utf8
        chars"""
        
        if util.py3k:
            code = """# -*- coding: utf-8 -*-
% if 2 == 2: /an error
${'привет'}
% endif
"""
        else:
            code = """# -*- coding: utf-8 -*-
% if 2 == 2: /an error
${u'привет'}
% endif
"""
        try:
            template = Template(code)
            template.render_unicode()
        except exceptions.CompileException, ce:
            html_error = exceptions.html_error_template().render()
            assert ("CompileException: Fragment 'if 2 == 2: /an "
                    "error' is not a partial control "
                    "statement at line: 2 char: 1") in \
                    html_error.decode('utf-8')
                    
            if util.py3k:
                assert u"3 ${'привет'}".encode(sys.getdefaultencoding(),
                                            'htmlentityreplace') in html_error
            else:
                assert u"3 ${u'привет'}".encode(sys.getdefaultencoding(),
                                            'htmlentityreplace') in html_error
Exemplo n.º 29
0
        def decorated(*args, **kwargs):
            path = cherrypy.request.app.script_name + "/"
            tpl_dir = cherrypy.request.app.config[path].get('templates')
            template_name = f.template_name
            
            if tpl_dir is None:
                tpl_dir = args[0].templates
            
            try:
                values = f(*args, **kwargs)
                
                if useattr is not None:
                    template_name = values[useattr]
                
                lookup = TemplateLookup([tpl_dir])
                template = lookup.get_template(template_name)
                
                if not values.get('js'):
                    values['js'] = []

                if not values.get('css'):
                    values['css'] = []

                values['url'] = cherrypy.url

                return template.render(**values)
            
            except cherrypy.HTTPRedirect as e:
                raise e
            except cherrypy.HTTPError as e:
                raise e
            except cherrypy.NotFound as e:
                raise e
            except:
                return html_error_template().render()
Exemplo n.º 30
0
 def __call__(self, values):
     template_type = 'taylor.tmpl'
     try:
         tmpl = self.tmpls.get_template(template_type)
         return tmpl.render(**values)
     except:
         return exceptions.html_error_template().render()
Exemplo n.º 31
0
    def test_escapes_html_tags(self):
        from mako.exceptions import html_error_template

        x = Template("""
        X:
        <% raise Exception('<span style="color:red">Foobar</span>') %>
        """)

        try:
            x.render()
        except:
            # <h3>Exception: <span style="color:red">Foobar</span></h3>
            markup = html_error_template().render(full=False, css=False)
            if compat.py3k:
                assert '<span style="color:red">Foobar</span></h3>'\
                            .encode('ascii') not in markup
                assert '&lt;span style=&#34;color:red&#34;'\
                            '&gt;Foobar&lt;/span&gt;'\
                            .encode('ascii') in markup
            else:
                assert '<span style="color:red">Foobar</span></h3>' \
                            not in markup
                assert '&lt;span style=&#34;color:red&#34;'\
                            '&gt;Foobar&lt;/span&gt;' in markup
Exemplo n.º 32
0
def render_to_response(filename, dictionary, context_instance=None):
    '''
    :param filename:
    :param dictionary:
    :param context_instance:
    :return: rendered django HttpResponse
    '''

    dictionary.update(default_context)

    if context_instance:
        return render(context_instance.request, filename, dictionary)

    if hasattr(settings, 'MAKO_DEFAULT_CONTEXT'):
        dictionary.update(settings.MAKO_DEFAULT_CONTEXT)

    try:
        template = template_lookup.get_template(filename)
        return HttpResponse(template.render(**dictionary))
    except exceptions.TopLevelLookupException:
        raise
    except:
        traceback.print_exc()
        return HttpResponseServerError(exceptions.html_error_template().render())
Exemplo n.º 33
0
 def test_py_unicode_error_html_error_template(self):
     try:
         raise RuntimeError(u'日本')
     except:
         html_error = exceptions.html_error_template().render()
         assert u"RuntimeError: 日本".encode('ascii', 'ignore') in html_error
Exemplo n.º 34
0
def serve_template(templatename, **kwargs):
    try:
        template = ser_lookup.get_template(templatename)
        return template.render(**kwargs)
    except:
        return exceptions.html_error_template().render()
Exemplo n.º 35
0
 def test_py_unicode_error_html_error_template(self):
     try:
         raise RuntimeError("日本")
     except:
         html_error = exceptions.html_error_template().render()
         assert "RuntimeError: 日本".encode("ascii", "ignore") in html_error
Exemplo n.º 36
0
 def format_mako_error(exc_value):
     '''
     Implements ``Mako`` renderer error formatting.
     '''
     if isinstance(exc_value, (CompileException, SyntaxException)):
         return html_error_template().render(full=False, css=False)
import sys
import os
import json
from mako.template import Template
from mako import exceptions

template_file = os.path.abspath(sys.argv[1])
run_id = sys.argv[2]

config = json.load(open('runs/{}/config.json'.format(run_id)))
run = json.load(open('runs/{}/run.json'.format(run_id)))
info = json.load(open('runs/{}/info.json'.format(run_id)))

template = Template(filename=template_file)

try:
    out = template.render(config=config, run=run, info=info)
    open('runs/{}/report.html'.format(run_id), 'w').write(out)
    print('success!')
except:
    print('fail!')
    mako_exception = exceptions.html_error_template().render().decode('utf-8')
    open('runs/{}/exception.html'.format(run_id), 'w').write(mako_exception)
Exemplo n.º 38
0
    def index(self):
        # Set up the strings that will hold the HTML for the tables on this
        # page.
        webapps = ''
        systemservices = ''

        # Set up access to the system services database.  We're going to need
        # to read successive lines from it to build the HTML tables.
        error = ''
        connection = sqlite3.connect(self.servicedb)
        cursor = connection.cursor()

        # Use the contents of the services.webapps table to build an HTML table
        # of buttons that are either go/no-go indicators.  It's a bit
        # complicated, so I'll break it into smaller pieces.
        cursor.execute("SELECT name, status FROM webapps;")
        results = cursor.fetchall()
        if not results:
            # Display an error page that says that something went wrong.
            error = "<p>ERROR: Something went wrong in database " + self.servicedb + ", table webapps.  SELECT query failed.</p>"
        else:
            # Set up the opening tag of the table.
            webapp_row = '<tr>'

            # Roll through the list returned by the SQL query.
            for (name, status) in results:
                # Set up the first cell in the row, the name of the webapp.
                if status == 'active':
                    # White on green means that it's active.
                    webapp_row = webapp_row + "<td style='background-color:green; color:white;' >" + name + "</td>"
                else:
                    # White on red means that it's not active.
                    webapp_row = webapp_row + "<td style='background-color:red; color:white;' >" + name + "</td>"

                # Set up the second cell in the row, the toggle that will either
                # turn the web app off or on.
                if status == 'active':
                    # Give the option to deactivate the app.
                    webapp_row = webapp_row + "<td><button type='submit' name='app' value='" + name + "' style='background-color:red; color:white;' >Deactivate</button></td>"
                else:
                    # Give the option to activate the app.
                    webapp_row = webapp_row + "<td><button type='submit' name='app' value='" + name + "' style='background-color:green; color:white;' title='activate' >Activate</button></td>"

                # Set the closing tag of the row.
                webapp_row = webapp_row + "</tr>\n"

            # Add that row to the buffer of HTML for the webapp table.
            webapps = webapps + webapp_row

        # Do the same thing for system services.
        cursor.execute("SELECT name, status FROM daemons;")
        results = cursor.fetchall()
        if not results:
            # Display an error page that says that something went wrong.
            error = "<p>ERROR: Something went wrong in database " + this.servicedb + ", table daemons.  SELECT query failed.</p>"
        else:
            # Set up the opening tag of the table.
            services_row = '<tr>'

            # Roll through the list returned by the SQL query.
            for (name, status) in results:
                # Set up the first cell in the row, the name of the webapp.
                if status == 'active':
                    # White on green means that it's active.
                    services_row = services_row + "<td style='background-color:green; color:white;' >" + name + "</td>"
                else:
                    # White on red means that it's not active.
                    services_row = services_row + "<td style='background-color:red; color:white;' >" + name + "</td>"

                # Set up the second cell in the row, the toggle that will either
                # turn the web app off or on.
                if status == 'active':
                    # Give the option to deactivate the app.
                    services_row = services_row + "<td><button type='submit' name='service' value='" + name + "' style='background-color:red; color:white;' >Deactivate</button></td>"
                else:
                    # Give the option to activate the app.
                    services_row = services_row + "<td><button type='submit' name='service' value='" + name + "' style='background-color:green; color:white;' >Activate</button></td>"

                # Set the closing tag of the row.
                services_row = services_row + "</tr>\n"

            # Add that row to the buffer of HTML for the webapp table.
            systemservices = systemservices + services_row

        # Gracefully detach the system services database.
        cursor.close()

        # Render the HTML page.
        try:
            page = templatelookup.get_template("/services/index.html")
            return page.render(title="Byzantium Node Services",
                               purpose_of_page="Manipulate services",
                               error=error,
                               webapps=webapps,
                               systemservices=systemservices)
        except:
            # Holy crap, this is a better exception analysis method than the
            # one above, because it actually prints useful information to the
            # web browser, rather than forcing you to figure it out from stderr.
            # I might have to start using this more.
            return exceptions.html_error_template().render()
Exemplo n.º 39
0
    def render(model):
        from mako import exceptions
        login_page=None
        is_public=None
        if type(template_file) is dict:
            fileName=template_file["file"]
            login_page=template_file.get("login",None)
            is_public = template_file.get("is_public", False)
        else:
            fileName = template_file
        def get_csrftoken():
            if type(csrf(request)["csrf_token"]) is str:
                return csrf(request)["csrf_token"]
            else:
                return csrf(request)["csrf_token"].encode()

        render_model = {
            "get_res": get_res,
            "get_app_res": get_app_res,
            "get_global_res": get_global_res,
            "get_static": get_static,
            "get_abs_url": get_abs_url,
            "get_csrftoken": get_csrftoken,
            "model": model,
            "get_view_path": get_view_path,
            "get_user": get_user,
            "get_app_url": get_app_url,
            "get_app_host": get_app_host,
            "get_static": get_static,
            "get_language": get_language,
            "template_file": template_file,
            "get_api_key":get_api_key,
            "get_api_path":get_api_path,
            "register_view":register_view,
            "request":request,
            "encryptor":encryptor
        }
        # mylookup = TemplateLookup(directories=config._default_settings["TEMPLATES_DIRS"])
        if fileName != None:
            ret_res=None
            mylookup = TemplateLookup(directories=[os.getcwd() + "/" + request.get_app().template_dir],
                                      default_filters=['decode.utf8'],
                                      input_encoding='utf-8',
                                      output_encoding='utf-8',
                                      encoding_errors='replace'
                                      )
            try:
                ret_res=mylookup.get_template(fileName).render(**render_model)

            except exceptions.MakoException as ex:
                logger.debug(ex)
                raise (ex)
            except Exception as ex:
                logger.debug(exceptions.html_error_template().render())
                raise (Exception(exceptions.html_error_template().render()))
            return HttpResponse(ret_res)


        else:
            mylookup = TemplateLookup(directories=["apps/templates"],
                                      default_filters=['decode.utf8'],
                                      input_encoding='utf-8',
                                      output_encoding='utf-8',
                                      encoding_errors='replace'
                                      )
            return HttpResponse(mylookup.get_template(fileName).render(**render_model))
Exemplo n.º 40
0
    def render(self, context=None, request=None, def_name=None):
        '''
        Renders a template using the Mako system.  This method signature conforms to
        the Django template API, which specifies that template.render() returns a string.

            @context  A dictionary of name=value variables to send to the template page.  This can be a real dictionary
                      or a Django Context object.
            @request  The request context from Django.  If this is None, any TEMPLATE_CONTEXT_PROCESSORS defined in your settings
                      file will be ignored but the template will otherwise render fine.
            @def_name Limits output to a specific top-level Mako <%block> or <%def> section within the template.
                      If the section is a <%def>, any parameters must be in the context dictionary.  For example,
                      def_name="foo" will call <%block name="foo"></%block> or <%def name="foo()"></def> within
                      the template.

        Returns the rendered template as a unicode string.

        The method triggers two signals:
            1. dmp_signal_pre_render_template: you can (optionally) return a new Mako Template object from a receiver to replace
               the normal template object that is used for the render operation.
            2. dmp_signal_post_render_template: you can (optionally) return a string to replace the string from the normal
               template object render.
        '''
        # set up the context dictionary, which is the variables available throughout the template
        context_dict = {}
        # if request is None, add some default items because the context processors won't happen
        if request is None:
            context_dict['settings'] = settings
            context_dict['STATIC_URL'] = settings.STATIC_URL
        # let the context_processors add variables to the context.
        if not isinstance(context, Context):
            context = Context(context) if request is None else RequestContext(
                request, context)
        with context.bind_template(self):
            for d in context:
                context_dict.update(d)
        context_dict.pop(
            'self', None
        )  # some contexts have self in them, and it messes up render_unicode below because we get two selfs

        # send the pre-render signal
        if DMP_OPTIONS['SIGNALS'] and request is not None:
            for receiver, ret_template_obj in dmp_signal_pre_render_template.send(
                    sender=self,
                    request=request,
                    context=context,
                    template=self.mako_template):
                if ret_template_obj is not None:
                    if isinstance(ret_template_obj, MakoTemplateAdapter):
                        self.mako_template = ret_template_obj.mako_template  # if the signal function sends a MakoTemplateAdapter back, use the real mako template inside of it
                    else:
                        self.mako_template = ret_template_obj  # if something else, we assume it is a mako.template.Template, so use it as the template

        # do we need to limit down to a specific def?
        # this only finds within the exact template (won't go up the inheritance tree)
        # I wish I could make it do so, but can't figure this out
        render_obj = self.mako_template
        if def_name is None:
            def_name = self.def_name
        if def_name:  # do we need to limit to just a def?
            render_obj = self.mako_template.get_def(def_name)

        # PRIMARY FUNCTION: render the template
        template_name = '%s::%s' % (self.mako_template.filename
                                    or 'string', def_name or 'body')
        if log.isEnabledFor(logging.INFO):
            log.info('rendering template %s', template_name)
        if settings.DEBUG:
            try:
                content = render_obj.render_unicode(**context_dict)
            except Exception as e:
                log.exception('exception raised during template rendering: %s',
                              e)  # to the console
                content = html_error_template().render_unicode(
                )  # to the browser
        else:  # this is outside the above "try" loop because in non-DEBUG mode, we want to let the exception throw out of here (without having to re-raise it)
            content = render_obj.render_unicode(**context_dict)

        # send the post-render signal
        if DMP_OPTIONS['SIGNALS'] and request is not None:
            for receiver, ret_content in dmp_signal_post_render_template.send(
                    sender=self,
                    request=request,
                    context=context,
                    template=self.mako_template,
                    content=content):
                if ret_content is not None:
                    content = ret_content  # sets it to the last non-None return in the signal receiver chain

        # return
        return content
Exemplo n.º 41
0
    def process_mako(my, html):

        from mako.template import Template
        from mako import exceptions
        html = '%s%s' % (CustomLayoutWdg.HEADER, html)

        # remove CDATA tags
        html = html.replace("<![CDATA[", "")
        html = html.replace("]]>", "")
        #html = html.decode('utf-8')
      
        if my.encoding == 'ascii':
            template = Template(html)
        else:
            template = Template(html, output_encoding=my.encoding, input_encoding=my.encoding)

        # get the api version of the sobject
        if not my.is_table_element:
            if my.sobject_dicts == None:
                my.sobject_dicts = []
                for sobject in my.sobjects:
                    sobject_dict = sobject.get_sobject_dict()
                    my.sobject_dicts.append(sobject_dict)

        if my.sobject:
            sobject = my.sobject.get_sobject_dict()
        else:
            sobject = {}



        # find out if there is a plugin associated with this
        plugin = my.kwargs.get("plugin")
        if not plugin or plugin == '{}':
            plugin = {}

        """
        if not plugin and isinstance(my.config, SObject):
            plugin = Search.eval("@SOBJECT(config/plugin_content.config/plugin)", my.config, single=True)
        """

        if plugin:
            if isinstance(plugin, dict):
                pass
            else:
                plugin = plugin.get_sobject_dict()
            plugin_code = plugin.get("code")
            plugin_dir = my.server.get_plugin_dir(plugin)
        else:
            plugin_code = ""
            plugin_dir = ""
            plugin = {}
        my.kwargs['plugin_dir'] = plugin_dir
        my.kwargs['plugin_code'] = plugin_code

        try:
            html = template.render(server=my.server, search=Search, sobject=sobject, sobjects=my.sobject_dicts, data=my.data, plugin=plugin, kwargs=my.kwargs)


            # we have to replace all & signs to &amp; for it be proper html
            html = html.replace("&", "&amp;")
            return html
        except Exception, e:
            if str(e) == """'str' object has no attribute 'caller_stack'""":
                raise TacticException("Mako variable 'context' has been redefined.  Please use another variable name")
            else:
                print "Error in view [%s]: " % my.view, exceptions.text_error_template().render()
                #html = exceptions.html_error_template().render(css=False)
                html = exceptions.html_error_template().render()
                html = html.replace("body { font-family:verdana; margin:10px 30px 10px 30px;}", "")
                return html
Exemplo n.º 42
0
    the contextual Template will be located if it exists, and the error handling options specified
    on that Template will be interpreted here.
    """
    template = context._with_template
    if template is not None and (template.format_exceptions
                                 or template.error_handler):
        error = None
        try:
            callable_(context, *args, **kwargs)
        except Exception, e:
            error = e
        except:
            e = sys.exc_info()[0]
            error = e
        if error:
            if template.error_handler:
                result = template.error_handler(context, error)
                if not result:
                    raise error
            else:
                error_template = exceptions.html_error_template()
                context._buffer_stack[:] = [
                    util.FastEncodingBuffer(error_template.output_encoding,
                                            error_template.encoding_errors)
                ]
                context._with_template = error_template
                error_template.render_context(context, error=error)
    else:
        callable_(context, *args, **kwargs)
Exemplo n.º 43
0
    def _serve(self,
               context,
               request,
               base_dir=None,
               include_dirs=None,
               page_name=None,
               object_name=None,
               render_template=False,
               methods=None,
               **kwargs):

        if methods is None:
            methods = ('GET', )
        if page_name is None:
            page_name = request.matchdict.get('page_name') or 'index'
        if object_name is None:
            object_name = request.matchdict.get('object_name', '')
        if page_name.endswith('/'):
            page_name += 'index'

        if request.method in methods:

            # Find the real file.
            for ext in ['', '.html', '.md']:
                filename = os.path.join(base_dir, page_name + ext)
                if os.path.exists(filename):
                    break

            # Ensure we found it.
            if not(os.path.exists(filename)) or \
               not(os.path.isfile(filename)):
                return Response(status=404)

            try:
                raw_page_data = open(filename, 'r').read()

                # Render if necessary.
                if render_template:
                    lookup = TemplateLookup(directories=include_dirs)
                    template = Template(raw_page_data, lookup=lookup)
                    loggedin = authenticated_userid(request) is not None
                    template_args = {
                        "object": object_name,
                        "loggedin": loggedin,
                    }
                    template_args.update(kwargs)
                    page_data = template.render(**template_args)
                else:
                    page_data = raw_page_data

                # Check for supported types.
                ext = page_name.split('.')[-1]
                headers = {"Content-type": MIMEMAP.get(ext, "text/html")}
            except Exception:
                traceback.print_exc()
                page_data = exceptions.html_error_template().render()
                headers = {"Content-type": "text/html"}

            return Response(body=page_data, headers=headers)
        else:
            return Response(status=403)
Exemplo n.º 44
0
 def _debug_render(self, env):
     try:
         return self.template.render(**env)
     except Exception:
         return exceptions.html_error_template().render()
Exemplo n.º 45
0
from mako import exceptions
import xml.dom.minidom
import copy, os

from settings import context
"""
    :warning: Due to the pretty printer, the memory overhead of this quite large ( O(10MB) ).
"""

if __name__ == "__main__":
    dir_path = os.path.dirname(os.path.realpath(__file__))
    mylookup = TemplateLookup(
        directories=[os.path.join(dir_path, 'templates')])
    temp = Template('<%include file="microboonevX.mako"/>',
                    lookup=mylookup,
                    strict_undefined=True)
    try:
        with_wires = temp.render(data="world", attributes=context)
    except:
        print exceptions.html_error_template().render()

    open("microboonevX.gdml", 'w').write(with_wires)
    context_wowires = context
    context_wowires['wires_on'] = False
    try:
        without_wires = temp.render(data="world", attributes=context_wowires)
    except:
        print exceptions.html_error_template().render()

    xml = xml.dom.minidom.parseString(without_wires)
    open("microboonevX_nowires.gdml", 'w').write(xml.toprettyxml())
Exemplo n.º 46
0
def reports_to_html(reports, output, output_unit = None, title = None,
			   commit_url = None, verbose = False, reference_report = None,
			   reference_commit = None, embed = True):

	# select the right unit
	if output_unit is None:
		output_unit = "FPS"

	# Parse the results and then create one report with the following structure:
	# commit -> report_name -> test -> bench results
	db = dict()
	db["commits"] = collections.OrderedDict()
	db["reports"] = list()
	db["events"] = dict()
	db["tests"] = list()
	db["metrics"] = dict()
	db['env_sets'] = dict()
	db["envs"] = dict()
	db["targets"] = dict()
	db["targets_raw"] = dict()
	db["target_result"] = dict()
	human_envs = dict()

	if reference_report is None and reference_commit is not None:
		reference_report = reports[0]

	# set all the targets
	if reference_report is not None and len(reference_report.commits) > 0:
		if reference_commit is not None:
			ref_commit = reference_report.find_commit_by_id(reference_commit)
		else:
			ref_commit = reference_report.commits[-1]

		db['reference_name'] = "{} ({})".format(reference_report.name, ref_commit.label)
		db['reference'] = reference_report
		for result in ref_commit.results.values():
			average_raw = result.result().mean()
			average = convert_unit(average_raw, result.unit, output_unit)
			average = float("{0:.2f}".format(average))
			average_raw = float("{0:.2f}".format(average_raw))
			if (not result.test.full_name in db["targets"] or
				db["targets"][result.test.full_name] == 0):
					db["targets"][result.test.full_name] = average
					db["targets_raw"][result.test.full_name] = average_raw
					db["target_result"][result.test.full_name] = result

			__env_add_result__(db, human_envs, reference_report, ref_commit, result)

	for report in reports:
		report.events = [e for e in report.events if not isinstance(e, EventResultNeedsMoreRuns)]

	db["events"] = Report.event_tree(reports)

	for report in reports:
		db["reports"].append(report)

		# make sure all the tests are listed in db["envs"]
		for test in report.tests:
			db["envs"][test.full_name] = dict()

		for event in report.events:
			if type(event) is EventPerfChange:
				for result in event.commit_range.new.results.values():
					if result.test.full_name != event.test.full_name:
						continue
					result.annotation = str(event)

		# add all the commits
		for commit in report.commits:
			if len(commit.results) == 0 and not hasattr(commit, 'annotation'):
				continue

			if not commit.label in db["commits"]:
				db["commits"][commit.label] = dict()
				db["commits"][commit.label]['reports'] = dict()
				db["commits"][commit.label]['commit'] = commit
				if not commit.build_broken():
					db["commits"][commit.label]['build_color'] = "#00FF00"
				else:
					db["commits"][commit.label]['build_color'] = "#FF0000"
				db["commits"][commit.label]['build_error'] = str(RunnerErrorCode(commit.compil_exit_code)).split('.')[1]
			db["commits"][commit.label]['reports'][report.name] = dict()

			# Add the results and perform some stats
			score_sum = 0
			count = 0
			for result in commit.results.values():
				if not result.test.full_name in db["tests"]:
					db["tests"].append(result.test.full_name)
					db["metrics"][result.test.full_name] = []
				db["commits"][commit.label]['reports'][report.name][result.test.full_name] = result
				average_raw = result.result().mean()
				if average_raw is not None and result.unit is not None:
					average = convert_unit(average_raw, result.unit, output_unit)
				else:
					average_raw = 0
					average = 0
					result.unit = "unknown"
				score_sum += average
				count += 1

				result.average_raw = float("{0:.2f}".format(average_raw))
				result.average = float("{0:.2f}".format(average))
				result.margin_str = float("{0:.2f}".format(result.result().margin() * 100))

				# Compare to the target
				if (not result.test.full_name in db["targets"] or
				(db["targets"][result.test.full_name] == 0 and 'reference_name' not in db)):
					db["targets"][result.test.full_name] = result.average
					db["targets_raw"][result.test.full_name] = result.average_raw
				result.diff_target = compute_perf_difference(output_unit,
				                                             db["targets"][result.test.full_name],
				                                             result.average)

				# Get the metrics
				result.metrics = dict()
				for metric in result.results(BenchSubTestType.METRIC):
					if metric not in db["metrics"][result.test.full_name]:
						db["metrics"][result.test.full_name].append(metric)

					result.metrics[metric] = result.result(metric)


				# Environment
				__env_add_result__(db, human_envs, report, commit, result)

			if count > 0:
				avg = score_sum / count
			else:
				avg = 0
			db["commits"][commit.label]['reports'][report.name]["average"] = float("{0:.2f}".format(avg))
			db["commits"][commit.label]['reports'][report.name]["average_unit"] = output_unit

	# Generate the environment
	for bench in human_envs:
		env = human_envs[bench]
		if env is not None:
			for key in sorted(list(env.values)):
				if not bench in db['envs']:
					continue
				cur = db['envs'][bench]
				fields = key.split(":")
				for f in range(0, len(fields)):
					field = fields[f].strip()
					if f < len(fields) - 1:
						if field not in cur:
							cur[field] = dict()
						cur = cur[field]
					else:
						cur[field] = env.values[key]

	# Generate the environment diffs
	db['env_diff_keys'] = dict()
	for bench in db['env_sets']:
		final_union = set()
		for report in db['env_sets'][bench]:
			diff = db['env_sets'][bench][0]['set'] ^ report['set']
			final_union = final_union | diff
		db['env_diff_keys'][bench] = sorted(dict(final_union).keys())

	# Sort the tests by name to avoid ever-changing layouts
	db["tests"] = np.sort(db["tests"])

	# Support creating new URLs
	if commit_url is not None:
		db["commit_url"] = commit_url

	# Generate the report
	html_template="""
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

	<%! import os %>
	<%! import cgi %>
	<%! import html %>
	<%! import base64 %>
	<%! from ezbench.smartezbench import compute_perf_difference, BenchSubTestType, Event, EventRenderingChange %>

	<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<title>${title}</title>
			<meta http-equiv="content-type" content="text/html; charset=utf-8" />
			<style>
				body { font-size: 10pt; }
				table { font-size: 8pt; }

				/* http://martinivanov.net/2011/09/26/css3-treevew-no-javascript/ */
				.css-treeview input + label + ul
				{
					display: none;
				}
				.css-treeview input:checked + label + ul
				{
					display: block;
				}
				.css-treeview input
				{
					position: absolute;
					opacity: 0;
				}
				.css-treeview label,
				.css-treeview label::before
				{
					cursor: pointer;
				}
				.css-treeview input:disabled + label
				{
					cursor: default;
					opacity: .6;
				}
				table{
					border-collapse:collapse;
				}
				table td{
					padding:5px; border:#4e95f4 1px solid;
				}
				table tr:nth-child(odd){
					background: #b8d1f3;
				}
				table tr:nth-child(even){
					background: #dae5f4;
				}

				.tree_node:hover {
					cursor: pointer;
					text-decoration: underline;
				}

				.close_button {
					color: black;
					background-color: grey;
					cursor:pointer;
				}
				.close_button:hover {
					text-decoration:underline;
				}

				/* env_dump*/
				.ed_c {
					color: black;
				}
				.ed_nc {
					color: gray;
				}
			</style>
			<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
			<script type="text/javascript">
				google.charts.load('current', {'packages':['corechart', 'table']});
				google.charts.setOnLoadCallback(drawTrend);
				google.charts.setOnLoadCallback(drawDetails);
				google.charts.setOnLoadCallback(drawTable);

				var currentCommit = "${default_commit}";

				function showColumn(dataTable, chart, activColumns, series, col, show) {
					seriesCol = 0
					for (i = 0; i < col; i++)
						if (dataTable.getColumnType(i) == 'number')
							seriesCol++
					if (!show) {
						activColumns[col] = {
							label: dataTable.getColumnLabel(col),
							type: dataTable.getColumnType(col),
							calc: function () {
								return null;
							}
						};
						series[seriesCol].color = '#CCCCCC';
					}
					else {
						activColumns[col] = col;
						series[seriesCol].color = null;
					}
				}

				function showAllColumns(dataTable, chart, activColumns, series, show) {
					for (var i = 1; i < dataTable.getNumberOfColumns(); i++) {
						if (dataTable.getColumnType(i) == 'number')
							showColumn(dataTable, chart, activColumns, series, i, show)
					}
				}

				function handle_selection(sel, dataTable, series, activColumns, chart) {
					var col = sel[0].column;

					var allActive = true;
					for (var i = 1; i < dataTable.getNumberOfColumns(); i++) {
						if (dataTable.getColumnType(i) == 'number' && activColumns[i] != i) {
							allActive = false;
						}
					}
					if (activColumns[col] == col) {
						// The clicked columns is active
						if (allActive) {
							showAllColumns(dataTable, chart, activColumns, series, false);
							showColumn(dataTable, chart, activColumns, series, col, true);
						} else {
							showColumn(dataTable, chart, activColumns, series, col, false);
						}
					}
					else {
						// The clicked columns is inactive, show it
						showColumn(dataTable, chart, activColumns, series, col, true);
					}

					var activeColsCount = 0;
					for (var i = 1; i < dataTable.getNumberOfColumns(); i++) {
						if (dataTable.getColumnType(i) == 'number' && activColumns[i] == i) {
							activeColsCount++;
						}
					}
					if (activeColsCount == 0)
						showAllColumns(dataTable, chart, activColumns, series, true);

					return activeColsCount
				}

				function adjustChartSize(id_chart, reportsCount, testCount) {
					var size = 75 + reportsCount * (25 + (testCount * 8));
					id_chart.style.height = size + "px";
					id_chart.style.width = "100%";
				}

				function trendUnselect() {
					trend_chart.setSelection(null);
				}

				function drawTrend() {
					var dataTable = new google.visualization.DataTable();

	<%def name="tooltip_commit_table(commit)">\\
	<h3>${db["commits"][commit]['commit'].full_name.replace('"', '&quot;')} <span class='close_button' onclick='javascript:trendUnselect();' title='Close this tooltip'>[X]</span></h3>\\
	<h4>Commit\\
	% if 'commit_url' in db:
	(<a href='${db["commit_url"].format(commit=commit)}' target='_blank'>URL</a>)\\
	% endif
	</h4><table>\\
	<tr><td><b>Author:</b></td><td>${cgi.escape(db["commits"][commit]['commit'].author)}</td></tr>\\
	<tr><td><b>Commit date:</b></td><td>${db["commits"][commit]['commit'].commit_date}</td></tr>\\
	<tr><td><b>Build exit code:</b></td><td bgcolor='${db["commits"][commit]['build_color']}'><center>${db["commits"][commit]['build_error']}</center></td></tr>\\
	% if len(db["commits"][commit]['commit'].bugs) > 0:
	<tr><td><b>Referenced bugs</b></td><td><ul>\\
	% for bug in db["commits"][commit]['commit'].bugs:
	<li><a href='${bug.replace('"', '&quot;')}' target='_blank'>${bug.replace('"', '&quot;')}</a></li>\\
	% endfor
	</ul></td></tr>\\
	% endif
	% if hasattr(db["commits"][commit]['commit'], "annotation_long"):
	<tr><td><b>Annotation:</b></td><td>${cgi.escape(db["commits"][commit]['commit'].annotation_long)}</td></tr>\\
	% endif
	</table>\\
	</%def>

	% if len(db['reports']) > 1:
					dataTable.addColumn('string', 'Commit');
					dataTable.addColumn({type: 'string', role: 'tooltip', p: { html: true }});
					% for report in db["reports"]:
					dataTable.addColumn('number', '${report.name}');
					% endfor
					dataTable.addRows([
					% for commit in db["commits"]:
						['${commit}', "${tooltip_commit_table(commit)}<h4>Perf</h4><table>\\
	% for report in db["reports"]:
	% if report.name in db["commits"][commit]['reports']:
	<tr><td><b>${report.name}:</b></td><td>${db["commits"][commit]['reports'][report.name]["average"]} ${output_unit}</td></tr>\\
	% endif
	% endfor
	</table><p></p>"\\
							% for report in db["reports"]:
								% if report.name in db["commits"][commit]['reports']:
	, ${db["commits"][commit]['reports'][report.name]["average"]}\\
								% else:
	, null\\
								% endif
							% endfor
	],
					% endfor
					]);
	% else:
					<%
						report = db["reports"][0].name
					%>
					dataTable.addColumn('string', 'Commits');
					dataTable.addColumn({type: 'string', role:'annotation'});
					% for test in db["tests"]:
					dataTable.addColumn('number', '${test}');
					dataTable.addColumn({type: 'string', role: 'tooltip', p: { html: true }});
					% endfor

					dataTable.addRows([
					% for commit in db["commits"]:
	["${commit}"\\
						% if hasattr(db["commits"][commit]['commit'], 'annotation'):
	, "${db["commits"][commit]['commit'].annotation}"\\
						%else:
	, null\\
						% endif
						% for test in db["tests"]:
							% if test in db["commits"][commit]['reports'][report]:
	<%
		result = db["commits"][commit]['reports'][report][test]
		diff_target = "{0:.2f}".format(result.diff_target)
	%>\\
	, ${diff_target}, "${tooltip_commit_table(commit)}<h4>Perf</h4><table><tr><td><b>Test</b></td><td>${test}</td></tr><tr><td><b>Target</b></td><td>${db['targets'][test]} ${output_unit} (${diff_target}%)</td></tr><tr><td><b>Raw value</b></td><td>${result.average_raw} ${result.unit} +/- ${result.margin_str}% (n=${len(result.result())})</td></tr><tr><td><b>Converted value</b></td><td>${result.average} ${output_unit} +/- ${result.margin_str}% (n=${len(result.result())})</td></tr></table><br/>"\\
								% else:
	, null, "${test}"\\
								% endif
						% endfor
	],
					% endfor
					]);
	% endif

					var activColumns = [];
					var series = {};
					for (var i = 0; i < dataTable.getNumberOfColumns(); i++) {
						activColumns.push(i);
						if (i > 0) {
							series[i - 1] = {};
						}
					}

					var options = {
					chart: {
							title: 'Performance trend across multiple commits'
						},
					% if len(db['reports']) > 1:
						focusTarget: 'category',
						vAxis: {title: 'Average result (${output_unit})'},
					% else:
						annotations: {style: 'line', textStyle: {fontSize: 12}},
						vAxis: {title: '% of target (%)'},
					% endif
						legend: { position: 'top', textStyle: {fontSize: 12}, maxLines: 3},
						tooltip: {trigger: 'selection', isHtml: true},
						crosshair: { trigger: 'both' },
						hAxis: {title: 'Commits', slantedText: true, slantedTextAngle: 45},
						series: series,
						chartArea: {left:"6%", width:"95%"}
					};

					trend_chart = new google.visualization.LineChart(document.getElementById('trends_chart'));
					trend_chart.draw(dataTable, options);

					google.visualization.events.addListener(trend_chart, 'select', function () {
						var sel = trend_chart.getSelection();
						// See https://developers.google.com/chart/interactive/docs/reference#visgetselection
						if (sel.length > 0 && typeof sel[0].row === 'object') {
							handle_selection(sel, dataTable, series, activColumns, trend_chart)

							// Redraw the chart with the masked columns
							var view = new google.visualization.DataView(dataTable);
							view.setColumns(activColumns);
							trend_chart.draw(view, options);
						}

						if (sel.length > 0 && typeof sel[0].row === 'number') {
							// update the other graph if there were changes
							var commit = dataTable.getValue(sel[0].row, 0)
							if (commit != currentCommit) {
								currentCommit = commit;
								drawDetails();
								drawTable();
							}
						}

						if (sel.length == 0) {
							trend_chart.setSelection(null);
						}
					});
				}

			function drawDetails() {
					var dataTable = new google.visualization.DataTable();
					dataTable.addColumn('string', 'Report');
					dataTable.addColumn('number', 'Average');
					dataTable.addColumn({type: 'string', role: 'tooltip', p: { html: true }});
					% for test in db["tests"]:
					dataTable.addColumn('number', '${test}');
					dataTable.addColumn({type: 'string', role: 'tooltip', p: { html: true }});
					% endfor

					% for commit in db["commits"]:
					if (currentCommit == "${commit}") {
						dataTable.addRows([
						% for report in db["reports"]:
							% if report.name in db["commits"][commit]['reports']:
								["${report.name}", ${db["commits"][commit]['reports'][report.name]["average"]}, "<h3>${report.name} - Average</h3><p>\\
								% for r in db["reports"]:
	<%
										if not r.name in db["commits"][commit]:
											continue
										diff = compute_perf_difference(output_unit, db["commits"][commit]['reports'][report.name]["average"], db["commits"][commit]['reports'][r.name]["average"])
										diff = float("{0:.2f}".format(diff))
										btag = btagend = ""
										if r.name == report.name:
											btag="<b>"
											btagend="</b>"
									%>\\
	${btag}${r.name}: ${db["commits"][commit]['reports'][r.name]["average"]} ${output_unit} (${diff}%)${btagend}<br/>\\
								% endfor
	</p>"\\
								% for test in db["tests"]:
									% if report.name in db["commits"][commit]['reports'] and test in db["commits"][commit]['reports'][report.name]:
	, ${db["commits"][commit]['reports'][report.name][test].average}, "<h3>${report.name} - ${test}</h3><p>\\
										% for r in db["reports"]:
	<%
												if not r.name in db["commits"][commit]['reports'] or test not in db["commits"][commit]['reports'][r.name]:
													continue
												diff = compute_perf_difference(output_unit, db["commits"][commit]['reports'][report.name][test].average, db["commits"][commit]['reports'][r.name][test].average)
												diff = float("{0:.2f}".format(diff))
												btag = btagend = ""
												if r.name == report.name:
													btag="<b>"
													btagend="</b>"
											%>\\
	${btag}${r.name}: ${db["commits"][commit]['reports'][r.name][test].average} ${output_unit} (${diff}%)${btagend}<br/>\\
										% endfor
	</p>"\\
							% else:
	, null, "${test}"\\
							% endif
							% endfor
	],
							% endif
						% endfor
						]);
					}
					% endfor

					// adjust the size of the chart to fit the data
					adjustChartSize(details_chart, dataTable.getNumberOfRows(), Math.floor(dataTable.getNumberOfColumns() / 2));

					var activColumns = [];
					var series = {};
					for (var i = 0; i < dataTable.getNumberOfColumns(); i++) {
						activColumns.push(i);
						if (i > 0) {
							series[i - 1] = {};
						}
					}
					series[0] = {type: 'line'};


					var options = {
						title : 'Performance of commit ' + currentCommit,
						legend: {textStyle: {fontSize: 12}},
						tooltip: {trigger: 'focus', isHtml: true},
						vAxis: {title: 'Reports', textStyle: {fontSize: 12}},
						hAxis: {title: 'Average result (${output_unit})', textStyle: {fontSize: 12}},
						seriesType: 'bars',
						orientation: 'vertical',
						series: series
					};

					var chart = new google.visualization.ComboChart(document.getElementById('details_chart'));
					chart.draw(dataTable, options);

					google.visualization.events.addListener(chart, 'select', function () {
						var sel = chart.getSelection();
						// See https://developers.google.com/chart/interactive/docs/reference#visgetselection
						if (sel.length > 0 && typeof sel[0].row === 'object') {
							activeCols = handle_selection(sel, dataTable, series, activColumns, chart)

							// reduce the size of the chart to fit the data
							adjustChartSize(details_chart, dataTable.getNumberOfRows(), activeCols);

							// Redraw the chart with the masked columns
							var view = new google.visualization.DataView(dataTable);
							view.setColumns(activColumns);
							chart.draw(view, options);
						}

						if (sel.length == 0) {
							chart.setSelection(null);
						}
					});
				}

				function drawTable() {
					% if len(db["reports"]) > 1:
						var dataTable = new google.visualization.DataTable();
						dataTable.addColumn('string', 'Test');
						dataTable.addColumn('string', 'Report 1');
						dataTable.addColumn('string', 'Report 2');
						dataTable.addColumn('number', '%');
						dataTable.addColumn('string', 'Comments');

						% for commit in db["commits"]:
						if (currentCommit == "${commit}") {
							% for report1 in db["reports"]:
								% if report1.name in db["commits"][commit]['reports']:
									% for report2 in db["reports"]:
										% if report2.name != report1.name and report2.name in db["commits"][commit]['reports']:
											% for test in db["tests"]:
												% if (test in db["commits"][commit]['reports'][report1.name] and test in db["commits"][commit]['reports'][report2.name]):
												<%
													r1 = db["commits"][commit]['reports'][report1.name][test]
													r2 = db["commits"][commit]['reports'][report2.name][test]
													perf_diff = compute_perf_difference(r1.unit, r1.average_raw, r2.average_raw)
													perf_diff = "{0:.2f}".format(perf_diff)
												%>
							dataTable.addRows([['${test}', '${report1.name}', '${report2.name}', ${perf_diff}, "${r1.average_raw} => ${r2.average_raw} ${r1.unit}"]])
												% endif
											% endfor
										% endif
									% endfor
								% endif
							% endfor
						}
						%endfor
					% else:
						var dataTable = new google.visualization.DataTable();
						dataTable.addColumn('string', 'Test');
						dataTable.addColumn('string', 'Report');
						dataTable.addColumn('number', '% of target');
						dataTable.addColumn('string', 'Comments');

						% for commit in db["commits"]:
						if (currentCommit == "${commit}") {
							% for report1 in db["reports"]:
								% if report1.name in db["commits"][commit]['reports']:
									% for test in db["tests"]:
										% if (test in db["commits"][commit]['reports'][report1.name] and test in db["targets"]):
										<%
											r1 = db["commits"][commit]['reports'][report1.name][test]
											perf_diff = compute_perf_difference(r1.unit, db["targets_raw"][test], r1.average_raw)
											perf_diff = "{0:.2f}".format(perf_diff)
										%>\\
dataTable.addRows([['${test}', '${report1.name}', ${perf_diff}, "${r1.average_raw}(${report1.name}) => ${db["targets_raw"][test]}(target) ${r1.unit}"]])
										% endif
									% endfor
								% endif
							% endfor
						}
						%endfor
					% endif
					var chart = new google.visualization.Table(document.getElementById('details_table'));
					chart.draw(dataTable, {showRowNumber: true, width: '100%', height: '100%'});
				}
			</script>
		</head>

		<body>
			<h1>${title}</h1>

			% if 'reference_name' in db:
				<p>With targets taken from: ${db['reference_name']}</p>
			% endif

			<h2>Trends</h2>

			<center><div id="trends_chart" style="width: 100%; height: 500px;"></div></center>

			<h2>Details</h2>

			<center><div id="details_chart" style="width: 100%; height: 500px;"></div></center>

			<center><div id="details_table" style="width: 100%; height: 500px;"></div></center>

			<h2>Events</h2>
			<%include file="events.mako"/>

			<h2>Tests</h2>

				% for test in db["tests"]:
					<h3>${test}</h3>

					<div class="css-treeview">
						<%def name="outputtreenode(node, id, label, attr = '')">
							<li><input type="checkbox" id="${id}" ${attr}/><label class="tree_node" for="${id}">+${label}</label><ul>
								<table>
								% for child in sorted(node):
									% if type(node[child]) is str:
										<tr><td>${child}</td><td>${node[child]}</td></tr>
									% endif
								% endfor
								</table>
								% for child in sorted(node):
									% if type(node[child]) is not str:
										${outputtreenode(node[child], "{}.{}".format(id, child.replace(' ', '_')), child, '')}
									% endif
								% endfor
							</ul></li>
						</%def>

						<ul>
							${outputtreenode(db["envs"][test], test + "_envroot", "Environment", 'checked="checked"')}
						</ul>
					</div>

					<table>
						<tr>
							<th>Key</th>
							% for env_set in db["env_sets"][test]:
							<%
								users = ""
								for user in env_set['users']:
									if len(users) > 0:
										users += "<br/>"
									users += "{}.{}#{}".format(user['log_folder'], user['commit'].label, user['run'])
							%>\\
							<th>${users}</th>
							% endfor
						</tr>
						% for key in db["env_diff_keys"][test]:
						<tr>
							<td>${key}</td>
							<%
								prev = None
							%>
							% for env_set in db["env_sets"][test]:
							<%
								if key in dict(env_set['set']):
									env_val = dict(env_set['set'])[key]
								else:
									env_val = "MISSING"

								if prev is None or env_val != prev:
									css_class = "ed_c"
								else:
									css_class = "ed_nc"
								prev = env_val
							%>
							<td class="${css_class}">${env_val}</td>
							% endfor
						</tr>
						% endfor
					</table>

					<h3>Metrics</h3>
					% for report in db["reports"]:
						<h4>${report.name}</h4>
						<table>
							<tr><th>Metric Name</th>
							% if 'reference' in db:
							<th>Target</th>
							%endif
							% for commit in db["commits"]:
							% if report.name in db["commits"][commit]['reports'] and test in db["commits"][commit]['reports'][report.name]:
							<th>${commit}</th>
							% endif
							% endfor

							% for metric in sorted(db["metrics"][test]):
<% ref_metric = None %>\\
								<tr><td>${metric}</td>
								% if 'reference' in db:
									% if (test in db["target_result"] and (metric in db["target_result"][test].results(BenchSubTestType.METRIC))):
									<%
										ref_metric = db["target_result"][test].result(metric)
									%>
									<td>${str(ref_metric)}</td>
									% else:
									<td>N/A</td>
									% endif
								%endif
								% for commit in db["commits"]:
									% if report.name in db["commits"][commit]['reports'] and test in db["commits"][commit]['reports'][report.name]:
									% if metric in db["commits"][commit]['reports'][report.name][test].results(BenchSubTestType.METRIC):
									<%
										m = db["commits"][commit]['reports'][report.name][test].metrics[metric]
									%>
										<td>${str(m)}\\
										% if ref_metric is not None:
<%
											diff = compute_perf_difference(unit, ref_metric.mean(), m.mean())
										%>${" ({:.2f}%)".format(diff)}\\
										% endif
</td>
									% else:
										<td>N/A</td>
									% endif
									% endif
								% endfor
								</tr>
							% endfor
						</table>
					%endfor

					<h3>Results</h3>
					<%
						unit_results = []
						stats_status = dict()
						statuses = set()

						target_changes = dict()
						changes = set()

						# Add the target report in the list of reports if it
						# contains tests for this test
						target_result = None
						if 'target_result' in db and test in db['target_result']:
							subtests = db['target_result'][test].results(BenchSubTestType.SUBTEST_STRING)
							if len(subtests) > 0:
								target_result = db['target_result'][test]
								target_result.name = "Target"
								stats_status[target_result.name] = dict()
								unit_results.append(target_result)

						for report in db['reports']:
							for commit in report.commits:
								for result in commit.results.values():
									if result.test.full_name != test:
										continue
									if result.test_type != "unit":
										continue
									result.name = "{}.{}".format(report.name, commit.label)
									stats_status[result.name] = dict()
									target_changes[result.name] = dict()
									unit_results.append(result)

						all_tests = set()
						for result in unit_results:
							all_tests |= set(result.results(BenchSubTestType.SUBTEST_STRING))
							result.unit_results = dict()

						unit_tests = set()
						for test in all_tests:
							value = None
							for result in unit_results:
								if "<" in test: # Hide subsubtests
									continue
								subtest = result.result(test)
								if subtest is None or len(subtest) == 0:
									status = "missing"
								else:
									if len(subtest.to_set()) == 1:
										status = subtest[0]
									else:
										status = "unstable"
								result.unit_results[test] = status

								# Collect stats on all the status
								if status not in stats_status[result.name]:
									stats_status[result.name][status] = 0
									statuses |= set([status])
								stats_status[result.name][status] += 1

								if value == None and status != "missing":
									value = status
									continue
								if value != status and status != "missing":
									unit_tests |= set([test])

								if (target_result is None or result == target_result or
									target_result.unit_results[test] == status):
									continue

								change = "{} -> {}".format(target_result.unit_results[test],
								                           status)
								if change not in target_changes[result.name]:
									target_changes[result.name][change] = 0
									changes |= set([change])
								target_changes[result.name][change] += 1

						all_tests = []
					%>

					% if len(unit_results) > 0:
					<h4>Unit tests</h4>
						<h5>Basic stats</h4>
						<table>
						<tr><th>Version</th>
						% for status in sorted(statuses):
							<th>${status}</th>
						% endfor
						</tr>

						% for result in stats_status:
						<tr><td>${result}</td>\\
							% for status in sorted(statuses):
								% if status in stats_status[result]:
<td>${stats_status[result][status]}</td>\\
								% else:
<td>0</td>\\
								% endif
							% endfor
						</tr>
						% endfor
						</table>

						% if 'target_result' in db and test in db['target_result']:
						<h5>Status changes</h4>
						<table>
						<tr><th>Version</th>
						% for result in target_changes:
							<th>${result}</th>
						% endfor
						</tr>

						% for change in sorted(changes):
						<tr><td>${change}</td>
							% for result in target_changes:
								% if change in target_changes[result]:
							<td>${target_changes[result][change]}</td>
								% else:
							<td>0</td>
								% endif
							% endfor
						</tr>
						% endfor
						</table>
						% endif

						<h5>Changes</h4>
						<div style='overflow:auto; width:100%;max-height:1000px;'>
						<table>
							<tr><th>test name (${len(unit_tests)})</th>
							% for result in unit_results:
							<th>${result.name}</th>
							% endfor
							</tr>

							% for test in sorted(unit_tests):
	<tr><td>${html.escape(test)}</td>\\
								% for result in unit_results:
	<td>${result.unit_results[test]}</td>\\
								% endfor
	</tr>
							% endfor
						</table>
						</div>
					% endif
				% endfor
		</body>

	</html>
	"""
	# Check that we have commits
	if len(db["commits"]) == 0 and verbose:
		print("No commits were found, cancelling...")
	else:
		# Create the html file
		if verbose:
			print("Generating the HTML")

		if title is None:
			report_names = [r.name for r in reports]
			title = "Performance report on the runs named '{run_name}'".format(run_name=report_names)

		templatedir = os.path.join(ezbench_dir, 'templates')

		lookup = TemplateLookup(directories=[templatedir])
		template = Template(html_template, lookup=lookup)
		try:
			html = template.render(ezbench_dir=ezbench_dir, title=title, db=db,
			                       output=output, output_unit=output_unit,
					       default_commit=list(db["commits"])[-1],
					       events=db["events"], embed=embed)
		except:
			html = exceptions.html_error_template().render().decode()

		with open(output, 'w') as f:
			f.write(html)
			if verbose:
				print("Output HTML generated at: {}".format(output))
Exemplo n.º 47
0
 def wrapped(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except:
         return exceptions.html_error_template().render()
Exemplo n.º 48
0
def begin():

    templateFilename = 'rrdAlarmConfigurator.tmpl'
    # Imports for mako
    try:
        from mako.template import Template
        from mako.runtime import Context
        from mako.lookup import TemplateLookup
        from mako import exceptions
    except:
        ntop.printHTMLHeader('ntop Python Configuration Error', 1, 0)
        ntop.sendString(
            "<b><center><font color=red>Please install <A HREF=http://www.makotemplates.org/>Mako</A> template engine</font> (sudo easy_install Mako)</center></b>"
        )
        ntop.printHTMLFooter()
        return

    # Fix encoding
    #reload(sys)
    #sys.setdefaultencoding("latin1")

    rows = []
    pathRRDFiles = os.path.join(ntop.getDBPath(), 'rrd/')
    nameFileConfig = 'rrdAlarmConfig.txt'  #default nameFileConfig
    pathTempFile = ntop.getSpoolPath() + os.sep

    form = cgi.FieldStorage()
    #get  from the url the parameter configfile that contains the
    #path+filename of the configfile to read
    jsonPathRRD = form.getvalue('pathRRDS')

    help = form.getvalue('help')
    documentRoot = os.getenv('DOCUMENT_ROOT', '.')

    if jsonPathRRD:
        #a request from the autocomplete script, return a json string with the matching files names
        ntop.sendHTTPHeader(1)
        ntop.sendString(jsonListFileInPath(jsonPathRRD, pathRRDFiles))
        return
    elif help == 'true':
        #show help page
        templateFilename = 'rrdAlarmConfiguratorHelp.tmpl'
        ntop.printHTMLHeader('RRD Alarm Configurator Help', 1, 0)
    else:
        #normal operation
        requestFileConfig = form.getvalue('configFile')
        if requestFileConfig is not None:
            nameFileConfig = requestFileConfig

        #get all the scripts in the scripts directory
        listScripts = readScriptsDir(
            os.path.join(documentRoot, 'python/rrdalarm/scripts/'))

        ntop.printHTMLHeader('RRD Alarm Configurator', 1, 0)

        file_name = os.path.join(pathTempFile, nameFileConfig)

        try:
            configFile = open(file_name, 'rt')

            for line in configFile:
                line = line.rstrip()  #drop the \n at the end
                if len(line) > 0 and line[0] != '#':
                    rows.append(line.split('\t'))

            configFile.close()

        except IOError:
            try:
                open(file_name, 'w').close()  # Create an empty file if missing
            except:
                pass
            print >> sys.stderr, "RRDAlarm: empty configFile created " + file_name
        except:
            print >> sys.stderr, "RRDAlarm: Error reading configFile " + os.path.join(
                pathTempFile, nameFileConfig)
            raise
        #the elaboration will continue but no data will be displayed.

        #if requestFileConfig is not None:                 #if the nameFileConfig was specified by user show error
        #try:
        #    open(os.path.join(pathTempFile,nameFileConfig), 'w')
        #except:
        #    raise
        #else:
        #nameFileConfig='rrdAlarmConfig.txt'
        #ntop.sendString(exceptions.html_error_template().render())

    try:
        #pprint.pprint(listAllDirs(pathRRDFiles+'rrd/'), sys.stderr)
        basedir = os.path.join(documentRoot, 'python/templates')
        mylookup = TemplateLookup(directories=[basedir])
        myTemplate = mylookup.get_template(templateFilename)
        buf = StringIO()
        ctx = None
        if (help == 'true'):  #show help page
            ctx = Context(buf)
        else:
            ctx = Context(buf,
                          configRows=rows,
                          tempFilePath=pathTempFile,
                          nameFileConfig=nameFileConfig,
                          listScripts=listScripts,
                          pathRRDFiles=pathRRDFiles
                          )  #, rrdDirs=listAllDirs(pathRRDFiles+'rrd/')

        myTemplate.render_context(ctx)
        ntop.sendString(buf.getvalue())
    except:
        ntop.sendString(exceptions.html_error_template().render())

    ntop.printHTMLFooter()
Exemplo n.º 49
0
def serve(environ, start_response):
    """serves requests using the WSGI callable interface."""
    fieldstorage = cgi.FieldStorage(fp=environ['wsgi.input'],
                                    environ=environ,
                                    keep_blank_values=True)
    # S13: template context has to be a bit more sensible.
    d = dict()
    d["args"] = dict([(k, getfield(fieldstorage[k])) for k in fieldstorage])
    d["env"] = environ
    if os.path.isfile(os.path.join(root, "extra.py")):
        import extra  # S13: we want extra Python stuff available.
        d["extra"] = extra
    else:
        d["extra"] = None

    uri = environ.get('PATH_INFO', '/')
    if uri.endswith("/"):  # S13: use the index.html if no filename is given.
        uri += 'index.html'
    else:
        uri = re.sub(r'^/$', '/index.html', uri)

    if re.match(r'.*\.(html|json)$', uri):  # S13: accept JSON requests.
        try:
            template = lookup.get_template(uri)
            if uri.endswith(".json"):  # S13: include the corrent type.
                start_response("200 OK",
                               [('Content-Type', 'application/json')])
            else:
                start_response("200 OK", [('Content-Type', 'text/html')])
            return [template.render(**d)]
        except exceptions.TopLevelLookupException:
            start_response("404 Not Found", [])
            return ["Cant find template '%s'" % uri]
        except:
            if error_style == 'text':
                start_response("200 OK", [('Content-Type', 'text/plain')])
                return [exceptions.text_error_template().render()]
            else:
                start_response("200 OK", [('Content-Type', 'text/html')])
                return [exceptions.html_error_template().render()]
    elif re.match(r'.*\.(%s)$' % "|".join(dis_ext), uri):
        start_response("404 Not Found", [("Content-Type", "text/plain")])
        return ["File not found."]
    else:
        u = re.sub(r'^\/+', '', uri)
        filename = os.path.join(root, u)
        # S13: if the request is a file, proceed as usual.
        if os.path.isfile(filename):
            # S13: use mimetypes.guess_type.
            mt = guess_type(filename)[0]
            if mt: start_response("200 OK", [('Content-Type', mt)])
            else: start_response("200 OK", [])
            return [file(filename).read()]
        # S13: if the request is a directory, redirect to the index.
        elif os.path.isdir(filename):
            uri += "/"
            start_response("302 Moved Permanently", [("Location", uri)])
            return []
        # S13: if it's neither, respond with a 404.
        else:
            start_response("404 Not Found", [("Content-Type", "text/plain")])
            return ["File not found."]
Exemplo n.º 50
0
                                #failed ipv6 adn ipv4 conversion
                                print >> sys.stderr, "fastbit.py: IMPOSSIBLE TO FORMAT value: " + str(
                                    x[j]) + " TO IP ADDR"

                    #x[1]=socket.inet_ntop(socket.AF_INET,struct.pack('>L',x[1]))
                rows = res['values']
            #pprint.pprint(res, sys.stderr)

    try:
        basedir = os.path.join(documentRoot, 'python/templates')
        mylookup = TemplateLookup(directories=[basedir])
        myTemplate = mylookup.get_template(templateFilename)
        buf = StringIO()

        ctx = Context(buf,
                      columns=cols,
                      values=rows,
                      queryPar=queryPar,
                      queryHistory=history['history'])

        myTemplate.render_context(ctx)
        ntop.sendString(buf.getvalue())
    except:
        ntop.sendString(exceptions.html_error_template().render())

    ntop.printHTMLFooter()


'''HERE STARTS THE SCRIPTS'''

begin()
Exemplo n.º 51
0
	def GET(self,node_url_name):
		try: 
			return template_desktop.get_template('new_topic.html').render(node=self.node,site=config.site)
		except:
			return exceptions.html_error_template().render()
Exemplo n.º 52
0
def begin():
    rrdFilesPath = os.path.join(ntop.getDBPath(), 'rrd')
    ''' Check the existence of the rrd database files that maintain history of this script'''
    def updateDBS(time, resultsValue, durationValue):
        #global rrdFilesPath
        rrdAlarmDbPath = os.path.join(rrdFilesPath, 'rrdAlarm/')
        if not os.path.exists(rrdAlarmDbPath) or not os.path.isdir(
                rrdAlarmDbPath):
            os.mkdir(rrdAlarmDbPath)

        nameDuration = "duration"
        nameResults = "results"
        #nameDuration="duration.rrd"
        #nameResults="results.rrd"
        #archives=['RRA:AVERAGE:0.5:1:60',
        #         'RRA:AVERAGE:0.5:30:6']#1 hour and 3 hours data
        try:
            #rrdtool.update(os.path.join(rrdAlarmDbPath,nameDuration), 'N:'+str(durationValue))
            ntop.updateRRDGauge(rrdAlarmDbPath, nameDuration, durationValue, 0)
        #print>>sys.stderr, 'Updating'+str(durationValue)
        except:
            print >> sys.stderr, 'RRDAlarm: Error Updating rrdDB ' + nameDuration
            '''
            dataSources=['DS:duration:GAUGE:120:0:U']
            rrdtool.create(rrdAlarmDbPath+nameDuration, '--start', str(time), '--step', str(60), dataSources[0], archives[0], archives[1] )
            rrdtool.update(rrdAlarmDbPath+nameDuration,'N:'+str(durationValue))'''

        try:
            #rrdtool.update(os.path.join(rrdAlarmDbPath,nameResults), 'N:'+str(resultsValue))
            ntop.updateRRDGauge(rrdAlarmDbPath, nameResults, resultsValue, 0)
        except:
            print >> sys.stderr, 'RRDAlarm: Error Updating rrdDB ' + nameResults
            '''
            dataSources=['DS:results:GAUGE:120:0:U']
            rrdtool.create(rrdAlarmDbPath+nameResults, '--start', str(time), '--step', str(60), dataSources[0], archives[0], archives[1] )
            rrdtool.update(rrdAlarmDbPath+nameResults, 'N:'+str(resultsValue))'''

    '''Function that must be called as a new thread so its execution time can 
       be controlled and limited by the main thread.
       all the controls on the rrds are executed by this function
       '''

    def controlT():
        #global rrdFilesPath
        ntopSpoolPath = ntop.getSpoolPath()
        nameFileConfig = 'rrdAlarmConfig.txt'
        tempFileName = 'rrdAlarmStart.tmp'

        configuration = None
        TWODECIMAL = decimal.Decimal(10)**-2
        timeStart = time.time()

        alarmsFired = 0
        checkedFiles = 0
        fmt = '%a, %d %b %Y %H:%M:%S'  #format of the time showed
        form = cgi.FieldStorage()
        #get the parameter passed via the url
        noHTML = bool(form.getvalue('noHTML'))
        configFile = form.getvalue('configFile')
        if configFile and len(configFile) > 0:
            nameFileConfig = str(configFile)
        try:
            tempFile = open(os.path.join(ntopSpoolPath, tempFileName), 'r')

            configuration = pickle.load(tempFile)
            tempFile.close()

            if configuration and (
                    timeStart < float(configuration['timeStart']) + float(60)):
                ntop.sendHTTPHeader(1)
                ntop.printHTMLHeader('RRD Alarm Called too early!', 1, 0)
                ntop.sendString(
                    "Wait at least a minute. Last Time started: %s" %
                    time.strftime(fmt,
                                  time.localtime(configuration['timeStart'])))
                ntop.printHTMLFooter()
                return 0  #exit because the script was started less than one minute ago

            else:
                configuration['timeStart'] = timeStart

        except IOError:  #the tempFile does not exist or some other problem
            print >> sys.stderr, 'RRDAlarm: IOError while accessing tempfile ' + tempFileName
            configuration = createNewConfiguration(
                rrdFilesPath, os.path.join(ntopSpoolPath, nameFileConfig),
                timeStart)

        except pickle.PickleError, pickle.UnpicklingError:
            print >> sys.stderr, "RRDAlarm: Problems during the UnPickling load, tempFile Delete..."
            os.remove(os.path.join(ntopSpoolPath, tempFileName))
            return -1

        if configuration['lastModified'] != os.path.getmtime(
                os.path.join(ntopSpoolPath, nameFileConfig)):
            #if the configuration file has been changed the temp file must be rebuild and so the configuration dictionary
            configuration = createNewConfiguration(
                rrdFilesPath, os.path.join(ntopSpoolPath, nameFileConfig),
                timeStart)

        listRows = []
        parameterDic = {
        }  #for each parameter inserted as a key a tupla will be assigned 'parameter':{actionToPerform, text}

        for threshold in configuration[
                'listThresholds']:  #for all the thresholds
            listFiles = threshold.getListFilename()
            checkedFiles = checkedFiles + len(listFiles)

            for fileN in listFiles:  #for all the filenames referred by the threshold
                #rrd_argv=[fileN,'AVERAGE', '--start', threshold.getStartTime(), '--end', threshold.getStartTime()]
                #Return :((start, end, step), (name1, name2, ...), [(data1, data2, ..), ...])

                #print>>sys.stderr, '\nLOOK for the parameters '+str(threshold.getStartTime())+' '+str(threshold.getEndTime())+' '+str(fileN)
                rrdObj = ((0, 0, 0), (), [])  #empty object
                try:
                    #rrdObj=rrdtool.fetch(fileN, 'AVERAGE', '--start', threshold.getStartTime(), '--end', threshold.getEndTime())
                    rrdObj = ntop.rrd_fetch(fileN, 'AVERAGE',
                                            threshold.getStartTime(),
                                            threshold.getEndTime())
                except Exception as e:
                    print >> sys.stderr, 'start.py PyRRDTool exception ' + str(
                        e)

                step = rrdObj[0][2]
                start = float(rrdObj[0][0])
                end = float(rrdObj[0][1])

                valueDataTuple = rrdObj[2]
                #for all the values returned check the threshold (from the end) if alarm has to be fired
                i = len(valueDataTuple)

                while i > 0:
                    #for value in valueDataTuple:
                    timeA = (step * i) + start
                    i = i - 1
                    value = valueDataTuple[i]

                    if threshold.checkIfFire(
                            value[0]):  #controls if the threshold was exceeded
                        notFired = False
                        alarmsFired = alarmsFired + 1
                        listRows.append(
                            (threshold.getUniqueId(), fileN, value[0],
                             threshold.getType(), threshold.getValue(),
                             time.strftime(fmt, time.localtime(timeA)), timeA,
                             threshold.getActionToPerform(), 'ALARM FIRED'))
                        strAlarm = '<ALARM>\nID: %i FILENAME: %s\nVALUE: %s TYPE: %s THRESHOLD VALUE: %f\n LOCALTIME: %s START: %s END: %s\n</ALARM>\n' % (
                            threshold.getUniqueId(), fileN, value[0],
                            threshold.getType(), threshold.getValue(),
                            time.strftime(fmt, time.localtime(timeA)),
                            threshold.getStartTime(), threshold.getEndTime())
                        if parameterDic.has_key(
                                threshold.getActionParameter()):
                            parameterDic[threshold.getActionParameter(
                            )]['textAlarm'] = parameterDic[
                                threshold.getActionParameter(
                                )]['textAlarm'] + strAlarm
                        else:
                            parameterDic[threshold.getActionParameter()] = {
                                'actionToPerform':
                                threshold.getActionToPerform(),
                                'textAlarm': strAlarm
                            }
                        break
                        #print>>sys.stderr, 'The type of the threshold was misconfigured!'
                else:
                    #no alarm was fired
                    listRows.append(
                        (threshold.getUniqueId(), fileN, '-',
                         threshold.getType(), threshold.getValue(),
                         time.strftime(fmt,
                                       time.localtime(end)), end, 'None', 'OK')
                    )  #at least one alarm was fired, adding the action to perform, text and parameter to the global dictionary

        saveTempFile(
            configuration, os.path.join(ntopSpoolPath, tempFileName)
        )  #save all the informations usefull for future calling of this script

        documentRoot = os.getenv('DOCUMENT_ROOT', '.')
        performActions(
            parameterDic, documentRoot
        )  #performs all the actions for the alarms fired (if any)

        duration = decimal.Decimal(str(time.time() - timeStart)).quantize(
            TWODECIMAL, decimal.ROUND_CEILING)
        updateDBS(
            int(timeStart), alarmsFired, duration
        )  #update rrds that trace the history of this script  TODO check where to place this

        ntop.sendHTTPHeader(1)

        if not noHTML:  #if this parameter was passed and if true send just the empty http response
            ntop.printHTMLHeader('RRD Alarm Report', 1, 0)
            try:
                basedir = os.path.join(documentRoot, 'python/templates')
                mylookup = TemplateLookup(directories=[basedir])
                myTemplate = mylookup.get_template('rrdAlarmStart.tmpl')
                buf = StringIO()

                ctx = Context(buf,
                              listRows=listRows,
                              duration=duration,
                              checkedFiles=checkedFiles,
                              alarmsFired=alarmsFired)
                myTemplate.render_context(ctx)
                ntop.sendString(buf.getvalue())
            except:
                ntop.sendString(exceptions.html_error_template().render())
                return 1

            #finally:
            #condition.notify()
            ntop.printHTMLFooter()

        print >> sys.stderr, '%s CET Exit rrdAlarm' % time.strftime(
            fmt, time.localtime(time.time()))
        return 0
Exemplo n.º 53
0
  def render(self, request, template, params={}, def_name=None):
    '''Runs a template and returns a string.  Normally, you probably want to call render_to_response instead
       because it gives a full HttpResponse or Http404.
       
       This method raises a mako.exceptions.TopLevelLookupException if the template is not found.
    
       The method throws two signals: 
         1. dmp_signal_pre_render_template: you can (optionally) return a new Mako Template object from a receiver to replace
            the normal template object that is used for the render operation.
         2. dmp_signal_post_render_template: you can (optionally) return a string to replace the string from the normal
            template object render.
    
       @request  The request context from Django.  If this is None, 1) any TEMPLATE_CONTEXT_PROCESSORS defined in your settings
                 file will be ignored and 2) DMP signals will not be sent, but the template will otherwise render fine.
       @template The template file path to render.  This is relative to the app_path/controller_TEMPLATES_DIR/ directory.
                 For example, to render app_path/templates/page1, set template="page1.html", assuming you have
                 set up the variables as described in the documentation above.
       @params   A dictionary of name=value variables to send to the template page.
       @def_name Limits output to a specific top-level Mako <%block> or <%def> section within the template.
                 If the section is a <%def>, it must have no parameters.  For example, def_name="foo" will call
                 <%block name="foo"></%block> or <%def name="foo()"></def> within the template.
                 The block/def must be defined in the exact template.  DMP does not support calling defs from 
                 super-templates.
    '''
    # must convert the request context to a real dict to use the ** below
    context_dict = {}
    context_dict['request'] = request
    context_dict['settings'] = settings
    try:
      context_dict['STATIC_URL'] = settings.STATIC_URL  # this is used so much in templates, it's useful to have it always available
    except AttributeError:
      pass
    context = Context(params) if request == None else RequestContext(request, params)  # Django's RequestContext automatically runs all the TEMPLATE_CONTEXT_PROCESSORS and populates with variables
    with context.bind_template(self):
      for d in context:
        context_dict.update(d)

    # get the template
    template_obj = self.get_template(template)

    # send the pre-render signal
    if get_setting('SIGNALS', False) and request != None:
      for receiver, ret_template_obj in signals.dmp_signal_pre_render_template.send(sender=self, request=request, context=context, template=template_obj):
        if ret_template_obj != None:  # changes the template object to the received
          template_obj = ret_template_obj

    # do we need to limit down to a specific def?
    # this only finds within the exact template (won't go up the inheritance tree)
    # I wish I could make it do so, but can't figure this out
    render_obj = template_obj  
    if def_name:  # do we need to limit to just a def?
      render_obj = template_obj.get_def(def_name)

    # PRIMARY FUNCTION: render the template
    log.debug('DMP :: rendering template %s' % template_obj.filename)
    if settings.DEBUG:
      try:
        content = render_obj.render_unicode(**context_dict)
      except:
        content = html_error_template().render_unicode()
    else:  # this is outside the above "try" loop because in non-DEBUG mode, we want to let the exception throw out of here (without having to re-raise it)
      content = render_obj.render_unicode(**context_dict)
      
    # send the post-render signal
    if get_setting('SIGNALS', False) and request != None:
      for receiver, ret_content in signals.dmp_signal_post_render_template.send(sender=self, request=request, context=context, template=template_obj, content=content):
        if ret_content != None:
          content = ret_content  # sets it to the last non-None return in the signal receiver chain
          
    # return
    return content
Exemplo n.º 54
0
def begin():
    # Imports for mako
    try:
        from mako.template import Template
        from mako.runtime import Context
        from mako.lookup import TemplateLookup
        from mako import exceptions
    except:
        ntop.printHTMLHeader('ntop Python Configuration Error', 1, 1)
        ntop.sendString(
            "<b><center><font color=red>Please install <A HREF=http://www.makotemplates.org/>Mako</A> template engine</font><p></b><br>(1) 'sudo yum install python-setuptools' (on RedHat-like systems)<br>(2) 'sudo easy_install Mako'</font></center>"
        )
        ntop.printHTMLFooter()
        return
    # Fix encoding
    #reload(sys)
    #sys.setdefaultencoding("latin1")

    templateFilename = 'ipPortQuery.tmpl'

    #fb_DB = '/tmp/'               #ntop.getPreference ("fastbitDBPath");    #default location of the fastbit DB
    databasePath = ntop.getPreference("fastbit.DBPath")
    #default location of the fastbit DB

    if databasePath is None or databasePath == '':
        ntop.printHTMLHeader('ntop Fastbit Configuration Error', 1, 1)
        ntop.sendString(
            "<b><center><font color=red>Please set fastbit.DBPath ntop preferences from <i>Admin/Configure/Preferences</i> menu (es: fastbit.DBPath=/tmp/)</b></font></center>"
        )
        ntop.printHTMLFooter()
        return

    #pathFastBit=os.path.join(databasePath,'fastbit'+os.path.sep)

    form = cgi.FieldStorage()
    #get from the url the parameter configfile that contains the
    #path+filename of the configfile to read
    fromAuto = form.getvalue('fromAuto')

    if fromAuto:
        #print>>sys.stderr, "AJAX REQUEST FOR PARTITION IN  "+databasePath+" "+fromAuto
        jsonString = expandFrom(fromAuto, os.path.join(databasePath, ""))
        ntop.sendHTTPHeader(12)
        ntop.sendString(jsonString)
        return

    documentRoot = os.getenv('DOCUMENT_ROOT', '.')

    selectArg = 'PROTOCOL,IPV4_SRC_ADDR,L4_SRC_PORT,IPV4_DST_ADDR,L4_DST_PORT,IN_BYTES,IN_PKTS'
    fromArg = form.getvalue('partition')

    ipSrc = form.getvalue('ipSrc')
    ipDst = form.getvalue('ipDst')

    portSrc = form.getvalue('portSrc')
    portDst = form.getvalue('portDst')

    limit = int(form.getvalue('limit', 100))

    if limit < 0:
        limit = 0
    res = None  #variable to store the results of the query
    ntop.printHTMLHeader('IP-Port Query', 1, 0)
    #regex to check passed parameters
    ipV4Type = re.compile(
        r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
    )
    portType = re.compile(r'\d{1,5}')

    #pprint.pprint(ipV4Type.match(str(ipSrc)), sys.stderr)
    formatErrorString = ''
    #without the partition do nothing
    if fromArg:
        whereArg = '1=1'  #to avoid leaving the where clause empty
        if ipSrc:
            #print>>sys.stderr, 'ECCO '+str(ipV4Type.match(ipSrc))
            if ipV4Type.match(ipSrc):
                whereArg = whereArg + ' AND IPV4_SRC_ADDR=' + str(
                    ipToNumber(ipSrc))
            else:
                formatErrorString = formatErrorString + 'Source ip format invalid! ipv4 format required. '
        if portSrc:
            if portType.match(portSrc):
                whereArg = whereArg + ' AND L4_SRC_PORT=' + str(portSrc)
            else:
                formatErrorString = formatErrorString + 'Source Port format invalid! Number required. '
        if ipDst:
            if ipV4Type.match(ipDst):
                whereArg = whereArg + ' AND IPV4_DST_ADDR=' + str(
                    ipToNumber(ipDst))
            else:
                formatErrorString = formatErrorString + 'Destination ip format invalid! ipv4 format required. '
        if portDst:
            if portType.match(portDst):
                whereArg = whereArg + ' AND L4_DST_PORT=' + str(portDst)
            else:
                formatErrorString = formatErrorString + 'Destination Port format invalid! Number required. '
        try:
            #pipe = subprocess.Popen (['ntop.getPreference ("fastbit.fbquery")', "-c", selectArg, "-d", fromArg, "-q", whereArg, "-P", "-L", limit],
            #print>>sys.stderr, "Query passed: SELECT %s FROM %s WHERE %s LIMIT %i" %(selectArg,os.path.join(databasePath, fromArg),  whereArg, limit)
            if formatErrorString == '':
                res = fastbit.query(os.path.join(databasePath, fromArg),
                                    selectArg, whereArg, limit)
            else:
                print >> sys.stderr, 'ipPortQuery: ERROR ' + formatErrorString
                ntop.sendString('<center><font color=red>%s</font></center>' %
                                formatErrorString)
            #print>>sys.stderr, 'Number of records: %i' % len(res['values'])
        except:
            print >> sys.stderr, 'ERROR Executing query: ' + (
                "SELECT %s FROM %s WHERE %s LIMIT %i" %
                (selectArg, os.path.join(databasePath,
                                         fromArg), whereArg, limit))
            res = {}
        if res is not None and 'columns' in res and 'values' in res:

            toFormat = getAddrCols(
                selectArg)  #get a list of addr column numbers

            for x in res['values']:
                x[0] = getNameByProto(x[0])  #format protocol number to text
                for j in toFormat:
                    #for every number in the list format as an IP ADDR
                    ipStr = numberToIp(x[j])
                    x[j] = '<a href="/%s.html" class="tooltip">%s</a>' % (
                        ipStr, ipStr
                    )  #format ip number to string ip and create a link to ntop host page
    '''else:
        print>>sys.stderr, 'ipPortQuery: ERROR partition required'
        ntop.sendString('<center><font color=red>Partition field required!</font></center>')'''
    #pprint.pprint(res, sys.stderr)
    #if res is not None:
    #    res['columns']=['Protocol', 'IP Source Addr', 'IP Dest. Addr', 'Source Port', 'Dest. Port', 'Bytes Rcvd', 'Packets Rcvd']

    try:
        basedir = os.path.join(documentRoot, 'python/templates')
        mylookup = TemplateLookup(directories=[basedir])
        myTemplate = mylookup.get_template(templateFilename)
        buf = StringIO()

        ctx = Context(buf, results=res)

        myTemplate.render_context(ctx)
        ntop.sendString(buf.getvalue())
    except:
        ntop.sendString(exceptions.html_error_template().render())

    ntop.printHTMLFooter()
Exemplo n.º 55
0
    def _post(self, f):
        """<comment-ja>
        HTTP Method別処理を実行した後の処理を行います。
          1. HTTP Responseコード 4xx or 5xx についての処理
          2. テンプレート処理
          3. HTTP Headerをセット
        @param f: 実行結果
        @type f: bool or web.HTTPError
        @return: HTTP Response
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """
        try:  # default view set.
            self.view.me = self.me
            if self.is_standalone() is True:
                self.view.standalone = self.input.standalone
        except AttributeError:
            pass

        # Content-Type
        # TODO
        # "Resource interpreted as script but transferred with MIME type text/plain."
        #if self.__template__.media == 'part':
        #    web.header('Content-Type', 'text/html; charset=utf-8', True)
        #elif self.__template__.media == 'input':
        #    web.header('Content-Type', 'text/html; charset=utf-8', True)
        #elif self.__template__.media == 'html':
        #    web.header('Content-Type', 'text/html; charset=utf-8', True)
        #elif self.__template__.media == 'json':
        #    web.header('Content-Type', 'application/json; charset=utf-8', True)
        #elif self.__template__.media == 'xml':`
        #    web.header('Content-Type', 'text/xml; charset=utf-8', True)
        #elif self.__template__.media == 'gif':
        #    web.header('Content-Type', 'Content-type: image/gif', True)
        #elif self.__template__.media == 'png':
        #    web.header('Content-Type', 'Content-type: image/png', True)
        #elif self.__template__.media == 'jpg':
        #    web.header('Content-Type', 'Content-type: image/jpeg', True)
        #elif self.__template__.media == 'jpeg':
        #    web.header('Content-Type', 'Content-type: image/jpeg', True)
        #elif self.__template__.media == 'ico':
        #    web.header('Content-Type', 'Content-type: image/x-icon', True)
        #elif self.__template__.media == 'css':
        #    web.header('Content-Type', 'Content-type: text/css; charset=utf-8', True)
        #elif self.__template__.media == 'js':
        #    web.header('Content-Type', 'Content-type: text/javascript; charset=utf-8', True)
        #elif self.__template__.media == 'jar':
        #    web.header('Content-Type', 'Content-type: application/java-archiver', True)
        #else:
        #    web.header('Content-Type', 'text/plain; charset=utf-8', True)

        # HTTP Header - No Cache
        now = datetime.now()
        web.lastmodified(now)
        web.httpdate(now)
        # TODO
        #web.expire(0)
        #web.header('Expires', web.httpdate(datetime(1970,1,1)))
        #web.header('Last-Modified',  web.httpdate(datetime(1970,1,1)))
        #web.header('ETag', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
        web.header('Cache-Control', 'no-cache,private')
        web.header('Pragma', 'no-cache')

        ##
        if self.download.type == OUTPUT_TYPE_NORMAL:  # Nomal
            if self.me is None:
                self.view.USER_DATE_FORMAT = DEFAULT_LANGS[
                    self.languages[0]]['DATE_FORMAT']
            else:
                self.view.USER_DATE_FORMAT = DEFAULT_LANGS[
                    self.me.languages]['DATE_FORMAT']

            if isinstance(f, web.HTTPError) is True:
                self.logger.info('HTTP Response - %s Headers-->%s' %
                                 (f.__class__.__name__, web.ctx.headers))
                raise f

            if f is True:
                path = '%s/%s.%s' % (
                    self.__template__.dir,
                    self.__template__.file,
                    self.__template__.media,
                )
            else:
                #if self.__template__.media in ERROR_MEDIA:
                #    path = 'error/error.%s' % self.__template__.media
                #else:
                #    path = 'error/error.%s' % DEFAULT_MEDIA
                self.logger.info('"gadget" execution error - %s' %
                                 str(self.__class__))
                raise web.internalerror("Execution errors")

            self.logger.debug('lang=%s %s : template=%s' \
                              % (','.join(self.languages), str(self), path))

            try:
                _r = mako_render(self._,
                                 path,
                                 title=self._('Karesansui'),
                                 view=self.view)
                return _r
            except:
                if web.wsgi._is_dev_mode() is True and os.environ.has_key(
                        'FCGI') is False:
                    return exceptions.html_error_template().render(full=True)
                else:
                    self.logger.error(
                        '"mako render" execution error - path=%s' % path)
                    self.logger_trace.error(traceback.format_exc())
                    raise web.internalerror("Execution errors")

        elif self.download.type == OUTPUT_TYPE_FILE:  # file download
            if self.download.file is None or os.path.isfile(
                    self.download.file) is False:
                self.logger.error(
                    'Could not find files to download. - path=%s' %
                    self.download.file)
                return web.internalerror()
            web.header('Content-Type', 'Content-type: image/png', True)
            fp = open(self.download.file, "rb")
            try:
                _r = fp.read()
            finally:
                fp.close()

            if self.download.once is True and os.path.isfile(
                    self.download.file) is True:
                os.unlink(self.download.file)

            return _r

        elif self.download.type == OUTPUT_TYPE_STREAM:  # io stream download
            if self.download.stream is None:
                self.logger.error("Data stream has not been set.")
            return self.download.stream

        else:
            self.logger.error(
                'Was specified assuming no output type. - type=%d' %
                self.download.type)
            raise web.internalerror()
Exemplo n.º 56
0
def main():
    try:
        this_user = uqauth.get_user()
    except uqauth.Redirected:
        return

    form = cgi.FieldStorage(keep_blank_values=True)
    is_admin = (this_user in ADMINS and 'noadmin' not in form)

    user = form.getvalue('user', this_user)
    if user != this_user and not this_user in ADMINS:
        print UNAUTHORISED.format(this_user)
        return

    message = None
    if (is_admin and os.environ.get('REQUEST_METHOD') == 'POST' and
            'action' in form):
        before_submissions = get_submissions(user)
        if form.getvalue('action') == 'allow_late':
            action = (lambda tutorial: not is_submitted_on_time(tutorial, before_submissions)
                      and support.set_allow_late(user, tutorial, this_user, True))
        elif form.getvalue('action') == 'disallow_late':
            action = (lambda tutorial: not is_submitted_on_time(tutorial, before_submissions)
                      and support.set_allow_late(user, tutorial, this_user, False))
        else:
            action = None
            message = ('alert-danger', 'Action unknown or not specified.')

        problems = form.getlist('problem')
        if action and problems:
            count = sum(map(action, problems))
            if count == 0:
                message = ('alert-warning', '0 entries modified.')
            elif count < len(problems):
                message = ('alert-success', '{} entries modified, {} unchanged.'
                           .format(count, len(problems)-count))
            else:
                message = ('alert-success', '{} entries modified.'
                           .format(count))
        elif action and not problems:
            message = ('alert-warning', 'No problems specified.')

    user_info = (support.get_user(user) or
                 support.User(user, 'UNKNOWN', 'UNKNOWN', 'not_enrolled'))
    submissions = get_submissions(user)

    print "Content-Type: text/html\n"

    data = {
        'name': 'Change Me',
        'id': 'changeme',
        'user': user_info,
        'openIndex': -1,
        'is_admin': is_admin,
        'message': message,
    };

    # Count the number of on-time or accepted-late submissions the student has made.
    data['mark'] = 0
    data['late'] = 0

    data['groups'] = []
    group = None

    for tutorial, submission in submissions:
        if not group or tutorial.problem_set_name != group['slug']:
            group = {
                'slug': tutorial.problem_set_name,
                'title': tutorial.problem_set_name.replace('_', ' '),
                'due': (tutorial.due + TZ_DELTA).strftime(DATE_FORMAT),
                'problems': [],
                'mark': 0,
                'late': 0,
            }

            data['groups'].append(group)

        tut = process_tutorial(tutorial, submission)
        group['problems'].append(tut)

        if (submission is not None and submission.date is not None and
                (submission.date <= tutorial.due or submission.allow_late)):
            group['mark'] += 1
            data['mark'] += 1
        elif submission is not None and submission.date is not None:
            group['late'] += 1
            data['late'] += 1

    data['total'] = len(submissions)

    try:
        print(Template(filename="./templates/progress.html").render(**data))
    except:
        print(exceptions.html_error_template().render())
Exemplo n.º 57
0
 def index(self):
     try:
         tmpl = lookup.get_template("index.html")
         return tmpl.render(pgname="main", jobs=self.ctrl.getJobs())
     except:
         return exceptions.html_error_template().render()
Exemplo n.º 58
0
def main():
    try:
        user = uqauth.get_user()
    except uqauth.Redirected:
        return

    # two sets of possibilities
    # (1) the help list (need to be a tutor)
    #       (a) viewing the list
    #       (b) submitting the form (eg, deleting an entry)
    # (2) asking for help (as a student)
    #       (a) for the first time (no message query param)
    #       (b) submitting the form
    form = cgi.FieldStorage(keep_blank_values=True)

    view_list = user in TUTORS and 'noadmin' not in form
    if view_list and user not in TUTORS:
        print UNAUTHORISED.format(user)
        return

    print "Content-Type: text/html\n"

    data = {}

    if view_list:
        template = Template(filename="./templates/help_list.html")

        # make our changes if the form was submitted
        if os.environ.get('REQUEST_METHOD') == 'POST' and 'mark_as' in form:
            user = form.getvalue('username')
            status = form.getvalue('mark_as')

            support.set_help_request_status(user, status)

        help_list = sorted(support.get_help_list(),
                           key=lambda hi: hi.timestamp)

        data['date_offset'] = TZ_DELTA  # hacky workaround
        data['help_list'] = help_list
        data['open_index'] = -1
    else:
        template = Template(filename='./templates/help_request.html')

        data['user'] = user

        message = form.getvalue('message')
        traceback = form.getvalue('traceback')

        if os.environ.get('REQUEST_METHOD') == 'POST' \
                and 'message' is not None:
            info = uqauth.get_user_info()
            name = info.get('name', '')

            support.log_help_request(user, name, message, traceback)

            data['alert_message'] = ('alert-success',
                                     'Your help request has been logged')

        pending_request = support.get_pending_help_request(user)
        data['has_pending'] = pending_request is not None
        data['is_ignored'] = False
        data['queue_position'] = None

        if pending_request is not None:
            message = pending_request.message
            traceback = pending_request.traceback

            data['is_ignored'] = \
                pending_request.status == support.HELP_STATUS_IGNORED
            data['queue_position'] = support.get_position_in_help_queue(user)

        data['message'] = message
        data['traceback'] = traceback

    try:
        print(template.render(**data))
    except:
        print(exceptions.html_error_template().render())
Exemplo n.º 59
0
Arquivo: view.py Projeto: duhan2/web
 def createList_px(self, data_opl, listform):
     #-------------------------------------------------------
     #Parameter für history Funktion
     if listform == "startseite":
         template_o = self.lookup_o.get_template('Startseite.tpl')
         markup_s = template_o.render(data_o=data_opl, listform=listform)
         return markup_s
     elif listform == "pflegemitarbeiterdaten":
         try:
             template_o = self.lookup_o.get_template(
                 'Pflege_Mitarbeiterdaten.tpl')
             markup_s = template_o.render(data_o=data_opl,
                                          listform=listform)
             return markup_s
         except:
             return exceptions.html_error_template().render()
     elif listform == "pflegeweiterbildungen":
         try:
             template_o = self.lookup_o.get_template(
                 'Pflege_Weiterbildungen.tpl')
             markup_s = template_o.render(data_o=data_opl,
                                          listform=listform)
             return markup_s
         except:
             return exceptions.html_error_template().render()
     elif listform == "sichtweisemitarbeiter":
         try:
             template_o = self.lookup_o.get_template(
                 'Sichtweise_Mitarbeiter.tpl')
             markup_s = template_o.render(data_o=data_opl,
                                          listform=listform)
             return markup_s
         except:
             return exceptions.html_error_template().render()
     elif listform == "sichtweiseweiterbildungen":
         try:
             template_o = self.lookup_o.get_template(
                 'Sichtweise_Weiterbildungen.tpl')
             markup_s = template_o.render(data_o=data_opl,
                                          listform=listform)
             return markup_s
         except:
             return exceptions.html_error_template().render()
     elif listform == "mitarbeiter":
         try:
             template_o = self.lookup_o.get_template('Mitarbeiter.tpl')
             markup_s = template_o.render(data_o=data_opl,
                                          listform=listform)
             return markup_s
         except:
             return exceptions.html_error_template().render()
     elif listform == "weiterbildungen":
         try:
             template_o = self.lookup_o.get_template('Weiterbildungen.tpl')
             markup_s = template_o.render(data_o=data_opl,
                                          listform=listform)
             return markup_s
         except:
             return exceptions.html_error_template().render()
     elif listform == "zertifikate":
         try:
             template_o = self.lookup_o.get_template('Zertifikate.tpl')
             markup_s = template_o.render(data_o=data_opl,
                                          listform=listform)
             return markup_s
         except:
             return exceptions.html_error_template().render()
Exemplo n.º 60
0
 def output_errors(self, status_code, **kwargs):
     self.write(html_error_template().render())