dyndoc-ruby-core 1.2.5 → 1.4.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,7 @@ module Dyndoc
19
19
  }
20
20
 
21
21
  require 'logger'
22
- @@dyn_logger = Logger.new @@cfg_dir[:log_file], 7, 1048576
22
+ @@dyn_logger = nil
23
23
 
24
24
  def Dyndoc.cfg_dir
25
25
  @@cfg_dir
@@ -29,10 +29,18 @@ module Dyndoc
29
29
  Settings[:cfg_dyn]
30
30
  end
31
31
 
32
+ def Dyndoc.add_logger
33
+ @@dyn_logger = Logger.new(@@cfg_dir[:log_file], 7, 1048576)
34
+ end
35
+
32
36
  def Dyndoc.logger
33
37
  @@dyn_logger
34
38
  end
35
39
 
40
+ def Dyndoc.logger_info(txt)
41
+ @@dyn_logger.info(txt)
42
+ end
43
+
36
44
  @@dyn_block={}
37
45
 
38
46
  def Dyndoc.dyn_block
@@ -1,21 +1,24 @@
1
- # include("ruby.jl")
1
+
2
2
 
3
3
  module Dyndoc
4
4
 
5
- import Base.setindex!,Base.getindex,Base.IO,Base.show,Base.showarray
6
- importall Ruby
5
+
6
+ using Main.Ruby
7
+
8
+ import Base.setindex!,Base.getindex,Base.IO,Base.show #,Base.showarray
9
+ import Main.Ruby.start,Main.Ruby.stop,Main.Ruby.run,Main.Ruby.alive
7
10
 
8
11
  export DynVector,DynArray,getindex,setindex!,show,Vector,sync,getkey
9
12
 
10
13
  # this is just a wrapper of Vector type with update of all connected vectors
11
- # when change on the vector occurs
14
+ # when change on the vector occurs
12
15
 
13
16
 
14
- type DynVector
17
+ mutable struct DynVector
15
18
  ary::Vector
16
- key::ASCIIString
19
+ key::String
17
20
 
18
- DynVector(a::Vector,k::ASCIIString)=(x=new();x.ary=a;x.key=k;x)
21
+ #DynVector(a::Vector,k::String)=(x=new();x.ary=a;x.key=k;x)
19
22
  end
20
23
 
21
24
  function getindex(dynvect::DynVector,i::Integer)
@@ -26,35 +29,34 @@ end
26
29
  function setindex!(dynvect::DynVector,value,i::Integer)
27
30
  dynvect.ary[i]=value
28
31
  ## println("inisde vect:",Ruby.alive())
29
- if Ruby.alive()
30
- Ruby.run("Dyndoc::Vector[\""*dynvect.key*"\"].sync(:jl)")
32
+ if Ruby.alive()
33
+ Ruby.run("Dyndoc::Vector[\""*dynvect.key*"\"].sync(:jl)")
31
34
  end
32
35
  end
33
36
 
34
- show(io::IO,dynvect::DynVector)=showarray(io,dynvect.ary)
37
+ show(io::IO,dynvect::DynVector)=show(io,dynvect.ary)
35
38
 
36
39
  # gather all the julia vectors connected to dyndoc.
37
40
 
38
- type DynArray
41
+ mutable struct DynArray
39
42
  vars::Dict
40
-
41
43
  DynArray()=(x=new();x.vars=Dict();x)
42
44
  end
43
45
 
44
46
  global const Vec=DynArray()
45
47
 
46
- function getindex(dynary::DynArray,key::ASCIIString)
48
+ function getindex(dynary::DynArray,key::String)
47
49
  #println("getindex(" * key * ")->todo")
48
50
  #if Ruby.alive()
49
51
  #println("getindex(" * key * ")->to sync")
50
- #Ruby.run("Dyndoc::Vector[\""*key*"\"].sync_to(:jl)")
52
+ #Ruby.run("Dyndoc::Vector[\""*key*"\"].sync_to(:jl)")
51
53
  #end
52
54
  #println("getindex(" * key * ")->done")
53
55
  dynary.vars[key]
54
56
  end
55
57
  getindex(dynary::DynArray,key::Symbol)=getindex(dynary,string(key))
56
58
 
57
- function setindex!(dynary::DynArray,value,key::ASCIIString)
59
+ function setindex!(dynary::DynArray,value,key::String)
58
60
  #println("key:" * key)
59
61
  #println(keys(dynary.vars))
