From 3d623f425ef35fe777ed6277a869a7819077bac9 Mon Sep 17 00:00:00 2001 From: Teo ShaoWei Date: Sat, 27 Oct 2018 02:44:24 +0800 Subject: [PATCH] Support Julia 0.7 This commit implements the latest syntax for Julia 0.7, and so might break on Julia 0.6 and before. If needs be, `Compat` can be used to assist in backward compatibility, or alternatively just break support for earlier Julia versions. Changelog:- - Update `isdefined` to include the containing module, which is compulsory. - Replace the depreciated `matchall` with an implementation using `eachmatch`. - Replace the depreciated `chr2ind` with an implementation using `eachindex`. Instead of doing index arithmetic on Unicode string which give incorrect result, use `prevind` and `nextind`. - Replace the depreciated `takebuf_string(s)` with a `String(take!(s))` call. - Replace `Base.Test` with `Test`. - Update `replace` to use a pair to indicate the replacement. - Update `parse` of integer string to state the compulsory type to map to. For now, all numeric are assumed to be `Float64` as further casting to specific integer types can be done from there. Similarly, casting to boolean is specified accordingly. - Replace `parse` with `Meta.parse` when parsing code strings. - Use logging functions `@warn` and `@error`. `warn` has been depreciated for `@warn`, and to standardise the logging we use `@error` too. This result in a `SystemError` thrown instead of an `ErrorException`. - Update REQUIRE to use Julia 0.7. --- REQUIRE | 2 +- src/AppConf.jl | 44 +++++++++++++++++++++++--------------------- test/runtests.jl | 4 ++-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/REQUIRE b/REQUIRE index df77a41..5a10078 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,2 @@ -julia 0.3 +julia 0.7 Compat diff --git a/src/AppConf.jl b/src/AppConf.jl index e3a3c77..00e036c 100644 --- a/src/AppConf.jl +++ b/src/AppConf.jl @@ -28,13 +28,12 @@ macro prod(e) end function findeq(ln::AbstractString) - for i = 1:length(ln) - c = chr2ind(ln, i); - if ln[c] == '=' - return c - end - end - return -1 + for c in eachindex(ln) + if ln[c] == '=' + return c + end + end + return -1 end function stripcomments(ln::AbstractString) @@ -50,9 +49,10 @@ end function evalEnv(ln::AbstractString) nln = ln - for v in matchall(r"\$[A-Z|_|-]+", ln) - vname = replace(v, "\$", "") - nln = replace(nln, v, ENV[vname]) + for m in eachmatch(r"\$[A-Z|_|-]+", ln) + v = m.match + vname = replace(v, "\$" => "") + nln = replace(nln, v => ENV[vname]) end nln end @@ -82,11 +82,11 @@ end islist(str::AbstractString) = str[1] == '[' && str[length(str)] == ']' istuple(str::AbstractString) = str[1] == '(' && str[length(str)] == ')' -parselist(str::AbstractString) = map((x) -> isnumeric(x) ? parse(x) : cleanstring(x), +parselist(str::AbstractString) = map((x) -> isnumeric(x) ? parse(Float64, x) : cleanstring(x), split(match(r"\[(.*)\]", str).captures[1], ",")) function parsetuple(str::AbstractString) - res = map((x) -> isnumeric(x) ? parse(x) : cleanstring(x), + res = map((x) -> isnumeric(x) ? parse(Float64, x) : cleanstring(x), split(match(r"\((.*)\)", str).captures[1], ",")) return ntuple((i) -> res[i], length(res)) end @@ -96,16 +96,16 @@ function parseconf(file::AbstractString) tpath = abspath("$file.template") if !isfile(file) if isfile(tpath) - warn("No file found at $file, using template at $tpath") + @warn "No file found, using template" file_path=file temp_file_path = tpath file = tpath else - error("No file found at $file") + @error "No file found" file_path=file end end f = open(file) inquotes = false - if !isdefined(:conf) + if !isdefined(AppConf, :conf) conf = Dict{AbstractString, Any}() end while !eof(f) @@ -114,10 +114,12 @@ function parseconf(file::AbstractString) if ix == -1 continue end - key = strip(ln[1:ix - 1]) - val = strip(chomp(ln[ix + 1:end])) - if isnumeric(val) || val == "true" || val == "false" - conf[key] = parse(val) + key = strip(ln[1:prevind(ln, ix)]) + val = strip(chomp(ln[nextind(ln, ix):end])) + if isnumeric(val) + conf[key] = parse(Float64, val) + elseif val == "true" || val == "false" + conf[key] = parse(Bool, val) # Handle single-line lists elseif islist(val) conf[key] = parselist(val) @@ -135,9 +137,9 @@ function parseconf(file::AbstractString) curLn = strip(readline(f)) end println(s, curLn) - lst = takebuf_string(s) + lst = String(take!(s)) # parse and eval may not be performance optimal. - conf[key] = eval(parse(lst)) + conf[key] = eval(Meta.parse(lst)) else conf[key] = cleanstring(val) end diff --git a/test/runtests.jl b/test/runtests.jl index 9589357..0966ab3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using AppConf, Base.Test +using AppConf, Test @dev x = "dev" @prod x = "prod" @@ -42,4 +42,4 @@ parseconf("sample.conf") @test parseconf("foo.conf")["template"] == true -@test_throws ErrorException parseconf("bar.conf") +@test_throws SystemError parseconf("bar.conf")