Example #1
0
    def __init__(self, *args, **kwds):
        self.startup_token = kwds.pop('startup_token', None)
        Flask.__init__(self, *args, **kwds)
        self.session_interface = OldSecureCookieSessionInterface()

        self.config['SESSION_COOKIE_HTTPONLY'] = False

        self.root_path = SAGENB_ROOT

        self.add_static_path('/css', os.path.join(DATA, "sage", "css"))
        self.add_static_path('/images', os.path.join(DATA, "sage", "images"))
        self.add_static_path('/javascript', DATA)
        self.add_static_path('/static', DATA)
        self.add_static_path('/java', DATA)
        self.add_static_path(
            '/java/jmol',
            os.path.join(os.environ["SAGE_ROOT"], "local", "share", "jmol"))
        self.add_static_path(
            '/jsmol',
            os.path.join(os.environ["SAGE_ROOT"], "local", "share", "jsmol"))
        self.add_static_path(
            '/jsmol/js',
            os.path.join(os.environ["SAGE_ROOT"], "local", "share", "jsmol",
                         "js"))
        self.add_static_path(
            '/j2s',
            os.path.join(os.environ["SAGE_ROOT"], "local", "share", "jsmol",
                         "j2s"))
        self.add_static_path(
            '/jsmol/j2s',
            os.path.join(os.environ["SAGE_ROOT"], "local", "share", "jsmol",
                         "j2s"))
        self.add_static_path(
            '/j2s/core',
            os.path.join(os.environ["SAGE_ROOT"], "local", "share", "jsmol",
                         "j2s", "core"))
        import mimetypes
        mimetypes.add_type('text/plain', '.jmol')

        #######
        # Doc #
        #######
        #These "should" be in doc.py
        DOC = os.path.join(SAGE_DOC, 'html', 'en')
        self.add_static_path('/pdf', os.path.join(SAGE_DOC, 'pdf'))
        self.add_static_path('/doc/static', DOC)

        # Template globals
        self.add_template_global(url_for)
        # Template filters
        self.add_template_filter(css_escape)
        self.add_template_filter(number_of_rows)
        self.add_template_filter(clean_name)
        self.add_template_filter(prettify_time_ago)
        self.add_template_filter(max)
        self.add_template_filter(lambda x: repr(unicode_str(x))[1:],
                                 name='repr_str')
        self.add_template_filter(dumps, 'tojson')
Example #2
0
def worksheet_new_cell_after(worksheet):
    """Add a new cell after a given cell."""
    id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_cell_after(id, input=input)
    worksheet.increase_state_number()

    from sagenb.notebook.twist import encode_list
    return encode_list([cell.id(), cell.html(div_wrap=False), id])
Example #3
0
def worksheet_new_cell_after(worksheet):
    """Add a new cell after a given cell."""
    id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_cell_after(id, input=input)
    worksheet.increase_state_number()
    
    from sagenb.notebook.twist import encode_list
    return encode_list([cell.id(), cell.html(div_wrap=False), id])
Example #4
0
def render_worksheet_list(args, pub, username):
    """
    Returns a rendered worksheet listing.

    INPUT:

    -  ``args`` - ctx.args where ctx is the dict passed
       into a resource's render method

    -  ``pub`` - boolean, True if this is a listing of
       public worksheets

    -  ``username`` - the user whose worksheets we are
       listing
       
    - ``kwds`` - additional info for template rendering

    OUTPUT:

    a string
    """

    from sagenb.notebook.notebook import sort_worksheet_list
    from sagenb.misc.misc import unicode_str, SAGE_VERSION

    typ = args['typ'] if 'typ' in args else 'active'
    search = unicode_str(args['search']) if 'search' in args else None
    sort = args['sort'] if 'sort' in args else 'last_edited'
    reverse = (args['reverse'] == 'True') if 'reverse' in args else False
    readonly = g.notebook.readonly_user(g.username)
    try:
        if not pub:
            worksheets = g.notebook.worksheet_list_for_user(username,
                                                            typ=typ,
                                                            sort=sort,
                                                            search=search,
                                                            reverse=reverse)
        else:
            worksheets = g.notebook.worksheet_list_for_public(username,
                                                              sort=sort,
                                                              search=search,
                                                              reverse=reverse)
    except ValueError as E:
        # for example, the sort key was not valid
        print "Error displaying worksheet listing: ", E
        return current_app.message(_("Error displaying worksheet listing."))

    worksheet_filenames = [x.filename() for x in worksheets]

    if pub and (not username or username == tuple([])):
        username = '******'

    accounts = g.notebook.user_manager().get_accounts()
    sage_version = SAGE_VERSION
    site_name = g.site_name
    return render_template('html/worksheet_listing.html', **locals())