60
62
  if(haskey(dynary.vars,key))
@@ -65,7 +67,7 @@ function setindex!(dynary::DynArray,value,key::ASCIIString)
65
67
  end
66
68
 
67
69
  ## println("inside array:",Ruby.alive())
68
-
70
+
69
71
  if Ruby.alive()
70
72
  Ruby.run("Dyndoc::Vector[\""*key*"\"].sync(:jl)")
71
73
  end
@@ -73,13 +75,13 @@ end
73
75
  setindex!(dynary::DynArray,value,key::Symbol)=setindex!(dynary,value,string(key))
74
76
 
75
77
 
76
- sync(dynary::DynArray,key::ASCIIString)= if Ruby.alive() Ruby.run("Dyndoc::Vector[\""*key*"\"].sync(:jl)") end
78
+ sync(dynary::DynArray,key::String)= if Ruby.alive() Ruby.run("Dyndoc::Vector[\""*key*"\"].sync(:jl)") end
77
79
 
78
80
  show(io::IO,dynary::DynArray)=show(io,dynary.vars)
79
81
 
80
82
  # NO MORE KEY WITH THE FORM "<name>@<ruby id object>"
81
83
  # function getkey(dynary::DynArray,k::Symbol)
82
- # for k2 in keys(dynary.vars)
84
+ # for k2 in keys(dynary.vars)
83
85
  # if split(k2,"@")[1] == string(k)
84
86
  # return k2
85
87
  # end
@@ -90,4 +92,3 @@ show(io::IO,dynary::DynArray)=show(io,dynary.vars)
90
92
  # setindex!(dynary::DynArray,value,key::Symbol)=setindex!(dynary,value,getkey(dynary,key))
91
93
 
92
94
  end
93
-
@@ -1,87 +1,55 @@
1
- #cmd="a=1\n(a)\nfor i in 1:3\nprintln(i)\nend"
2
-
3
- # # Unused! See capture_julia
4
- # function capture_cmd(cmd::AbstractString)
5
- # add,cmd0=true,AbstractString[]
6
- # res=Any[] #Dict{AbstractString,Any}()
7
- # for l=split(cmd,"\n")
8
- # #println("l => ",l)
9
- # push!(cmd0,l)
10
- # pcmd0=Base.parse_input_line(join(cmd0,"\n"))
11
- # #print(join(cmd0,"\n")*":");println(pcmd0)
12
- # add = typeof(pcmd0)==Expr && pcmd0.head == :continue
13
- # if !add
14
- # #print("ici:")
15
- # #println(Base.eval(pcmd0))
16
- # push!(res,(join(cmd0,"\n"),eval(pcmd0)))
17
- # cmd0=AbstractString[]
18
- # end
19
- # #println(res)
20
- # end
21
- # res
22
- # end
23
-
24
- module DyndocSandbox
25
- importall Ruby
26
- importall Dyndoc
1
+ module Dyndoc2Sandbox
2
+ import Main.Ruby.run,Main.Ruby.alive
3
+ import Main.Dyndoc.DynVector,Main.Dyndoc.DynArray,Main.Dyndoc.getindex,Main.Dyndoc.setindex!,Main.Dyndoc.show,Main.Dyndoc.Vector,Main.Dyndoc.sync,Main.Dyndoc.getkey
4
+ using InteractiveUtils
5
+ end
27
6
 
28
- # Replace OUTPUT_STREAM references so we can capture output.
29
- OUTPUT_STREAM = IOBuffer()
30
- print(x) = Base.print(OUTPUT_STREAM, x)
31
- println(x) = Base.println(OUTPUT_STREAM, x)
32
7
 
33
- # Output
34
- MIME_OUTPUT = Array(Tuple, 0)
35
- emit(mime, data) = push!(MIME_OUTPUT, (mime, data))
8
+ function replace_dyndoc2sandbox(txt)
9
+ replace(replace(txt,"Main.Dyndoc2Sandbox." => ""),"Main.Dyndoc2Sandbox" => "Main")
36
10
  end
37
11
 
38
- function get_stdout_iobuffer()
39
- #seek(DyndocSandbox.OUTPUT_STREAM, 0)
40
- #jl4rb_out =
41
- takebuf_string(DyndocSandbox.OUTPUT_STREAM)
42
- #truncate(DyndocSandbox.OUTPUT_STREAM, 0)
43
- #jl4rb_out
12
+ function echo_repl_julia(res)
13
+ # buf = IOBuffer();
14
+ # td = TextDisplay(buf);
15
+ # display(td, res);
16
+ # takebuf_string(buf)
17
+ #println(typeof(res))
18
+ replace_dyndoc2sandbox(repr("text/plain",res))
44
19
  end
