Example #1
0
def css(color="default"):
    r"""
    Return the CSS header used by the FEMhub Online Lab.
    
    INPUT:
    
    
    -  ``color`` - string or pair of html colors, e.g.,
       'gmail' 'grey' ``('#ff0000', '#0000ff')``
    
    
    EXAMPLES::
    
        sage: import sagenb.notebook.css as c
        sage: type(c.css())
        <type 'str'>
    """
    # TODO: Implement a theming system, with a register.
    if color in ("default", "grey", "gmail", None):
        color1 = None
        color2 = None
    elif isinstance(color, (tuple, list)):
        color1, color2 = color
    else:
        raise ValueError, "unknown color scheme %s" % color

    main_css = template(os.path.join("css", "main.css"), color1=color1, color2=color2, color_theme=color)

    user_css_path = os.path.join(DOT_SAGENB, "notebook.css")
    user_css = ""
    if os.path.exists(user_css_path):
        user_css = "\n" + open(user_css_path).read()

    return main_css + user_css
Example #2
0
    def html(self, error_code=None, **kwargs):
        """
        Returns HTML and JavaScript for a reCAPTCHA challenge and
        response field.

        INPUT:

        - ``error_code`` - a string (default: None); an optional error
          code to embed in the HTML, giving feedback about the user's
          *previous* response
        
        - ``kwargs`` - a dictionary of extra keyword arguments

        OUTPUT:
        
        - a string; HTML and JavaScript to render the reCAPTCHA
          challenge

        TESTS::

            sage: from sagenb.flask_version import base # random output -- depends on warnings issued by other sage packages
            sage: app = base.create_app(tmp_dir(ext='.sagenb'))
            sage: ctx = app.app_context()
            sage: ctx.push()
            sage: nb = base.notebook
            sage: from sagenb.notebook.challenge import reCAPTCHAChallenge
            sage: chal = reCAPTCHAChallenge(nb.conf(), remote_ip = 'localhost')
            sage: chal.html()
            u'...recaptcha...'
            sage: chal.html('incorrect-captcha-sol')
            u'...incorrect-captcha-sol...'

        """
        error_param = ''
        if error_code:
            error_param = '&error=%s' % error_code

        template_dict = {
            'api_server': self.api_server,
            'public_key': self.public_key,
            'error_param': error_param,
            'lang': self.lang
        }

        return template(os.path.join('html', 'recaptcha.html'),
                        **template_dict)
Example #3
0
    def html(self, error_code = None, **kwargs):
        """
        Returns HTML and JavaScript for a reCAPTCHA challenge and
        response field.

        INPUT:

        - ``error_code`` - a string (default: None); an optional error
          code to embed in the HTML, giving feedback about the user's
          *previous* response
        
        - ``kwargs`` - a dictionary of extra keyword arguments

        OUTPUT:
        
        - a string; HTML and JavaScript to render the reCAPTCHA
          challenge

        TESTS::

            sage: from sagenb.flask_version import base # random output -- depends on warnings issued by other sage packages
            sage: app = base.create_app(tmp_dir(ext='.sagenb'))
            sage: ctx = app.app_context()
            sage: ctx.push()
            sage: nb = base.notebook
            sage: from sagenb.notebook.challenge import reCAPTCHAChallenge
            sage: chal = reCAPTCHAChallenge(nb.conf(), remote_ip = 'localhost')
            sage: chal.html()
            u'...recaptcha...'
            sage: chal.html('incorrect-captcha-sol')
            u'...incorrect-captcha-sol...'

        """
        error_param = ''
        if error_code:
            error_param = '&error=%s' % error_code

        template_dict = { 'api_server' : self.api_server,
                          'public_key' : self.public_key,
                          'error_param' : error_param,
                          'lang' : self.lang }

        return template(os.path.join('html', 'recaptcha.html'),
                        **template_dict)
Example #4
0
    def html(self, error_code=None, **kwargs):
        """
        Returns HTML and JavaScript for a reCAPTCHA challenge and
        response field.

        INPUT:

        - ``error_code`` - a string (default: None); an optional error
          code to embed in the HTML, giving feedback about the user's
          *previous* response
        
        - ``kwargs`` - a dictionary of extra keyword arguments

        OUTPUT:
        
        - a string; HTML and JavaScript to render the reCAPTCHA
          challenge

        TESTS::

            sage: from sagenb.notebook.challenge import reCAPTCHAChallenge
            sage: tmp = tmp_dir() + '.sagenb'
            sage: import sagenb.notebook.notebook as n
            sage: nb = n.Notebook(tmp)
            sage: chal = reCAPTCHAChallenge(nb.conf(), remote_ip = 'localhost')
            sage: chal.html()
            u'...recaptcha...'
            sage: chal.html('incorrect-captcha-sol')
            u'...incorrect-captcha-sol...'

        """
        error_param = ''
        if error_code:
            error_param = '&error=%s' % error_code

        template_dict = {
            'api_server': self.api_server,
            'public_key': self.public_key,
            'error_param': error_param,
            'lang': self.lang
        }

        return template(os.path.join('html', 'recaptcha.html'),
                        **template_dict)