Example #5
0
def worksheet_eval(worksheet):
    """
    Evaluate a worksheet cell.

    If the request is not authorized (the requester did not enter the
    correct password for the given worksheet), then the request to
    evaluate or introspect the cell is ignored.

    If the cell contains either 1 or 2 question marks at the end (not
    on a comment line), then this is interpreted as a request for
    either introspection to the documentation of the function, or the
    documentation of the function and the source code of the function
    respectively.
    """
    from sagenb.notebook.twist import encode_list
    from base import notebook_updates

    id = get_cell_id()
    input_text = unicode_str(request.values.get('input',
                                                '')).replace('\r\n',
                                                             '\n')  #DOS

    worksheet.increase_state_number()

    cell = worksheet.get_cell_with_id(id)
    cell.set_input_text(input_text)

    if request.values.get('save_only', '0') == '1':
        notebook_updates()
        return ''
    elif request.values.get('text_only', '0') == '1':
        notebook_updates()
        return encode_list([str(id), cell.html()])
    else:
        new_cell = int(request.values.get(
            'newcell', 0))  #wheter to insert a new cell or not

    cell.evaluate(username=g.username)

    if cell.is_last():
        new_cell = worksheet.append_new_cell()
        s = encode_list(
            [new_cell.id(), 'append_new_cell',
             new_cell.html(div_wrap=False)])
    elif new_cell:
        new_cell = worksheet.new_cell_after(id)
        s = encode_list([
            new_cell.id(), 'insert_cell',
            new_cell.html(div_wrap=False),
            str(id)
        ])
    else:
        s = encode_list([cell.next_id(), 'no_new_cell', str(id)])

    notebook_updates()
    return s
Example #6
0
def worksheet_new_text_cell_after(worksheet):
    """Add a new text cell after a given cell."""    
    id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_text_cell_after(id, input=input)
    worksheet.increase_state_number()
    
    from sagenb.notebook.twist import encode_list
    # XXX: Does editing correspond to TinyMCE?  If so, we should try
    # to centralize that code.
    return encode_list([cell.id(), cell.html(editing=True), id])
Example #7
0
def worksheet_new_text_cell_after(worksheet):
    """Add a new text cell after a given cell."""
    id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_text_cell_after(id, input=input)
    worksheet.increase_state_number()

    from sagenb.notebook.twist import encode_list
    # XXX: Does editing correspond to TinyMCE?  If so, we should try
    # to centralize that code.
    return encode_list([cell.id(), cell.html(editing=True), id])
Example #8
0
def worksheet_new_cell_before(worksheet):
    """Add a new cell before a given cell."""
    r = {}
    r["id"] = id = get_cell_id()
    input = unicode_str(request.values.get("input", ""))
    cell = worksheet.new_cell_before(id, input=input)
    worksheet.increase_state_number()

    r["new_id"] = cell.id()
    # r['new_html'] = cell.html(div_wrap=False)

    return encode_response(r)
    def worksheet_new_cell_before(self, worksheet):
        """Add a new cell before a given cell."""
        r = {}
        r['id'] =  id = self.get_cell_id()
        input = unicode_str(self.request_values.get('input', ''))
        cell = worksheet.new_cell_before(id, input=input)
        worksheet.increase_state_number()

        r['new_id'] = cell.id()
        #r['new_html'] = cell.html(div_wrap=False)

        return encode_response(r)
Example #10
0
def worksheet_new_cell_after(worksheet):
    """Add a new cell after a given cell."""
    r = {}
    r['id'] = id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_cell_after(id, input=input)
    worksheet.increase_state_number()

    r['new_id'] = cell.id()
    r['new_html'] = cell.html(div_wrap=True)

    return encode_response(r)
Example #11
0
def worksheet_new_cell_after(worksheet):
    """Add a new cell after a given cell."""
    r = {}
    r['id'] = id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_cell_after(id, input=input)
    worksheet.increase_state_number()

    r['new_id'] = cell.id()
    r['new_html'] = cell.html(div_wrap=True)

    return encode_response(r)
