Skip to content

Commit ed6d369

Browse files
committed
ENH: modernize the src/utils.jl file
1 parent 2d12d4b commit ed6d369

File tree

3 files changed

+131
-138
lines changed

3 files changed

+131
-138
lines changed

src/Plotly.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ Post a local Plotly plot to the Plotly cloud.
6363
6464
Must be signed in first.
6565
"""
66-
function post(p::Plot; fileopt=:overwrite, filename=nothing, kwargs...)
67-
fileopt = filename == nothing ? :create : Symbol(fileopt)
66+
function post(p::Plot; fileopt=get_config().fileopt, filename=nothing, kwargs...)
67+
fileopt = Symbol(fileopt)
6868
grid_fn = string(filename, "_", "Grid")
6969
clean_p = srcify(p; fileopt=fileopt, grid_fn=grid_fn, kwargs...)
7070
if fileopt == :overwrite
@@ -76,7 +76,7 @@ function post(p::Plot; fileopt=:overwrite, filename=nothing, kwargs...)
7676
return RemotePlot(res["web_url"])
7777
end
7878
end
79-
if fileopt == :create
79+
if fileopt == :create || fileopt == :new
8080
if filename == nothing
8181
res = plot_create(clean_p; kwargs...)
8282
else
@@ -195,7 +195,7 @@ end
195195
extract_grid_data(p::Plot) = extract_grid_data!(deepcopy(p))
196196

197197
"""
198-
srcify!(p::Plot; fileopt::Symbol=:overwrite, grid_fn=nothing, kwargs...)
198+
srcify!(p::Plot; fileopt::Symbol=get_config().fileopt, grid_fn=nothing, kwargs...)
199199
200200
This function does three things:
201201
@@ -209,7 +209,7 @@ then the changes described above will happen in-place on the grid.
209209
210210
If either of those conditions are not met, then a new grid will be created.
211211
"""
212-
function srcify!(p::Plot; fileopt::Symbol=:overwrite, grid_fn=nothing, kwargs...)
212+
function srcify!(p::Plot; fileopt::Symbol=get_config().fileopt, grid_fn=nothing, kwargs...)
213213
data_for_grid = extract_grid_data!(p)
214214
temp_map = Dict()
215215
for (k, v) in data_for_grid
@@ -227,7 +227,7 @@ function srcify!(p::Plot; fileopt::Symbol=:overwrite, grid_fn=nothing, kwargs...
227227
end
228228
end
229229

230-
if fileopt == :create
230+
if fileopt == :create || fileopt == :new
231231
# add order to each grid
232232
for (i, (k, v)) in enumerate(data_for_grid)
233233
v["order"] = i-1

src/utils.jl

Lines changed: 100 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,56 @@
11
using 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
116
end
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+
)
1636
end
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
3363
end
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
5689
end
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
79104
end
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)
110126
end
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)
141149
end
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()
165166
end
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()
188176
end

src/v2.jl

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -349,26 +349,6 @@ end
349349

350350
try_lookup(path; kwargs...) = try_me(file_lookup, path; kwargs...)
351351

352-
353-
"""
354-
grid_overwrite(cols::Associative; fid::String="", path::String="")
355-
356-
Replace the data in the grid assocaited with fid `fid` or at the path `path`
357-
with data in `cols`. `cols` should be an associative mapping from column names
358-
to column data. The output of this function is an associative mapping from
359-
column names to column uids in the updated grid.
360-
361-
There are three possible scenarios for the data:
362-
363-
1. The column appears both in the grid and in `cols`. In this case the data in
364-
that column of the grid will be updated to match the data in `cols`
365-
2. The column appears only in `cols`. In this case a new column will be created
366-
in the grid
367-
3. The column appears only in the grid. Nothing happens...
368-
369-
NOTE: only one of `fid` and `path` can be passed
370-
371-
"""
372352
function grid_overwrite!(cols::Associative; fid::String="", path::String="")
373353
!isempty(fid) && !isempty(path) && error("Can't pass both fid and path")
374354
if !isempty(fid)
@@ -380,7 +360,10 @@ function grid_overwrite!(cols::Associative; fid::String="", path::String="")
380360
end
381361

382362
grid_info == nothing && error("can't overwrite a grid that doesn't exit")
363+
grid_overwrite!(grid_info, cols)
364+
end
383365

366+
function grid_overwrite!(grid_info::Associative, cols::Associative)
384367
col_name_uid = Dict()
385368
for col in grid_info["cols"]
386369
col_name_uid[col["name"]] = col["uid"]
@@ -418,3 +401,25 @@ function grid_overwrite!(cols::Associative; fid::String="", path::String="")
418401

419402
out
420403
end
404+
405+
406+
"""
407+
grid_overwrite(cols::Associative; fid::String="", path::String="")
408+
409+
Replace the data in the grid assocaited with fid `fid` or at the path `path`
410+
with data in `cols`. `cols` should be an associative mapping from column names
411+
to column data. The output of this function is an associative mapping from
412+
column names to column uids in the updated grid.
413+
414+
There are three possible scenarios for the data:
415+
416+
1. The column appears both in the grid and in `cols`. In this case the data in
417+
that column of the grid will be updated to match the data in `cols`
418+
2. The column appears only in `cols`. In this case a new column will be created
419+
in the grid
420+
3. The column appears only in the grid. Nothing happens...
421+
422+
NOTE: only one of `fid` and `path` can be passed
423+
424+
"""
425+
grid_overwrite!

0 commit comments

Comments
 (0)