module Bio::Shell::Ghost

Attributes

cache[RW]

A hash to store temporal (per session) configurations

config[RW]

A hash to store persistent configurations

Public Instance Methods

check_marshal() click to toggle source

object

# File lib/bio/shell/core.rb, line 298
def check_marshal
  if @config[:marshal] and @config[:marshal] != MARSHAL
    raise "Marshal version mismatch"
  end
end
close_history() click to toggle source
# File lib/bio/shell/core.rb, line 381
def close_history
  if @cache[:histfile]
    STDERR.print "Saving history (#{history_file}) ... "
    @cache[:histfile].close
    STDERR.puts "done"
  end
end
closing_splash() click to toggle source
# File lib/bio/shell/core.rb, line 565
def closing_splash
  STDERR.puts
  STDERR.puts
  if @config[:color]
    STDERR.print splash_message_color
  else
    STDERR.print splash_message
  end
  STDERR.puts
  STDERR.puts
end
config_color() click to toggle source
# File lib/bio/shell/core.rb, line 250
def config_color
  bind = Bio::Shell.cache[:binding]
  flag = ! @config[:color]
  @config[:color] = flag
  if flag
    IRB.conf[:PROMPT_MODE] = :BIORUBY_COLOR
    eval("conf.prompt_mode = :BIORUBY_COLOR", bind)
  else
    IRB.conf[:PROMPT_MODE] = :BIORUBY
    eval("conf.prompt_mode = :BIORUBY", bind)
  end
end
config_echo() click to toggle source
# File lib/bio/shell/core.rb, line 242
def config_echo
  bind = Bio::Shell.cache[:binding]
  flag = ! @config[:echo]
  @config[:echo] = IRB.conf[:ECHO] = flag
  eval("conf.echo = #{flag}", bind)
  STDERR.puts "Echo #{flag ? 'on' : 'off'}"
end
config_message(str = nil) click to toggle source
# File lib/bio/shell/core.rb, line 274
def config_message(str = nil)
  str ||= MESSAGE
  @config[:message] = str
  opening_splash
end
config_pager(cmd = nil) click to toggle source
# File lib/bio/shell/core.rb, line 263
def config_pager(cmd = nil)
  @config[:pager] = cmd
end
config_show() click to toggle source
# File lib/bio/shell/core.rb, line 236
def config_show
  @config.each do |k, v|
    STDERR.puts "#{k}\t= #{v.inspect}"
  end
end
config_splash() click to toggle source
# File lib/bio/shell/core.rb, line 267
def config_splash
  flag = ! @config[:splash]
  @config[:splash] = flag
  STDERR.puts "Splash #{flag ? 'on' : 'off'}"
  opening_splash
end
configure(savedir) click to toggle source

save/restore the environment

# File lib/bio/shell/core.rb, line 114
def configure(savedir)
  @config = {}
  @cache  = {
    :savedir => savedir,
    :workdir => Dir.pwd,
  }
  create_save_dir
  load_config
  load_plugin
end
create_flat_dir(dbname) click to toggle source

bioflat

# File lib/bio/shell/core.rb, line 187
def create_flat_dir(dbname)
  dir = File.join(bioflat_dir, dbname.to_s.strip)
  unless File.directory?(dir)
    FileUtils.makedirs(dir)
  end
  return dir
end
create_real_dir(dir) click to toggle source
# File lib/bio/shell/core.rb, line 173
def create_real_dir(dir)
  unless File.directory?(dir)
    begin
      STDERR.print "Creating directory (#{dir}) ... "
      FileUtils.makedirs(dir)
      STDERR.puts "done"
    rescue
      warn "Error: Failed to create directory (#{dir}) : #{$!}"
    end
  end
end
create_save_dir() click to toggle source

directories

# File lib/bio/shell/core.rb, line 149
def create_save_dir
  create_real_dir(session_dir)
  create_real_dir(plugin_dir)
  create_real_dir(data_dir)
end
create_save_dir_ask() click to toggle source
# File lib/bio/shell/core.rb, line 155
def create_save_dir_ask
  if File.directory?(session_dir)
    @cache[:save] = true
  end
  unless @cache[:save]
    if ask_yes_or_no("Save session in '#{@cache[:workdir]}' directory? [y/n] ")
      create_real_dir(session_dir)
      create_real_dir(plugin_dir)
      create_real_dir(data_dir)
      create_real_dir(bioflat_dir)
      @cache[:save] = true
    else
      @cache[:save] = false
    end
  end
  return @cache[:save]
end
find_flat_dir(dbname) click to toggle source
# File lib/bio/shell/core.rb, line 195
def find_flat_dir(dbname)
  dir = File.join(bioflat_dir, dbname.to_s.strip)
  if File.exist?(dir)
    return dir
  else
    return nil
  end
end
load_config() click to toggle source

config

# File lib/bio/shell/core.rb, line 206
def load_config
  load_config_file(config_file)
end
load_config_file(file) click to toggle source
# File lib/bio/shell/core.rb, line 210
def load_config_file(file)
  if File.exist?(file)
    STDERR.print "Loading config (#{file}) ... "
    if hash = YAML.load(File.read(file))
      @config.update(hash)
    end
    STDERR.puts "done"
  end