Example #12
0
def worksheet_new_cell_before(worksheet):
    """Add a new cell before a given cell."""
    r = {}
    r['id'] =  id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_cell_before(id, input=input)
    worksheet.increase_state_number()
    
    r['new_id'] = cell.id()
    r['new_html'] = cell.html(div_wrap=False)

    from sagenb.notebook.misc import encode_response
    return encode_response(r)
Example #13
0
def worksheet_new_cell_before(worksheet):
    """Add a new cell before a given cell."""
    r = {}
    r['id'] = id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_cell_before(id, input=input)
    worksheet.increase_state_number()

    r['new_id'] = cell.id()
    r['new_html'] = cell.html(div_wrap=False)

    from sagenb.notebook.misc import encode_response
    return encode_response(r)
Example #14
0
def worksheet_list():
    """
    Returns a worksheet listing.

    INPUT:

    -  ``args`` - ctx.args where ctx is the dict passed
       into a resource's render method

    -  ``pub`` - boolean, True if this is a listing of
       public worksheets

    -  ``username`` - the user whose worksheets we are
       listing

    OUTPUT:

    a string
    """
    
    from sagenb.notebook.notebook import sort_worksheet_list
    from sagenb.misc.misc import unicode_str, SAGE_VERSION
    from sagenb.notebook.misc import encode_response
    
    r = {}

    pub = 'pub' in request.args    
    readonly = g.notebook.readonly_user(g.username)
    typ = request.args['type'] if 'type' in request.args else 'active'
    search = unicode_str(request.args['search']) if 'search' in request.args else None
    sort = request.args['sort'] if 'sort' in request.args else 'last_edited'
    reverse = (request.args['reverse'] == 'True') if 'reverse' in request.args else False
    try:
        if not pub:
            r['worksheets'] = [x.basic() for x in g.notebook.worksheet_list_for_user(g.username, typ=typ, sort=sort, search=search, reverse=reverse)]
        else:
            r['worksheets'] = [x.basic() for x in g.notebook.worksheet_list_for_public(g.username, sort=sort, search=search, reverse=reverse)]

    except ValueError as E:
        # for example, the sort key was not valid
        print "Error displaying worksheet listing: ", E
        return current_app.message(_("Error displaying worksheet listing."))

    #if pub and (not g.username or g.username == tuple([])):
    #    r['username'] = '******'

    r['accounts'] = g.notebook.user_manager().get_accounts()
    r['sage_version'] = SAGE_VERSION
    # r['pub'] = pub
    
    return encode_response(r)
Example #15
0
def worksheet_new_text_cell_after(worksheet):
    """Add a new text cell after a given cell."""
    r = {}
    r['id'] = id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_text_cell_after(id, input=input)
    worksheet.increase_state_number()

    r['new_id'] = cell.id()
    r['new_html'] = cell.html(editing=True)

    # XXX: Does editing correspond to TinyMCE?  If so, we should try
    # to centralize that code.
    return encode_response(r)
Example #16
0
def worksheet_new_text_cell_after(worksheet):
    """Add a new text cell after a given cell."""
    r = {}
    r['id'] = id = get_cell_id()
    input = unicode_str(request.values.get('input', ''))
    cell = worksheet.new_text_cell_after(id, input=input)
    worksheet.increase_state_number()

    r['new_id'] = cell.id()
    r['new_html'] = cell.html(editing=True)

    # XXX: Does editing correspond to TinyMCE?  If so, we should try
    # to centralize that code.
    return encode_response(r)
def render_worksheet_list(args, pub, username):
    """
    Returns a rendered worksheet listing.

    INPUT:

    -  ``args`` - ctx.args where ctx is the dict passed
       into a resource's render method

    -  ``pub`` - boolean, True if this is a listing of
       public worksheets

    -  ``username`` - the user whose worksheets we are
       listing

    OUTPUT:

    a string
    """

    from sagenb.notebook.notebook import sort_worksheet_list
    from sagenb.misc.misc import unicode_str, SAGE_VERSION

    typ = args['typ'] if 'typ' in args else 'active'
    search = unicode_str(args['search']) if 'search' in args else None
    sort = args['sort'] if 'sort' in args else 'last_edited'
    reverse = (args['reverse'] == 'True') if 'reverse' in args else False

    if not pub:
        worksheets = g.notebook.worksheet_list_for_user(username,
                                                        typ=typ,
                                                        sort=sort,
                                                        search=search,
                                                        reverse=reverse)

    else:
        worksheets = g.notebook.worksheet_list_for_public(username,
                                                          sort=sort,
                                                          search=search,
                                                          reverse=reverse)

    worksheet_filenames = [x.filename() for x in worksheets]

    if pub and (not username or username == tuple([])):
        username = '******'

    accounts = g.notebook.user_manager().get_accounts()
    sage_version = SAGE_VERSION
    return render_template('html/worksheet_listing.html', **locals())
