Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f375701

Browse files
committed
Start cacheing graph reference.
1 parent 3f7d242 commit f375701

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

plotly/files.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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")
78
TEST_DIR = os.path.join(os.path.expanduser("~"), ".test")
89
TEST_FILE = os.path.join(PLOTLY_DIR, ".permission_test")
910

plotly/graph_reference.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
This module handles accessing, storing, and managing the graph reference.
3+
4+
"""
5+
from __future__ import absolute_import
6+
7+
import json
8+
import requests
9+
10+
from plotly import exceptions, files, utils
11+
12+
GRAPH_REFERENCE_PATH = '/plot-schema.json'
13+
14+
def get_graph_reference():
15+
"""
16+
Attempts to load local copy of graph reference or makes GET request if DNE.
17+
18+
:return: (dict) The graph reference.
19+
:raises: (PlotlyError) When graph reference DNE and GET request fails.
20+
21+
"""
22+
if files.check_file_permissions():
23+
graph_reference = utils.load_json_dict(files.GRAPH_REFERENCE_FILE)
24+
config = utils.load_json_dict(files.CONFIG_FILE)
25+
plotly_domain = config['plotly_domain']
26+
else:
27+
graph_reference = {}
28+
plotly_domain = files.FILE_CONTENT[files.CONFIG_FILE]['plotly_domain']
29+
30+
if not graph_reference:
31+
32+
graph_reference_url = plotly_domain + GRAPH_REFERENCE_PATH
33+
34+
try:
35+
response = requests.get(graph_reference_url)
36+
response.raise_for_status()
37+
except requests.exceptions.RequestException:
38+
raise exceptions.PlotlyError(
39+
"The schema used to validate Plotly figures has never been "
40+
"downloaded to your computer. You'll need to connect to a "
41+
"Plotly server at least once to do this.\n"
42+
"You're seeing this error because the attempt to download "
43+
"the schema from '{}' failed.".format(graph_reference_url)
44+
)
45+
46+
# TODO: Hash this so we don't have to load it every time. The server
47+
# will need to accept a hash query param as well.
48+
graph_reference = json.loads(response.content)
49+
50+
return utils.decode_unicode(graph_reference)
51+
52+
GRAPH_REFERENCE = get_graph_reference()

plotly/tools.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"""
1010
from __future__ import absolute_import
1111

12-
import os.path
1312
import warnings
1413

1514
import six
@@ -19,6 +18,7 @@
1918

2019
from plotly import utils
2120
from plotly import exceptions
21+
from plotly import graph_reference
2222
from plotly import session
2323
from plotly.files import (CONFIG_FILE, CREDENTIALS_FILE, FILE_CONTENT,
2424
GRAPH_REFERENCE_FILE, check_file_permissions)
@@ -64,8 +64,6 @@ def get_config_defaults():
6464
return dict(FILE_CONTENT[CONFIG_FILE]) # performs a shallow copy
6565

6666

67-
68-
6967
def ensure_local_plotly_files():
7068
"""Ensure that filesystem is setup/filled out in a valid way"""
7169
if check_file_permissions():
@@ -81,7 +79,14 @@ def ensure_local_plotly_files():
8179
if key not in FILE_CONTENT[fn]:
8280
del contents[key]
8381
utils.save_json_dict(fn, contents)
84-
ensure_graph_reference_file()
82+
83+
# make a request to get graph reference if DNE.
84+
utils.ensure_file_exists(GRAPH_REFERENCE_FILE)
85+
graph_reference_dict = utils.load_json_dict(GRAPH_REFERENCE_FILE)
86+
87+
if not graph_reference_dict:
88+
utils.save_json_dict(GRAPH_REFERENCE_FILE,
89+
graph_reference.GRAPH_REFERENCE)
8590
else:
8691
warnings.warn("Looks like you don't have 'read-write' permission to "
8792
"your 'home' ('~') directory or to our '~/.plotly' "
@@ -225,6 +230,14 @@ def reset_config_file():
225230
ensure_local_plotly_files() # put the defaults back
226231

227232

233+
### graph reference tools ###
234+
235+
def reset_graph_reference_file():
236+
"""Temporary until we can use local hash in request for graph reference."""
237+
utils.ensure_file_exists(GRAPH_REFERENCE_FILE)
238+
utils.save_json_dict(GRAPH_REFERENCE_FILE, {})
239+
240+
228241
### embed tools ###
229242

230243
def get_embed(file_owner_or_url, file_id=None, width="100%", height=525):

0 commit comments

Comments
 (0)