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

Commit

Permalink
Merge pull request #384 from fchapoton/imports_step1
Browse files Browse the repository at this point in the history
some modernizing of imports, toward python3 compatible imports
  • Loading branch information
dimpase committed May 25, 2016
2 parents 79b4e1f + d24c4b8 commit f5b4fec
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 33 deletions.
22 changes: 15 additions & 7 deletions sagenb/misc/misc.py
Expand Up @@ -30,7 +30,15 @@ def g(*args, **kwds):

min_password_length = 6

import os, cPickle, socket, sys
import os
import socket
import sys

try:
import cPickle as pickle
except ImportError:
import pickle


def print_open_msg(address, port, secure=False, path=""):
"""
Expand Down Expand Up @@ -249,12 +257,12 @@ def browser():
try:
from sage.structure.sage_object import loads, dumps, load, save
except ImportError:
loads = cPickle.loads
dumps = cPickle.dumps
loads = pickle.loads
dumps = pickle.dumps
def load(filename):
return cPickle.loads(open(filename).read())
return pickle.loads(open(filename).read())
def save(obj, filename):
s = cPickle.dumps(obj, protocol=2)
s = pickle.dumps(obj, protocol=2)
open(filename,'wb').write(s)

try:
Expand Down Expand Up @@ -403,7 +411,7 @@ def set_permissive_permissions(filename):
stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP)

def encoded_str(obj, encoding='utf-8'):
ur"""
r"""
Takes an object and returns an encoded str human-readable representation.
EXAMPLES::
Expand All @@ -421,7 +429,7 @@ def encoded_str(obj, encoding='utf-8'):
return str(obj)

def unicode_str(obj, encoding='utf-8'):
ur"""
r"""
Takes an object and returns a unicode human-readable representation.
EXAMPLES::
Expand Down
8 changes: 4 additions & 4 deletions sagenb/notebook/all.py
Expand Up @@ -10,11 +10,11 @@

#from test_notebook import notebook_playback

from sage_email import email
from .sage_email import email

from notebook_object import notebook, inotebook
from .notebook_object import notebook, inotebook

from interact import interact, input_box, slider, range_slider, selector, checkbox, input_grid, text_control, color_selector
from .interact import interact, input_box, slider, range_slider, selector, checkbox, input_grid, text_control, color_selector

# For doctesting.
import sagenb
from . import sagenb
23 changes: 12 additions & 11 deletions sagenb/notebook/compress/JavaScriptCompressor.py
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*
# -*- coding: utf-8 -*
# JavaScriptCompressor class,
# removes comments or pack JavaScript source[s] code.
# ______________________________________________________________
Expand Down Expand Up @@ -55,7 +55,9 @@
# @Credits Dean Edwards for his originally idea [dean.edwards.name] and his JavaScript packer
# @License GNU General Public License (GPL)

import re, time, string, SourceMap, BaseConvert
import re
import time
from . import SourceMap, BaseConvert
class JavaScriptCompressor:

# public variables
Expand Down Expand Up @@ -111,7 +113,7 @@ def __clean(self, str):
if type != "regexp":
# clean.append("\n")
pass
return re.sub("/(\n)+/", "\n", re.sub("/^\s*|\s*$/", "", string.join(clean, "")))
return re.sub("/(\n)+/", "\n", re.sub("/^\s*|\s*$/", "", "".join(clean)))
def __commonInitMethods(self, jsSource, packed):
header = ""
sources = ""
Expand All @@ -124,7 +126,7 @@ def __commonInitMethods(self, jsSource, packed):
header = self.__getHeader()
for a in range(0, self.__totalSources):
tempSources.append(self.__sources[a]["code"])
sources = string.join(tempSources, ";")
sources = ";".join(tempSources)
if packed:
sources = self.__pack(sources)
self.__sourceNewLength = len(sources)
Expand All @@ -133,11 +135,11 @@ def __commonInitMethods(self, jsSource, packed):
def __doReplacement(self, matchobj):
return self.__BC.toBase(self.__wordsParser(matchobj.group(0)))
def __getHeader(self):
return string.join([
return "".join([
"/* ", self.__getScriptNames(), "JavaScriptCompressor ", self.version, " [www.devpro.it], ",
"thanks to Dean Edwards for idea [dean.edwards.name]",
" */\n"
], "")
])
def __getScriptNames(self):
a = 0
result = []
Expand All @@ -148,7 +150,7 @@ def __getScriptNames(self):
if a > 0:
a = a - 1
result[a] = result[a] + " with ";
return string.join(result, ", ")
return ", ".join(result)
def __getSize(self, size):
times = 0
fsize = float(size)
Expand All @@ -166,17 +168,16 @@ def __getSize(self, size):
def __pack(self, str):
self.__container = []
str = self.__addslashes(re.sub("\w+", self.__doReplacement, self.__clean(str))).replace("\n", "\\n")
return 'eval(function(A,G){return A.replace(/(\\w+)/g,function(a,b){return G[parseInt(b,36)]})}("' + str + '","' + string.join(self.__container, ",") + '".split(",")));'
return 'eval(function(A,G){return A.replace(/(\\w+)/g,function(a,b){return G[parseInt(b,36)]})}("' + str + '","' + ",".join(self.__container) + '".split(",")));'
def __setStats(self):
endTime = "%.3f" % ((time.clock() - self.__startTime) / 1000)
self.stats = string.join([
self.stats = " ".join([
self.__getSize(self.__sourceLength),
"to",
self.__getSize(self.__sourceNewLength),
"in",
endTime,
"seconds"
], " ")
"seconds"])
def __sourceManager(self, jsSource):
b = len(jsSource)
dictType = type({})
Expand Down
7 changes: 4 additions & 3 deletions sagenb/notebook/js.py
Expand Up @@ -27,10 +27,11 @@
###########################################################################

import os
import keyboards
from template import template

from . import keyboards
from .template import template
from sagenb.misc.misc import SAGE_URL
from compress.JavaScriptCompressor import JavaScriptCompressor
from .compress.JavaScriptCompressor import JavaScriptCompressor
from hashlib import sha1

# Debug mode? If sagenb lives under SAGE_ROOT/, we minify/pack and cache
Expand Down
9 changes: 6 additions & 3 deletions sagenb/notebook/notebook.py
Expand Up @@ -27,9 +27,12 @@
import socket
import time
import bz2
import cPickle
from cgi import escape

try:
import cPickle as pickle
except ImportError:
import pickle

# Sage libraries
from sagenb.misc.misc import (pad_zeros, cputime, tmp_dir, load, save,
Expand Down Expand Up @@ -1840,7 +1843,7 @@ def migrate_old_notebook_v1(dir):
Back up and migrates an old saved version of notebook to the new one (`sagenb`)
"""
nb_sobj = os.path.join(dir, 'nb.sobj')
old_nb = cPickle.loads(open(nb_sobj).read())
old_nb = pickle.loads(open(nb_sobj).read())