Example #18
0
def render_worksheet_list(args, pub, username):
    """
    Returns a rendered worksheet listing.

    INPUT:

    -  ``args`` - ctx.args where ctx is the dict passed
       into a resource's render method

    -  ``pub`` - boolean, True if this is a listing of
       public worksheets

    -  ``username`` - the user whose worksheets we are
       listing

    OUTPUT:

    a string
    """

    from sagenb.notebook.notebook import sort_worksheet_list
    from sagenb.misc.misc import unicode_str, SAGE_VERSION

    typ = args['typ'] if 'typ' in args else 'active'
    search = unicode_str(args['search']) if 'search' in args else None
    sort = args['sort'] if 'sort' in args else 'last_edited'
    reverse = (args['reverse'] == 'True') if 'reverse' in args else False
    readonly = g.notebook.readonly_user(g.username)
    try:
        if not pub:
            worksheets = g.notebook.worksheet_list_for_user(username, typ=typ, sort=sort,
                                                              search=search, reverse=reverse)
        else:
            worksheets = g.notebook.worksheet_list_for_public(username, sort=sort,
                                                                search=search, reverse=reverse)
    except ValueError as E:
        # for example, the sort key was not valid
        print "Error displaying worksheet listing: ", E
        return current_app.message(_("Error displaying worksheet listing."))

    worksheet_filenames = [x.filename() for x in worksheets]

    if pub and (not username or username == tuple([])):
        username = '******'

    accounts = g.notebook.user_manager().get_accounts()
    sage_version = SAGE_VERSION
    return render_template('html/worksheet_listing.html', **locals())
Example #19
0
def worksheet_eval(worksheet):
    """
    Evaluate a worksheet cell.

    If the request is not authorized (the requester did not enter the
    correct password for the given worksheet), then the request to
    evaluate or introspect the cell is ignored.

    If the cell contains either 1 or 2 question marks at the end (not
    on a comment line), then this is interpreted as a request for
    either introspection to the documentation of the function, or the
    documentation of the function and the source code of the function
    respectively.
    """    
    from sagenb.notebook.twist import encode_list
    from base import notebook_updates
    
    id = get_cell_id()
    input_text = unicode_str(request.values.get('input', '')).replace('\r\n', '\n') #DOS

    worksheet.increase_state_number()

    cell = worksheet.get_cell_with_id(id)
    cell.set_input_text(input_text)

    if request.values.get('save_only', '0') == '1':
        notebook_updates()
        return ''
    elif request.values.get('text_only', '0') == '1':
        notebook_updates()
        return encode_list([str(id), cell.html()])
    else:
        new_cell = int(request.values.get('newcell', 0)) #wheter to insert a new cell or not

    cell.evaluate(username=g.username)

    if cell.is_last():
        new_cell = worksheet.append_new_cell()
        s = encode_list([new_cell.id(), 'append_new_cell', new_cell.html(div_wrap=False)])
    elif new_cell:
        new_cell = worksheet.new_cell_after(id)
        s = encode_list([new_cell.id(), 'insert_cell', new_cell.html(div_wrap=False), str(id)])
    else:
        s = encode_list([cell.next_id(), 'no_new_cell', str(id)])

    notebook_updates()
    return s
Example #20
0
def displayhook_hack(string):
    """
    Modified version of string so that ``exec``'ing it results in
    displayhook possibly being called.
    
    STRING:

        - ``string`` - a string

    OUTPUT:

        - string formated so that when exec'd last line is printed if
          it is an expression

    EXAMPLES::
    
        sage: from sagenb.misc.format import displayhook_hack
        sage: displayhook_hack('\n12\n')
        "\nexec compile(u'12' + '\\n', '', 'single')"
        sage: displayhook_hack('\ndef my_fun(foo):\n    print foo\n')
        '\ndef my_fun(foo):\n        print foo'
        sage: print displayhook_hack('\nclass A:\n    def __init__(self, foo):\n        self.foo\nb = A(8)\nb')
        <BLANKLINE>
        class A:
            def __init__(self, foo):
                self.foo
        b = A(8)
        exec compile(u'b' + '\n', '', 'single')
    """
    # This function is all so the last line (or single lines) will
    # implicitly print as they should, unless they are an assignment.
    # If anybody knows a better way to do this, please tell me!
    string = string.splitlines()
    i = len(string) - 1
    if i >= 0:
        while len(string[i]) > 0 and string[i][0] in ' \t':
            i -= 1
        final_lines = unicode_str('\n'.join(string[i:]))
        if not final_lines.startswith('def '):
            try:
                compile(final_lines + '\n', '', 'single')
                string[
                    i] = "exec compile(%r + '\\n', '', 'single')" % final_lines
                string = string[:i + 1]
            except SyntaxError, msg:
                pass
