Skip to content

Load graph reference from pkg copy of plot schema #614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 5 additions & 38 deletions plotly/graph_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@

from plotly import files, utils

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


# For backwards compat, we keep this list of previously known objects.
# Moving forward, we only add new trace names.
Expand Down Expand Up @@ -65,46 +62,16 @@

def get_graph_reference():
"""
Attempts to load local copy of graph reference or makes GET request if DNE.
Load graph reference JSON (aka plot-schema)

:return: (dict) The graph reference.
:raises: (PlotlyError) When graph reference DNE and GET request fails.

"""
default_config = files.FILE_CONTENT[files.CONFIG_FILE]
if files.check_file_permissions():
graph_reference = utils.load_json_dict(files.GRAPH_REFERENCE_FILE)
config = utils.load_json_dict(files.CONFIG_FILE)

# TODO: https://github.com/plotly/python-api/issues/293
plotly_api_domain = config.get('plotly_api_domain',
default_config['plotly_api_domain'])
else:
graph_reference = {}
plotly_api_domain = default_config['plotly_api_domain']

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

graph_reference_url = '{}{}?sha1={}'.format(plotly_api_domain,
GRAPH_REFERENCE_PATH, sha1)

try:
response = requests.get(graph_reference_url,
timeout=GRAPH_REFERENCE_DOWNLOAD_TIMEOUT)
response.raise_for_status()
except requests.exceptions.RequestException:
if not graph_reference:
path = os.path.join('graph_reference', 'default-schema.json')
s = resource_string('plotly', path).decode('utf-8')
graph_reference = json.loads(s)
else:
if six.PY3:
content = str(response.content, encoding='utf-8')
else:
content = response.content
data = json.loads(content)
if data['modified']:
graph_reference = data['schema']

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

return utils.decode_unicode(graph_reference)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,7 @@ def set_graph_reference(self, graph_reference):
if files.check_file_permissions():
utils.save_json_dict(files.GRAPH_REFERENCE_FILE, graph_reference)

@attr('slow')
def test_get_graph_reference_outdated(self):

# if the hash of the current graph reference doesn't match the hash of
# the graph reference a Plotly server has, we should update!

outdated_graph_reference = {'real': 'old'}
self.set_graph_reference(outdated_graph_reference)
graph_reference = gr.get_graph_reference()
self.assertNotEqual(graph_reference, outdated_graph_reference)

def test_get_graph_reference_bad_request_local_copy(self):

# if the request fails (mocked by using a bad url here) and a local
# copy of the graph reference exists, we can just use that.

tools.set_config_file(plotly_api_domain='api.am.not.here.ly')
local_graph_reference = {'real': 'local'}
self.set_graph_reference(local_graph_reference)
graph_reference = gr.get_graph_reference()
self.assertEqual(graph_reference, local_graph_reference)

def test_get_graph_reference_bad_request_no_copy(self):
def test_get_graph_reference(self):

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

Expand All @@ -59,29 +37,6 @@ def test_get_graph_reference_bad_request_no_copy(self):
graph_reference = gr.get_graph_reference()
self.assertEqual(graph_reference, default_graph_reference)

@attr('slow')
def test_default_schema_is_up_to_date(self):
api_domain = files.FILE_CONTENT[files.CONFIG_FILE]['plotly_api_domain']
graph_reference_url = '{}{}?sha1'.format(api_domain,
gr.GRAPH_REFERENCE_PATH)
response = requests.get(graph_reference_url)
if six.PY3:
content = str(response.content, encoding='utf-8')
else:
content = response.content
schema = json.loads(content)['schema']

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

msg = (
'The default, hard-coded plot schema we ship with pip is out of '
'sync with the prod plot schema!\n'
'Run `make update_default_schema` to fix it!'
)
self.assertEqual(schema, default_schema, msg=msg)


class TestStringToClass(PlotlyTestCase):

Expand Down