mirror of https://github.com/mpastell/Weave.jl
Fix to deserialing cached content. Caching now works as previously documented and tests pass
parent
6ef016cc5f
commit
01a235fd88
25
src/cache.jl
25
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
|
||||
|
||||
|
|
15
src/run.jl
15
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
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue