[BioRuby-cvs] bioruby/lib/bio/shell core.rb,1.21,1.22

Katayama Toshiaki k at dev.open-bio.org
Sun Dec 24 08:36:02 UTC 2006


Update of /home/repository/bioruby/bioruby/lib/bio/shell
In directory dev.open-bio.org:/tmp/cvs-serv9476

Modified Files:
	core.rb 
Log Message:
* independent from IRB (except for some config routines)
* interface (Core) is separated from internals (Ghost)


Index: core.rb
===================================================================
RCS file: /home/repository/bioruby/bioruby/lib/bio/shell/core.rb,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** core.rb	27 Feb 2006 09:09:57 -0000	1.21
--- core.rb	24 Dec 2006 08:36:00 -0000	1.22
***************
*** 9,23 ****
  #
  
  
! module Bio::Shell::Ghost
! 
!   SAVEDIR = "session/"
    CONFIG  = "config"
    OBJECT  = "object"
    HISTORY = "history"
!   SCRIPT  = "script.rb"
!   PLUGIN  = "plugin/"
    DATADIR = "data/"
!   BIOFLAT = "bioflat/"
  
    MARSHAL = [ Marshal::MAJOR_VERSION, Marshal::MINOR_VERSION ]
--- 9,22 ----
  #
  
+ module Bio::Shell::Core
  
!   SAVEDIR = "shell/session/"
    CONFIG  = "config"
    OBJECT  = "object"
    HISTORY = "history"
!   SCRIPT  = "shell/script.rb"
!   PLUGIN  = "shell/plugin/"
    DATADIR = "data/"
!   BIOFLAT = "data/bioflat/"
  
    MARSHAL = [ Marshal::MAJOR_VERSION, Marshal::MINOR_VERSION ]
***************
*** 37,42 ****
    }
  
!   def history
!     SAVEDIR + HISTORY
    end
  
--- 36,41 ----
    }
  
!   def colors
!     ESC_SEQ
    end
  
***************
*** 45,63 ****
    end
  
!   def esc_seq
!     ESC_SEQ
    end
  
!   ### save/restore the environment
  
!   def setup
!     @config = {}
!     @cache  = {}
!     check_version
!     check_marshal
!     load_config
!     load_plugin
    end
!  
    # A hash to store persistent configurations
    attr_accessor :config
--- 44,83 ----
    end
  
!   def script_dir
!     File.dirname(SCRIPT)
    end
  
!   def object_file
!     SAVEDIR + OBJECT
!   end
  
!   def history_file
!     SAVEDIR + HISTORY
    end
! 
!   def ask_yes_or_no(message)
!     loop do
!       print "#{message}"
!       answer = gets
!       if answer.nil?
!         # readline support might be broken
!         return false
!       elsif /^\s*[Nn]/.match(answer)
!         return false
!       elsif /^\s*[Yy]/.match(answer)
!         return true
!       else
!         # loop
!       end
!     end
!   end
! 
! end
! 
! 
! module Bio::Shell::Ghost
! 
!   include Bio::Shell::Core
! 
    # A hash to store persistent configurations
    attr_accessor :config
***************
*** 66,76 ****
--- 86,108 ----
    attr_accessor :cache
  
+   ### save/restore the environment
+ 
+   def configure
+     @config = {}
+     @cache  = {}
+     create_save_dir
+     load_config
+     load_plugin
+   end
+  
    def load_session
      load_object
      load_history
      opening_splash
+     open_history
    end
  
    def save_session
+     close_history
      closing_splash
      if create_save_dir_ask
***************
*** 79,97 ****
        save_config
      end
    end
  
!   ### setup
! 
!   def check_version
!     if RUBY_VERSION < "1.8.2"
!       raise "BioRuby shell runs on Ruby version >= 1.8.2"
!     end
!   end
! 
!   def check_marshal
!     if @config[:marshal] and @config[:marshal] != MARSHAL
!       raise "Marshal version mismatch"
!     end
!   end
  
    def create_save_dir
--- 111,119 ----
        save_config
      end
+     puts "Leaving directory '#{@cache[:workdir]}'"
+     puts "History is saved in '#{@cache[:workdir]}/#{SAVEDIR + HISTORY}'"
    end
  
!   ### directories
  
    def create_save_dir
***************
*** 108,114 ****
        if ask_yes_or_no("Save session in '#{SAVEDIR}' directory? [y/n] ")
          create_real_dir(SAVEDIR)
-         create_real_dir(DATADIR)
          create_real_dir(PLUGIN)
! #       create_real_dir(BIOFLAT)
          @cache[:save] = true
        else
--- 130,136 ----
        if ask_yes_or_no("Save session in '#{SAVEDIR}' directory? [y/n] ")
          create_real_dir(SAVEDIR)
          create_real_dir(PLUGIN)
!         create_real_dir(DATADIR)
!         create_real_dir(BIOFLAT)
          @cache[:save] = true
        else
***************
*** 119,144 ****
    end
  
-   def ask_yes_or_no(message)
-     loop do
-       print "#{message}"
-       answer = gets
-       if answer.nil?
-         # readline support might be broken
-         return false
-       elsif /^\s*[Nn]/.match(answer)
-         return false
-       elsif /^\s*[Yy]/.match(answer)
-         return true
-       else
-         # loop
-       end
-     end
-   end
- 
    def create_real_dir(dir)
      unless File.directory?(dir)
        begin
          print "Creating directory (#{dir}) ... "