Example #21
0
def displayhook_hack(string):
    """
    Modified version of string so that ``exec``'ing it results in
    displayhook possibly being called.
    
    STRING:

        - ``string`` - a string

    OUTPUT:

        - string formated so that when exec'd last line is printed if
          it is an expression

    EXAMPLES::
    
        sage: from sagenb.misc.format import displayhook_hack
        sage: displayhook_hack('\n12\n')
        "\nexec compile(u'12' + '\\n', '', 'single')"
        sage: displayhook_hack('\ndef my_fun(foo):\n    print(foo)\n')
        '\ndef my_fun(foo):\n        print(foo)'
        sage: print(displayhook_hack('\nclass A:\n    def __init__(self, foo):\n        self.foo\nb = A(8)\nb'))
        <BLANKLINE>
        class A:
            def __init__(self, foo):
                self.foo
        b = A(8)
        exec compile(u'b' + '\n', '', 'single')
    """
    # This function is all so the last line (or single lines) will
    # implicitly print as they should, unless they are an assignment.
    # If anybody knows a better way to do this, please tell me!
    string = string.splitlines()
    i = len(string)-1
    if i >= 0:
        while len(string[i]) > 0 and string[i][0] in ' \t':
            i -= 1
        final_lines = unicode_str('\n'.join(string[i:]))
        if not final_lines.startswith('def '):
            try:
                compile(final_lines + '\n', '', 'single')
                string[i] = "exec compile(%r + '\\n', '', 'single')" % final_lines
                string = string[:i+1]
            except SyntaxError:
                pass
    return '\n'.join(string)
Example #22
0
    def process_doc_html(self, doc_in):
        r"""
        Returns processed HTML input as HTML output.  This is the only
        method that needs to be called externally.

        INPUT:

        - ``doc_in`` - a string containing properly formed HTML

        OUTPUT:

        - a string; the processed HTML

        EXAMPLES::

            sage: rst = ""
            sage: rst += "Title\n"
            sage: rst += "-----\n"
            sage: rst += "n"
            sage: rst += "Some text\n"
            sage: from docutils.core import publish_string
            sage: html = publish_string(rst, writer_name='html')
            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: txt = p.process_doc_html(html)
            sage: len(txt)
            51
            sage: txt
            u'<h1 class="title">Title</h1>\n\n<p>nSome text</p>\n\n\n\n'

        """
        # self.feed() is a SGMLParser method and starts everything
        # off; Most of the functions here are extensions to
        # SGMLParser, and may never actually be visibly called here.

        # This module works with unicode literals. In case that input data is
        # ascii, exceptions may occur. So, input data must be converted to
        # unicode if it were not.
        doc_in = unicode_str(doc_in)
        self.feed(doc_in)  #SGMLParser call
        self.close()  #SGMLParser call
        self.hand_off_temp_pieces('to_doc_pieces')
        return self.all_pieces.replace('\\(', '').replace('\\)', '').replace(
            '\\[', '').replace('\\]', '')
