Skip to content

Commit e55e81c

Browse files
committed
moved validation function to utils//test working
1 parent fe31851 commit e55e81c

File tree

4 files changed

+57
-62
lines changed

4 files changed

+57
-62
lines changed

plotly/tests/test_core/test_stream/test_stream.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
tk = 'vaia8trjjb'
1919
config = {'plotly_domain': 'https://plot.ly',
2020
'plotly_streaming_domain': 'stream.plot.ly',
21-
'plotly_api_domain': 'https://api.plot.ly',
21+
'plotly_api_domain': 'https://api.plot.ly',
2222
'plotly_ssl_verification': False}
2323

2424

plotly/tests/test_core/test_tools/test_file_tools.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from plotly import tools, session
22
from plotly.tests.utils import PlotlyTestCase
33

4-
import ipdb
54
import warnings
65

76

@@ -54,14 +53,16 @@ def test_set_config_file_world_readable(self):
5453
self.assertRaises(TypeError, tools.set_config_file, **kwargs)
5554

5655
def test_set_config_expected_error_msg(self):
57-
kwargs = {'plotly_domain': 'http://www.foo-bar.com'}
58-
#self.assertRaises(UserWarning, tools.set_config_file, **kwargs)
59-
print 'abcd'
60-
b = tools.set_config_file(**kwargs)
61-
print type(b)
62-
6356

57+
# Check that UserWarning is being called with http plotly_domain
6458

59+
with warnings.catch_warnings(record=True) as w:
60+
warnings.simplefilter("always")
61+
kwargs = {'plotly_domain': 'http://www.foo-bar.com'}
62+
tools.set_config_file(**kwargs)
63+
assert len(w) == 1
64+
assert issubclass(w[-1].category, UserWarning)
65+
assert "plotly_domain" in str(w[-1].message)
6566

6667
def test_reset_config_file(self):
6768

plotly/tools.py

+11-53
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,6 @@
4949
ALTERNATIVE_HISTNORM = 'probability'
5050

5151