Example #5
0
def css(color='default'):
    r"""
    Return the CSS header used by the Sage Notebook.
    
    INPUT:
    
    
    -  ``color`` - string or pair of html colors, e.g.,
       'gmail' 'grey' ``('#ff0000', '#0000ff')``
    
    
    EXAMPLES::
    
        sage: import sagenb.notebook.css as c
        sage: type(c.css()[0])
        <type 'str'>
    """
    # TODO: the color argument does nothing right now, since
    # the main.css file does not use it at all
    global _css_cache
    if _css_cache is None:
        # TODO: Implement a theming system, with a register.
        if color in ('default', 'grey', 'gmail', None):
            color1 = None
            color2 = None
        elif isinstance(color, (tuple, list)):
            color1, color2 = color
        else:
            raise ValueError, "unknown color scheme %s" % color

        main_css = template(os.path.join('css', 'main.css'),
                            color1=color1,
                            color2=color2,
                            color_theme=color)

        user_css_path = os.path.join(DOT_SAGENB, 'notebook.css')
        user_css = ''
        if os.path.exists(user_css_path):
            user_css = '\n' + open(user_css_path).read()

        data = main_css + user_css
        _css_cache = (data, sha1(data).hexdigest())
    return _css_cache
Example #6
0
    def html(self, error_code = None, **kwargs):
        """
        Returns HTML and JavaScript for a reCAPTCHA challenge and
        response field.

        INPUT:

        - ``error_code`` - a string (default: None); an optional error
          code to embed in the HTML, giving feedback about the user's
          *previous* response
        
        - ``kwargs`` - a dictionary of extra keyword arguments

        OUTPUT:
        
        - a string; HTML and JavaScript to render the reCAPTCHA
          challenge

        TESTS::

            sage: from sagenb.notebook.challenge import reCAPTCHAChallenge
            sage: tmp = tmp_dir() + '.sagenb'
            sage: import sagenb.notebook.notebook as n
            sage: nb = n.Notebook(tmp)
            sage: chal = reCAPTCHAChallenge(nb.conf(), remote_ip = 'localhost')
            sage: chal.html()
            u'...recaptcha...'
            sage: chal.html('incorrect-captcha-sol')
            u'...incorrect-captcha-sol...'

        """
        error_param = ''
        if error_code:
            error_param = '&error=%s' % error_code

        template_dict = { 'api_server' : self.api_server,
                          'public_key' : self.public_key,
                          'error_param' : error_param,
                          'lang' : self.lang }

        return template(os.path.join('html', 'recaptcha.html'),
                        **template_dict)
Example #7
0
def css(color='default'):
    r"""
    Return the CSS header used by the Sage Notebook.
    
    INPUT:
    
    
    -  ``color`` - string or pair of html colors, e.g.,
       'gmail' 'grey' ``('#ff0000', '#0000ff')``
    
    
    EXAMPLES::
    
        sage: import sagenb.notebook.css as c
        sage: type(c.css()[0])
        <type 'str'>
    """
    # TODO: the color argument does nothing right now, since 
    # the main.css file does not use it at all
    global _css_cache
    if _css_cache is None:
        # TODO: Implement a theming system, with a register.
        if color in ('default', 'grey', 'gmail', None):
            color1 = None
            color2 = None
        elif isinstance(color, (tuple,list)):
            color1, color2 = color
        else:
            raise ValueError, "unknown color scheme %s"%color

        main_css = template(os.path.join('css', 'main.css'),
                            color1 = color1, color2 = color2,
                            color_theme = color)

        user_css_path = os.path.join(DOT_SAGENB, 'notebook.css')
        user_css = ''
        if os.path.exists(user_css_path):
            user_css = '\n' + open(user_css_path).read()

        data = main_css + user_css
        _css_cache = (data, sha1(data).hexdigest())
    return _css_cache
