11using JSON
22
3- const default_endpoints = Dict (
4- " base" => " https://plot.ly" ,
5- " api" => " https://api.plot.ly/v2"
6- )
7-
8- type PlotlyCredentials
3+ struct PlotlyCredentials
94 username:: String
105 api_key:: String
116end
127
13- type PlotlyConfig
8+ mutable struct PlotlyConfig
149 plotly_domain:: String
1510 plotly_api_domain:: String
11+ plotly_streaming_domain:: String
12+ plotly_proxy_authorization:: Bool
13+ plotly_ssl_verification:: Bool
14+ sharing:: String
15+ world_readable:: Bool
16+ auto_open:: Bool
17+ fileopt:: Symbol
18+ end
19+
20+ const DEFAULT_CONFIG = PlotlyConfig (
21+ " https://plot.ly" ,
22+ " https://api.plot.ly/v2" ,
23+ " stream.plot.ly" ,
24+ false ,
25+ true ,
26+ " public" ,
27+ true ,
28+ true ,
29+ :create
30+ )
31+
32+ function Base. merge (config:: PlotlyConfig , other:: Associative )
33+ PlotlyConfig (
34+ [get (other, string (name), getfield (config, name)) for name in fieldnames (config)]. ..
35+ )
1636end
1737
18- function signin (username:: String , api_key:: String , endpoints= nothing )
19- # Define session credentials/endpoint configuration, where endpoint is a Dict
38+ Base. show (io:: IO , config:: PlotlyConfig ) = dump (IOContext (io, limit= true ), config)
39+
40+ function Base. Dict (config:: PlotlyConfig )
41+ Dict (k => getfield (config, k) for k in fieldnames (config))
42+ end
2043
44+ """
45+ signin(username::String, api_key::String, endpoints=nothing)
46+
47+ Define session credentials/endpoint configuration, where endpoint is a Dict
48+ """
49+ function signin (username:: String , api_key:: String , endpoints:: Union{Void,Associative} = nothing )
2150 global plotlycredentials = PlotlyCredentials (username, api_key)
2251
23- # if endpoints are specified both the base and api domains must be specified
52+ # if endpoints are specified both the base and api domains must be
53+ # specified
2454 if endpoints != nothing
2555 try
2656 base_domain = endpoints[" plotly_domain" ]
@@ -32,9 +62,12 @@ function signin(username::String, api_key::String, endpoints=nothing)
3262 end
3363end
3464
35- function get_credentials ()
36- # Return the session credentials if defined --> otherwise use .credentials specs
65+ """
66+ get_credentials()
3767
68+ Return the session credentials if defined --> otherwise use .credentials specs
69+ """
70+ function get_credentials ()
3871 if ! isdefined (Plotly, :plotlycredentials )
3972
4073 creds = get_credentials_file ()
@@ -55,134 +88,89 @@ function get_credentials()
5588 return plotlycredentials
5689end
5790
58- function get_config ()
59- # Return the session configuration if defined --> otherwise use .config specs
60-
61- if ! isdefined (Plotly,:plotlyconfig )
91+ """
92+ get_config()
6293
94+ Return the session configuration if defined --> otherwise use .config specs
95+ """
96+ function get_config ()
97+ if ! isdefined (Plotly, :plotlyconfig )
6398 config = get_config_file ()
64-
65- if isempty (config)
66- base_domain = default_endpoints[" base" ]
67- api_domain = default_endpoints[" api" ]
68- else
69- base_domain = get (config, " plotly_domain" , default_endpoints[" base" ])
70- api_domain = get (config, " plotly_api_domain" , default_endpoints[" api" ])
71- end
72-
73- global plotlyconfig = PlotlyConfig (base_domain, api_domain)
74-
99+ global plotlyconfig = merge (DEFAULT_CONFIG, config)
75100 end
76101
77102 # will persist for the remainder of the session
78103 return plotlyconfig
79104end
80105
81- function set_credentials_file (input_creds:: Dict )
82- # Save Plotly endpoint configuration as JSON key-value pairs in
83- # userhome/.plotly/.credentials. This includes username and api_key.
106+ """
107+ set_credentials_file(input_creds::Associative)
84108
85- # plotly credentials file
86- userhome = homedir ()
87- plotly_credentials_folder = joinpath (userhome, " .plotly" )
88- plotly_credentials_file = joinpath (plotly_credentials_folder, " .credentials" )
109+ Save Plotly endpoint configuration as JSON key-value pairs in
110+ userhome/.plotly/.credentials. This includes username and api_key.
111+ """
112+ function set_credentials_file (input_creds:: Associative )
113+ credentials_folder = joinpath (homedir (), " .plotly" )
114+ credentials_file = joinpath (credentials_folder, " .credentials" )
89115
90- # check to see if dir/file exists --> if not, create it
91- try
92- mkdir (plotly_credentials_folder)
93- catch err
94- isa (err, SystemError) || rethrow (err)
95- end
116+ # check to see if dir/file exists --> if not, create it
117+ ! isdir (credentials_folder) && mkdir (credentials_folder)
96118
97119 prev_creds = get_credentials_file ()
120+ creds = merge (prev_creds, input_creds)
98121
99- # merge input creds with prev creds
100- if ! isempty (prev_creds)
101- creds = merge (prev_creds, input_creds)
102- else
103- creds = input_creds
122+ # write the json strings to the cred file
123+ open (credentials_file, " w" ) do creds_file
124+ write (creds_file, JSON. json (creds))
104125 end
105-
106- # write the json strings to the cred file
107- creds_file = open (plotly_credentials_file, " w" )
108- write (creds_file, JSON. json (creds))
109- close (creds_file)
110126end
111127
112- function set_config_file (input_config:: Dict )
113- # Save Plotly endpoint configuration as JSON key-value pairs in
114- # userhome/.plotly/.config. This includes the plotly_domain, and plotly_api_domain.
128+ """
129+ set_config_file(input_config::Associative)
115130
116- # plotly configuration file
117- userhome = homedir ()
118- plotly_config_folder = joinpath (userhome, " .plotly" )
119- plotly_config_file = joinpath (plotly_config_folder, " .config" )
131+ Save Plotly endpoint configuration as JSON key-value pairs in
132+ userhome/.plotly/.config. This includes the plotly_domain, and
133+ plotly_api_domain.
134+ """
135+ function set_config_file (input_config:: Associative )
136+ config_folder = joinpath (homedir (), " .plotly" )
137+ config_file = joinpath (config_folder, " .config" )
120138
121- # check to see if dir/file exists --> if not create it
122- try
123- mkdir (plotly_config_folder)
124- catch err
125- isa (err, SystemError) || rethrow (err)
126- end
139+ # check to see if dir/file exists --> if not create it
140+ ! isdir (config_folder) && mkdir (config_folder)
127141
128142 prev_config = get_config_file ()
143+ config = merge (prev_config, input_config)
129144
130- # merge input config with prev config
131- if ! isempty (prev_config)
132- config = merge (prev_config, input_config)
133- else
134- config = input_config
145+ # write the json strings to the config file
146+ open (config_file, " w" ) do config_file
147+ write (config_file, JSON. json (config))
135148 end
136-
137- # write the json strings to the config file
138- config_file = open (plotly_config_file, " w" )
139- write (config_file, JSON. json (config))
140- close (config_file)
141149end
142150
143- function get_credentials_file ()
144- # Load user credentials as a Dict
145-
146- # plotly credentials file
147- userhome = homedir ()
148- plotly_credentials_folder = joinpath (userhome, " .plotly" )
149- plotly_credentials_file = joinpath (plotly_credentials_folder, " .credentials" )
151+ """
152+ set_config_file(config::PlotlyConfig)
150153
151- if ! isfile (plotly_credentials_file)
152- creds = Dict ()
153- else
154- creds_file = open (plotly_credentials_file)
155- creds = JSON. parse (creds_file)
156-
157- if creds == nothing
158- creds = Dict ()
159- end
160-
161- end
154+ Set the values in the configuration file to match the values in config
155+ """
156+ set_config_file (config:: PlotlyConfig ) = set_config_file (Dict (config))
162157
163- return creds
158+ """
159+ get_credentials_file()
164160
161+ Load user credentials informaiton as a dict
162+ """
163+ function get_credentials_file ()
164+ cred_file = joinpath (homedir (), " .plotly" , " .credentials" )
165+ isfile (cred_file) ? JSON. parsefile (cred_file) : Dict ()
165166end
166167
167- function get_config_file ()
168- # Load endpoint configuration as a Dict
169-
170- # plotly configuration file
171- userhome = homedir ()
172- plotly_config_folder = joinpath (userhome, " .plotly" )
173- plotly_config_file = joinpath (plotly_config_folder, " .config" )
168+ """
169+ get_config_file()
174170
175- if ! isfile (plotly_config_file)
176- config = Dict ()
177- else
178- config_file = open (plotly_config_file)
179- config = JSON. parse (config_file)
180-
181- if config == nothing
182- config = Dict ()
183- end
184-
185- end
186-
187- return config
171+ Load endpoint configuration as a Dict
172+ """
173+ function get_config_file ()
174+ config_file = joinpath (homedir (), " .plotly" , " .config" )
175+ isfile (config_file) ? JSON. parsefile (config_file) : Dict ()
188176end
0 commit comments