Fix to deserialing cached content. Caching now works as previously documented and tests pass

pull/35/head
= 2016-04-23 19:18:44 +03:00
parent 6ef016cc5f
commit 01a235fd88
4 changed files with 18 additions and 29 deletions

View File

@ -1,4 +1,4 @@
julia 0.4 julia 0.4
Compat Compat
ArgParse ArgParse
JSON JLD

View File

@ -1,49 +1,34 @@
import JSON, JLD import JLD
function write_cache(doc::WeaveDoc, cache_path) function write_cache(doc::WeaveDoc, cache_path)
cache_dir = "$(doc.cwd)/$cache_path" cache_dir = "$(doc.cwd)/$cache_path"
isdir(cache_dir) || mkpath(cache_dir) isdir(cache_dir) || mkpath(cache_dir)
name = "$cache_dir/$(doc.basename).json"
JLD.save("$cache_dir/$(doc.basename).jld", Dict("doc" => doc)) JLD.save("$cache_dir/$(doc.basename).jld", Dict("doc" => doc))
open(name, "w") do io
write(io, JSON.json(doc))
end
return nothing return nothing
end end
function read_cache(doc::WeaveDoc, cache_path) function read_cache(doc::WeaveDoc, cache_path)
#name = "$(doc.cwd)/$cache_path/$(doc.basename).json"
name = "$(doc.cwd)/$cache_path/$(doc.basename).jld" name = "$(doc.cwd)/$cache_path/$(doc.basename).jld"
isfile(name) || return nothing isfile(name) || return nothing
#parsed = JSON.parsefile(name)
return JLD.load(name)["doc"] return JLD.load(name)["doc"]
end end
#read_cache returns a dictionary, parse to back to chunk
function restore_chunk(chunk::CodeChunk, cached) function restore_chunk(chunk::CodeChunk, cached)
chunks = filter(x -> x.number == chunk.number && chunks = filter(x -> x.number == chunk.number &&
string(typeof(x)) == "Weave.CodeChunk", cached.chunks) 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[] new_chunks = Any[]
for c in chunks for c in chunks
newc = CodeChunk(c.content, c.number, c.start_line, c.optionstring, c.options) newc = CodeChunk(c.content, c.number, c.start_line, c.optionstring, c.options)
newc.result_no = c.result_no newc.result_no = c.result_no
newc.figures = c.figures newc.figures = c.figures
newc.result = c.result newc.result = c.result
newc.output = c.output
push!(new_chunks, newc) push!(new_chunks, newc)
end 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 return new_chunks
end end

View File

@ -53,7 +53,15 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
for i = 1:n for i = 1:n
chunk = doc.chunks[i] 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) result_chunks = restore_chunk(chunk, cached)
else else
result_chunks = run_chunk(chunk, report, SandBox) result_chunks = run_chunk(chunk, report, SandBox)
@ -89,9 +97,6 @@ end
function run_chunk(chunk::CodeChunk, report::Report, SandBox::Module) 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) result_chunk = eval_chunk(chunk, report, SandBox)
end end
@ -293,7 +298,7 @@ function collect_results(chunk::CodeChunk, fmt::ScriptResult)
end end
if content != "" if content != ""
startswith(content, "\n") || (content = "\n" * 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) push!(result_chunks, rchunk)
end end

View File

@ -9,9 +9,8 @@ using Base.Test
info("Test: Chunk options") info("Test: Chunk options")
include("chunk_options.jl") include("chunk_options.jl")
# Cache is currently not implemented for new output format info("Test: Caching")
#info("Test: Caching") include("cache_test.jl")
#include("cache_test.jl")
if VERSION < v"0.5-dev" if VERSION < v"0.5-dev"
info("Test: Chunk options with Gadfly") info("Test: Chunk options with Gadfly")