Ticket #8473: trac_8473-sagenb.2.patch

File trac_8473-sagenb.2.patch, 7.5 KB (added by iandrus, 10 years ago)
  • sagenb/notebook/notebook_object.py

    # HG changeset patch
    # User Ivan Andrus <darthandrus@gmail.com>
    # Date 1294151712 -3600
    # Node ID 87bfab3ab454dd219201bfb22522ec77d5a77bad
    # Parent  64df5f737c20f65d328490837a5839a8d094622a
    #8473/sagenb: Add support for upload from file urls and --notebook upload=
    
    diff --git a/sagenb/notebook/notebook_object.py b/sagenb/notebook/notebook_object.py
    a b  
    2222    r"""
    2323    Start the Sage Notebook server.  More details about using these
    2424    options, as well as tips and tricks, may be available at `this
    25     Sage wiki page`_.
     25    Sage wiki page`_.  If a notebook server is already running in the
     26    directory, this will open a browser to the running notebook.
     27
    2628
    2729    INPUT:
    28    
     30
    2931        - ``directory`` -- string; directory that contains the Sage
    3032          notebook files; the default is
    3133          ``.sage/sage_notebook.sagenb``, in your home directory.
     
    7072              nb.set_accounts(True)
    7173              nb.add_user("username", "password", "email@place", "user")
    7274              nb.save()
    73              
     75
    7476        - ``open_viewer`` -- boolean (default: True) whether to pop up
    7577          a web browser.  You can override the default browser by
    7678          setting the ``SAGE_BROWSER`` environment variable, e.g., by
    7779          putting
    7880
    7981          ::
    80          
     82
    8183              export SAGE_BROWSER="firefox"
    82              
     84
    8385          in the file .bashrc in your home directory.
    8486
     87        - ``upload`` -- string (default: None) Full path to a local file
     88          (sws, txt, zip) to be uploaded and turned into a worksheet(s).
     89          This is equivalent to manually uploading a file via
     90          ``http://localhost:8000/upload`` or to fetching
     91          ``http://localhost:8000/upload_worksheet?url=file:///...``
     92          in a script except that (hopefully) you will already be
     93          logged in.
     94
    8595        - ``timeout`` -- integer (default: 0) seconds until idle
    8696          worksheet sessions automatically timeout, i.e., the
    8797          corresponding Sage session terminates. 0 means "never
  • sagenb/notebook/run_notebook.py

    diff --git a/sagenb/notebook/run_notebook.py b/sagenb/notebook/run_notebook.py
    a b  
    160160
    161161             timeout       = 0,
    162162
     163             upload        = None,
    163164             open_viewer   = True,
    164165
    165166             sagetex_path  = "",
     
    259260                if str(e).startswith('Another twistd server is running,'):
    260261                    print 'Another Sage Notebook server is running, PID %d.' % pid
    261262                    old_interface, old_port, old_secure = get_old_settings(conf)
    262                     if open_viewer and old_port:
     263                    if old_port and (open_viewer or upload):
    263264                        old_interface = old_interface or 'localhost'
    264265
    265                         print 'Opening web browser at http%s://%s:%s/ ...' % (
    266                             's' if old_secure else '', old_interface, old_port)
     266                        startpath = '/'
     267                        if upload:
     268                            import urllib
     269                            startpath = '/upload_worksheet?url=file://%s' % (urllib.quote(upload))
     270
     271                        print 'Opening web browser at http%s://%s:%s%s ...' % (
     272                            's' if old_secure else '', old_interface, old_port, startpath)
    267273
    268274                        from sagenb.misc.misc import open_page as browse_to
    269                         browse_to(old_interface, old_port, old_secure, '/')
     275                        browse_to(old_interface, old_port, old_secure, startpath)
    270276                        return
    271277                    print '\nPlease either stop the old server or run the new server in a different directory.'
    272278                    return
     
    289295        notebook_opts = '"%s",interface="%s",port=%s,secure=%s' % (
    290296            os.path.abspath(directory), interface, port, secure)
    291297
    292         if open_viewer:
     298        if open_viewer or upload:
    293299            if require_login:
    294300                start_path = "'/?startup_token=%s' % startup_token"
     301            elif upload:
     302                start_path = "'/upload_worksheet?url=file://%s'" % upload
    295303            else:
    296304                start_path = "'/'"
    297305            if interface:
    298306                hostname = interface
    299307            else:
    300308                hostname = 'localhost'
    301             open_page = "from sagenb.misc.misc import open_page; open_page('%s', %s, %s, %s)" % (hostname, port, secure, start_path)
     309            open_page = "from sagenb.misc.misc import open_page; open_page('%s', %s, %s, %s);" % (hostname, port, secure, start_path)
     310
     311            # If we have to login and upload a file, then we do them
     312            # in that order and hope that the login is fast enough.
     313            if require_login and upload:
     314                import urllib
     315                open_page += "open_page('%s', %s, %s, '/upload_worksheet?url=file://%s');" % (hostname, port, secure, urllib.quote(upload))
     316
    302317        else:
    303318            open_page = ''
    304319
  • sagenb/notebook/twist.py

    diff --git a/sagenb/notebook/twist.py b/sagenb/notebook/twist.py
    a b  
    445445
    446446        url = ctx.args['url'][0].strip()
    447447        dir = ''  # we will delete the directory below if it is used
    448         if url != '':
     448        if url != '' and url[0:7] != 'file://':
    449449            # downloading a file from the internet
    450450            filename = tmp_filename()+".sws"
     451
     452        elif url != '' and notebook.interface == 'localhost':
     453            # uploading a file from the user's computer (as a file url)
     454            dir = tmp_dir()
     455            fin = url[7:]
     456            if fin.startswith('localhost'):
     457                fin = fin[9:]
     458            filename = fin.split('/')[-1]
     459            if filename == '':
     460                return HTMLResponse(stream=message("Please specify a worksheet to load.%s" % backlinks))
     461            # Make tmp file in Sage temp directory
     462            filename = os.path.join(dir, filename)
     463            f = file(filename,'wb')
     464            # Then download to that file.
     465            f.write(open(fin).read())
     466            # TODO: Server blocking issues (?!)
     467            f.close()
     468
    451469        else:
    452470            # uploading a file from the user's computer
    453471            dir = tmp_dir()
     
    511529
    512530            return RedirectResponse('/home/'+W.filename())
    513531
    514         if url != '':
     532        if url != '' and url[0:7] != 'file://':
    515533            # We use the downloadPage function which returns a
    516534            # deferred which we are allowed to return to the server.
    517535            # The server waits until the download is finished and then runs
     
    637655
    638656        response = RedirectResponse(worksheet_url + '/datafile?name=%s' % name)
    639657
    640         if url != '':
     658        if url != '' and url[0:7] != 'file://':
    641659            # Here we use twisted's downloadPage function which
    642660            # returns a deferred object.  We return the deferred to
    643661            # the server, and it will wait until the download has
     
    657675            d.addCallback(callback)
    658676            d.addErrback(errback)
    659677            return d
     678
     679        elif url != '' and notebook.interface == 'localhost':
     680            f = file(dest, 'wb')
     681            fin = url[7:]
     682            if fin.startswith('localhost'):
     683                fin = fin[9:]
     684            f.write(open(fin).read())
     685            f.close()
     686            return response
     687
    660688        elif newfield:
    661689            open(dest, 'w').close()
    662690            return response