Module | Bio::Shell::Ghost
In: | lib/bio/shell/core.rb

### Methods

check_marshal close_history closing_splash config_color config_echo config_message config_pager config_show config_splash configure create_flat_dir create_real_dir create_save_dir create_save_dir_ask find_flat_dir load_config load_config_file load_history load_history_file load_object load_object_file load_plugin load_plugin_dir load_session open_history opening_splash save_config save_config_file save_history save_history_file save_object save_object_file save_script save_script_file save_session script script_begin script_end splash_message splash_message_action splash_message_action_color splash_message_color store_history

### Included Modules

Bio::Shell::Core

### Attributes

cache | [RW] | A hash to store temporal (per session) configurations
config | [RW] | A hash to store persistent configurations

### Public Instance methods

check_marshal()

object

[Source]

         # 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

close_history()

[Source]

         # File lib/bio/shell/core.rb, line 382
    382:   def close_history
    383:     if @cache[:histfile]
    384:       STDERR.print "Saving history (#{history_file}) ... "
    385:       @cache[:histfile].close
    386:       STDERR.puts "done"
    387:     end
    388:   end

closing_splash()

[Source]

         # File lib/bio/shell/core.rb, line 566
    566:   def closing_splash
    567:     STDERR.puts
    568:     STDERR.puts
    569:     if @config[:color]
    570:       STDERR.print splash_message_color
    571:     else
    572:       STDERR.print splash_message
    573:     end
    574:     STDERR.puts
    575:     STDERR.puts
    576:   end

config_color()

[Source]

         # 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

config_echo()

[Source]

         # 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

config_message(str = nil)

[Source]

         # 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

config_pager(cmd = nil)

[Source]

         # File lib/bio/shell/core.rb, line 264
    264:   def config_pager(cmd = nil)
    265:     @config[:pager] = cmd
    266:   end

config_show()

[Source]

         # 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

config_splash()

[Source]

         # 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

configure(savedir)

save/restore the environment

[Source]

         # 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

create_flat_dir(dbname)

bioflat

[Source]

         # 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

create_real_dir(dir)

[Source]

         # 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

create_save_dir()

directories

[Source]

         # 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

create_save_dir_ask()

[Source]

         # 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

find_flat_dir(dbname)

[Source]

         # 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

load_config()

config

[Source]

         # File lib/bio/shell/core.rb, line 207
    207:   def load_config
    208:     load_config_file(config_file)
    209:   end

load_config_file(file)

[Source]

         # 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

load_history()

[Source]

         # File lib/bio/shell/core.rb, line 390
    390:   def load_history
    391:     if @cache[:readline]
    392:       load_history_file(history_file)
    393:     end
    394:   end

load_history_file(file)

