Skip to content

Commit c0df02f

Browse files
authored
Merge pull request #913 from krinsman/step7_13
Migrate to traitlets from tornado.options
2 parents f8e1145 + 942cc47 commit c0df02f

File tree

12 files changed

+302
-226
lines changed

12 files changed

+302
-226
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ NBViewer is configurable using a config file, by default called `nbviewer_config
199199

200200
If you don't know which attributes of NBViewer you can configure using the config file, run `python -m nbviewer --generate-config` (or `python -m nbviewer --generate-config --config-file="my_custom_name.py"`) to write a default config file which has all of the configurable options commented out and set to their default values. To change a configurable option to a new value, uncomment the corresponding line and change the default value to the new value.
201201

202-
The config file uses [the standard configuration syntax for Jupyter projects](https://traitlets.readthedocs.io/en/stable/config.html).
202+
You can also run `python -m nbviewer --help-all` to see all of the configurable options. This is a more comprehensive version of `python -m nbviewer --help`, which gives a list of the most common ones along with flags and aliases you can use to set their values temporarily via the command line.
203+
204+
The config file uses [the standard configuration syntax for Jupyter projects](https://traitlets.readthedocs.io/en/stable/config.html). For example, to configure the default port used to be 9000, add the line `c.NBViewer.port = 9000` to the config file. If you want to do this just once, you can also run `python -m nbviewer --NBViewer.port=9000` at the command line. (`NBViewer.port` also has the alias `port`, making it also possible to do, in this specific case, `python -m nbviewer --port=9000`. However not all configurable options have shorthand aliases like this; you can check using the outputs of `python -m nbviewer --help` and `python -m nbviewer --help-all` to see which ones do and which ones don't.)
203205

204206
One thing this allows you to do, for example, is to write your custom implementations of any of the standard page rendering [handlers](https://www.tornadoweb.org/en/stable/guide/structure.html#subclassing-requesthandler) included in NBViewer, e.g. by subclassing the original handlers to include custom logic along with custom output possibilities, and then have these custom handlers always loaded by default, by modifying the corresponding lines in the config file. This is effectively another way to extend NBViewer.
205207

nbviewer/app.py

Lines changed: 208 additions & 114 deletions
Large diffs are not rendered by default.

nbviewer/providers/github/tests/test_github.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import requests
1010

11-
from unittest import SkipTest
12-
1311
from ....tests.base import NBViewerTestCase, FormatHTMLMixin, skip_unless_github_auth
1412

1513
class GitHubTestCase(NBViewerTestCase):

nbviewer/providers/local/tests/test_localfile.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66
#-----------------------------------------------------------------------------
77

88
import requests
9-
import sys
10-
from nbviewer.utils import url_path_join
119

1210
from ....tests.base import NBViewerTestCase, FormatHTMLMixin
1311

1412
class LocalFileDefaultTestCase(NBViewerTestCase):
1513
@classmethod
16-
def get_server_args(cls):
17-
return [
18-
'--localfiles=.',
19-
]
14+
def get_server_cmd(cls):
15+
return super().get_server_cmd() + [ '--localfiles=.' ]
2016

2117
def test_url(self):
2218
## assumes being run from base of this repo
@@ -32,10 +28,8 @@ class FormatHTMLLocalFileDefaultTestCase(LocalFileDefaultTestCase,
3228

3329
class LocalFileRelativePathTestCase(NBViewerTestCase):
3430
@classmethod
35-
def get_server_args(cls):
36-
return [
37-
'--localfiles=nbviewer',
38-
]
31+
def get_server_cmd(cls):
32+
return super().get_server_cmd() + [ '--localfiles=nbviewer' ]
3933

4034
def test_url(self):
4135
## assumes being run from base of this repo
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3-
from ....tests.async_base import AsyncNbviewerTestCase
3+
import requests
4+
from ....tests.base import NBViewerTestCase
45

5-
6-
class ForceUTF8TestCase(AsyncNbviewerTestCase):
6+
class ForceUTF8TestCase(NBViewerTestCase):
77
def test_utf8(self):
88
""" #507, bitbucket returns no content headers, but _is_ serving utf-8
99
"""
10-
response = self.fetch(
11-
'/urls/bitbucket.org/sandiego206/asdasd/raw/master/Untitled.ipynb'
10+
response = requests.get(
11+
self.url('/urls/bitbucket.org/sandiego206/asdasd/raw/master/Untitled.ipynb')
1212
)
13-
self.assertEqual(response.code, 200)
14-
self.assertIn("ñ", response.body)
13+
self.assertEqual(response.status_code, 200)
14+
self.assertIn("ñ", response.content)

nbviewer/tests/async_base.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

nbviewer/tests/base.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@
1313
import time
1414
import requests
1515
from contextlib import contextmanager
16-
from threading import Thread, Event
1716
from unittest import TestCase, skipIf
1817

1918
from tornado.escape import to_unicode
20-
from tornado.ioloop import IOLoop
2119
from tornado.log import app_log
22-
import tornado.options
2320

2421
from nbviewer.utils import url_path_join
25-
from nbviewer.app import main
2622
from nbviewer.providers.github.client import AsyncGitHubClient
2723

24+
from subprocess import Popen
25+
from subprocess import DEVNULL as devnull
26+
import os
27+
import sys
2828

2929
class NBViewerTestCase(TestCase):
3030
"""A base class for tests that need a running nbviewer server."""
3131

3232
port = 12341
3333

34+
environment_variables = {}
35+
3436
def assertIn(self, observed, expected, *args, **kwargs):
3537
return super().assertIn(
3638
to_unicode(observed),
@@ -69,30 +71,22 @@ def wait_until_dead(cls):
6971
else:
7072
time.sleep(.1)
7173

74+
@classmethod
75+
def get_server_cmd(cls):
76+
return [ sys.executable, '-m', 'nbviewer', '--port=%d' % cls.port, ]
77+
7278
@classmethod
7379
def setup_class(cls):
74-
cls._start_evt = Event()
75-
cls.server = Thread(target=cls._server_main)
76-
cls.server.start()
77-
cls._start_evt.wait()
80+
server_cmd = cls.get_server_cmd()
81+
cls.server = Popen(server_cmd,
82+
stdout=devnull, stderr=devnull,
83+
# Set environment variables if any
84+
env=dict(os.environ, **cls.environment_variables))
7885
cls.wait_until_alive()
7986

80-
@classmethod
81-
def get_server_args(cls):
82-
return []
83-
84-
@classmethod
85-
def _server_main(cls):
86-
cls._server_loop = loop = IOLoop()
87-
loop.make_current()
88-
cls._server_loop.add_callback(cls._start_evt.set)
89-
main(['', '--port=%d' % cls.port] + cls.get_server_args())
90-
loop.close(all_fds=True)
91-
9287
@classmethod
9388
def teardown_class(cls):
94-
cls._server_loop.add_callback(cls._server_loop.stop)
95-
cls.server.join()
89+
cls.server.terminate()
9690
cls.wait_until_dead()
9791

9892
@classmethod

nbviewer/tests/test_app.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,57 @@ def test_generate_config():
3636
os.remove(cfg_file)
3737
assert cfg_file in out
3838
assert 'NBViewer.name' not in cfg_text # This shouldn't be configurable
39+
assert 'NBViewer.answer_yes' in cfg_text
40+
assert 'NBViewer.base_url' in cfg_text
41+
assert 'NBViewer._base_url' not in cfg_text # This shouldn't be configurable
42+
assert 'NBViewer.binder_base_url' in cfg_text
43+
assert 'NBViewer.cache_expiry_max' in cfg_text
44+
assert 'NBViewer.cache_expiry_min' in cfg_text
45+
assert 'NBViewer.client' in cfg_text
46+
assert 'NBViewer.config_file' in cfg_text
47+
assert 'NBViewer.content_security_policy' in cfg_text
48+
assert 'NBViewer.default_format' in cfg_text
49+
assert 'NBViewer.frontpage' in cfg_text
50+
assert 'NBViewer.generate_config' in cfg_text
51+
assert 'NBViewer.host' in cfg_text
52+
assert 'NBViewer.index' in cfg_text
53+
assert 'NBViewer.ipywidgets_base_url' in cfg_text
54+
assert 'NBViewer.jupyter_js_widgets_version' in cfg_text
55+
assert 'NBViewer.jupyter_widgets_html_manager_version' in cfg_text
56+
assert 'NBViewer.localfile_any_user' in cfg_text
3957
assert 'NBViewer.local_handler' in cfg_text
58+
assert 'NBViewer.localfile_follow_symlinks' in cfg_text
59+
assert 'NBViewer.localfiles' in cfg_text
60+
assert 'NBViewer.mathjax_url' in cfg_text
61+
assert 'NBViewer.max_cache_uris' in cfg_text
62+
assert 'NBViewer.mc_threads' in cfg_text
63+
assert 'NBViewer.no_cache' in cfg_text
64+
assert 'NBViewer.no_check_certificate' in cfg_text
65+
assert 'NBViewer.port' in cfg_text
66+
assert 'NBViewer.processes' in cfg_text
67+
assert 'NBViewer.providers' in cfg_text
68+
assert 'NBViewer.provider_rewrites' in cfg_text
69+
assert 'NBViewer.proxy_host' in cfg_text
70+
assert 'NBViewer.proxy_port' in cfg_text
71+
assert 'NBViewer.rate_limit' in cfg_text
72+
assert 'NBViewer.rate_limit_interval' in cfg_text
73+
assert 'NBViewer.render_timeout' in cfg_text
74+
assert 'NBViewer.sslcert' in cfg_text
75+
assert 'NBViewer.sslkey' in cfg_text
4076
assert 'NBViewer.static_path' in cfg_text
77+
assert 'NBViewer.static_url_prefix' in cfg_text
78+
assert 'NBViewer._static_url_prefix' not in cfg_text # This shouldn't be configurable
79+
assert 'NBViewer.statsd_host' in cfg_text
80+
assert 'NBViewer.statsd_port' in cfg_text
81+
assert 'NBViewer.statsd_prefix' in cfg_text
82+
assert 'NBViewer.template_path' in cfg_text
83+
assert 'NBViewer.default_endpoint' not in cfg_text # Shouldn't be configurable, is a property
84+
assert 'NBViewer.env' not in cfg_text # Shouldn't be configurable, is a property
85+
assert 'NBViewer.fetch_kwargs' not in cfg_text
86+
assert 'NBViewer.formats' not in cfg_text
87+
assert 'NBViewer.frontpage_setup' not in cfg_text
88+
assert 'NBViewer.pool' not in cfg_text
89+
assert 'NBViewer.rate_limiter' not in cfg_text
90+
assert 'NBViewer.static_paths' not in cfg_text
91+
assert 'NBViewer.template_paths' not in cfg_text
92+

nbviewer/tests/test_config.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import os
22
from subprocess import Popen
33
from .base import NBViewerTestCase
4-
import sys
54
import requests
6-
from tornado.ioloop import IOLoop
7-
from nbviewer.app import main
85

96
tmpl_fixture = "nbviewer/tests/templates"
107

@@ -19,22 +16,12 @@ def test_used_custom_template(self):
1916

2017
class TemplatePathCLITestCase(NBViewerTestCase, CustomTemplateStub):
2118
@classmethod
22-
def get_server_args(cls):
23-
return super().get_server_args() + [
24-
'--template_path={}'.format(tmpl_fixture),
25-
]
19+
def get_server_cmd(cls):
20+
return super().get_server_cmd() + [
21+
'--template-path={}'.format(tmpl_fixture), ]
2622

2723

28-
class TemplatePathEnvTestCase(NBViewerTestCase, CustomTemplateStub):
29-
30-
@classmethod
31-
def _server_main(cls):
32-
cls._server_loop = loop = IOLoop()
33-
loop.make_current()
34-
cls._server_loop.add_callback(cls._start_evt.set)
3524

36-
# Set environment variable
37-
os.environ['NBVIEWER_TEMPLATE_PATH'] = tmpl_fixture
25+
class TemplatePathEnvTestCase(NBViewerTestCase, CustomTemplateStub):
3826

39-
main(['', '--port=%d' % cls.port] + cls.get_server_args())
40-
loop.close(all_fds=True)
27+
environment_variables = {'NBVIEWER_TEMPLATE_PATH': tmpl_fixture}

nbviewer/tests/test_index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from unittest import SkipTest
2-
31
from .base import NBViewerTestCase
42

3+
from unittest import skip
54
from nbviewer import index
65

76
class ElasticSearchTestCase(NBViewerTestCase):
7+
@skip
8+
@classmethod
89
def test_finish_notebook(self):
910
pass

0 commit comments

Comments
 (0)