From 01a235fd8802b94b7834a65baed970f5dbb77fff Mon Sep 17 00:00:00 2001 From: = Date: Sat, 23 Apr 2016 19:18:44 +0300 Subject: [PATCH] Fix to deserialing cached content. Caching now works as previously documented and tests pass --- REQUIRE | 2 +- src/cache.jl | 25 +++++-------------------- src/run.jl | 15 ++++++++++----- test/runtests.jl | 5 ++--- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/REQUIRE b/REQUIRE index c432c42..e50c478 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,4 +1,4 @@ julia 0.4 Compat ArgParse -JSON +JLD diff --git a/src/cache.jl b/src/cache.jl index 1de1dc5..2476f11 100644 --- a/src/cache.jl +++ b/src/cache.jl @@ -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 diff --git a/src/run.jl b/src/run.jl index cc82ebb..6be773e 100644 --- a/src/run.jl +++ b/src/run.jl @@ -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) @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 4e7c945..e43caa6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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")