Example #23
0
    def __init__(self, *args, **kwds):
        self.startup_token = kwds.pop('startup_token', None)
        Flask.__init__(self, *args, **kwds)
        self.session_interface = OldSecureCookieSessionInterface()

        self.config['SESSION_COOKIE_HTTPONLY'] = False

        self.root_path = SAGENB_ROOT

        self.add_static_path('/css', os.path.join(DATA, "sage", "css"))
        self.add_static_path('/images', os.path.join(DATA, "sage", "images"))
        self.add_static_path('/javascript', DATA)
        self.add_static_path('/static', DATA)
        self.add_static_path('/java', DATA)
        self.add_static_path('/java/jmol', os.path.join(os.environ["SAGE_SHARE"],"jmol"))
        self.add_static_path('/jsmol', os.path.join(os.environ["SAGE_SHARE"],"jsmol"))
        self.add_static_path('/jsmol/js', os.path.join(os.environ["SAGE_SHARE"],"jsmol","js"))
        self.add_static_path('/j2s', os.path.join(os.environ["SAGE_SHARE"],"jsmol","j2s"))
        self.add_static_path('/jsmol/j2s', os.path.join(os.environ["SAGE_SHARE"],"jsmol","j2s"))
        self.add_static_path('/j2s/core', os.path.join(os.environ["SAGE_SHARE"],"jsmol","j2s","core"))
        self.add_static_path('/threejs', os.path.join(os.environ["SAGE_SHARE"],"threejs"))
        import mimetypes
        mimetypes.add_type('text/plain','.jmol')


        #######
        # Doc #
        #######
        #These "should" be in doc.py
        DOC = os.path.join(SAGE_DOC, 'html', 'en')
        self.add_static_path('/pdf', os.path.join(SAGE_DOC, 'pdf'))
        self.add_static_path('/doc/static', DOC)

        # Template globals
        self.add_template_global(url_for)
        # Template filters
        self.add_template_filter(css_escape)
        self.add_template_filter(number_of_rows)
        self.add_template_filter(clean_name)
        self.add_template_filter(prettify_time_ago)
        self.add_template_filter(max)
        self.add_template_filter(lambda x: repr(unicode_str(x))[1:],
                                 name='repr_str')
        self.add_template_filter(dumps, 'tojson')
Example #24
0
    def process_doc_html(self, doc_in):
        r"""
        Returns processed HTML input as HTML output.  This is the only
        method that needs to be called externally.

        INPUT:

        - ``doc_in`` - a string containing properly formed HTML

        OUTPUT:

        - a string; the processed HTML

        EXAMPLES::

            sage: rst = ""
            sage: rst += "Title\n"
            sage: rst += "-----\n"
            sage: rst += "n"
            sage: rst += "Some text\n"
            sage: from docutils.core import publish_string
            sage: html = publish_string(rst, writer_name='html')
            sage: from sagenb.notebook.docHTMLProcessor import docutilsHTMLProcessor
            sage: p = docutilsHTMLProcessor()
            sage: txt = p.process_doc_html(html)
            sage: len(txt)
            51
            sage: txt
            u'<h1 class="title">Title</h1>\n\n<p>nSome text</p>\n\n\n\n'

        """        
        # self.feed() is a SGMLParser method and starts everything
        # off; Most of the functions here are extensions to
        # SGMLParser, and may never actually be visibly called here.

        # This module works with unicode literals. In case that input data is
        # ascii, exceptions may occur. So, input data must be converted to
        # unicode if it were not.
        doc_in = unicode_str(doc_in)  
        self.feed(doc_in) #SGMLParser call
        self.close()     #SGMLParser call
        self.hand_off_temp_pieces('to_doc_pieces')
        return self.all_pieces.replace('\\(', '').replace('\\)', '').replace('\\[', '').replace('\\]', '')
Example #25
0
    EXAMPLES::

        sage: from sagenb.notebook.template import clean_name
        sage: print clean_name('this!is@bad+string')
        this_is_bad_string
    """
    return ''.join([x if x.isalnum() else '_' for x in name])


env.filters['css_escape'] = css_escape
env.filters['number_of_rows'] = number_of_rows
env.filters['clean_name'] = clean_name
env.filters['prettify_time_ago'] = prettify_time_ago
env.filters['max'] = max
env.filters['repr_str'] = lambda x: repr(unicode_str(x))[1:]
env.filters['tojson'] = json.dumps


def template(filename, **user_context):
    """
    Returns HTML, CSS, etc., for a template file rendered in the given
    context.

    INPUT:

    - ``filename`` - a string; the filename of the template relative
      to ``sagenb/data/templates``

    - ``user_context`` - a dictionary; the context in which to evaluate
      the file's template variables
