Skip to content

Commit

Permalink
Fix to deserialing cached content. Caching now works as previously do…
Browse files Browse the repository at this point in the history
…cumented and tests pass
  • Loading branch information
mpastell committed Apr 23, 2016
1 parent 6ef016c commit 01a235f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
julia 0.4
Compat
ArgParse
JSON
JLD
25 changes: 5 additions & 20 deletions src/cache.jl
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
import JSON, JLD
import JLD

function write_cache(doc::WeaveDoc, cache_path)
cache_dir = "$(doc.cwd)/$cache_path"
isdir(cache_dir) || mkpath(cache_dir)
name = "$cache_dir/$(doc.basename).json"
JLD.save("$cache_dir/$(doc.basename).jld", Dict("doc" => doc))
open(name, "w") do io
write(io, JSON.json(doc))
end
return nothing
end

function read_cache(doc::WeaveDoc, cache_path)
#name = "$(doc.cwd)/$cache_path/$(doc.basename).json"
name = "$(doc.cwd)/$cache_path/$(doc.basename).jld"
isfile(name) || return nothing
#parsed = JSON.parsefile(name)
return JLD.load(name)["doc"]
end

#read_cache returns a dictionary, parse to back to chunk

function restore_chunk(chunk::CodeChunk, cached)
chunks = filter(x -> x.number == chunk.number &&
string(typeof(x)) == "Weave.CodeChunk", cached.chunks)

#Chunk types, don't match after loading. Need to reinitialize
#Chunk types, don't match after loading. Fix by constructing chunks
#from loaded content
new_chunks = Any[]
for c in chunks
newc = CodeChunk(c.content, c.number, c.start_line, c.optionstring, c.options)
newc.result_no = c.result_no
newc.figures = c.figures
newc.result = c.result
newc.output = c.output
push!(new_chunks, newc)
end
#options = Dict{Symbol, Any}()
#info(cached["chunks"][idx])
#for (keys,vals) = cached["chunks"][idx]["options"]
# options[symbol(keys)] = vals
#end
#haskey(options, :term_state) && (options[:term_state] = symbol(options[:term_state]))
#chunk.options = options
#chunk.content = cached["chunks"][idx]["content"]
#chunk.output = cached["chunks"][idx]["output"]
#chunk.figures = cached["chunks"][idx]["figures"]

return new_chunks
end

Expand Down
15 changes: 10 additions & 5 deletions src/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",

for i = 1:n
chunk = doc.chunks[i]
if cached != nothing && (cache == :all || (cache ==:user && chunk.options[:cache]))

if typeof(chunk) == CodeChunk
options = merge(rcParams[:chunk_defaults], chunk.options)
merge!(chunk.options, options)
end

restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache])

if cached != nothing && (cache == :all || restore)
result_chunks = restore_chunk(chunk, cached)
else
result_chunks = run_chunk(chunk, report, SandBox)
Expand Down Expand Up @@ -89,9 +97,6 @@ end


function run_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
#Defaults, already merged before, this merges format specific things
options = merge(rcParams[:chunk_defaults], chunk.options)
merge!(chunk.options, options)
result_chunk = eval_chunk(chunk, report, SandBox)
end

Expand Down Expand Up @@ -293,7 +298,7 @@ function collect_results(chunk::CodeChunk, fmt::ScriptResult)
end
if content != ""
startswith(content, "\n") || (content = "\n" * content)
rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options))
rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
push!(result_chunks, rchunk)
end

Expand Down
5 changes: 2 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ using Base.Test
info("Test: Chunk options")
include("chunk_options.jl")

# Cache is currently not implemented for new output format
#info("Test: Caching")
#include("cache_test.jl")
info("Test: Caching")
include("cache_test.jl")

if VERSION < v"0.5-dev"
info("Test: Chunk options with Gadfly")
Expand Down

0 comments on commit 01a235f

Please sign in to comment.