######################################################################
# Tell user what is going on and make a backup
Expand Down Expand Up @@ -1999,7 +2002,7 @@ def migrate_old_worksheet(old_worksheet):
for username in old_nb.user_manager().users().keys():
history_file = os.path.join(dir, 'worksheets', username, 'history.sobj')
if os.path.exists(history_file):
new_nb._user_history[username] = cPickle.loads(open(history_file).read())
new_nb._user_history[username] = pickle.loads(open(history_file).read())

# Save our newly migrated notebook to disk
new_nb.save()
Expand Down
10 changes: 7 additions & 3 deletions sagenb/notebook/notebook_object.py
Expand Up @@ -12,11 +12,15 @@
# http://www.gnu.org/licenses/
#############################################################################

import time, os, shutil, signal, tempfile
import time
import os
import shutil
import signal
import tempfile

import notebook as _notebook
from . import notebook as _notebook

import run_notebook
from . import run_notebook

class NotebookObject:
r"""
Expand Down
4 changes: 2 additions & 2 deletions sagenb/notebook/worksheet.py
Expand Up @@ -160,7 +160,7 @@ def __init__(self, name=None, id_number=None,
notebook_worksheet_directory=None, system=None,
owner=None, pretty_print=False,
auto_publish=False, create_directories=True, live_3D=False):
ur"""
r"""
Create and initialize a new worksheet.
INPUT:
Expand Down Expand Up @@ -702,7 +702,7 @@ def delete_notebook_specific_data(self):
self.__viewers = []

def name(self, username=None):
ur"""
r"""
Return the name of this worksheet.
OUTPUT: string
Expand Down

0 comments on commit f5b4fec

Please sign in to comment.