Skip to content

Commit 3f7d242

Browse files
committed
Create files.py which has no import issues.
1 parent 15e2272 commit 3f7d242

File tree

4 files changed

+57
-50
lines changed

4 files changed

+57
-50
lines changed

plotly/files.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
3+
# file structure
4+
PLOTLY_DIR = os.path.join(os.path.expanduser("~"), ".plotly")
5+
CREDENTIALS_FILE = os.path.join(PLOTLY_DIR, ".credentials")
6+
CONFIG_FILE = os.path.join(PLOTLY_DIR, ".config")
7+
TEST_DIR = os.path.join(os.path.expanduser("~"), ".test")
8+
TEST_FILE = os.path.join(PLOTLY_DIR, ".permission_test")
9+
10+
# this sets both the DEFAULTS and the TYPES for these files
11+
FILE_CONTENT = {CREDENTIALS_FILE: {'username': '',
12+
'api_key': '',
13+
'proxy_username': '',
14+
'proxy_password': '',
15+
'stream_ids': []},
16+
CONFIG_FILE: {'plotly_domain': 'https://plot.ly',
17+
'plotly_streaming_domain': 'stream.plot.ly',
18+
'plotly_api_domain': 'https://api.plot.ly',
19+
'plotly_ssl_verification': True,
20+
'plotly_proxy_authorization': False,
21+
'world_readable': True}}
22+
23+
try:
24+
os.mkdir(TEST_DIR)
25+
os.rmdir(TEST_DIR)
26+
if not os.path.exists(PLOTLY_DIR):
27+
os.mkdir(PLOTLY_DIR)
28+
f = open(TEST_FILE, 'w')
29+
f.write('testing\n')
30+
f.close()
31+
os.remove(TEST_FILE)
32+
_file_permissions = True
33+
except:
34+
_file_permissions = False
35+
36+
37+
def check_file_permissions():
38+
return _file_permissions

plotly/tests/test_core/test_tools/test_configuration.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
from unittest import TestCase
44

5-
from plotly.tools import get_config_defaults, _FILE_CONTENT, CONFIG_FILE
5+
from plotly.files import CONFIG_FILE, FILE_CONTENT
6+
from plotly.tools import get_config_defaults
67

78

89
class TestGetConfigDefaults(TestCase):
910

1011
def test_config_dict_is_equivalent_copy(self):
1112

12-
original = _FILE_CONTENT[CONFIG_FILE]
13+
original = FILE_CONTENT[CONFIG_FILE]
1314
copy = get_config_defaults()
1415
self.assertIsNot(copy, original)
1516
self.assertEqual(copy, original)

plotly/tests/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from numbers import Number as Num
44
from unittest import TestCase
55

6+
from plotly.files import CREDENTIALS_FILE, CONFIG_FILE, check_file_permissions
67
from plotly import session
7-
from plotly.tools import CREDENTIALS_FILE, CONFIG_FILE, _file_permissions
88

99

1010
class PlotlyTestCase(TestCase):
@@ -24,14 +24,14 @@ def tearDown(self):
2424
self.restore_file_credentials_and_config()
2525

2626
def stash_file_credentials_and_config(self):
27-
if _file_permissions:
27+
if check_file_permissions():
2828
with open(CREDENTIALS_FILE, 'r') as f:
2929
self._file_credentials = json.load(f)
3030
with open(CONFIG_FILE, 'r') as f:
3131
self._file_config = json.load(f)
3232

3333
def restore_file_credentials_and_config(self):
34-
if _file_permissions:
34+
if check_file_permissions():
3535
if self._file_credentials is not None:
3636
with open(CREDENTIALS_FILE, 'w') as f:
3737
json.dump(self._file_credentials, f)

plotly/tools.py

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from plotly import utils
2121
from plotly import exceptions
2222
from plotly import session
23+
from plotly.files import (CONFIG_FILE, CREDENTIALS_FILE, FILE_CONTENT,
24+
GRAPH_REFERENCE_FILE, check_file_permissions)
2325

2426

2527
# Warning format
@@ -48,39 +50,6 @@ def warning_on_one_line(message, category, filename, lineno,
4850
except ImportError:
4951
_numpy_imported = False
5052

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-
8453

8554
def get_config_defaults():
8655
"""
@@ -92,28 +61,27 @@ def get_config_defaults():
9261
# do something
9362
9463
"""
95-
return dict(_FILE_CONTENT[CONFIG_FILE]) # performs a shallow copy
64+
return dict(FILE_CONTENT[CONFIG_FILE]) # performs a shallow copy
9665

9766

98-
def check_file_permissions():
99-
return _file_permissions
10067

10168

10269
def ensure_local_plotly_files():
10370
"""Ensure that filesystem is setup/filled out in a valid way"""
104-
if _file_permissions:
71+
if check_file_permissions():
10572
for fn in [CREDENTIALS_FILE, CONFIG_FILE]:
10673
utils.ensure_file_exists(fn)
10774
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()):
10976
# TODO: removed type checking below, may want to revisit
11077
if key not in contents:
11178
contents[key] = val
11279
contents_keys = list(contents.keys())
11380
for key in contents_keys:
114-
if key not in _FILE_CONTENT[fn]:
81+
if key not in FILE_CONTENT[fn]:
11582
del contents[key]
11683
utils.save_json_dict(fn, contents)
84+
ensure_graph_reference_file()
11785
else:
11886
warnings.warn("Looks like you don't have 'read-write' permission to "
11987
"your 'home' ('~') directory or to our '~/.plotly' "
@@ -140,7 +108,7 @@ def set_credentials_file(username=None,
140108
:param (str) proxy_password: The pw associated with your Proxy un
141109
142110
"""
143-
if not _file_permissions:
111+
if not check_file_permissions():
144112
raise exceptions.PlotlyError("You don't have proper file permissions "
145113
"to run this function.")
146114
ensure_local_plotly_files() # make sure what's there is OK
@@ -168,11 +136,11 @@ def get_credentials_file(*args):
168136
get_credentials_file('username')
169137
170138
"""
171-
if _file_permissions:
139+
if check_file_permissions():
172140
ensure_local_plotly_files() # make sure what's there is OK
173141
return utils.load_json_dict(CREDENTIALS_FILE, *args)
174142
else:
175-
return _FILE_CONTENT[CREDENTIALS_FILE]
143+
return FILE_CONTENT[CREDENTIALS_FILE]
176144

177145

178146
def reset_credentials_file():
@@ -199,7 +167,7 @@ def set_config_file(plotly_domain=None,
199167
:param (bool) world_readable: True = public, False = private
200168
201169
"""
202-
if not _file_permissions:
170+
if not check_file_permissions():
203171
raise exceptions.PlotlyError("You don't have proper file permissions "
204172
"to run this function.")
205173
ensure_local_plotly_files() # make sure what's there is OK
@@ -243,11 +211,11 @@ def get_config_file(*args):
243211
get_config_file('plotly_domain')
244212
245213
"""
246-
if _file_permissions:
214+
if check_file_permissions():
247215
ensure_local_plotly_files() # make sure what's there is OK
248216
return utils.load_json_dict(CONFIG_FILE, *args)
249217
else:
250-
return _FILE_CONTENT[CONFIG_FILE]
218+
return FILE_CONTENT[CONFIG_FILE]
251219

252220

253221
def reset_config_file():

0 commit comments

Comments
 (0)