Skip to content

Commit 92981d9

Browse files
committed
return empty dict from get_credentials/config_file if file d.n.e or is empty
1 parent a703035 commit 92981d9

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

src/utils.jl

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ function get_credentials()
3535
# Return the session credentials if defined --> otherwise use .credentials specs
3636

3737
if !isdefined(Plotly,:plotlycredentials)
38+
3839
creds = get_credentials_file()
40+
3941
try
4042
username = creds["username"]
4143
api_key = creds["api_key"]
44+
4245
global plotlycredentials = PlotlyCredentials(username, api_key)
46+
4347
catch
4448
error("Please 'signin(username, api_key)' before proceeding. See
4549
http://plot.ly/API for help!")
@@ -54,10 +58,19 @@ function get_config()
5458
# Return the session configuration if defined --> otherwise use .config specs
5559

5660
if !isdefined(Plotly,:plotlyconfig)
61+
5762
config = get_config_file()
58-
base_domain = get(config, "plotly_domain", default_endpoints["base"])
59-
api_domain = get(config, "plotly_api_domain", default_endpoints["api"])
63+
64+
if isempty(config)
65+
base_domain = default_endpoints["base"]
66+
api_domain = default_endpoints["api"]
67+
else
68+
base_domain = get(config, "plotly_domain", default_endpoints["base"])
69+
api_domain = get(config, "plotly_api_domain", default_endpoints["api"])
70+
end
71+
6072
global plotlyconfig = PlotlyConfig(base_domain, api_domain)
73+
6174
end
6275

6376
# will persist for the remainder of the session
@@ -68,26 +81,22 @@ function set_credentials_file(input_creds::Dict)
6881
# Save Plotly endpoint configuration as JSON key-value pairs in
6982
# userhome/.plotly/.credentials. This includes username and api_key.
7083

71-
prev_creds = {}
72-
73-
try
74-
prev_creds = get_credentials_file()
75-
end
76-
7784
# plotly credentials file
7885
userhome = get(ENV, "HOME", "")
7986
plotly_credentials_folder = joinpath(userhome, ".plotly")
8087
plotly_credentials_file = joinpath(plotly_credentials_folder, ".credentials")
8188

82-
#check to see if dir/file exists --> if not create it
89+
#check to see if dir/file exists --> if not, create it
8390
try
8491
mkdir(plotly_credentials_folder)
8592
catch err
8693
isa(err, SystemError) || rethrow(err)
8794
end
8895

96+
prev_creds = get_credentials_file()
97+
8998
#merge input creds with prev creds
90-
if prev_creds != {}
99+
if !isempty(prev_creds)
91100
creds = merge(prev_creds, input_creds)
92101
else
93102
creds = input_creds
@@ -103,12 +112,6 @@ function set_config_file(input_config::Dict)
103112
# Save Plotly endpoint configuration as JSON key-value pairs in
104113
# userhome/.plotly/.config. This includes the plotly_domain, and plotly_api_domain.
105114

106-
prev_config = {}
107-
108-
try
109-
prev_config = get_config_file()
110-
end
111-
112115
# plotly configuration file
113116
userhome = get(ENV, "HOME", "")
114117
plotly_config_folder = joinpath(userhome, ".plotly")
@@ -121,8 +124,10 @@ function set_config_file(input_config::Dict)
121124
isa(err, SystemError) || rethrow(err)
122125
end
123126

127+
prev_config = get_config_file()
128+
124129
#merge input config with prev config
125-
if prev_config != {}
130+
if !isempty(prev_config)
126131
config = merge(prev_config, input_config)
127132
else
128133
config = input_config
@@ -143,19 +148,19 @@ function get_credentials_file()
143148
plotly_credentials_file = joinpath(plotly_credentials_folder, ".credentials")
144149

145150
if !isfile(plotly_credentials_file)
146-
error(" No credentials file found. Please Set up your credentials
147-
file by running set_credentials_file({\"username\": \"your_plotly_username\", ...
148-
\"api_key\": \"your_plotly_api_key\"})")
149-
end
151+
creds = {}
152+
else
153+
creds_file = open(plotly_credentials_file)
154+
creds = JSON.parse(creds_file)
150155

151-
creds_file = open(plotly_credentials_file)
152-
creds = JSON.parse(creds_file)
156+
if creds == nothing
157+
creds = {}
158+
end
153159

154-
if creds == nothing
155-
creds = {}
156160
end
157161

158162
return creds
163+
159164
end
160165

161166
function get_config_file()
@@ -167,16 +172,15 @@ function get_config_file()
167172
plotly_config_file = joinpath(plotly_config_folder, ".config")
168173

169174
if !isfile(plotly_config_file)
170-
error(" No configuration file found. Please Set up your configuration
171-
file by running set_config_file({\"plotly_domain\": \"your_plotly_domain\", ...
172-
\"plotly_api_domain\": \"your_plotly_api_domain\"})")
173-
end
175+
config = {}
176+
else
177+
config_file = open(plotly_config_file)
178+
config = JSON.parse(config_file)
174179

175-
config_file = open(plotly_config_file)
176-
config = JSON.parse(config_file)
180+
if config == nothing
181+
config = {}
182+
end
177183

178-
if config == nothing
179-
config = {}
180184
end
181185

182186
return config

0 commit comments

Comments
 (0)