52-
http_msg = (
53-
"The plotly_domain and plotly_api_domain of your config file must start "
54-
"with 'https', 'http'.\n If you are not using On-Prem then run the "
55-
"following code to ensure your plotly_domain and plotly_api_domain start "
56-
"with 'https':\n\n\n"
57-
"import plotly\n"
58-
"plotly.tools.set_config_file(\n"
59-
" plotly_domain='https://plot.ly',\n"
60-
" plotly_api_domain='https://api.plot.ly'\n"
61-
")\n\n\n"
62-
"If you are using On-Prem then you will need to use your company's "
63-
"domain and api_domain urls:\n\n\n"
64-
"import plotly\n"
65-
"plotly.tools.set_config_file(\n"
66-
" plotly_domain='https://plotly.your-company.com',\n"
67-
" plotly_api_domain='https://plotly.your-company.com'\n"
68-
")\n\n\n"
69-
"Make sure to replace `your-company.com` with the URL of your Plotly "
70-
"On-Premise server.\nSee "
71-
"https://plot.ly/python/getting-started/#special-instructions-for-plotly-onpremise-users "
72-
"for more help with getting started with On-Prem."
73-
)
74-
75-
76-
def _validate_domains(*domains):
77-
for d in domains:
78-
if not d.lower().startswith('https'):
79-
warnings.warn(http_msg, category=UserWarning)
80-
81-
8252
# Warning format
8353
def warning_on_one_line(message, category, filename, lineno,
8454
file=None, line=None):
@@ -216,14 +186,15 @@ def set_config_file(plotly_domain=None,
216186
:param (bool) world_readable: True = public, False = private
217187
218188
"""
189+
# import ipdb; ipdb.set_trace()
219190
if not check_file_permissions():
220191
raise exceptions.PlotlyError("You don't have proper file permissions "
221192
"to run this function.")
222193
ensure_local_plotly_files() # make sure what's there is OK
223194
utils.validate_world_readable_and_sharing_settings({
224195
'sharing': sharing, 'world_readable': world_readable})
225196

226-
settings = get_config_file(validate=False)
197+
settings = get_config_file()
227198
if isinstance(plotly_domain, six.string_types):
228199
settings['plotly_domain'] = plotly_domain
229200
elif plotly_domain is not None:
@@ -250,12 +221,9 @@ def set_config_file(plotly_domain=None,
250221
raise TypeError('auto_open should be a boolean')
251222

252223
# validate plotly_domain and plotly_api_domain
253-
list_of_domains = []
254-
if plotly_domain is not None:
255-
list_of_domains.append(plotly_domain)
256-
if plotly_api_domain is not None:
257-
list_of_domains.append(plotly_api_domain)
258-
_validate_domains(*list_of_domains)
224+
utils.validate_plotly_domains(
225+
{'plotly_domain': plotly_domain, 'plotly_api_domain': plotly_api_domain}
226+
)
259227

260228
if isinstance(world_readable, bool):
261229
settings['world_readable'] = world_readable
@@ -272,7 +240,7 @@ def set_config_file(plotly_domain=None,
272240
ensure_local_plotly_files() # make sure what we just put there is OK
273241

274242

275-
def get_config_file(validate=True, *args):
243+
def get_config_file(*args):
276244
"""Return specified args from `~/.plotly/.config`. as tuple.
277245
278246
Returns all if no arguments are specified.
@@ -283,19 +251,9 @@ def get_config_file(validate=True, *args):
283251
"""
284252
if check_file_permissions():
285253
ensure_local_plotly_files() # make sure what's there is OK
286-
returned_obj = utils.load_json_dict(CONFIG_FILE, *args)
254+
return utils.load_json_dict(CONFIG_FILE, *args)
287255
else:
288-
returned_obj = FILE_CONTENT[CONFIG_FILE]
289-
290-
list_of_domains = []
291-
for domain in ['plotly_domain', 'plotly_api_domain']:
292-
if domain in returned_obj:
293-
list_of_domains.append(returned_obj[domain])
294-
295-
if validate:
296-
_validate_domains(*list_of_domains)
297-
298-
return returned_obj
256+
return FILE_CONTENT[CONFIG_FILE]
299257

300258

301259
def reset_config_file():
@@ -331,7 +289,7 @@ def get_embed(file_owner_or_url, file_id=None, width="100%", height=525):
331289
332290
"""
333291
plotly_rest_url = (session.get_session_config().get('plotly_domain') or
334-
get_config_file(validate=False)['plotly_domain'])
292+
get_config_file()['plotly_domain'])
335293
if file_id is None: # assume we're using a url
336294
url = file_owner_or_url
337295
if url[:len(plotly_rest_url)] != plotly_rest_url:
@@ -428,7 +386,7 @@ def embed(file_owner_or_url, file_id=None, width="100%", height=525):
428386
if file_id:
429387
plotly_domain = (
430388
session.get_session_config().get('plotly_domain') or
431-
get_config_file(validate=False)['plotly_domain']
389+
get_config_file()['plotly_domain']
432390
)
433391
url = "{plotly_domain}/~{un}/{fid}".format(
434392
plotly_domain=plotly_domain,
@@ -454,7 +412,7 @@ def embed(file_owner_or_url, file_id=None, width="100%", height=525):
454412

455413

456414
### mpl-related tools ###
457-
@utils.template_doc(**get_config_file(validate=False))
415+
@utils.template_doc(**get_config_file())
458416
def mpl_to_plotly(fig, resize=False, strip_style=False, verbose=False):
459417
"""Convert a matplotlib figure to plotly dictionary and send.
460418

plotly/utils.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
"""
88
from __future__ import absolute_import
99

10+
import decimal
1011
import os.path
1112
import re
1213
import sys
1314
import threading
14-
import decimal
15+
import warnings
1516
from collections import deque
1617

1718
import pytz
@@ -32,6 +33,30 @@
3233
lock = threading.Lock()
3334

3435

36+
http_msg = (
37+
"The plotly_domain and plotly_api_domain of your config file must start "
38+
"with 'https', 'http'.\n If you are not using On-Prem then run the "
39+
"following code to ensure your plotly_domain and plotly_api_domain start "
40+
"with 'https':\n\n\n"
41+
"import plotly\n"
42+
"plotly.tools.set_config_file(\n"
43+
" plotly_domain='https://plot.ly',\n"
44+
" plotly_api_domain='https://api.plot.ly'\n"
45+
")\n\n\n"
46+
"If you are using On-Prem then you will need to use your company's "
47+
"domain and api_domain urls:\n\n\n"
48+
"import plotly\n"
49+
"plotly.tools.set_config_file(\n"
50+
" plotly_domain='https://plotly.your-company.com',\n"
51+
" plotly_api_domain='https://plotly.your-company.com'\n"
52+
")\n\n\n"
53+
"Make sure to replace `your-company.com` with the URL of your Plotly "
54+
"On-Premise server.\nSee "
55+
"https://plot.ly/python/getting-started/#special-instructions-for-plotly-onpremise-users "
56+
"for more help with getting started with On-Prem."
57+
)
58+
59+
3560
### general file setup tools ###
3661

3762
def load_json_dict(filename, *args):
@@ -297,6 +322,7 @@ def encode_as_decimal(obj):
297322
else:
298323
raise NotEncodable
299324

325+
300326
### unicode stuff ###
301327
def decode_unicode(coll):
302328
if isinstance(coll, list):
@@ -436,6 +462,16 @@ def validate_world_readable_and_sharing_settings(option_set):
436462
)
437463

438464

465+
def validate_plotly_domains(option_set):
466+
domains_not_none = []
467+
for d in ['plotly_domain', 'plotly_api_domain']:
468+
if d in option_set and option_set[d]:
469+
domains_not_none.append(option_set[d])
470+
471+
if not all(d.lower().startswith('https') for d in domains_not_none):
472+
warnings.warn(http_msg, category=UserWarning)
473+
474+
439475
def set_sharing_and_world_readable(option_set):
440476
if 'world_readable' in option_set and 'sharing' not in option_set:
441477
option_set['sharing'] = (

0 commit comments

Comments
 (0)