Skip to content

Commit 9b783a3

Browse files
authored
Merge pull request #112 from sebastianpech/fix#111
Fix#111
2 parents 6359aed + 517dfaa commit 9b783a3

File tree

8 files changed

+39
-78
lines changed

8 files changed

+39
-78
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.6.2
2+
* `@tag!` and `@tagsave` now support using `;` as keywords separator (#111)
3+
14
# 1.6.0
25
* `quickactivate` doesn't do anything anymore if you try to activate to already active project.
36
* New macro `@quickactivate`

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DrWatson"
22
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
33
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
4-
version = "1.6.1"
4+
version = "1.6.2"
55

66

77

src/DrWatson.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const PATH_SEPARATOR = joinpath("_", "_")[2]
66

77
# Misc functions for kw-macros
88
convert_to_kw(ex::Expr) = Expr(:kw,ex.args...)
9-
iskwdefinition(ex) = false
10-
iskwdefinition(ex::Expr) = ex.head == Symbol("=")
9+
convert_to_kw(ex) = error("invalid keyword argument syntax \"$ex\"")
1110

1211
# Pure Julia implementation
1312
include("project_setup.jl")

src/project_setup.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ export projectdir, datadir, srcdir, plotsdir, scriptsdir, papersdir
55
export projectname
66
export findproject, quickactivate, @quickactivate
77

8-
@deprecate scriptdir scriptsdir #TODO: remove in next release
9-
108
"""
119
function is_standard_julia_project()
1210

src/saving_files.jl

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,19 @@ Same as [`tagsave`](@ref) but one more field `:script` is added that records
9999
the local path of the script and line number that called `@tagsave`, see [`@tag!`](@ref).
100100
"""
101101
macro tagsave(file,d,args...)
102+
args = Any[args...]
103+
# Keywords added after a ; are moved to the front of the expression
104+
# that is passed to the macro. So instead of getting the filename in file
105+
# an Expr is passed.
106+
if file isa Expr && file.head == :parameters
107+
length(args) > 0 || return :(throw(MethodError(@tagsave,$(esc(file)),$(esc(d)),$(esc.(args)...))))
108+
extra_kw_def = file.args
109+
file = d
110+
d = popfirst!(args)
111+
append!(args,extra_kw_def)
112+
end
102113
s = QuoteNode(__source__)
103-
N = length(args)
104-
(N == 0 || all(iskwdefinition.(args))) &&
105-
return :(tagsave($(esc(file)), $(esc(d)),$(esc.(convert_to_kw.(args))...),source=$s))
106-
# First optional arg. is not needed as if it's not provided dispatch
107-
# is done through the kw-version of the function (ie. this line is
108-
# never reached)
109-
default = [
110-
:(projectdir()), #gitpath
111-
:(true), #storepatch
112-
]
113-
:(tagsave($(esc(file)),$(esc(d)), $(esc.(args)...), $(esc.(default[N:end])...), $s))
114-
# Use this code after the deprecation warning for the non-kw version
115-
# is removed.
116-
# throw(MethodError(@tagsave,args...))
114+
return :(tagsave($(esc(file)), $(esc(d)), $(esc.(convert_to_kw.(args))...),source=$s))
117115
end
118116

119117
################################################################################
@@ -204,10 +202,4 @@ function tmpsave(dicts, tmp = projectdir("_research", "tmp");
204202
wsave(joinpath(tmp, x), copy(dicts[i]))
205203
end
206204
r
207-
end
208-
209-
@deprecate tagsave(file, d, p::String) tagsave(file, d, gitpath=p)
210-
@deprecate tagsave(file, d, safe::Bool, gitpath = projectdir(), storepatch = true, source = nothing) tagsave(file,d,safe=safe,gitpath=gitpath,storepatch=storepatch,source=source)
211-
# TODO: When removing the deprecation warning, the tests must be adapted
212-
# to only use the kw-version of this function. Also the code from the
213-
# macro version for parsing the non-kw arguments can be replaced.
205+
end

src/saving_tools.jl

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,18 @@ Dict{Symbol,Any} with 3 entries:
199199
```
200200
"""
201201
macro tag!(d,args...)
202+
args = Any[args...]
203+
# Keywords added after a ; are moved to the front of the expression
204+
# that is passed to the macro. So instead of getting the dict in d
205+
# an Expr is passed.
206+
if d isa Expr && d.head == :parameters
207+
length(args) > 0 || return :(throw(MethodError(@tag!,$(esc(d)),$(esc.(args)...))))
208+
extra_kw_def = d.args
209+
d = popfirst!(args)
210+
append!(args,extra_kw_def)
211+
end
202212
s = QuoteNode(__source__)
203-
N = length(args)
204-
(N == 0 || all(iskwdefinition.(args))) &&
205-
return :(tag!($(esc(d)),$(esc.(convert_to_kw.(args))...),source=$s))
206-
# First optional arg. is not needed as if it's not provided dispatch
207-
# is done through the kw-version of the function (ie. this line is
208-
# never reached)
209-
default = [
210-
:(true), #storepatch
211-
]
212-
:(tag!($(esc(d)), $(esc.(args)...), $(esc.(default[N:end])...), $s))
213-
# Use this code after the deprecation warning for the non-kw version
214-
# is removed.
215-
# throw(MethodError(@tag!,args...))
213+
return :(tag!($(esc(d)),$(esc.(convert_to_kw.(args))...),source=$s))
216214
end
217215

218216
"""
@@ -303,9 +301,4 @@ tagsave(savename(s), struct2dict(s))
303301
"""
304302
function struct2dict(s)
305303
Dict(x => getfield(s, x) for x in fieldnames(typeof(s)))
306-
end
307-
308-
@deprecate tag!(d::Dict, gitpath, storepatch = true, source = nothing) tag!(d,gitpath=gitpath,storepatch=storepatch,source=source)
309-
# TODO: When removing the deprecation warning, the tests must be adapted
310-
# to only use the kw-version of this function. Also the code from the
311-
# macro version for parsing the non-kw arguments can be replaced.
304+
end

test/savefiles_tests.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ end
1919
# tagsave #
2020
################################################################################
2121
t = f(simulation)
22-
tagsave(savename(simulation, "bson"), t, findproject())
22+
tagsave(savename(simulation, "bson"), t, gitpath=findproject())
2323
file = load(savename(simulation, "bson"))
2424
@test "gitcommit" keys(file)
2525
@test file["gitcommit"] |> typeof == String
2626
rm(savename(simulation, "bson"))
2727

2828
t = f(simulation)
29-
@tagsave(savename(simulation, "bson"), t, false, findproject())
29+
@tagsave(savename(simulation, "bson"), t, safe=false, gitpath=findproject())
3030
file = load(savename(simulation, "bson"))
3131
@test "gitcommit" keys(file)
3232
@test file["gitcommit"] |> typeof == String
@@ -35,7 +35,7 @@ file = load(savename(simulation, "bson"))
3535
@test file["script"] == joinpath("test", "savefiles_tests.jl#29")
3636

3737
t = f(simulation)
38-
@tagsave(savename(simulation, "bson"), t, true, findproject())
38+
@tagsave(savename(simulation, "bson"), t, safe=true, gitpath=findproject())
3939
sn = savename(simulation, "bson")[1:end-5]*"_#1"*".bson"
4040
@test isfile(sn)
4141
rm(sn)
@@ -58,13 +58,9 @@ rm(sn)
5858
rm(savename(simulation, "bson"))
5959
@test !isfile(savename(simulation, "bson"))
6060

61-
ex = @macroexpand @tagsave("name",d,false)
62-
@test ex.args[1].name == :tagsave
63-
@test ex.args[2] == "name"
64-
@test ex.args[3] == :d
65-
@test ex.args[4] == false
66-
@test ex.args[5] == :(projectdir())
67-
@test ex.args[6] == true
61+
ex = @macroexpand @tagsave("testname.bson", (@dict a b c ), storepatch=false; safe=true)
62+
ex2 = @macroexpand @tagsave("testname.bson", @dict a b c; storepatch=false, safe=true)
63+
@test ex.args[1:end-1] == ex2.args[1:end-1]
6864

6965
# Remove leftover
7066

@@ -110,4 +106,4 @@ end
110106
@test data[3] == load("test.#backup.jld2")
111107
rm("test.#backup.jld2")
112108
rm("test.#backup_#1.jld2")
113-
rm("test.#backup_#2.jld2")
109+
rm("test.#backup_#2.jld2")

test/stools_tests.jl

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ com = gitdescribe(dirname(@__DIR__))
1212
d1 = Dict(:x => 3, :y => 4)
1313
d2 = Dict("x" => 3, "y" => 4)
1414
for d in (d1, d2)
15-
d = tag!(d, @__DIR__)
15+
d = tag!(d, gitpath=@__DIR__)
1616

1717
@test haskey(d, keytype(d)(:gitcommit))
1818
@test d[keytype(d)(:gitcommit)] |> typeof <: String
1919
end
2020

2121
# @tag!
2222
for d in (d1, d2)
23-
d = @tag!(d, @__DIR__)
23+
d = @tag!(d, gitpath=@__DIR__)
2424
@test haskey(d, keytype(d)(:gitcommit))
2525
@test d[keytype(d)(:gitcommit)] |> typeof <: String
2626
@test split(d[keytype(d)(:script)], '#')[1] == basename(@__FILE__)
@@ -39,26 +39,6 @@ d_new = tag!(d,gitpath=@__DIR__,source="foo")
3939
d_new = @tag!(d,gitpath=@__DIR__)
4040
@test split(d_new[keytype(d_new)(:script)], '#')[1] == basename(@__FILE__)
4141

42-
ex = @macroexpand @tag!(d)
43-
44-
@test ex.args[1].name == :tag!
45-
@test ex.args[2] == :d
46-
@test ex.args[3].head == :kw
47-
48-
ex = @macroexpand @tag!(d,"path")
49-
50-
@test ex.args[1].name == :tag!
51-
@test ex.args[2] == :d
52-
@test ex.args[3] == "path"
53-
@test ex.args[4] == true
54-
55-
ex = @macroexpand @tag!(d,"path",false)
56-
57-
@test ex.args[1].name == :tag!
58-
@test ex.args[2] == :d
59-
@test ex.args[3] == "path"
60-
@test ex.args[4] == false
61-
6242
ex = @macroexpand @tag!(d,gitpath="path")
6343

6444
@test ex.args[1].name == :tag!

0 commit comments

Comments
 (0)