[Source]

         # File lib/bio/shell/core.rb, line 396
    396:   def load_history_file(file)
    397:     if File.exists?(file)
    398:       STDERR.print "Loading history (#{file}) ... "
    399:       File.open(file).each do |line|
    400:         unless line[/^# /]
    401:           Readline::HISTORY.push line.chomp
    402:         end
    403:       end
    404:       STDERR.puts "done"
    405:     end
    406:   end

load_object()

[Source]

         # 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

load_object_file(file)

[Source]

         # 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

load_plugin()

plugin

[Source]

         # File lib/bio/shell/core.rb, line 283
    283:   def load_plugin
    284:     load_plugin_dir(plugin_dir)
    285:   end

load_plugin_dir(dir)

[Source]

         # 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

load_session()

[Source]

         # 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

open_history()

history

[Source]

         # File lib/bio/shell/core.rb, line 372
    372:   def open_history
    373:     @cache[:histfile] = File.open(history_file, "a")
    374:     @cache[:histfile].sync = true
    375:   end

opening_splash()

[Source]

         # File lib/bio/shell/core.rb, line 544
    544:   def opening_splash
    545:     STDERR.puts
    546:     if @config[:splash]
    547:       if @config[:color]
    548:         splash_message_action_color
    549:       else
    550:         splash_message_action
    551:       end
    552:     end
    553:     if @config[:color]
    554:       STDERR.print splash_message_color
    555:     else
    556:       STDERR.print splash_message
    557:     end
    558:     STDERR.puts
    559:     STDERR.puts
    560:     STDERR.print "  Version : BioRuby #{Bio::BIORUBY_VERSION_ID}"
    561:     STDERR.print " / Ruby #{RUBY_VERSION}"
    562:     STDERR.puts
    563:     STDERR.puts
    564:   end

save_config()

[Source]

         # File lib/bio/shell/core.rb, line 221
    221:   def save_config
    222:     save_config_file(config_file)
    223:   end

save_config_file(file)

[Source]

         # 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

save_history()

not used (use open_history/close_history instead)

[Source]

         # File lib/bio/shell/core.rb, line 409
    409:   def save_history
    410:     if @cache[:readline]
    411:       save_history_file(history_file)
    412:     end
    413:   end

save_history_file(file)

[Source]

         # File lib/bio/shell/core.rb, line 415
    415:   def save_history_file(file)
    416:     begin
    417:       STDERR.print "Saving history (#{file}) ... "
    418:       File.open(file, "w") do |f|
    419:         f.puts Readline::HISTORY.to_a
    420:       end
    421:       STDERR.puts "done"
    422:     rescue
    423:       warn "Error: Failed to save (#{file}) : #{$!}"
    424:     end
    425:   end

save_object()

[Source]

         # File lib/bio/shell/core.rb, line 335
    335:   def save_object
    336:     save_object_file(object_file)
    337:   end

save_object_file(file)

[Source]

         # 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.collect! { |x| x.to_s }
    347:         list -= ["_"]
    348:         hash = {}
    349:         list.each do |elem|
    350:           value = eval(elem, bind)
    351:           if value
    352:             begin
    353:               Marshal.dump(value)
    354:               hash[elem] = value
    355:             rescue
    356:               # value could not be dumped.
    357:             end
    358:           end
    359:         end
    360:         Marshal.dump(hash, f)
    361:         @config[:marshal] = MARSHAL
    362:       end
    363:       STDERR.puts "done"
    364:     rescue
    365:       File.rename("#{file}.old", file) if File.exist?("#{file}.old")
    366:       warn "Error: Failed to save (#{file}) : #{$!}"
    367:     end
    368:   end

save_script()

[Source]

         # File lib/bio/shell/core.rb, line 460
    460:   def save_script
    461:     if @script_begin and @script_end and @script_begin <= @script_end
    462:       if File.exists?(script_file)
    463:         message = "Overwrite script file (#{script_file})? [y/n] "
    464:       else
    465:         message = "Save script file (#{script_file})? [y/n] "
    466:       end
    467:       if ask_yes_or_no(message)
    468:         save_script_file(script_file)
    469:       else
    470:         STDERR.puts " ... save aborted."
    471:       end
    472:     elsif @script_begin and @script_end and @script_begin - @script_end == 1
    473:       STDERR.puts " ... script aborted."
    474:     else
    475:       STDERR.puts "Error: Script range #{@script_begin}..#{@script_end} is invalid"
    476:     end
    477:   end

save_script_file(file)

[Source]

         # File lib/bio/shell/core.rb, line 479
    479:   def save_script_file(file)
    480:     begin
    481:       STDERR.print "Saving script (#{file}) ... "
    482:       File.open(file, "w") do |f|
    483:         f.puts "#!/usr/bin/env bioruby"
    484:         f.puts
    485:         f.puts Readline::HISTORY.to_a[@script_begin..@script_end]
    486:         f.puts
    487:       end
    488:       STDERR.puts "done"
    489:     rescue
    490:       @script_begin = nil
    491:       warn "Error: Failed to save (#{file}) : #{$!}"
    492:     end
    493:   end

save_session()

[Source]

         # 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

script(mode = nil)

script

[Source]

         # File lib/bio/shell/core.rb, line 429
    429:   def script(mode = nil)
    430:     case mode
    431:     when :begin, "begin", :start, "start"
    432:       @cache[:script] = true
    433:       script_begin
    434:     when :end, "end", :stop, "stop"
    435:       @cache[:script] = false
    436:       script_end
    437:       save_script
    438:     else
    439:       if @cache[:script]
    440:         @cache[:script] = false
    441:         script_end
    442:         save_script
    443:       else
    444:         @cache[:script] = true
    445:         script_begin
    446:       end
    447:     end
    448:   end

script_begin()

[Source]

         # File lib/bio/shell/core.rb, line 450
    450:   def script_begin
    451:     STDERR.puts "-- 8< -- 8< -- 8< --  Script  -- 8< -- 8< -- 8< --"
    452:     @script_begin = Readline::HISTORY.size
    453:   end

script_end()

[Source]

         # File lib/bio/shell/core.rb, line 455
    455:   def script_end
    456:     STDERR.puts "-- >8 -- >8 -- >8 --  Script  -- >8 -- >8 -- >8 --"
    457:     @script_end = Readline::HISTORY.size - 2
    458:   end

splash_message()

splash

[Source]

         # File lib/bio/shell/core.rb, line 497
    497:   def splash_message
    498:     @config[:message] ||= MESSAGE
    499:     @config[:message].to_s.split(//).join(" ")
    500:   end

splash_message_action(message = nil)

[Source]

         # File lib/bio/shell/core.rb, line 509
    509:   def splash_message_action(message = nil)
    510:     s = message || splash_message
    511:     l = s.length
    512:     x = " "
    513:     0.step(l,2) do |i|
    514:       l1 = l-i;  l2 = l1/2;  l4 = l2/2
    515:       STDERR.print "#{s[0,i]}#{x*l1}#{s[i,1]}\r"
    516:       sleep(0.001)
    517:       STDERR.print "#{s[0,i]}#{x*l2}#{s[i,1]}#{x*(l1-l2)}\r"
    518:       sleep(0.002)
    519:       STDERR.print "#{s[0,i]}#{x*l4}#{s[i,1]}#{x*(l2-l4)}\r"
    520:       sleep(0.004)
    521:       STDERR.print "#{s[0,i+1]}#{x*l4}\r"
    522:       sleep(0.008)
    523:     end
    524:   end

splash_message_action_color(message = nil)

[Source]

         # File lib/bio/shell/core.rb, line 526
    526:   def splash_message_action_color(message = nil)
    527:     s = message || splash_message
    528:     l = s.length
    529:     c = colors
    530:     x = " "
    531:     0.step(l,2) do |i|
    532:       l1 = l-i;  l2 = l1/2;  l4 = l2/2
    533:       STDERR.print "#{c[:n]}#{s[0,i]}#{x*l1}#{c[:y]}#{s[i,1]}\r"
    534:       sleep(0.001)
    535:       STDERR.print "#{c[:n]}#{s[0,i]}#{x*l2}#{c[:g]}#{s[i,1]}#{x*(l1-l2)}\r"
    536:       sleep(0.002)
    537:       STDERR.print "#{c[:n]}#{s[0,i]}#{x*l4}#{c[:r]}#{s[i,1]}#{x*(l2-l4)}\r"
    538:       sleep(0.004)
    539:       STDERR.print "#{c[:n]}#{s[0,i+1]}#{x*l4}\r"
    540:       sleep(0.008)
    541:     end
    542:   end

splash_message_color()

[Source]

         # File lib/bio/shell/core.rb, line 502
    502:   def splash_message_color
    503:     str = splash_message
    504:     ruby = colors[:ruby]
    505:     none = colors[:none]
    506:     return str.sub(/R u b y/) { "#{ruby}R u b y#{none}" }
    507:   end

store_history(line)

[Source]

         # File lib/bio/shell/core.rb, line 377
    377:   def store_history(line)
    378:     Bio::Shell.cache[:histfile].puts "# #{Time.now}"
    379:     Bio::Shell.cache[:histfile].puts line
    380:   end

[Validate]