45
20
 
46
- function get_stderr_iobuffer()
47
- #jl4rb_out = takebuf_string(STDERR.buffer)
48
- #jl4rb_out
49
- ## THIS FAILS WHEN DYNDOC DAEMONIZED SO AUTOMATIC EMPTY RESULT for now
50
- ## MAYBE TO DELETE SOON!
51
- ""
21
+ ## Rmk: The process is based on what is done in weave.jl (src/run.jl)
22
+ getstdout() = stdout #Base.STDOUT
23
+ function capture_output_expr(expr)
24
+ #oldSTDOUT = STDOUT
25
+ oldSTDOUT = getstdout()
26
+ out = nothing
27
+ obj = nothing
28
+ rw, wr = redirect_stdout()
29
+ reader = @async read(rw, String) # @async readstring(rw)
30
+ try
31
+ obj = Core.eval(Dyndoc2Sandbox, expr)
32
+ obj != nothing && display(obj)
33
+ #catch E
34
+ # throw_errors && throw(E)
35
+ # display(E)
36
+ # @warn("ERROR: $(typeof(E)) occurred, including output in Weaved document")
37
+
38
+ finally
39
+ redirect_stdout(oldSTDOUT)
40
+ close(wr)
41
+ out = fetch(reader) #wait(reader)
42
+ close(rw)
43
+ end
44
+ return (obj, out)
52
45
  end
53
46
 
54
- # export weave
55
- # module DyndocSandbox
56
- # # Copied from Gadfly.jl/src/weave.jl
57
- # # Replace OUTPUT_STREAM references so we can capture output.
58
- # OUTPUT_STREAM = IOString()
59
- # print(x) = Base.print(OUTPUT_STREAM, x)
60
- # println(x) = Base.println(OUTPUT_STREAM, x)
61
-
62
- # function eval(expr)
63
- # result = try
64
- # Base.eval(DyndocSandbox, expr)
65
- # seek(DyndocSandbox.OUTPUT_STREAM, 0)
66
- # output = takebuf_string(DyndocSandbox.OUTPUT_STREAM)
67
- # truncate(DyndocSandbox.OUTPUT_STREAM, 0)
68
- # output
69
- # catch e
70
- # io = IOBuffer()
71
- # print(io, "ERROR: ")
72
- # Base.error_show(io, e)
73
- # tmp = bytestring(io)
74
- # close(io)
75
- # tmp
76
- # end
77
- # result
78
- # end
79
- # end
80
-
81
- function capture_julia(cmd::AbstractString)
47
+ function capture_output_julia(cmd::AbstractString)
82
48
  add,cmd0=true,AbstractString[]
83
49
  res=Any[] #Dict{AbstractString,Any}()
84
50
  #println(cmd)
51
+ #cmd=replace(cmd,r"\$","\$")
52
+
85
53
  for l=split(cmd,"\n")
86
54
  #println("l => ",l)
87
55
  push!(cmd0,l)
@@ -91,17 +59,18 @@ function capture_julia(cmd::AbstractString)
91
59
  if !add
92
60
  #print("ici:")
93
61
  #println(Base.eval(pcmd0))
94
- result,error = "",""
62
+ result,error,out = "","",""
95
63
  try
96
- result=eval(DyndocSandbox, pcmd0)
64
+ result,out=capture_output_expr(pcmd0)
97
65
  catch e
98
- #io = IOBuffer()
99
- #print(io, "ERROR: ")
100
- #Base.error_show(io, e)
101
- error = "Error: $(string(e))"
102
- #close(io)
103
- end
104
- push!(res,(join(cmd0,"\n"),string(result),get_stdout_iobuffer(),error,get_stderr_iobuffer()))
66
+ #io = IOBuffer()
67
+ #print(io, "ERROR: ")
68
+ #Base.error_show(io, e)
69
+ error = "ERROR: $(sprint(showerror,e))"
70
+ #close(io)
71
+ end
72
+ println(l)
73
+ push!(res,(join(cmd0,"\n"), echo_repl_julia(result),replace_dyndoc2sandbox(out),replace_dyndoc2sandbox(error),""))
105
74
  cmd0=AbstractString[]
