Skip to content

Commit

Permalink
Remove some of ipython_genutils no-op.
Browse files Browse the repository at this point in the history
ipython_genutils is complicated to package on some linux distro because
of nose, and in general not useful as it mostly offered python 2/3
compat layer.

Goal always has been to remove any usage of ipython_genutils
  • Loading branch information
Carreau committed Mar 8, 2021
1 parent 36218db commit b4d3403
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 39 deletions.
3 changes: 1 addition & 2 deletions jupyter_server/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

from traitlets.config import Application
from ipython_genutils.path import filefind
from ipython_genutils.py3compat import string_types

from jupyter_core.paths import is_hidden
import jupyter_server
Expand Down Expand Up @@ -723,7 +722,7 @@ def set_headers(self):
def initialize(self, path, default_filename=None, no_cache_paths=None):
self.no_cache_paths = no_cache_paths or []

if isinstance(path, string_types):
if isinstance(path, str):
path = [path]

self.root = tuple(
Expand Down
9 changes: 3 additions & 6 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
Any, Dict, Unicode, Integer, List, Bool, Bytes, Instance,
TraitError, Type, Float, observe, default, validate
)
from ipython_genutils import py3compat
from jupyter_core.paths import jupyter_runtime_dir, jupyter_path
from jupyter_server._sysinfo import get_sys_info

Expand Down Expand Up @@ -195,7 +194,7 @@ def init_settings(self, jupyter_app, kernel_manager, contents_manager,
"template_path",
jupyter_app.template_file_path,
)
if isinstance(_template_path, py3compat.string_types):
if isinstance(_template_path, str):
_template_path = (_template_path,)
template_path = [os.path.expanduser(path) for path in _template_path]

Expand Down Expand Up @@ -223,7 +222,7 @@ def init_settings(self, jupyter_app, kernel_manager, contents_manager,
now = utcnow()

root_dir = contents_manager.root_dir
home = py3compat.str_to_unicode(os.path.expanduser('~'), encoding=sys.getfilesystemencoding())
home = os.path.expanduser("~")
if root_dir.startswith(home + os.path.sep):
# collapse $HOME to ~
root_dir = '~' + root_dir[len(home):]
Expand Down Expand Up @@ -925,8 +924,6 @@ def _default_allow_remote(self):
# Address is a hostname
for info in socket.getaddrinfo(self.ip, self.port, 0, socket.SOCK_STREAM):
addr = info[4][0]
if not py3compat.PY3:
addr = addr.decode('ascii')

try:
parsed = ipaddress.ip_address(addr.split('%')[0])
Expand Down Expand Up @@ -1258,7 +1255,7 @@ def _default_root_dir(self):
self._root_dir_set = True
return os.path.dirname(os.path.abspath(self.file_to_run))
else:
return py3compat.getcwd()
return os.getcwd()

@validate('root_dir')
def _root_dir_validate(self, proposal):
Expand Down
1 change: 0 additions & 1 deletion jupyter_server/services/config/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import errno
from tornado import web

from ipython_genutils.py3compat import PY3
from ...base.handlers import APIHandler

class ConfigHandler(APIHandler):
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/services/contents/filecheckpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
File-based Checkpoints implementations.
"""
import os
from os import getcwd
import shutil

from tornado.web import HTTPError
Expand All @@ -16,7 +17,6 @@

from anyio import run_sync_in_worker_thread
from jupyter_core.utils import ensure_dir_exists
from ipython_genutils.py3compat import getcwd
from traitlets import Unicode

from jupyter_server import _tz as tz
Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/services/contents/fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
)
import nbformat

from ipython_genutils.py3compat import str_to_unicode

from traitlets.config import Configurable
from traitlets import Bool
Expand Down Expand Up @@ -231,7 +230,7 @@ def perm_to_403(self, os_path=''):
# this may not work perfectly on unicode paths on Python 2,
# but nobody should be doing that anyway.
if not os_path:
os_path = str_to_unicode(e.filename or 'unknown file')
os_path = e.filename or "unknown file"
path = to_api_path(os_path, root=self.root_dir)
raise HTTPError(403, u'Permission denied: %s' % path) from e
else:
Expand Down
5 changes: 2 additions & 3 deletions jupyter_server/services/contents/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from ipython_genutils.importstring import import_item
from traitlets import Any, Unicode, Bool, TraitError, observe, default, validate
from ipython_genutils.py3compat import getcwd, string_types

from jupyter_core.paths import exists, is_hidden, is_file_hidden
from jupyter_server import _tz as tz
Expand All @@ -47,7 +46,7 @@ def _default_root_dir(self):
try:
return self.parent.root_dir
except AttributeError:
return getcwd()
return os.getcwd()

post_save_hook = Any(None, config=True, allow_none=True,
help="""Python callable or importstring thereof
Expand All @@ -70,7 +69,7 @@ def _default_root_dir(self):
@validate('post_save_hook')
def _validate_post_save_hook(self, proposal):
value = proposal['value']
if isinstance(value, string_types):
if isinstance(value, str):
value = import_item(value)
if not callable(value):
raise TraitError("post_save_hook must be callable")
Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
validate,
default,
)
from ipython_genutils.py3compat import string_types
from jupyter_server.transutils import _i18n
from jupyter_server.utils import ensure_async

