mirror of https://github.com/mpastell/Weave.jl
Working on caching. Saving and restoring added, problems with formatting
parent
ee262e81a4
commit
6635eb5c74
|
@ -100,7 +100,7 @@ function weave(source ; doctype = "pandoc", plotlib="Gadfly",
|
|||
doc = read_doc(source, informat) #Reader toimii, muuten kesken...
|
||||
doc = run(doc, doctype = doctype, plotlib=plotlib,
|
||||
informat = informat, out_path=out_path,
|
||||
fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path)
|
||||
fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path)
|
||||
formatted = format(doc)
|
||||
|
||||
outname = "$(doc.cwd)/$(doc.basename).$(doc.format.formatdict[:extension])"
|
||||
|
@ -131,9 +131,10 @@ end
|
|||
|
||||
export weave, list_out_formats, tangle
|
||||
|
||||
include("chunks.jl")
|
||||
include("run.jl")
|
||||
include("config.jl")
|
||||
include("chunks.jl")
|
||||
include("readers.jl")
|
||||
include("run.jl")
|
||||
include("cache.jl")
|
||||
include("formatters.jl")
|
||||
end
|
||||
|
|
23
src/cache.jl
23
src/cache.jl
|
@ -1,10 +1,25 @@
|
|||
import JSON
|
||||
|
||||
function cache(doc::WeaveDoc, cache_path)
|
||||
|
||||
import HDF5, JLD
|
||||
|
||||
function write_cache(doc::WeaveDoc, cache_path)
|
||||
isdir(cache_path) || mkdir(cache_path)
|
||||
name = "$cache_path/$(doc.basename).jld"
|
||||
JLD.save(name, "doc", doc)
|
||||
#open(name, "w") do io
|
||||
# write(io, JSON.json(doc))
|
||||
#end
|
||||
return nothing
|
||||
end
|
||||
|
||||
function read_cache(doc::WeaveDoc, cache_path)
|
||||
name = "$cache_path/$(doc.basename).jld"
|
||||
isfile(name) || return nothing
|
||||
return JLD.load(name, "doc")
|
||||
#parsed = JSON.parsefile(name)
|
||||
#doc = WeaveDoc(parsed["source"], parsed["chunks"],
|
||||
#parsed["cwd"], parsed["doctype"])
|
||||
end
|
||||
|
||||
|
||||
|
||||
#Todo caching of data, can get the contents of module using:
|
||||
#names(ReportSandBox, all=true)
|
||||
|
|
|
@ -6,10 +6,18 @@ type WeaveDoc
|
|||
chunks::Array
|
||||
cwd::String
|
||||
format
|
||||
doctype::String
|
||||
function WeaveDoc(source, chunks)
|
||||
path, fname = splitdir(abspath(source))
|
||||
basename = splitext(fname)[1]
|
||||
new(source, basename, path, chunks, "", nothing)
|
||||
new(source, basename, path, chunks, "", nothing, "")
|
||||
end
|
||||
|
||||
function WeaveDoc(source, chunks, cwd, doctype)
|
||||
path, fname = splitdir(abspath(source))
|
||||
basename = splitext(fname)[1]
|
||||
format = formats[doctype]
|
||||
new(source, basename, path, chunks, cwd, format, doctype)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ const rcParams =
|
|||
:fig=> true,
|
||||
:include=> true,
|
||||
:eval => true,
|
||||
:cache => false,
|
||||
:fig_cap=> nothing,
|
||||
#Size in inches
|
||||
:fig_width => 6,
|
||||
|
|
32
src/run.jl
32
src/run.jl
|
@ -2,8 +2,13 @@
|
|||
|
||||
|
||||
function run(doc::WeaveDoc; doctype = "pandoc", plotlib="Gadfly", informat="noweb",
|
||||
out_path=:doc, fig_path = "figures", fig_ext = nothing, cache_path = "cache")
|
||||
out_path=:doc, fig_path = "figures", fig_ext = nothing,
|
||||
cache_path = "cache", cache = :off)
|
||||
#cache :all, :user, :off
|
||||
|
||||
|
||||
doc.cwd = get_cwd(doc, out_path)
|
||||
doc.doctype = doctype
|
||||
doc.format = formats[doctype]
|
||||
set_rc_params(doc.format.formatdict, fig_path, fig_ext)
|
||||
|
||||
|
@ -18,13 +23,32 @@ function run(doc::WeaveDoc; doctype = "pandoc", plotlib="Gadfly", informat="nowe
|
|||
report = Report(doc.cwd, doc.basename, doc.format.formatdict)
|
||||
pushdisplay(report)
|
||||
|
||||
if cache != :off
|
||||
cached = read_cache(doc, cache_path)
|
||||
cached == nothing && info("No cached results found, running code")
|
||||
else
|
||||
cached = nothing
|
||||
end
|
||||
|
||||
executed = Any[]
|
||||
for chunk in copy(doc.chunks)
|
||||
result_chunk = eval_chunk(chunk, report, SandBox)
|
||||
n = length(doc.chunks)
|
||||
|
||||
for i = 1:n
|
||||
chunk = doc.chunks[i]
|
||||
if cached != nothing && (cache == :all || (cache ==:user && chunk.options.cache))
|
||||
result_chunk = cached.chunks[i]
|
||||
else
|
||||
result_chunk = eval_chunk(chunk, report, SandBox)
|
||||
end
|
||||
push!(executed, result_chunk)
|
||||
end
|
||||
|
||||
popdisplay(report)
|
||||
|
||||
if cache != :off
|
||||
write_cache(doc, cache_path)
|
||||
end
|
||||
|
||||
#Clear variables from used sandbox
|
||||
clear_sandbox(SandBox)
|
||||
doc.chunks = executed
|
||||
|
@ -136,6 +160,8 @@ function eval_chunk(chunk::DocChunk, report::Report, SandBox)
|
|||
chunk
|
||||
end
|
||||
|
||||
|
||||
|
||||
#Set all variables to nothing
|
||||
function clear_sandbox(SandBox::Module)
|
||||
for name = names(SandBox, true)
|
||||
|
|
Loading…
Reference in New Issue