!         Dir.mkdir(dir)
          puts "done"
        rescue
--- 141,149 ----
    end
  
    def create_real_dir(dir)
      unless File.directory?(dir)
        begin
          print "Creating directory (#{dir}) ... "
!         FileUtils.mkdir_p(dir)
          puts "done"
        rescue
***************
*** 152,160 ****
    def create_flat_dir(dbname)
      dir = BIOFLAT + dbname.to_s.strip
-     unless File.directory?(BIOFLAT)
-       Dir.mkdir(BIOFLAT)
-     end
      unless File.directory?(dir)
!       Dir.mkdir(dir)
      end
      return dir
--- 157,162 ----
    def create_flat_dir(dbname)
      dir = BIOFLAT + dbname.to_s.strip
      unless File.directory?(dir)
!       FileUtils.mkdir_p(dir)
      end
      return dir
***************
*** 209,213 ****
  
    def config_echo
!     bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
      flag = ! @config[:echo]
      @config[:echo] = IRB.conf[:ECHO] = flag
--- 211,215 ----
  
    def config_echo
!     bind = Bio::Shell.cache[:binding]
      flag = ! @config[:echo]
      @config[:echo] = IRB.conf[:ECHO] = flag
***************
*** 217,221 ****
  
    def config_color
!     bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
      flag = ! @config[:color]
      @config[:color] = flag
--- 219,223 ----
  
    def config_color
!     bind = Bio::Shell.cache[:binding]
      flag = ! @config[:color]
      @config[:color] = flag
***************
*** 264,269 ****
    ### object
  
    def load_object
!     load_object_file(SAVEDIR + OBJECT)
    end
  
--- 266,282 ----
    ### object
  
+   def check_marshal
+     if @config[:marshal] and @config[:marshal] != MARSHAL
+       raise "Marshal version mismatch"
+     end
+   end
+ 
    def load_object
!     begin
!       check_marshal
!       load_object_file(SAVEDIR + OBJECT)
!     rescue
!       warn "Error: Load aborted : #{$!}"
!     end
    end
  
***************
*** 272,276 ****
        print "Loading object (#{file}) ... "
        begin
!         bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
          hash = Marshal.load(File.read(file))
          hash.each do |k, v|
--- 285,289 ----
        print "Loading object (#{file}) ... "
        begin
!         bind = Bio::Shell.cache[:binding]
          hash = Marshal.load(File.read(file))
          hash.each do |k, v|
***************
*** 288,292 ****
      end
    end
!   
    def save_object
      save_object_file(SAVEDIR + OBJECT)
--- 301,305 ----
      end
    end
! 
    def save_object
      save_object_file(SAVEDIR + OBJECT)
***************
*** 296,324 ****
      begin
        print "Saving object (#{file}) ... "
        File.open(file, "w") do |f|
!         begin
!           bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
!           list = eval("local_variables", bind)
!           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
-         rescue
-           warn "Error: Failed to dump (#{file}) : #{$!}"
          end
        end
        puts "done"
      rescue
        warn "Error: Failed to save (#{file}) : #{$!}"
      end
--- 309,335 ----
      begin
        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 -= ["_"]
!         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
        puts "done"
      rescue
+       File.rename("#{file}.old", file) if File.exist?("#{file}.old")
        warn "Error: Failed to save (#{file}) : #{$!}"
      end
***************
*** 327,330 ****
--- 338,355 ----
    ### history
  
+   def open_history
+     @cache[:histfile] = File.open(SAVEDIR + HISTORY, "a")
+     @cache[:histfile].sync = true
+   end
+ 
+   def store_history(line)
+     Bio::Shell.cache[:histfile].puts "# #{Time.now}"
+     Bio::Shell.cache[:histfile].puts line
+   end
+ 
+   def close_history
+     @cache[:histfile].close if @cache[:histfile]
+   end
+ 
    def load_history
      if @cache[:readline]
***************
*** 337,343 ****
        print "Loading history (#{file}) ... "
        File.open(file).each do |line|
!         #Readline::HISTORY.push line.chomp
! 	date, hist = line.chomp.split("\t")
!         Readline::HISTORY.push hist if hist
        end
        puts "done"
--- 362,368 ----
        print "Loading history (#{file}) ... "
        File.open(file).each do |line|
!         unless line[/^# /]
!           Readline::HISTORY.push line.chomp
!         end
        end
        puts "done"
***************
*** 345,348 ****
--- 370,374 ----
    end
    
+   # not used (use open_history/close_history instead)
    def save_history
      if @cache[:readline]
***************
*** 440,445 ****
    def splash_message_color
      str = splash_message
!     ruby = ESC_SEQ[:ruby]
!     none = ESC_SEQ[:none]
      return str.sub(/R u b y/) { "#{ruby}R u b y#{none}" }
    end
--- 466,471 ----
    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
***************
*** 465,469 ****
      s = message || splash_message
      l = s.length
!     c = ESC_SEQ
      x = " "
      0.step(l,2) do |i|
--- 491,495 ----
      s = message || splash_message
      l = s.length
!     c = colors
      x = " "
      0.step(l,2) do |i|




More information about the bioruby-cvs mailing list