Expand Down Expand Up @@ -106,7 +105,7 @@ def _notary_default(self):
@validate('pre_save_hook')
def _validate_pre_save_hook(self, proposal):
value = proposal['value']
if isinstance(value, string_types):
if isinstance(value, str):
value = import_item(self.pre_save_hook)
if not callable(value):
raise TraitError("pre_save_hook must be callable")
Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/services/kernels/kernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

from jupyter_server.utils import to_os_path, ensure_async
from jupyter_server._tz import utcnow, isoformat
from ipython_genutils.py3compat import getcwd

from jupyter_server.prometheus.metrics import KERNEL_CURRENTLY_RUNNING_TOTAL

Expand Down Expand Up @@ -56,7 +55,7 @@ def _default_root_dir(self):
try:
return self.parent.root_dir
except AttributeError:
return getcwd()
return os.getcwd()

@validate('root_dir')
def _update_root_dir(self, proposal):
Expand Down
3 changes: 1 addition & 2 deletions jupyter_server/services/sessions/sessionmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from tornado import web

from traitlets.config.configurable import LoggingConfigurable
from ipython_genutils.py3compat import unicode_type
from traitlets import Instance

from jupyter_server.utils import ensure_async
Expand Down Expand Up @@ -81,7 +80,7 @@ async def session_exists(self, path):

def new_session_id(self):
"Create a uuid for a new session"
return unicode_type(uuid.uuid4())
return str(uuid.uuid4())

async def create_session(self, path=None, name=None, type=None, kernel_name=None, kernel_id=None):
"""Creates a session and returns its model"""
Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/tests/nbconvert/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
new_notebook, new_markdown_cell, new_code_cell, new_output,
)

from ipython_genutils.py3compat import which
from shutil import which


from base64 import encodebytes
Expand Down
25 changes: 16 additions & 9 deletions jupyter_server/tests/test_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from datetime import datetime
from tornado.web import HTTPError
from tornado.httpclient import HTTPRequest, HTTPResponse
from ipython_genutils.py3compat import str_to_unicode
from jupyter_server.serverapp import ServerApp
from jupyter_server.gateway.managers import GatewayClient
from jupyter_server.utils import ensure_async
Expand Down Expand Up @@ -51,7 +50,7 @@ async def mock_gateway_request(url, **kwargs):

# Fetch all kernelspecs
if endpoint.endswith('/api/kernelspecs') and method == 'GET':
response_buf = StringIO(str_to_unicode(json.dumps(kernelspecs)))
response_buf = StringIO(json.dumps(kernelspecs))
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response

