Skip to content
This repository has been archived by the owner on Jun 18, 2020. It is now read-only.

Sws cmdline #31

Merged
merged 9 commits into from
Jun 15, 2012
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions flask_version/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,18 @@ def worksheet_do_upload_data(worksheet):
return current_app.message(_('Suspicious filename "%(filename)s" encountered uploading file.%(backlinks)s', filename=filename, backlinks=backlinks), worksheet_url)
os.unlink(dest)


response = redirect(worksheet_datafile.url_for(worksheet, name=name))

if url != '':
if url != '' and url[0:7] == 'file://':
f = file(dest, 'wb')
fin = url[7:]
if fin.startswith('localhost'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this check be done with regexp instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Feb 15, 2012, at 11:07 PM, AlexJuarez wrote:

response = redirect(worksheet_datafile.url_for(worksheet, name=name))
  • if url != '':
  • if url != '' and url[0:7] == 'file://':
  •    f = file(dest, 'wb')
    
  •    fin = url[7:]
    
  •    if fin.startswith('localhost'):
    

can this check be done with regexp instead?


Reply to this email directly or view it on GitHub:
https://github.com/sagemath/sagenb/pull/31/files#r455079

Done. Now that the line is gone I don't know how to comment on it, so hopefully you get this. :-)

Thanks for taking the time to review this.

-Ivan

fin = fin[9:]
f.write(open(fin).read())
f.close()
return response

elif url != '':
#XXX: Finish me!
pass
elif new_field:
Expand Down
19 changes: 15 additions & 4 deletions flask_version/worksheet_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,15 @@ def upload_worksheet():
from sage.misc.misc import tmp_filename, tmp_dir
from werkzeug.utils import secure_filename
import zipfile

backlinks = _("""Return to <a href="/upload" title="Upload a worksheet"><strong>Upload File</strong></a>.""")

url = request.values['url'].strip()
dir = ''
if url:
if url != '':
if url[0:7] == 'file://' and g.notebook.interface != 'localhost':
return current_app.message(_("Unable to load file URL's when not running on localhost.\n%(backlinks)s",backlinks=backlinks))

#Downloading a file from the internet
import urllib, urlparse
filename = tmp_filename() + ('.zip' if url.endswith('.zip') else '.sws')
Expand All @@ -262,12 +265,21 @@ def upload_worksheet():
return current_app.message("Unknown worksheet extension: %s. %s" % (extension, backlinks))
filename = tmp_filename()+extension
try:
urllib.urlretrieve(url, filename)
if url[0:7] != 'file://':
urllib.urlretrieve(url, filename)
else:
fin = url[7:]
if fin.startswith('localhost'):
fin = fin[9:]
import shutil
shutil.copy(fin,filename)

except IOError as err:
if err.strerror == 'unknown url type' and err.filename == 'https':
return current_app.message(_("This Sage notebook is not configured to load worksheets from 'https' URLs. Try a different URL or download the worksheet and upload it directly from your computer.\n%(backlinks)s",backlinks=backlinks))
else:
raise

else:
#Uploading a file from the user's computer
dir = tmp_dir()
Expand Down Expand Up @@ -328,4 +340,3 @@ def upload_worksheet():

from worksheet import url_for_worksheet
return redirect(url_for_worksheet(W))

19 changes: 14 additions & 5 deletions sagenb/notebook/notebook_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class NotebookObject:
r"""
Start the Sage Notebook server. More details about using these
options, as well as tips and tricks, may be available at `this
Sage wiki page`_.
Sage wiki page`_. If a notebook server is already running in the
directory, this will open a browser to the running notebook.

INPUT:

- ``directory`` -- string; directory that contains the Sage
notebook files; the default is
``.sage/sage_notebook.sagenb``, in your home directory.
Expand Down Expand Up @@ -73,18 +74,26 @@ class NotebookObject:
user_manager.set_accounts(True)
user_manager.add_user("username", "password", "email@place", "user")
nb.save()

- ``open_viewer`` -- boolean (default: True) whether to pop up
a web browser. You can override the default browser by
setting the ``SAGE_BROWSER`` environment variable, e.g., by
putting

::

export SAGE_BROWSER="firefox"

in the file .bashrc in your home directory.

- ``upload`` -- string (default: None) Full path to a local file
(sws, txt, zip) to be uploaded and turned into a worksheet(s).
This is equivalent to manually uploading a file via
``http://localhost:8080/upload`` or to fetching
``http://localhost:8080/upload_worksheet?url=file:///...``
in a script except that (hopefully) you will already be
logged in.

- ``timeout`` -- integer (default: 0) seconds until idle
worksheet sessions automatically timeout, i.e., the
corresponding Sage session terminates. 0 means "never
Expand Down
33 changes: 25 additions & 8 deletions sagenb/notebook/run_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,21 @@ def notebook_setup(self=None):
def notebook_twisted(self,
directory = None,
port = 8080,
interface = 'localhost',
interface = 'localhost',
address = None,
port_tries = 50,
secure = False,
reset = False,
require_login = True,
require_login = True,
accounts = None,
openid = None,

server_pool = None,
ulimit = '',

timeout = 0,

upload = None,
open_viewer = True,

sagetex_path = "",
Expand Down Expand Up @@ -348,14 +349,19 @@ def run(port):
if str(e).startswith('Another twistd server is running,'):
print 'Another Sage Notebook server is running, PID %d.' % pid
old_interface, old_port, old_secure = get_old_settings(conf)
if open_viewer and old_port:
if old_port and (open_viewer or upload):
old_interface = old_interface or 'localhost'

startpath = '/'
if upload:
import urllib
startpath = '/upload_worksheet?url=file://%s' % (urllib.quote(upload))

print 'Opening web browser at http%s://%s:%s/ ...' % (
's' if old_secure else '', old_interface, old_port)

from sagenb.misc.misc import open_page as browse_to
browse_to(old_interface, old_port, old_secure, '/')
browse_to(old_interface, old_port, old_secure, 'startpath')
return
print '\nPlease either stop the old server or run the new server in a different directory.'
return
Expand All @@ -378,13 +384,24 @@ def run(port):
notebook_opts = '"%s",interface="%s",port=%s,secure=%s' % (
os.path.abspath(directory), interface, port, secure)

if open_viewer:
start_path = "'/?startup_token=%s' % startup_token"
if open_viewer or upload:
if require_login:
start_path = "'/?startup_token=%s' % startup_token"
elif upload:
start_path = "'/upload_worksheet?url=file://%s'" % upload
else:
start_path = "'/'"
if interface:
hostname = interface
else:
hostname = 'localhost'
open_page = "from sagenb.misc.misc import open_page; open_page('%s', %s, %s, %s)" % (hostname, port, secure, start_path)
open_page = "from sagenb.misc.misc import open_page; open_page('%s', %s, %s, %s);" % (hostname, port, secure, start_path)
# If we have to login and upload a file, then we do them
# in that order and hope that the login is fast enough.
if require_login and upload:
import urllib
open_page += "open_page('%s', %s, %s, '/upload_worksheet?url=file://%s');" % (hostname, port, secure, urllib.quote(upload))

else:
open_page = ''

Expand Down