20
20
from plotly import utils
21
21
from plotly import exceptions
22
22
from plotly import session
23
+ from plotly .files import (CONFIG_FILE , CREDENTIALS_FILE , FILE_CONTENT ,
24
+ GRAPH_REFERENCE_FILE , check_file_permissions )
23
25
24
26
25
27
# Warning format
@@ -48,39 +50,6 @@ def warning_on_one_line(message, category, filename, lineno,
48
50
except ImportError :
49
51
_numpy_imported = False
50
52
51
- PLOTLY_DIR = os .path .join (os .path .expanduser ("~" ), ".plotly" )
52
- CREDENTIALS_FILE = os .path .join (PLOTLY_DIR , ".credentials" )
53
- CONFIG_FILE = os .path .join (PLOTLY_DIR , ".config" )
54
- TEST_DIR = os .path .join (os .path .expanduser ("~" ), ".test" )
55
- TEST_FILE = os .path .join (PLOTLY_DIR , ".permission_test" )
56
-
57
- # this sets both the DEFAULTS and the TYPES for these items
58
- _FILE_CONTENT = {CREDENTIALS_FILE : {'username' : '' ,
59
- 'api_key' : '' ,
60
- 'proxy_username' : '' ,
61
- 'proxy_password' : '' ,
62
- 'stream_ids' : []},
63
- CONFIG_FILE : {'plotly_domain' : 'https://plot.ly' ,
64
- 'plotly_streaming_domain' : 'stream.plot.ly' ,
65
- 'plotly_api_domain' : 'https://api.plot.ly' ,
66
- 'plotly_ssl_verification' : True ,
67
- 'plotly_proxy_authorization' : False ,
68
- 'world_readable' : True }}
69
-
70
-
71
- try :
72
- os .mkdir (TEST_DIR )
73
- os .rmdir (TEST_DIR )
74
- if not os .path .exists (PLOTLY_DIR ):
75
- os .mkdir (PLOTLY_DIR )
76
- f = open (TEST_FILE , 'w' )
77
- f .write ('testing\n ' )
78
- f .close ()
79
- os .remove (TEST_FILE )
80
- _file_permissions = True
81
- except :
82
- _file_permissions = False
83
-
84
53
85
54
def get_config_defaults ():
86
55
"""
@@ -92,28 +61,27 @@ def get_config_defaults():
92
61
# do something
93
62
94
63
"""
95
- return dict (_FILE_CONTENT [CONFIG_FILE ]) # performs a shallow copy
64
+ return dict (FILE_CONTENT [CONFIG_FILE ]) # performs a shallow copy
96
65
97
66
98
- def check_file_permissions ():
99
- return _file_permissions
100
67
101
68
102
69
def ensure_local_plotly_files ():
103
70
"""Ensure that filesystem is setup/filled out in a valid way"""
104
- if _file_permissions :
71
+ if check_file_permissions () :
105
72
for fn in [CREDENTIALS_FILE , CONFIG_FILE ]:
106
73
utils .ensure_file_exists (fn )
107
74
contents = utils .load_json_dict (fn )
108
- for key , val in list (_FILE_CONTENT [fn ].items ()):
75
+ for key , val in list (FILE_CONTENT [fn ].items ()):
109
76
# TODO: removed type checking below, may want to revisit
110
77
if key not in contents :
111
78
contents [key ] = val
112
79
contents_keys = list (contents .keys ())
113
80
for key in contents_keys :
114
- if key not in _FILE_CONTENT [fn ]:
81
+ if key not in FILE_CONTENT [fn ]:
115
82
del contents [key ]
116
83
utils .save_json_dict (fn , contents )
84
+ ensure_graph_reference_file ()
117
85
else :
118
86
warnings .warn ("Looks like you don't have 'read-write' permission to "
119
87
"your 'home' ('~') directory or to our '~/.plotly' "
@@ -140,7 +108,7 @@ def set_credentials_file(username=None,
140
108
:param (str) proxy_password: The pw associated with your Proxy un
141
109
142
110
"""
143
- if not _file_permissions :
111
+ if not check_file_permissions () :
144
112
raise exceptions .PlotlyError ("You don't have proper file permissions "
145
113
"to run this function." )
146
114
ensure_local_plotly_files () # make sure what's there is OK
@@ -168,11 +136,11 @@ def get_credentials_file(*args):
168
136
get_credentials_file('username')
169
137
170
138
"""
171
- if _file_permissions :
139
+ if check_file_permissions () :
172
140
ensure_local_plotly_files () # make sure what's there is OK
173
141
return utils .load_json_dict (CREDENTIALS_FILE , * args )
174
142
else :
175
- return _FILE_CONTENT [CREDENTIALS_FILE ]
143
+ return FILE_CONTENT [CREDENTIALS_FILE ]
176
144
177
145
178
146
def reset_credentials_file ():
@@ -199,7 +167,7 @@ def set_config_file(plotly_domain=None,
199
167
:param (bool) world_readable: True = public, False = private
200
168
201
169
"""
202
- if not _file_permissions :
170
+ if not check_file_permissions () :
203
171
raise exceptions .PlotlyError ("You don't have proper file permissions "
204
172
"to run this function." )
205
173
ensure_local_plotly_files () # make sure what's there is OK
@@ -243,11 +211,11 @@ def get_config_file(*args):
243
211
get_config_file('plotly_domain')
244
212
245
213
"""
246
- if _file_permissions :
214
+ if check_file_permissions () :
247
215
ensure_local_plotly_files () # make sure what's there is OK
248
216
return utils .load_json_dict (CONFIG_FILE , * args )
249
217
else :
250
- return _FILE_CONTENT [CONFIG_FILE ]
218
+ return FILE_CONTENT [CONFIG_FILE ]
251
219
252
220
253
221
def reset_config_file ():
0 commit comments