def worksheet_list():
    """
    Returns a worksheet listing.

    INPUT:

    -  ``args`` - ctx.args where ctx is the dict passed
       into a resource's render method

    -  ``pub`` - boolean, True if this is a listing of
       public worksheets

    -  ``username`` - the user whose worksheets we are
       listing

    OUTPUT:

    a string
    """

    from sagenb.notebook.notebook import sort_worksheet_list
    from sagenb.misc.misc import unicode_str, SAGE_VERSION
    from sagenb.notebook.misc import encode_response
    import re

    r = {}

    pub = 'pub' in request.args
    readonly = g.notebook.readonly_user(g.username)
    typ = request.args['type'] if 'type' in request.args else 'active'
    search = unicode_str(request.args['search']) if 'search' in request.args else None
    # Support for the 'tag:' and 'tags:' keyword
    tags = []
    option = ''
    if search != None:
        strs = search.split()
        singletags = [s[4:] for s in strs if s.startswith('tag:')]
        multitags = [s[5:].split(',') for s in strs if s.startswith('tags:')]
        tags = singletags + [tag for m in multitags for tag in m]
        option = [s[7:] for s in strs if s.startswith('option:')]
        if len(option) > 0 and (option[0] in ['published']): #currently only one option allowed
            option = option[0]
        search = " ".join([s for s in strs if not s.startswith('tag:') and not s.startswith('tags:') and not s.startswith('option:')])

    sort = request.args['sort'] if 'sort' in request.args else 'last_edited'
    reverse = (request.args['reverse'] == 'True') if 'reverse' in request.args else False
    try:
        if not pub:
            WList = [x for x in g.notebook.worksheet_list_for_user(g.username, typ=typ, sort=sort, search=search, reverse=reverse)]
            r['worksheets'] = []
            for W in WList:
                if not option or any([option == 'published' and W.has_published_version()]):
                    try:
                        if tags==[] or all([t in W.pokaltags()[g.username] for t in tags]):
                            d = W.basic()
                            d.update({'owner_nickname': g.notebook.user_manager().user(W.owner()).get_nickname()})
                            temp = W.basic()['collaborators']
                            d.update({'collaborators_nicknames':[g.notebook.user_manager().user(x).get_nickname() for x in temp]})
                            r['worksheets'].append(d)
                    except KeyError:
                        pass
                
        else:
            return current_app.message(_("Public listing is not supported."))

    except ValueError as E:
        # for example, the sort key was not valid
        print "Error displaying worksheet listing: ", E
        return current_app.message(_("Error displaying worksheet listing."))

    #if pub and (not g.username or g.username == tuple([])):
    #    r['username'] = '******'

    r['accounts'] = g.notebook.user_manager().get_accounts()
    r['sage_version'] = SAGE_VERSION
    # r['pub'] = pub

    return encode_response(r)
Example #27
0
    EXAMPLES::

        sage: from sagenb.notebook.template import clean_name
        sage: print clean_name('this!is@bad+string')
        this_is_bad_string
    """
    return ''.join([x if x.isalnum() else '_' for x in name])

env.filters['css_escape'] = css_escape
env.filters['number_of_rows'] = number_of_rows
env.filters['clean_name'] = clean_name
env.filters['prettify_time_ago'] = prettify_time_ago
env.filters['math_parse'] = math_parse
env.filters['max'] = max
env.filters['repr_str'] = lambda x: repr(unicode_str(x))[1:]

def template(filename, **user_context):
    """
    Returns HTML, CSS, etc., for a template file rendered in the given
    context.

    INPUT:

    - ``filename`` - a string; the filename of the template relative
      to ``sagenb/data/templates``

    - ``user_context`` - a dictionary; the context in which to evaluate
      the file's template variables

    OUTPUT:
Example #28
0
def worksheet_eval(worksheet):
    """
    Evaluate a worksheet cell.

    If the request is not authorized (the requester did not enter the
    correct password for the given worksheet), then the request to
    evaluate or introspect the cell is ignored.

    If the cell contains either 1 or 2 question marks at the end (not
    on a comment line), then this is interpreted as a request for
    either introspection to the documentation of the function, or the
    documentation of the function and the source code of the function
    respectively.
    """
    from base import notebook_updates

    r = {}

    r['id'] = id = get_cell_id()
    cell = worksheet.get_cell_with_id(id)
    public = worksheet.tags().get('_pub_', [False])[0] #this is set in pub_worksheet

    if public and not cell.is_interactive_cell():
        r['command'] = 'error'
        r['message'] = 'Cannot evaluate non-interactive public cell with ID %r.' % id
        return encode_response(r)

    worksheet.increase_state_number()

    if public:
        # Make public input cells read-only.
        input_text = cell.input_text()
    else:
        input_text = unicode_str(request.values.get('input', '')).replace('\r\n', '\n') #DOS

    # Handle an updated / recomputed interact.  TODO: JSON encode
    # the update data.
    if 'interact' in request.values:
        r['interact'] = 1
        input_text = INTERACT_UPDATE_PREFIX
        variable = request.values.get('variable', '')
        if variable!='':
            adapt_number = int(request.values.get('adapt_number', -1))
            value = request.values.get('value', '')
            input_text += "\n_interact_.update('%s', '%s', %s, _interact_.standard_b64decode('%s'), globals())" % (id, variable, adapt_number, value)

        if int(request.values.get('recompute', 0)):
            input_text += "\n_interact_.recompute('%s')" % id

    cell.set_input_text(input_text)

    if int(request.values.get('save_only', '0')):
        notebook_updates()
        return encode_response(r)
    elif int(request.values.get('text_only', '0')):
        notebook_updates()
        r['cell_html'] = cell.html()
        return encode_response(r)

    cell.evaluate(username=g.username)

    new_cell = int(request.values.get('newcell', 0)) #whether to insert a new cell or not
    if new_cell:
        new_cell = worksheet.new_cell_after(id)
        r['command'] = 'insert_cell'
        r['new_cell_id'] = new_cell.id()
        r['new_cell_html'] = new_cell.html(div_wrap=False)
    else:
        r['next_id'] = cell.next_compute_id()

    notebook_updates()

    return encode_response(r)
