Skip to content

Commit

Permalink
write server list to stdout
Browse files Browse the repository at this point in the history
instead of logging, which goes to stderr and is often prefixed and/or filtered by log levels
  • Loading branch information
minrk committed May 12, 2023
1 parent 35ffe5d commit 389b97b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
9 changes: 4 additions & 5 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,20 +677,19 @@ class JupyterServerListApp(JupyterApp):

def start(self):
"""Start the server list application."""
info = self.log.info
serverinfo_list = list(list_running_servers(self.runtime_dir, log=self.log))
if self.jsonlist:
info(json.dumps(serverinfo_list, indent=2))
print(json.dumps(serverinfo_list, indent=2))
elif self.json:
for serverinfo in serverinfo_list:
info(json.dumps(serverinfo))
print(json.dumps(serverinfo))
else:
info("Currently running servers:")
print("Currently running servers:")
for serverinfo in serverinfo_list:
url = serverinfo["url"]
if serverinfo.get("token"):
url = url + "?token=%s" % serverinfo["token"]
info("%s :: %s", url, serverinfo["root_dir"])
print(url, "::", serverinfo["root_dir"])


# -----------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ unfixable = [
# S108 Probable insecure usage of temporary file or directory
# PLC1901 `ext_pkg.version == ""` can be simplified to `not ext_pkg.version` as an empty string is falsey
"tests/*" = ["B011", "F841", "C408", "E402", "T201", "EM101", "EM102", "EM103", "PLR2004", "S108", "PLC1901"]
# print should be used in applications
"**/*app.py" = ["T201"]
# Ignore flake 8 errors from shimmed imports
"jupyter_server/base/zmqhandlers.py" = ["F401"]
# PLR2004 Magic value used in comparison
Expand Down
46 changes: 46 additions & 0 deletions tests/test_serverapp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import getpass
import json
import logging
import os
import pathlib
import sys
import warnings
from unittest.mock import patch

Expand All @@ -14,6 +16,7 @@
from jupyter_server.auth.security import passwd_check
from jupyter_server.serverapp import (
JupyterPasswordApp,
JupyterServerListApp,
ServerApp,
ServerWebApplication,
list_running_servers,
Expand All @@ -36,6 +39,49 @@ def test_help_output():
check_help_all_output("jupyter_server")


@pytest.mark.parametrize(
"format",
[
"json",
"jsonlist",
"",
],
)
def test_server_list(jp_configurable_serverapp, capsys, format):
app = jp_configurable_serverapp(log=logging.getLogger())

app.write_server_info_file()

capsys.readouterr()
listapp = JupyterServerListApp(
parent=app,
)
if format:
setattr(listapp, format, True)
listapp.start()
captured = capsys.readouterr()
sys.stdout.write(captured.out)
sys.stderr.write(captured.err)
out = captured.out.strip()

if not format:
assert "Currently running servers:" in out
assert app.connection_url in out
assert len(out.splitlines()) == 2
return

if format == "jsonlist":
servers = json.loads(out)
elif format == "json":
servers = [json.loads(line) for line in out.splitlines()]
assert len(servers) == 1
sinfo = servers[0]

assert sinfo["port"] == app.port
assert sinfo["url"] == app.connection_url
assert sinfo["version"] == app.version


def test_server_info_file(tmp_path, jp_configurable_serverapp):
app = jp_configurable_serverapp(log=logging.getLogger())

Expand Down

0 comments on commit 389b97b

Please sign in to comment.