Expand All @@ -60,7 +59,7 @@ async def mock_gateway_request(url, **kwargs):
requested_kernelspec = endpoint.rpartition('/')[2]
kspecs = kernelspecs.get('kernelspecs')
if requested_kernelspec in kspecs:
response_buf = StringIO(str_to_unicode(json.dumps(kspecs.get(requested_kernelspec))))
response_buf = StringIO(json.dumps(kspecs.get(requested_kernelspec)))
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response
else:
Expand All @@ -75,7 +74,7 @@ async def mock_gateway_request(url, **kwargs):
assert name == kspec_name # Ensure that KERNEL_ env values get propagated
model = generate_model(name)
running_kernels[model.get('id')] = model # Register model as a running kernel
response_buf = StringIO(str_to_unicode(json.dumps(model)))
response_buf = StringIO(json.dumps(model))
response = await ensure_async(HTTPResponse(request, 201, buffer=response_buf))
return response

Expand All @@ -85,7 +84,7 @@ async def mock_gateway_request(url, **kwargs):
for kernel_id in running_kernels.keys():
model = running_kernels.get(kernel_id)
kernels.append(model)
response_buf = StringIO(str_to_unicode(json.dumps(kernels)))
response_buf = StringIO(json.dumps(kernels))
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
return response

Expand All @@ -101,8 +100,12 @@ async def mock_gateway_request(url, **kwargs):
raise HTTPError(404, message='Kernel does not exist: %s' % requested_kernel_id)
elif action == 'restart':
if requested_kernel_id in running_kernels:
response_buf = StringIO(str_to_unicode(json.dumps(running_kernels.get(requested_kernel_id))))
response = await ensure_async(HTTPResponse(request, 204, buffer=response_buf))
response_buf = StringIO(
json.dumps(running_kernels.get(requested_kernel_id))
)
response = await ensure_async(
HTTPResponse(request, 204, buffer=response_buf)
)
return response
else:
raise HTTPError(404, message='Kernel does not exist: %s' % requested_kernel_id)
Expand All @@ -120,8 +123,12 @@ async def mock_gateway_request(url, **kwargs):
if endpoint.rfind('/api/kernels/') >= 0 and method == 'GET':
requested_kernel_id = endpoint.rpartition('/')[2]
if requested_kernel_id in running_kernels:
response_buf = StringIO(str_to_unicode(json.dumps(running_kernels.get(requested_kernel_id))))
response = await ensure_async(HTTPResponse(request, 200, buffer=response_buf))
response_buf = StringIO(
json.dumps(running_kernels.get(requested_kernel_id))
)
response = await ensure_async(
HTTPResponse(request, 200, buffer=response_buf)
)
return response
else:
raise HTTPError(404, message='Kernel does not exist: %s' % requested_kernel_id)
Expand Down
12 changes: 4 additions & 8 deletions jupyter_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from urllib.parse import quote, unquote, urlparse, urljoin
from urllib.request import pathname2url

from ipython_genutils import py3compat


def url_path_join(*pieces):
Expand Down Expand Up @@ -61,19 +60,16 @@ def url_escape(path):
Turns '/foo bar/' into '/foo%20bar/'
"""
parts = py3compat.unicode_to_str(path, encoding='utf8').split('/')
return u'/'.join([quote(p) for p in parts])
parts = path.split("/")
return "/".join([quote(p) for p in parts])


def url_unescape(path):
"""Unescape special characters in a URL path
Turns '/foo%20bar/' into '/foo bar/'
"""
return u'/'.join([
py3compat.str_to_unicode(unquote(p), encoding='utf8')
for p in py3compat.unicode_to_str(path, encoding='utf8').split('/')
])
return "/".join([unquote(p) for p in path.split("/")])


def samefile_simple(path, other_path):
Expand Down Expand Up @@ -225,4 +221,4 @@ def wrapped():
# just return a Future, hoping that it will be awaited
result = asyncio.ensure_future(maybe_async)
return result
return wrapped()
return wrapped()

0 comments on commit b4d3403

Please sign in to comment.