106
75
  end
107
76
  #println(res)
@@ -0,0 +1,192 @@
1
+ #cmd="a=1\n(a)\nfor i in 1:3\nprintln(i)\nend"
2
+
3
+ # # Unused! See capture_julia
4
+ # function capture_cmd(cmd::AbstractString)
5
+ # add,cmd0=true,AbstractString[]
6
+ # res=Any[] #Dict{AbstractString,Any}()
7
+ # for l=split(cmd,"\n")
8
+ # #println("l => ",l)
9
+ # push!(cmd0,l)
10
+ # pcmd0=Base.parse_input_line(join(cmd0,"\n"))
11
+ # #print(join(cmd0,"\n")*":");println(pcmd0)
12
+ # add = typeof(pcmd0)==Expr && pcmd0.head == :continue
13
+ # if !add
14
+ # #print("ici:")
15
+ # #println(Base.eval(pcmd0))
16
+ # push!(res,(join(cmd0,"\n"),eval(pcmd0)))
17
+ # cmd0=AbstractString[]
18
+ # end
19
+ # #println(res)
20
+ # end
21
+ # res
22
+ # end
23
+
24
+ module DyndocSandbox
25
+ import Main.Ruby.start,Main.Ruby.stop,Main.Ruby.run,Main.Ruby.alive
26
+ import Main.Dyndoc.DynVector,Main.Dyndoc.DynArray,Main.Dyndoc.getindex,Main.Dyndoc.setindex!,Main.Dyndoc.show,Main.Dyndoc.Vector,Main.Dyndoc.sync,Main.Dyndoc.getkey
27
+
28
+ # Replace OUTPUT_STREAM references so we can capture output.
29
+ OUTPUT_STREAM = IOBuffer()
30
+ print(x) = Base.print(OUTPUT_STREAM, x)
31
+ println(x) = Base.println(OUTPUT_STREAM, x)
32
+
33
+ # Output
34
+ MIME_OUTPUT = Array(Tuple, 0)
35
+ emit(mime, data) = push!(MIME_OUTPUT, (mime, data))
36
+ end
37
+
38
+ function get_stdout_iobuffer()
39
+ #seek(DyndocSandbox.OUTPUT_STREAM, 0)
40
+ #jl4rb_out =
41
+ takebuf_string(DyndocSandbox.OUTPUT_STREAM)
42
+ #truncate(DyndocSandbox.OUTPUT_STREAM, 0)
43
+ #jl4rb_out
44
+ end
45
+
46
+ function get_stderr_iobuffer()
47
+ #jl4rb_out = takebuf_string(STDERR.buffer)
48
+ #jl4rb_out
49
+ ## THIS FAILS WHEN DYNDOC DAEMONIZED SO AUTOMATIC EMPTY RESULT for now
50
+ ## MAYBE TO DELETE SOON!
51
+ ""
52
+ end
53
+
54
+ # export weave
55
+ # module DyndocSandbox
56
+ # # Copied from Gadfly.jl/src/weave.jl
57
+ # # Replace OUTPUT_STREAM references so we can capture output.
58
+ # OUTPUT_STREAM = IOString()
59
+ # print(x) = Base.print(OUTPUT_STREAM, x)
60
+ # println(x) = Base.println(OUTPUT_STREAM, x)
61
+
62
+ # function eval(expr)
63
+ # result = try
64
+ # Base.eval(DyndocSandbox, expr)
65
+ # seek(DyndocSandbox.OUTPUT_STREAM, 0)
66
+ # output = takebuf_string(DyndocSandbox.OUTPUT_STREAM)
67
+ # truncate(DyndocSandbox.OUTPUT_STREAM, 0)
68
+ # output
69
+ # catch e
70
+ # io = IOBuffer()
71
+ # print(io, "ERROR: ")
72
+ # Base.error_show(io, e)
73
+ # tmp = bytestring(io)
74
+ # close(io)
75
+ # tmp
76
+ # end
77
+ # result
78
+ # end
79
+ # end
80
+
81
+
82
+ function capture_julia(cmd::AbstractString)
83
+ add,cmd0=true,AbstractString[]
84
+ res=Any[] #Dict{AbstractString,Any}()
85
+ #println(cmd)
86
+ #cmd=replace(cmd,r"\$","\$")
87
+ # for standard redirection
88
+ ##OUT: (outRead, outWrite) = redirect_stdout()
89
+ for l=split(cmd,"\n")
90
+ #println("l => ",l)
91
+ push!(cmd0,l)
92
+ pcmd0=Base.parse_input_line(join(cmd0,"\n"))
93
+ #print(join(cmd0,"\n")*":");println(pcmd0)
94
+ add = typeof(pcmd0)==Expr && pcmd0.head == :incomplete
95
+ if !add
96
+ #print("ici:")
97
+ #println(Base.eval(pcmd0))
98
+ result,error = "",""
99
+ try
100
+ result=eval(DyndocSandbox, pcmd0)
101
+ catch e
102
+ #io = IOBuffer()
103
+ #print(io, "ERROR: ")
104
+ #Base.error_show(io, e)
105
+ error = "Error: $(string(e))"
106
+ #close(io)
107
+ end
108
+
109
+ ##OUT: data_out = readavailable(outRead)
110
+
111
+ ##OUT: push!(res,(join(cmd0,"\n"),echo_repl_julia(result),get_stdout_iobuffer(),data_out,error,get_stderr_iobuffer()))
112
+ push!(res,(join(cmd0,"\n"),echo_repl_julia(result),get_stdout_iobuffer(),error,get_stderr_iobuffer()))
113
+ cmd0=AbstractString[]
114
+ end
115
+ #println(res)
116
+ end
117
+ ##OUT: close(outWrite);close(outRead)
118
+ res
119
+ end
120
+
121
+
122
+ module Dyndoc2Sandbox
123
+ import Main.Ruby.run,Main.Ruby.alive
124
+ import Main.Dyndoc.DynVector,Main.Dyndoc.DynArray,Main.Dyndoc.getindex,Main.Dyndoc.setindex!,Main.Dyndoc.show,Main.Dyndoc.Vector,Main.Dyndoc.sync,Main.Dyndoc.getkey
125
+ end
126
+
127
+ function echo_repl_julia(res)
128
+ buf = IOBuffer();
129
+ td = TextDisplay(buf);
130
+ display(td, res);
131
+ takebuf_string(buf)
132
+ end
133
+
134
+ ## Rmk: The process is based on what is done in weave.jl (src/run.jl)
135
+ getstdout() = stdout #Base.STDOUT
136
+ function capture_output_expr(expr)
137
+ #oldSTDOUT = STDOUT
138
+ oldSTDOUT = getstdout()
139
+ out = nothing
140
+ obj = nothing
141
+ rw, wr = redirect_stdout()
142
+ reader = @async read(rw, String) # @async readstring(rw)
143
+ try
144
+ obj = eval(Dyndoc2Sandbox, expr)
145
+ obj != nothing && display(obj)
146
+ catch E
147
+ throw_errors && throw(E)
148
+ display(E)
149
+ @warn("ERROR: $(typeof(E)) occurred, including output in Weaved document")
150
+
151
+ finally
152
+ redirect_stdout(oldSTDOUT)
153
+ close(wr)
154
+ out = wait(reader)
155
+ close(rw)
156
+ end
157
+ return (obj, out)
158
+ end
159
+
160
+ function capture_output_julia(cmd::AbstractString)
161
+ add,cmd0=true,AbstractString[]
162
+ res=Any[] #Dict{AbstractString,Any}()
163
+ #println(cmd)
164
+ #cmd=replace(cmd,r"\$","\$")
165
+
166
+ for l=split(cmd,"\n")
167
+ #println("l => ",l)
168
+ push!(cmd0,l)
169
+ pcmd0=Base.parse_input_line(join(cmd0,"\n"))
170
+ #print(join(cmd0,"\n")*":");println(pcmd0)
171
+ add = typeof(pcmd0)==Expr && pcmd0.head == :incomplete
172
+ if !add
173
+ #print("ici:")
174
+ #println(Base.eval(pcmd0))
175
+ result,error,out = "","",""
176
+ try
177
+ result,out=capture_output_expr(pcmd0)
178
+ catch e
179
+ #io = IOBuffer()
180
+ #print(io, "ERROR: ")
181
+ #Base.error_show(io, e)
182
+ error = "Error: $(string(e))"
183
+ #close(io)
184
+ end
185
+
186
+ push!(res,(join(cmd0,"\n"),echo_repl_julia(result),out,error,""))
187
+ cmd0=AbstractString[]
188
+ end
189
+ #println(res)
190
+ end
191
+ res
192
+ end