Skip to content

Commit 39a6a21

Browse files
committed
Use api.v2.plot_schema to get GRAPH_REFERENCE.
There is a circular import issue that needed to be fixed here as well.
1 parent 3310348 commit 39a6a21

File tree

4 files changed

+21
-37
lines changed

4 files changed

+21
-37
lines changed

Diff for: plotly/graph_reference.py

+7-21
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
import re
1111
from pkg_resources import resource_string
1212

13-
import requests
1413
import six
1514

16-
from plotly import files, utils
15+
from plotly import exceptions, files, utils
16+
from plotly.api import v2
1717

18-
GRAPH_REFERENCE_PATH = '/v2/plot-schema'
1918
GRAPH_REFERENCE_DOWNLOAD_TIMEOUT = 5 # seconds
2019

2120

@@ -72,37 +71,24 @@ def get_graph_reference():
7271
:raises: (PlotlyError) When graph reference DNE and GET request fails.
7372
7473
"""
75-
default_config = files.FILE_CONTENT[files.CONFIG_FILE]
7674
if files.check_file_permissions():
7775
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'])
8376
else:
8477
graph_reference = {}
85-
plotly_api_domain = default_config['plotly_api_domain']
8678

8779
sha1 = hashlib.sha1(six.b(str(graph_reference))).hexdigest()
8880

89-
graph_reference_url = '{}{}?sha1={}'.format(plotly_api_domain,
90-
GRAPH_REFERENCE_PATH, sha1)
9181
try:
92-
response = requests.get(graph_reference_url,
93-
timeout=GRAPH_REFERENCE_DOWNLOAD_TIMEOUT)
94-
response.raise_for_status()
95-
except requests.exceptions.RequestException:
82+
response = v2.plot_schema.retrieve(
83+
sha1, timeout=GRAPH_REFERENCE_DOWNLOAD_TIMEOUT
84+
)
85+
except exceptions.PlotlyRequestError:
9686
if not graph_reference:
9787
path = os.path.join('graph_reference', 'default-schema.json')
9888
s = resource_string('plotly', path).decode('utf-8')
9989
graph_reference = json.loads(s)
10090
else:
101-
if six.PY3:
102-
content = str(response.content, encoding='utf-8')
103-
else:
104-
content = response.content
105-
data = json.loads(content)
91+
data = response.json()
10692
if data['modified']:
10793
graph_reference = data['schema']
10894

Diff for: plotly/plotly/plotly.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from plotly import exceptions, tools, utils, files
2828
from plotly.api import v1, v2
29+
from plotly.graph_reference import GRAPH_REFERENCE
2930
from plotly.plotly import chunked_requests
3031
from plotly.session import (sign_in, update_session_plot_options,
3132
get_session_plot_options)
@@ -46,7 +47,7 @@
4647
}
4748

4849
# test file permissions and make sure nothing is corrupted
49-
tools.ensure_local_plotly_files()
50+
tools.ensure_local_plotly_files(graph_reference=GRAPH_REFERENCE)
5051

5152

5253
# don't break backwards compatibility

Diff for: plotly/tests/test_core/test_graph_reference/test_graph_reference.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
from nose.plugins.attrib import attr
1515

1616
from plotly import files, graph_reference as gr, tools, utils
17+
from plotly.api import v2
1718
from plotly.graph_reference import string_to_class_name, get_role
1819
from plotly.tests.utils import PlotlyTestCase
1920

21+
FAKE_API_DOMAIN = 'https://api.am.not.here.ly'
22+
2023

2124
class TestGraphReferenceCaching(PlotlyTestCase):
2225

@@ -40,7 +43,7 @@ def test_get_graph_reference_bad_request_local_copy(self):
4043
# if the request fails (mocked by using a bad url here) and a local
4144
# copy of the graph reference exists, we can just use that.
4245

43-
tools.set_config_file(plotly_api_domain='api.am.not.here.ly')
46+
tools.set_config_file(plotly_api_domain=FAKE_API_DOMAIN)
4447
local_graph_reference = {'real': 'local'}
4548
self.set_graph_reference(local_graph_reference)
4649
graph_reference = gr.get_graph_reference()
@@ -50,7 +53,7 @@ def test_get_graph_reference_bad_request_no_copy(self):
5053

5154
# if we don't have a graph reference we load an outdated default
5255

53-
tools.set_config_file(plotly_api_domain='api.am.not.here.ly')
56+
tools.set_config_file(plotly_api_domain=FAKE_API_DOMAIN)
5457
empty_graph_reference = {} # set it to a false-y value.
5558
self.set_graph_reference(empty_graph_reference)
5659
path = os.path.join('graph_reference', 'default-schema.json')
@@ -61,15 +64,8 @@ def test_get_graph_reference_bad_request_no_copy(self):
6164

6265
@attr('slow')
6366
def test_default_schema_is_up_to_date(self):
64-
api_domain = files.FILE_CONTENT[files.CONFIG_FILE]['plotly_api_domain']
65-
graph_reference_url = '{}{}?sha1'.format(api_domain,
66-
gr.GRAPH_REFERENCE_PATH)
67-
response = requests.get(graph_reference_url)
68-
if six.PY3:
69-
content = str(response.content, encoding='utf-8')
70-
else:
71-
content = response.content
72-
schema = json.loads(content)['schema']
67+
response = v2.plot_schema.retrieve('')
68+
schema = response.json()['schema']
7369

7470
path = os.path.join('graph_reference', 'default-schema.json')
7571
s = resource_string('plotly', path).decode('utf-8')

Diff for: plotly/tools.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from plotly import colors
2020
from plotly import utils
2121
from plotly import exceptions
22-
from plotly import graph_reference
2322
from plotly import session
2423
from plotly.files import (CONFIG_FILE, CREDENTIALS_FILE, FILE_CONTENT,
2524
GRAPH_REFERENCE_FILE, check_file_permissions)
@@ -127,7 +126,7 @@ def get_config_defaults():
127126
return dict(FILE_CONTENT[CONFIG_FILE]) # performs a shallow copy
128127

129128

130-
def ensure_local_plotly_files():
129+
def ensure_local_plotly_files(graph_reference=None):
131130
"""Ensure that filesystem is setup/filled out in a valid way.
132131
If the config or credential files aren't filled out, then write them
133132
to the disk.
@@ -148,8 +147,9 @@ def ensure_local_plotly_files():
148147

149148
# make a request to get graph reference if DNE.
150149
utils.ensure_file_exists(GRAPH_REFERENCE_FILE)
151-
utils.save_json_dict(GRAPH_REFERENCE_FILE,
152-
graph_reference.GRAPH_REFERENCE)
150+
if graph_reference is not None:
151+
# This is a workaround to keep from having weird circular imports.
152+
utils.save_json_dict(GRAPH_REFERENCE_FILE, graph_reference)
153153

154154
else:
155155
warnings.warn("Looks like you don't have 'read-write' permission to "
@@ -1362,6 +1362,7 @@ def validate(obj, obj_type):
13621362
13631363
"""
13641364
# TODO: Deprecate or move. #283
1365+
from plotly import graph_reference
13651366
from plotly.graph_objs import graph_objs
13661367

13671368
if obj_type not in graph_reference.CLASSES:

0 commit comments

Comments
 (0)