| Module | Bio::Shell::Ghost |
| In: |
lib/bio/shell/core.rb
|
| cache | [RW] | A hash to store temporal (per session) configurations |
| config | [RW] | A hash to store persistent configurations |
object
# File lib/bio/shell/core.rb, line 299
299: def check_marshal
300: if @config[:marshal] and @config[:marshal] != MARSHAL
301: raise "Marshal version mismatch"
302: end
303: end
# File lib/bio/shell/core.rb, line 381
381: def close_history
382: if @cache[:histfile]
383: STDERR.print "Saving history (#{history_file}) ... "
384: @cache[:histfile].close
385: STDERR.puts "done"
386: end
387: end
# File lib/bio/shell/core.rb, line 565
565: def closing_splash
566: STDERR.puts
567: STDERR.puts
568: if @config[:color]
569: STDERR.print splash_message_color
570: else
571: STDERR.print splash_message
572: end
573: STDERR.puts
574: STDERR.puts
575: end
# File lib/bio/shell/core.rb, line 251
251: def config_color
252: bind = Bio::Shell.cache[:binding]
253: flag = ! @config[:color]
254: @config[:color] = flag
255: if flag
256: IRB.conf[:PROMPT_MODE] = :BIORUBY_COLOR
257: eval("conf.prompt_mode = :BIORUBY_COLOR", bind)
258: else
259: IRB.conf[:PROMPT_MODE] = :BIORUBY
260: eval("conf.prompt_mode = :BIORUBY", bind)
261: end
262: end
# File lib/bio/shell/core.rb, line 243
243: def config_echo
244: bind = Bio::Shell.cache[:binding]
245: flag = ! @config[:echo]
246: @config[:echo] = IRB.conf[:ECHO] = flag
247: eval("conf.echo = #{flag}", bind)
248: STDERR.puts "Echo #{flag ? 'on' : 'off'}"
249: end
# File lib/bio/shell/core.rb, line 275
275: def config_message(str = nil)
276: str ||= MESSAGE
277: @config[:message] = str
278: opening_splash
279: end
# File lib/bio/shell/core.rb, line 264
264: def config_pager(cmd = nil)
265: @config[:pager] = cmd
266: end
# File lib/bio/shell/core.rb, line 237
237: def config_show
238: @config.each do |k, v|
239: STDERR.puts "#{k}\t= #{v.inspect}"
240: end
241: end
# File lib/bio/shell/core.rb, line 268
268: def config_splash
269: flag = ! @config[:splash]
270: @config[:splash] = flag
271: STDERR.puts "Splash #{flag ? 'on' : 'off'}"
272: opening_splash
273: end
save/restore the environment
# File lib/bio/shell/core.rb, line 115
115: def configure(savedir)
116: @config = {}
117: @cache = {
118: :savedir => savedir,
119: :workdir => Dir.pwd,
120: }
121: create_save_dir
122: load_config
123: load_plugin
124: end
bioflat
# File lib/bio/shell/core.rb, line 188
188: def create_flat_dir(dbname)
189: dir = File.join(bioflat_dir, dbname.to_s.strip)
190: unless File.directory?(dir)
191: FileUtils.makedirs(dir)
192: end
193: return dir
194: end
# File lib/bio/shell/core.rb, line 174
174: def create_real_dir(dir)
175: unless File.directory?(dir)
176: begin
177: STDERR.print "Creating directory (#{dir}) ... "
178: FileUtils.makedirs(dir)
179: STDERR.puts "done"
180: rescue
181: warn "Error: Failed to create directory (#{dir}) : #{$!}"
182: end
183: end
184: end
directories
# File lib/bio/shell/core.rb, line 150
150: def create_save_dir
151: create_real_dir(session_dir)
152: create_real_dir(plugin_dir)
153: create_real_dir(data_dir)
154: end
# File lib/bio/shell/core.rb, line 156
156: def create_save_dir_ask
157: if File.directory?(session_dir)
158: @cache[:save] = true
159: end
160: unless @cache[:save]
161: if ask_yes_or_no("Save session in '#{@cache[:workdir]}' directory? [y/n] ")
162: create_real_dir(session_dir)
163: create_real_dir(plugin_dir)
164: create_real_dir(data_dir)
165: create_real_dir(bioflat_dir)
166: @cache[:save] = true
167: else
168: @cache[:save] = false
169: end
170: end
171: return @cache[:save]
172: end
# File lib/bio/shell/core.rb, line 196
196: def find_flat_dir(dbname)
197: dir = File.join(bioflat_dir, dbname.to_s.strip)
198: if File.exists?(dir)
199: return dir
200: else
201: return nil
202: end
203: end
config
# File lib/bio/shell/core.rb, line 207
207: def load_config
208: load_config_file(config_file)
209: end
# File lib/bio/shell/core.rb, line 211
211: def load_config_file(file)
212: if File.exists?(file)
213: STDERR.print "Loading config (#{file}) ... "
214: if hash = YAML.load(File.read(file))
215: @config.update(hash)
216: end
217: STDERR.puts "done"
218: end
219: end
# File lib/bio/shell/core.rb, line 389
389: def load_history
390: if @cache[:readline]
391: load_history_file(history_file)
392: end
393: end
# File lib/bio/shell/core.rb, line 395
395: def load_history_file(file)
396: if File.exists?(file)
397: STDERR.print "Loading history (#{file}) ... "
398: File.open(file).each do |line|
399: unless line[/^# /]
400: Readline::HISTORY.push line.chomp
401: end
402: end
403: STDERR.puts "done"
404: end
405: end
# File lib/bio/shell/core.rb, line 305
305: def load_object
306: begin
307: check_marshal
308: load_object_file(object_file)
309: rescue
310: warn "Error: Load aborted : #{$!}"
311: end
312: end
# File lib/bio/shell/core.rb, line 314
314: def load_object_file(file)
315: if File.exists?(file)
316: STDERR.print "Loading object (#{file}) ... "
317: begin
318: bind = Bio::Shell.cache[:binding]
319: hash = Marshal.load(File.read(file))
320: hash.each do |k, v|
321: begin
322: Thread.current[:restore_value] = v
323: eval("#{k} = Thread.current[:restore_value]", bind)
324: rescue
325: STDERR.puts "Warning: object '#{k}' couldn't be loaded : #{$!}"
326: end
327: end
328: rescue
329: warn "Error: Failed to load (#{file}) : #{$!}"
330: end
331: STDERR.puts "done"
332: end
333: end
plugin
# File lib/bio/shell/core.rb, line 283
283: def load_plugin
284: load_plugin_dir(plugin_dir)
285: end
# File lib/bio/shell/core.rb, line 287
287: def load_plugin_dir(dir)
288: if File.directory?(dir)
289: Dir.glob("#{dir}/*.rb").sort.each do |file|
290: STDERR.print "Loading plugin (#{file}) ... "
291: load file
292: STDERR.puts "done"
293: end
294: end
295: end
# File lib/bio/shell/core.rb, line 126
126: def load_session
127: load_object
128: unless @cache[:mode] == :script
129: load_history
130: opening_splash
131: open_history
132: end
133: end
history
# File lib/bio/shell/core.rb, line 371
371: def open_history
372: @cache[:histfile] = File.open(history_file, "a")
373: @cache[:histfile].sync = true
374: end
# File lib/bio/shell/core.rb, line 543
543: def opening_splash
544: STDERR.puts
545: if @config[:splash]
546: if @config[:color]
547: splash_message_action_color
548: else
549: splash_message_action
550: end
551: end
552: if @config[:color]
553: STDERR.print splash_message_color
554: else
555: STDERR.print splash_message
556: end
557: STDERR.puts
558: STDERR.puts
559: STDERR.print " Version : BioRuby #{Bio::BIORUBY_VERSION_ID}"
560: STDERR.print " / Ruby #{RUBY_VERSION}"
561: STDERR.puts
562: STDERR.puts
563: end
# File lib/bio/shell/core.rb, line 221
221: def save_config
222: save_config_file(config_file)
223: end
# File lib/bio/shell/core.rb, line 225
225: def save_config_file(file)
226: begin
227: STDERR.print "Saving config (#{file}) ... "
228: File.open(file, "w") do |f|
229: f.puts @config.to_yaml
230: end
231: STDERR.puts "done"
232: rescue
233: warn "Error: Failed to save (#{file}) : #{$!}"
234: end
235: end
not used (use open_history/close_history instead)
# File lib/bio/shell/core.rb, line 408
408: def save_history
409: if @cache[:readline]
410: save_history_file(history_file)
411: end
412: end
# File lib/bio/shell/core.rb, line 414
414: def save_history_file(file)
415: begin
416: STDERR.print "Saving history (#{file}) ... "
417: File.open(file, "w") do |f|
418: f.puts Readline::HISTORY.to_a
419: end
420: STDERR.puts "done"
421: rescue
422: warn "Error: Failed to save (#{file}) : #{$!}"
423: end
424: end
# File lib/bio/shell/core.rb, line 335
335: def save_object
336: save_object_file(object_file)
337: end
# File lib/bio/shell/core.rb, line 339
339: def save_object_file(file)
340: begin
341: STDERR.print "Saving object (#{file}) ... "
342: File.rename(file, "#{file}.old") if File.exist?(file)
343: File.open(file, "w") do |f|
344: bind = Bio::Shell.cache[:binding]
345: list = eval("local_variables", bind)
346: list -= ["_"]
347: hash = {}
348: list.each do |elem|
349: value = eval(elem, bind)
350: if value
351: begin
352: Marshal.dump(value)
353: hash[elem] = value
354: rescue
355: # value could not be dumped.
356: end
357: end
358: end
359: Marshal.dump(hash, f)
360: @config[:marshal] = MARSHAL
361: end
362: STDERR.puts "done"
363: rescue
364: File.rename("#{file}.old", file) if File.exist?("#{file}.old")
365: warn "Error: Failed to save (#{file}) : #{$!}"
366: end
367: end
# File lib/bio/shell/core.rb, line 459
459: def save_script
460: if @script_begin and @script_end and @script_begin <= @script_end
461: if File.exists?(script_file)
462: message = "Overwrite script file (#{script_file})? [y/n] "
463: else
464: message = "Save script file (#{script_file})? [y/n] "
465: end
466: if ask_yes_or_no(message)
467: save_script_file(script_file)
468: else
469: STDERR.puts " ... save aborted."
470: end
471: elsif @script_begin and @script_end and @script_begin - @script_end == 1
472: STDERR.puts " ... script aborted."
473: else
474: STDERR.puts "Error: Script range #{@script_begin}..#{@script_end} is invalid"
475: end
476: end
# File lib/bio/shell/core.rb, line 478
478: def save_script_file(file)
479: begin
480: STDERR.print "Saving script (#{file}) ... "
481: File.open(file, "w") do |f|
482: f.puts "#!/usr/bin/env bioruby"
483: f.puts
484: f.puts Readline::HISTORY.to_a[@script_begin..@script_end]
485: f.puts
486: end
487: STDERR.puts "done"
488: rescue
489: @script_begin = nil
490: warn "Error: Failed to save (#{file}) : #{$!}"
491: end
492: end
# File lib/bio/shell/core.rb, line 135
135: def save_session
136: unless @cache[:mode] == :script
137: closing_splash
138: end
139: if create_save_dir_ask
140: #save_history # changed to use our own...
141: close_history
142: save_object
143: save_config
144: end
145: #STDERR.puts "Leaving directory '#{@cache[:workdir]}'"
146: end
# File lib/bio/shell/core.rb, line 428
428: def script(mode = nil)
429: case mode
430: when :begin, "begin", :start, "start"
431: @cache[:script] = true
432: script_begin
433: when :end, "end", :stop, "stop"
434: @cache[:script] = false
435: script_end
436: save_script
437: else
438: if @cache[:script]
439: @cache[:script] = false
440: script_end
441: save_script
442: else
443: @cache[:script] = true
444: script_begin
445: end
446: end
447: end
# File lib/bio/shell/core.rb, line 449
449: def script_begin
450: STDERR.puts "-- 8< -- 8< -- 8< -- Script -- 8< -- 8< -- 8< --"
451: @script_begin = Readline::HISTORY.size
452: end
# File lib/bio/shell/core.rb, line 454
454: def script_end
455: STDERR.puts "-- >8 -- >8 -- >8 -- Script -- >8 -- >8 -- >8 --"
456: @script_end = Readline::HISTORY.size - 2
457: end
splash
# File lib/bio/shell/core.rb, line 496
496: def splash_message
497: @config[:message] ||= MESSAGE
498: @config[:message].to_s.split(//).join(" ")
499: end
# File lib/bio/shell/core.rb, line 508
508: def splash_message_action(message = nil)
509: s = message || splash_message
510: l = s.length
511: x = " "
512: 0.step(l,2) do |i|
513: l1 = l-i; l2 = l1/2; l4 = l2/2
514: STDERR.print "#{s[0,i]}#{x*l1}#{s[i,1]}\r"
515: sleep(0.001)
516: STDERR.print "#{s[0,i]}#{x*l2}#{s[i,1]}#{x*(l1-l2)}\r"
517: sleep(0.002)
518: STDERR.print "#{s[0,i]}#{x*l4}#{s[i,1]}#{x*(l2-l4)}\r"
519: sleep(0.004)
520: STDERR.print "#{s[0,i+1]}#{x*l4}\r"
521: sleep(0.008)
522: end
523: end
# File lib/bio/shell/core.rb, line 525
525: def splash_message_action_color(message = nil)
526: s = message || splash_message
527: l = s.length
528: c = colors
529: x = " "
530: 0.step(l,2) do |i|
531: l1 = l-i; l2 = l1/2; l4 = l2/2
532: STDERR.print "#{c[:n]}#{s[0,i]}#{x*l1}#{c[:y]}#{s[i,1]}\r"
533: sleep(0.001)
534: STDERR.print "#{c[:n]}#{s[0,i]}#{x*l2}#{c[:g]}#{s[i,1]}#{x*(l1-l2)}\r"
535: sleep(0.002)
536: STDERR.print "#{c[:n]}#{s[0,i]}#{x*l4}#{c[:r]}#{s[i,1]}#{x*(l2-l4)}\r"
537: sleep(0.004)
538: STDERR.print "#{c[:n]}#{s[0,i+1]}#{x*l4}\r"
539: sleep(0.008)
540: end
541: end
# File lib/bio/shell/core.rb, line 501
501: def splash_message_color
502: str = splash_message
503: ruby = colors[:ruby]
504: none = colors[:none]
505: return str.sub(/R u b y/) { "#{ruby}R u b y#{none}" }
506: end