Skip to content

Commit fe8ad92

Browse files
committed
Fall back to default when graph_reference fails.
This means we *never* throw an error. Order of priority: 1. we successfully check with the plotly api 2. check fails, but there’s a local backup 3. check fails and no local backup. we resort to default file
1 parent 49ddf2a commit fe8ad92

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

plotly/graph_reference.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
import hashlib
88
import json
9+
import os
910
import requests
11+
from pkg_resources import resource_string
1012

1113
import six
1214

@@ -45,13 +47,9 @@ def get_graph_reference():
4547
response.raise_for_status()
4648
except requests.exceptions.RequestException:
4749
if not graph_reference:
48-
raise exceptions.PlotlyError(
49-
"The schema used to validate Plotly figures has never been "
50-
"downloaded to your computer. You'll need to connect to a "
51-
"Plotly server at least once to do this.\n"
52-
"You're seeing this error because the attempt to download "
53-
"the schema from '{}' failed.".format(graph_reference_url)
54-
)
50+
path = os.path.join('graph_reference', 'default-schema.json')
51+
s = resource_string('plotly', path).decode('utf-8')
52+
graph_reference = json.loads(s)
5553
else:
5654
if six.PY3:
5755
content = str(response.content, encoding='utf-8')

plotly/tests/test_core/test_graph_reference/test_graph_reference.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
44
"""
55
from __future__ import absolute_import
6+
import json
67

7-
from plotly import exceptions, files, graph_reference as gr, tools, utils
8+
import os
9+
from pkg_resources import resource_string
10+
11+
from plotly import files, graph_reference as gr, tools, utils
812
from plotly.tests.utils import PlotlyTestCase
913

1014

@@ -37,10 +41,13 @@ def test_get_graph_reference_bad_request_local_copy(self):
3741

3842
def test_get_graph_reference_bad_request_no_copy(self):
3943

40-
# if we don't have a graph reference we *have* to error, no choice :(
44+
# if we don't have a graph reference we load an outdated default
4145

4246
tools.set_config_file(plotly_api_domain='api.am.not.here.ly')
4347
empty_graph_reference = {} # set it to a false-y value.
4448
self.set_graph_reference(empty_graph_reference)
45-
with self.assertRaisesRegexp(exceptions.PlotlyError, 'schema'):
46-
gr.get_graph_reference()
49+
path = os.path.join('graph_reference', 'default-schema.json')
50+
s = resource_string('plotly', path).decode('utf-8')
51+
default_graph_reference = json.loads(s)
52+
graph_reference = gr.get_graph_reference()
53+
self.assertEqual(graph_reference, default_graph_reference)

0 commit comments

Comments
 (0)