Mostly fixed caching, echo=false needs fixing. Switched to JLD format.

pull/35/head
= 2016-04-23 18:15:59 +03:00
parent 0132cdd33b
commit 6ef016cc5f
4 changed files with 52 additions and 37 deletions

View File

@ -1,9 +1,10 @@
import JSON
import JSON, 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
@ -11,28 +12,42 @@ function write_cache(doc::WeaveDoc, cache_path)
end
function read_cache(doc::WeaveDoc, cache_path)
name = "$(doc.cwd)/$cache_path/$(doc.basename).json"
#name = "$(doc.cwd)/$cache_path/$(doc.basename).json"
name = "$(doc.cwd)/$cache_path/$(doc.basename).jld"
isfile(name) || return nothing
parsed = JSON.parsefile(name)
#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, idx)
options = Dict{Symbol, Any}()
for (keys,vals) = cached["chunks"][idx]["options"]
options[symbol(keys)] = vals
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
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
push!(new_chunks, newc)
end
haskey(options, :term_state) && (options[:term_state] = symbol(options[:term_state]))
#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"]
chunk.options = options
chunk.content = cached["chunks"][idx]["content"]
chunk.output = cached["chunks"][idx]["output"]
chunk.figures = cached["chunks"][idx]["figures"]
return chunk
return new_chunks
end
#Could be used to restore inline code in future
#function restore_chunk(chunk::DocChunk, cached, idx)
# chunk
#end
function restore_chunk(chunk::DocChunk, cached)
return chunk
end

View File

@ -26,13 +26,13 @@ type CodeChunk
number::Int
result_no::Int
start_line::Int
option_AbstractString::AbstractString
optionstring::AbstractString
options::Dict{Symbol, Any}
output::AbstractString
figures::Array{AbstractString}
result::Array{ChunkOutput}
function CodeChunk(content, number, start_line, option_AbstractString, options)
new(content, number, 0, start_line, option_AbstractString, options, "", AbstractString[], ChunkOutput[])
function CodeChunk(content, number, start_line, optionstring, options)
new(content, number, 0, start_line, optionstring, options, "", AbstractString[], ChunkOutput[])
end
end

View File

@ -3,7 +3,6 @@ function format(doc::WeaveDoc)
formatted = AbstractString[]
docformat = doc.format
#Complete format dictionaries with defaults
formatdict = docformat.formatdict
get!(formatdict, :termstart, formatdict[:codestart])

View File

@ -53,20 +53,25 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
for i = 1:n
chunk = doc.chunks[i]
result_chunks = run_chunk(chunk, report, SandBox, cached, cache, i)
#push!(executed, result_chunk)
if cached != nothing && (cache == :all || (cache ==:user && chunk.options[:cache]))
result_chunks = restore_chunk(chunk, cached)
else
result_chunks = run_chunk(chunk, report, SandBox)
end
executed = [executed; result_chunks]
end
popdisplay(report)
#Clear variables from used sandbox
clear_sandbox(SandBox)
doc.chunks = executed
if cache != :off
write_cache(doc, cache_path)
end
#Clear variables from used sandbox
clear_sandbox(SandBox)
doc.chunks = executed
return doc
end
@ -83,19 +88,14 @@ function detect_doctype(source::AbstractString)
end
function run_chunk(chunk::CodeChunk, report::Report, SandBox::Module, cached, cache, idx)
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)
if cached != nothing && (cache == :all || (cache ==:user && chunk.options[:cache]))
result_chunk = restore_chunk(chunk, cached, idx)
else
result_chunk = eval_chunk(chunk, report, SandBox)
end
result_chunk = eval_chunk(chunk, report, SandBox)
end
function run_chunk(chunk::DocChunk, report::Report, SandBox::Module, cached, cache, idx)
function run_chunk(chunk::DocChunk, report::Report, SandBox::Module)
return chunk
end
@ -193,6 +193,7 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
#else
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
#end
chunks
end
@ -281,7 +282,7 @@ function collect_results(chunk::CodeChunk, fmt::ScriptResult)
content *= r.code
else
content = "\n" * content * r.code
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))
content = ""
rchunk.result_no = result_no
result_no *=1
@ -308,7 +309,7 @@ function collect_results(chunk::CodeChunk, fmt::TermResult)
output *= prompt * r.code
output *= r.displayed * r.stdout
if !isempty(r.figures)
rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options))
rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
rchunk.output = output
output = ""
rchunk.figures = r.figures
@ -316,7 +317,7 @@ function collect_results(chunk::CodeChunk, fmt::TermResult)
end
end
if output != ""
rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.option_AbstractString, copy(chunk.options))
rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
rchunk.output = output
push!(result_chunks, rchunk)
end