end
load_history() click to toggle source
# File lib/bio/shell/core.rb, line 389
def load_history
  if @cache[:readline]
    load_history_file(history_file)
  end
end
load_history_file(file) click to toggle source
# File lib/bio/shell/core.rb, line 395
def load_history_file(file)
  if File.exist?(file)
    STDERR.print "Loading history (#{file}) ... "
    File.open(file).each do |line|
      unless line[/^# /]
        Readline::HISTORY.push line.chomp
      end
    end
    STDERR.puts "done"
  end
end
load_object() click to toggle source
# File lib/bio/shell/core.rb, line 304
def load_object
  begin
    check_marshal
    load_object_file(object_file)
  rescue
    warn "Error: Load aborted : #{$!}"
  end
end
load_object_file(file) click to toggle source
# File lib/bio/shell/core.rb, line 313
def load_object_file(file)
  if File.exist?(file)
    STDERR.print "Loading object (#{file}) ... "
    begin
      bind = Bio::Shell.cache[:binding]
      hash = Marshal.load(File.read(file))
      hash.each do |k, v|
        begin
          Thread.current[:restore_value] = v
          eval("#{k} = Thread.current[:restore_value]", bind)
        rescue
          STDERR.puts "Warning: object '#{k}' couldn't be loaded : #{$!}"
        end
      end
    rescue
      warn "Error: Failed to load (#{file}) : #{$!}"
    end
    STDERR.puts "done"
  end
end
load_plugin() click to toggle source

plugin

# File lib/bio/shell/core.rb, line 282
def load_plugin
  load_plugin_dir(plugin_dir)
end
load_plugin_dir(dir) click to toggle source
# File lib/bio/shell/core.rb, line 286
def load_plugin_dir(dir)
  if File.directory?(dir)
    Dir.glob("#{dir}/*.rb").sort.each do |file|
      STDERR.print "Loading plugin (#{file}) ... "
      load file
      STDERR.puts "done"
    end
  end
end
load_session() click to toggle source
# File lib/bio/shell/core.rb, line 125
def load_session
  load_object
  unless @cache[:mode] == :script
    load_history
    opening_splash
    open_history
  end
end
open_history() click to toggle source

history

# File lib/bio/shell/core.rb, line 371
def open_history
  @cache[:histfile] = File.open(history_file, "a")
  @cache[:histfile].sync = true
end
opening_splash() click to toggle source
# File lib/bio/shell/core.rb, line 543
def opening_splash
  STDERR.puts
  if @config[:splash]
    if @config[:color]
      splash_message_action_color
    else
      splash_message_action
    end
  end
  if @config[:color]
    STDERR.print splash_message_color
  else
    STDERR.print splash_message
  end
  STDERR.puts
  STDERR.puts
  STDERR.print "  Version : BioRuby #{Bio::BIORUBY_VERSION_ID}"
  STDERR.print " / Ruby #{RUBY_VERSION}"
  STDERR.puts
  STDERR.puts
end
save_config() click to toggle source
# File lib/bio/shell/core.rb, line 220
def save_config
  save_config_file(config_file)
end
save_config_file(file) click to toggle source
# File lib/bio/shell/core.rb, line 224
def save_config_file(file)
  begin
    STDERR.print "Saving config (#{file}) ... "
    File.open(file, "w") do |f|
      f.puts @config.to_yaml
    end
    STDERR.puts "done"
  rescue
    warn "Error: Failed to save (#{file}) : #{$!}"
  end
end
save_history() click to toggle source

not used (use open_history/close_history instead)

# File lib/bio/shell/core.rb, line 408
def save_history
  if @cache[:readline]
    save_history_file(history_file)
  end
end
save_history_file(file) click to toggle source
# File lib/bio/shell/core.rb, line 414
def save_history_file(file)
  begin
    STDERR.print "Saving history (#{file}) ... "
    File.open(file, "w") do |f|
      f.puts Readline::HISTORY.to_a
    end
    STDERR.puts "done"
  rescue
    warn "Error: Failed to save (#{file}) : #{$!}"
  end
end
save_object() click to toggle source
# File lib/bio/shell/core.rb, line 334
def save_object
  save_object_file(object_file)
end
save_object_file(file) click to toggle source
# File lib/bio/shell/core.rb, line 338
def save_object_file(file)
  begin
    STDERR.print "Saving object (#{file}) ... "
    File.rename(file, "#{file}.old") if File.exist?(file)
    File.open(file, "w") do |f|
      bind = Bio::Shell.cache[:binding]
      list = eval("local_variables", bind)
      list.collect! { |x| x.to_s }
      list -= ["_"]
      hash = {}
      list.each do |elem|
        value = eval(elem, bind)
        if value
          begin
            Marshal.dump(value)
            hash[elem] = value
          rescue
            # value could not be dumped.
          end
        end
      end
      Marshal.dump(hash, f)
      @config[:marshal] = MARSHAL
    end
    STDERR.puts "done"
  rescue
    File.rename("#{file}.old", file) if File.exist?("#{file}.old")
    warn "Error: Failed to save (#{file}) : #{$!}"
  end
end
save_script() click to toggle source
# File lib/bio/shell/core.rb, line 459
def save_script
  if @script_begin and @script_end and @script_begin <= @script_end
    if File.exist?(script_file)
      message = "Overwrite script file (#{script_file})? [y/n] "
    else
      message = "Save script file (#{script_file})? [y/n] "
    end
    if ask_yes_or_no(message)
      save_script_file(script_file)
    else
      STDERR.puts " ... save aborted."
    end
  elsif @script_begin and @script_end and @script_begin - @script_end == 1
    STDERR.puts " ... script aborted."
  else
    STDERR.puts "Error: Script range #{@script_begin}..#{@script_end} is invalid"
  end
end
save_script_file(file) click to toggle source
# File lib/bio/shell/core.rb, line 478
def save_script_file(file)
  begin
    STDERR.print "Saving script (#{file}) ... "
    File.open(file, "w") do |f|
      f.puts "#!/usr/bin/env bioruby"
      f.puts
      f.puts Readline::HISTORY.to_a[@script_begin..@script_end]
      f.puts
    end
    STDERR.puts "done"
  rescue
    @script_begin = nil
    warn "Error: Failed to save (#{file}) : #{$!}"
  end
end
save_session() click to toggle source
# File lib/bio/shell/core.rb, line 134
def save_session
  unless @cache[:mode] == :script
    closing_splash
  end
  if create_save_dir_ask
    #save_history     # changed to use our own...
    close_history
    save_object
    save_config
  end
  #STDERR.puts "Leaving directory '#{@cache[:workdir]}'"
end
script(mode = nil) click to toggle source

script

# File lib/bio/shell/core.rb, line 428
def script(mode = nil)
  case mode
  when :begin, "begin", :start, "start"
    @cache[:script] = true
    script_begin
  when :end, "end", :stop, "stop"
    @cache[:script] = false
    script_end
    save_script
  else
    if @cache[:script]
      @cache[:script] = false
      script_end
      save_script
    else
      @cache[:script] = true
      script_begin
    end
  end
end
script_begin() click to toggle source
# File lib/bio/shell/core.rb, line 449
def script_begin
  STDERR.puts "-- 8< -- 8< -- 8< --  Script  -- 8< -- 8< -- 8< --"
  @script_begin = Readline::HISTORY.size
end
script_end() click to toggle source
# File lib/bio/shell/core.rb, line 454
def script_end
  STDERR.puts "-- >8 -- >8 -- >8 --  Script  -- >8 -- >8 -- >8 --"
  @script_end = Readline::HISTORY.size - 2
end
splash_message() click to toggle source

splash

# File lib/bio/shell/core.rb, line 496
def splash_message
  @config[:message] ||= MESSAGE
  @config[:message].to_s.split(//).join(" ")
end
splash_message_action(message = nil) click to toggle source
# File lib/bio/shell/core.rb, line 508
def splash_message_action(message = nil)
  s = message || splash_message
  l = s.length
  x = " "
  0.step(l,2) do |i|
    l1 = l-i;  l2 = l1/2;  l4 = l2/2
    STDERR.print "#{s[0,i]}#{x*l1}#{s[i,1]}\r"
    sleep(0.001)
    STDERR.print "#{s[0,i]}#{x*l2}#{s[i,1]}#{x*(l1-l2)}\r"
    sleep(0.002)
    STDERR.print "#{s[0,i]}#{x*l4}#{s[i,1]}#{x*(l2-l4)}\r"
    sleep(0.004)
    STDERR.print "#{s[0,i+1]}#{x*l4}\r"
    sleep(0.008)
  end
end
splash_message_action_color(message = nil) click to toggle source
# File lib/bio/shell/core.rb, line 525
def splash_message_action_color(message = nil)
  s = message || splash_message
  l = s.length
  c = colors
  x = " "
  0.step(l,2) do |i|
    l1 = l-i;  l2 = l1/2;  l4 = l2/2
    STDERR.print "#{c[:n]}#{s[0,i]}#{x*l1}#{c[:y]}#{s[i,1]}\r"
    sleep(0.001)
    STDERR.print "#{c[:n]}#{s[0,i]}#{x*l2}#{c[:g]}#{s[i,1]}#{x*(l1-l2)}\r"
    sleep(0.002)
    STDERR.print "#{c[:n]}#{s[0,i]}#{x*l4}#{c[:r]}#{s[i,1]}#{x*(l2-l4)}\r"
    sleep(0.004)
    STDERR.print "#{c[:n]}#{s[0,i+1]}#{x*l4}\r"
    sleep(0.008)
  end
end
splash_message_color() click to toggle source
# File lib/bio/shell/core.rb, line 501
def splash_message_color
  str = splash_message
  ruby = colors[:ruby]
  none = colors[:none]
  return str.sub(/R u b y/) { "#{ruby}R u b y#{none}" }
end
store_history(line) click to toggle source
# File lib/bio/shell/core.rb, line 376
def store_history(line)
  Bio::Shell.cache[:histfile].puts "# #{Time.now}"
  Bio::Shell.cache[:histfile].puts line
end