Skip to content

Commit ef58782

Browse files
committed
Don’t auto-download graph reference on import.
Conflicts: plotly/api/v2/plot_schema.py plotly/graph_reference.py plotly/tests/test_core/test_api/test_v2/test_plot_schema.py plotly/tests/test_core/test_graph_reference/test_graph_reference.py
1 parent 1f1272d commit ef58782

File tree

6 files changed

+15
-91
lines changed

6 files changed

+15
-91
lines changed

makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ setup_subs :
1818

1919
update_default_schema :
2020
@echo "Making sure the default-schema.json file is up to date"
21-
python -c "import json;\
22-
from plotly.graph_reference import GRAPH_REFERENCE;\
21+
python -c "import requests;/
22+
from requests.compat import json as _json;\
23+
response = requests.get('https://api.plot.ly/v2/plot-schema?sha1';/
2324
f = open('plotly/graph_reference/default-schema.json', 'w');\
24-
json.dump(GRAPH_REFERENCE, f, indent=4, sort_keys=True,\
25-
separators=(',', ': '));\
25+
_json.dump(response.json()['schema'], f, indent=4,\
26+
sort_keys=True, separators=(',', ': '));\
2627
f.close()"
2728

2829
install : sync_subs

plotly/files.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
PLOTLY_DIR = os.path.join(os.path.expanduser("~"), ".plotly")
55
CREDENTIALS_FILE = os.path.join(PLOTLY_DIR, ".credentials")
66
CONFIG_FILE = os.path.join(PLOTLY_DIR, ".config")
7-
GRAPH_REFERENCE_FILE = os.path.join(PLOTLY_DIR, ".graph_reference")
87
TEST_DIR = os.path.join(os.path.expanduser("~"), ".test")
98
TEST_FILE = os.path.join(PLOTLY_DIR, ".permission_test")
109

plotly/graph_reference.py

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44
"""
55
from __future__ import absolute_import
66

7-
import hashlib
8-
import json
97
import os
108
import re
119
from pkg_resources import resource_string
1210

13-
import requests
1411
import six
12+
from requests.compat import json as _json
1513

16-
from plotly import files, utils
17-
18-
GRAPH_REFERENCE_PATH = '/v2/plot-schema'
19-
GRAPH_REFERENCE_DOWNLOAD_TIMEOUT = 5 # seconds
14+
from plotly import utils
2015

2116

2217
# For backwards compat, we keep this list of previously known objects.
@@ -66,45 +61,14 @@
6661

6762
def get_graph_reference():
6863
"""
69-
Attempts to load local copy of graph reference or makes GET request if DNE.
64+
Load graph reference JSON (aka plot-schema)
7065
7166
:return: (dict) The graph reference.
72-
:raises: (PlotlyError) When graph reference DNE and GET request fails.
7367
7468
"""
75-
default_config = files.FILE_CONTENT[files.CONFIG_FILE]
76-
if files.check_file_permissions():
77-
graph_reference = utils.load_json_dict(files.GRAPH_REFERENCE_FILE)
78-
config = utils.load_json_dict(files.CONFIG_FILE)
79-
80-
# TODO: https://github.com/plotly/python-api/issues/293
81-
plotly_api_domain = config.get('plotly_api_domain',
82-
default_config['plotly_api_domain'])
83-
else:
84-
graph_reference = {}
85-
plotly_api_domain = default_config['plotly_api_domain']
86-
87-
sha1 = hashlib.sha1(six.b(str(graph_reference))).hexdigest()
88-
89-
graph_reference_url = '{}{}?sha1={}'.format(plotly_api_domain,
90-
GRAPH_REFERENCE_PATH, sha1)
91-
try:
92-
response = requests.get(graph_reference_url,
93-
timeout=GRAPH_REFERENCE_DOWNLOAD_TIMEOUT)
94-
response.raise_for_status()
95-
except requests.exceptions.RequestException:
96-
if not graph_reference:
97-
path = os.path.join('graph_reference', 'default-schema.json')
98-
s = resource_string('plotly', path).decode('utf-8')
99-
graph_reference = json.loads(s)
100-
else:
101-
if six.PY3:
102-
content = str(response.content, encoding='utf-8')
103-
else:
104-
content = response.content
105-
data = json.loads(content)
106-
if data['modified']:
107-
graph_reference = data['schema']
69+
path = os.path.join('graph_reference', 'default-schema.json')
70+
s = resource_string('plotly', path).decode('utf-8')
71+
graph_reference = _json.loads(s)
10872

10973
return utils.decode_unicode(graph_reference)
11074

plotly/tests/test_core/test_graph_reference/test_graph_reference.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,17 @@
1313
import six
1414
from nose.plugins.attrib import attr
1515

16-
from plotly import files, graph_reference as gr, tools, utils
16+
from plotly import files, graph_reference as gr
1717
from plotly.graph_reference import string_to_class_name, get_role
1818
from plotly.tests.utils import PlotlyTestCase
1919

2020

2121
class TestGraphReferenceCaching(PlotlyTestCase):
2222

23-
def set_graph_reference(self, graph_reference):
24-
if files.check_file_permissions():
25-
utils.save_json_dict(files.GRAPH_REFERENCE_FILE, graph_reference)
26-
27-
@attr('slow')
28-
def test_get_graph_reference_outdated(self):
29-
30-
# if the hash of the current graph reference doesn't match the hash of
31-
# the graph reference a Plotly server has, we should update!
32-
33-
outdated_graph_reference = {'real': 'old'}
34-
self.set_graph_reference(outdated_graph_reference)
35-
graph_reference = gr.get_graph_reference()
36-
self.assertNotEqual(graph_reference, outdated_graph_reference)
37-
38-
def test_get_graph_reference_bad_request_local_copy(self):
39-
40-
# if the request fails (mocked by using a bad url here) and a local
41-
# copy of the graph reference exists, we can just use that.
42-
43-
tools.set_config_file(plotly_api_domain='api.am.not.here.ly')
44-
local_graph_reference = {'real': 'local'}
45-
self.set_graph_reference(local_graph_reference)
46-
graph_reference = gr.get_graph_reference()
47-
self.assertEqual(graph_reference, local_graph_reference)
48-
49-
def test_get_graph_reference_bad_request_no_copy(self):
23+
def test_get_graph_reference(self):
5024

5125
# if we don't have a graph reference we load an outdated default
5226

53-
tools.set_config_file(plotly_api_domain='api.am.not.here.ly')
54-
empty_graph_reference = {} # set it to a false-y value.
55-
self.set_graph_reference(empty_graph_reference)
5627
path = os.path.join('graph_reference', 'default-schema.json')
5728
s = resource_string('plotly', path).decode('utf-8')
5829
default_graph_reference = json.loads(s)
@@ -62,8 +33,7 @@ def test_get_graph_reference_bad_request_no_copy(self):
6233
@attr('slow')
6334
def test_default_schema_is_up_to_date(self):
6435
api_domain = files.FILE_CONTENT[files.CONFIG_FILE]['plotly_api_domain']
65-
graph_reference_url = '{}{}?sha1'.format(api_domain,
66-
gr.GRAPH_REFERENCE_PATH)
36+
graph_reference_url = '{}{}?sha1'.format(api_domain, '/v2/plot-schema')
6737
response = requests.get(graph_reference_url)
6838
if six.PY3:
6939
content = str(response.content, encoding='utf-8')

plotly/tests/utils.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,13 @@ def stash_files(self):
3737
if files.check_file_permissions():
3838
self._credentials = utils.load_json_dict(files.CREDENTIALS_FILE)
3939
self._config = utils.load_json_dict(files.CONFIG_FILE)
40-
self._graph_reference = \
41-
utils.load_json_dict(files.GRAPH_REFERENCE_FILE)
4240

4341
def restore_files(self):
4442
if files.check_file_permissions():
4543
if self._credentials is not None:
4644
utils.save_json_dict(files.CREDENTIALS_FILE, self._credentials)
4745
if self._config is not None:
4846
utils.save_json_dict(files.CONFIG_FILE, self._config)
49-
if self._graph_reference is not None:
50-
utils.save_json_dict(files.GRAPH_REFERENCE_FILE,
51-
self._graph_reference)
5247

5348
def stash_session(self):
5449
self._session = copy.deepcopy(session._session)

plotly/tools.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from plotly import graph_reference
2323
from plotly import session
2424
from plotly.files import (CONFIG_FILE, CREDENTIALS_FILE, FILE_CONTENT,
25-
GRAPH_REFERENCE_FILE, check_file_permissions)
25+
check_file_permissions)
2626

2727
DEFAULT_PLOTLY_COLORS = ['rgb(31, 119, 180)', 'rgb(255, 127, 14)',
2828
'rgb(44, 160, 44)', 'rgb(214, 39, 40)',
@@ -146,11 +146,6 @@ def ensure_local_plotly_files():
146146
del contents[key]
147147
utils.save_json_dict(fn, contents)
148148

149-
# make a request to get graph reference if DNE.
150-
utils.ensure_file_exists(GRAPH_REFERENCE_FILE)
151-
utils.save_json_dict(GRAPH_REFERENCE_FILE,
152-
graph_reference.GRAPH_REFERENCE)
153-
154149
else:
155150
warnings.warn("Looks like you don't have 'read-write' permission to "
156151
"your 'home' ('~') directory or to our '~/.plotly' "

0 commit comments

Comments
 (0)