Example #8
0
    def generate_report(self, result):
        """
        Return a HTML report with the results of all cases and tests.
        """
        # Jinja template dictionary.
        template_dict = {}

        template_dict['title'] = jinja2.escape(self.title)
        template_dict['description'] = jinja2.escape(self.description)
        template_dict['sagenb_version'] = SAGENB_VERSION
        template_dict['environment'] = self.extra_args.get('environment')
        template_dict['start_time'] = str(self.start_time)[:19]
        template_dict['stop_time'] = str(self.stop_time)[:19]
        template_dict['elapsed_time'] = self.elapsed_time
        template_dict['pass_total'] = result.success_count
        template_dict['fail_total'] = result.failure_count
        template_dict['error_total'] = result.error_count
        template_dict['count_total'] = result.total_count

        rows = []
        sorted_result = self.sort_result(result.result)

        # Iterate over cases.
        for i, (case_type, case_results) in enumerate(sorted_result):
            # Stats for this case.
            passes = 0
            failures = 0
            errors = 0
            for status, test_case, output, trace in case_results:
                if status == _TestResult.PASS:
                    passes += 1
                elif status == _TestResult.FAIL:
                    failures += 1
                else:
                    errors += 1

            # Case description.
            if case_type.__module__ == '__main__':
                name = case_type.__name__
            else:
                name = '%s.%s' % (case_type.__module__, case_type.__name__)
            doc = case_type.__doc__ and case_type.__doc__.split('\n')[0] or ''
            desc = jinja2.escape(doc and '%s: %s' % (name, doc) or name)

            case_id = name.replace('.', '-') + '_%d' % i
            case_class = failures > 0 and 'case_fail' or errors > 0 and 'case_error' or 'case_pass'
            count = passes + failures + errors

            rows.append(REPORT_CASE_TMPL.render(locals()))

            # Iterate over this case's tests.
            for j, (status, test_case, output, trace) in enumerate(case_results):
                self.report_for_one_test(rows, case_id, j, status,
                                         test_case, output, trace)

        template_dict['test_cases_and_tests'] = '\n'.join(rows)

        # Make the report self-contained.
        stylesheet = template(os.path.join('css', 'test_report.css'))
        template_dict['stylesheet'] = '<style type="text/css"><!--\n' + stylesheet + '\n--></style>'
        template_dict['stylesheet'] += IE_STYLE_FIX.render()

        jquery = open(os.path.join(DATA,
                                   'jquery/jquery-1.3.2.min.js'), 'r').read()
        template_dict['javascript'] = '<script type="text/javascript">\n' + jquery + '\n</script>'
        return template(os.path.join('html', 'test_report.html'),
                        **template_dict)
Example #9
0
    def generate_report(self, result):
        """
        Return a HTML report with the results of all cases and tests.
        """
        # Jinja template dictionary.
        template_dict = {}

        template_dict['title'] = jinja2.escape(self.title)
        template_dict['description'] = jinja2.escape(self.description)
        template_dict['sagenb_version'] = SAGENB_VERSION
        template_dict['environment'] = self.extra_args.get('environment')
        template_dict['start_time'] = str(self.start_time)[:19]
        template_dict['stop_time'] = str(self.stop_time)[:19]
        template_dict['elapsed_time'] = self.elapsed_time
        template_dict['pass_total'] = result.success_count
        template_dict['fail_total'] = result.failure_count
        template_dict['error_total'] = result.error_count
        template_dict['count_total'] = result.total_count

        rows = []
        sorted_result = self.sort_result(result.result)

        # Iterate over cases.
        for i, (case_type, case_results) in enumerate(sorted_result):
            # Stats for this case.
            passes = 0
            failures = 0
            errors = 0
            for status, test_case, output, trace in case_results:
                if status == _TestResult.PASS:
                    passes += 1
                elif status == _TestResult.FAIL:
                    failures += 1
                else:
                    errors += 1

            # Case description.
            if case_type.__module__ == '__main__':
                name = case_type.__name__
            else:
                name = '%s.%s' % (case_type.__module__, case_type.__name__)
            doc = case_type.__doc__ and case_type.__doc__.split('\n')[0] or ''
            desc = jinja2.escape(doc and '%s: %s' % (name, doc) or name)

            case_id = name.replace('.', '-') + '_%d' % i
            case_class = failures > 0 and 'case_fail' or errors > 0 and 'case_error' or 'case_pass'
            count = passes + failures + errors

            rows.append(REPORT_CASE_TMPL.render(locals()))

            # Iterate over this case's tests.
            for j, (status, test_case, output,
                    trace) in enumerate(case_results):
                self.report_for_one_test(rows, case_id, j, status, test_case,
                                         output, trace)

        template_dict['test_cases_and_tests'] = '\n'.join(rows)

        # Make the report self-contained.
        stylesheet = template(os.path.join('css', 'test_report.css'))
        template_dict[
            'stylesheet'] = '<style type="text/css"><!--\n' + stylesheet + '\n--></style>'
        template_dict['stylesheet'] += IE_STYLE_FIX.render()

        jquery = open(os.path.join(DATA, 'jquery/jquery-1.3.2.min.js'),
                      'r').read()
        template_dict[
            'javascript'] = '<script type="text/javascript">\n' + jquery + '\n</script>'
        return template(os.path.join('html', 'test_report.html'),
                        **template_dict)