Example #29
0
    EXAMPLES::

        sage: from sagenb.notebook.template import clean_name
        sage: print clean_name('this!is@bad+string')
        this_is_bad_string
    """
    return "".join([x if x.isalnum() else "_" for x in name])


env.filters["css_escape"] = css_escape
env.filters["number_of_rows"] = number_of_rows
env.filters["clean_name"] = clean_name
env.filters["prettify_time_ago"] = prettify_time_ago
env.filters["max"] = max
env.filters["repr_str"] = lambda x: repr(unicode_str(x))[1:]
env.filters["tojson"] = json.dumps


def template(filename, **user_context):
    """
    Returns HTML, CSS, etc., for a template file rendered in the given
    context.

    INPUT:

    - ``filename`` - a string; the filename of the template relative
      to ``sagenb/data/templates``

    - ``user_context`` - a dictionary; the context in which to evaluate
      the file's template variables
Example #30
0
def worksheet_eval(worksheet):
    """
    Evaluate a worksheet cell.

    If the request is not authorized (the requester did not enter the
    correct password for the given worksheet), then the request to
    evaluate or introspect the cell is ignored.

    If the cell contains either 1 or 2 question marks at the end (not
    on a comment line), then this is interpreted as a request for
    either introspection to the documentation of the function, or the
    documentation of the function and the source code of the function
    respectively.
    """
    from .base import notebook_updates

    r = {}

    r['id'] = id = get_cell_id()
    cell = worksheet.get_cell_with_id(id)
    public = worksheet.tags().get('_pub_',
                                  [False])[0]  #this is set in pub_worksheet

    if public and not cell.is_interactive_cell():
        r['command'] = 'error'
        r['message'] = 'Cannot evaluate non-interactive public cell with ID %r.' % id
        return encode_response(r)

    worksheet.increase_state_number()

    if public:
        # Make public input cells read-only.
        input_text = cell.input_text()
    else:
        input_text = unicode_str(request.values.get('input',
                                                    '')).replace('\r\n',
                                                                 '\n')  #DOS

    # Handle an updated / recomputed interact.  TODO: JSON encode
    # the update data.
    if 'interact' in request.values:
        r['interact'] = 1
        input_text = INTERACT_UPDATE_PREFIX
        variable = request.values.get('variable', '')
        if variable != '':
            adapt_number = int(request.values.get('adapt_number', -1))
            value = request.values.get('value', '')
            input_text += "\n_interact_.update('%s', '%s', %s, _interact_.standard_b64decode('%s'), globals())" % (
                id, variable, adapt_number, value)

        if int(request.values.get('recompute', 0)):
            input_text += "\n_interact_.recompute('%s')" % id

    cell.set_input_text(input_text)

    if int(request.values.get('save_only', '0')):
        notebook_updates()
        return encode_response(r)
    elif int(request.values.get('text_only', '0')):
        notebook_updates()
        r['cell_html'] = cell.html()
        return encode_response(r)

    cell.evaluate(username=g.username)

    new_cell = int(request.values.get('newcell',
                                      0))  #whether to insert a new cell or not
    if new_cell:
        new_cell = worksheet.new_cell_after(id)
        r['command'] = 'insert_cell'
        r['new_cell_id'] = new_cell.id()
        r['new_cell_html'] = new_cell.html(div_wrap=False)
    else:
        r['next_id'] = cell.next_compute_id()

    notebook_updates()

    return encode_response(r)