[BioRuby] open_uri (Fwd: [BioRuby-cvs] bioruby/lib/bio command.rb, 1.3, 1.4)

Toshiaki Katayama ktym at hgc.jp
Sun Mar 26 01:28:14 UTC 2006


Goto-san,

> +   # Same as OpenURI.open_uri(*arg).
> +   # If open-uri.rb is already loaded, ::OpenURI is used.
> +   # Otherwise, internal OpenURI in sandbox is used because
> +   # open-uri.rb redefines Kernel.open.

Your code seems to contain a lot of hacks, finding open-uri.rb from Ruby's load path,
searching a particular method from it etc...

I don't understand what the complicated part of your Sandbox module actually does
(or intends), but if your purpose is just to avoid redefine of Kenel.open, to put
something like

> require 'open-uri'
>
> module Kernel
>   private
>   alias open_uri open
>   alias open open_uri_original_open
> end

isn't enough?


Regards,
Toshiaki Katayama


-----test_open_uri.rb
#!/usr/bin/env ruby

require 'open-uri'

module Kernel
  private
  alias open_uri open
  alias open open_uri_original_open
end

url = "http://bioruby.org"

p "########## open_uri"
open_uri(url) do |f|
  puts f.read
end

p "########## open"
open(url) do |f|
  puts f.read
end


Begin forwarded message:

> From: Naohisa Goto <ngoto at pub.open-bio.org>
> Date: 2006年3月20日 19:34:59:JST
> To: bioruby-cvs at portal.open-bio.org
> Subject: [BioRuby-cvs] bioruby/lib/bio command.rb,1.3,1.4
>
> Update of /home/repository/bioruby/bioruby/lib/bio
> In directory pub.open-bio.org:/tmp/cvs-serv30042/lib/bio
>
> Modified Files:
> 	command.rb 
> Log Message:
> * New module Bio::Command::NetTools for miscellaneous network methods.
>   Currently, this module is intended to be used only inside
>   BioRuby library. Please do not use it in user's programs now.
> * New methods: Bio::Command::NetTools.open_uri(uri, *arg) and
>   Bio::Command::NetTools.read_uri(uri).
> * Changed license to Ruby's.
>
>
> Index: command.rb
> ===================================================================
> RCS file: /home/repository/bioruby/bioruby/lib/bio/command.rb,v
> retrieving revision 1.3
> retrieving revision 1.4
> diff -C2 -d -r1.3 -r1.4
> *** command.rb	4 Nov 2005 17:36:00 -0000	1.3
> --- command.rb	20 Mar 2006 10:34:57 -0000	1.4
> ***************
> *** 2,32 ****
>   # = bio/command.rb - general methods for external command execution
>   #
> ! # Copyright::	Copyright (C) 2003-2005
>   # 		Naohisa Goto <ng at bioruby.org>,
>   #		Toshiaki Katayama <k at bioruby.org>
> ! # License::	LGPL
>   #
>   #  $Id$
>   #
> - #--
> - #
> - #  This library is free software; you can redistribute it and/or
> - #  modify it under the terms of the GNU Lesser General Public
> - #  License as published by the Free Software Foundation; either
> - #  version 2 of the License, or (at your option) any later version.
> - #
> - #  This library is distributed in the hope that it will be useful,
> - #  but WITHOUT ANY WARRANTY; without even the implied warranty of
> - #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> - #  Lesser General Public License for more details.
> - #
> - #  You should have received a copy of the GNU Lesser General Public
> - #  License along with this library; if not, write to the Free Software
> - #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
> - #
> - #++
> - #
>
>   require 'open3'
>
>   module Bio
> --- 2,15 ----
>   # = bio/command.rb - general methods for external command execution
>   #
> ! # Copyright::	Copyright (C) 2003-2006
>   # 		Naohisa Goto <ng at bioruby.org>,
>   #		Toshiaki Katayama <k at bioruby.org>
> ! # License::	Ruby's
>   #
>   #  $Id$
>   #
>
>   require 'open3'
> + require 'uri'
>
>   module Bio
> ***************
> *** 162,165 ****
> --- 145,291 ----
>
>   end # module Tools
> + 
> + 
> + # = Bio::Command::NetTools
> + #
> + # Bio::Command::NetTools is a collection of miscellaneous methods
> + # for data transport through network.
> + #
> + # Library internal use only. Users should not directly use it.
> + #
> + # Note that it is under construction.
> + module NetTools
> + 
> +   # Same as OpenURI.open_uri(*arg).
> +   # If open-uri.rb is already loaded, ::OpenURI is used.
> +   # Otherwise, internal OpenURI in sandbox is used because
> +   # open-uri.rb redefines Kernel.open.
> +   def self.open_uri(uri, *arg)
> +     if defined? ::OpenURI
> +       ::OpenURI.open_uri(uri, *arg)
> +     else
> +       SandBox.load_openuri_in_sandbox
> +       uri = uri.to_s if ::URI::Generic === uri
> +       SandBox::OpenURI.open_uri(uri, *arg)
> +     end
> +   end
> + 
> +   # Same as OpenURI.open_uri(uri).read.
> +   # If open-uri.rb is already loaded, ::OpenURI is used.
> +   # Otherwise, internal OpenURI in sandbox is used becase
> +   # open-uri.rb redefines Kernel.open.
> +   def self.read_uri(uri)
> +     self.open_uri(uri).read
> +   end
> + 
> +   # Sandbox to load open-uri.rb.
> +   # Internal use only.
> +   module SandBox #:nodoc:
> + 
> +     # Dummy module definition.
> +     module Kernel #:nodoc:
> +       # dummy method
> +       def open(*arg); end #:nodoc:
> +     end #module Kernel
> +     
> +     # a method to find proxy. dummy definition
> +     module FindProxy; end #:nodoc:
> +     
> +     # dummy module definition
> +     module OpenURI #:nodoc:
> +       module OpenRead; end #:nodoc:
> +     end #module OpenURI
> +     
> +     # Dummy module definition.
> +     module URI #:nodoc:
> +       class Generic < ::URI::Generic #:nodoc:
> +         include SandBox::FindProxy
> +       end
> +       
> +       class HTTPS < ::URI::HTTPS #:nodoc:
> +         include SandBox::FindProxy
> +         include SandBox::OpenURI::OpenRead
> +       end
> +       
> +       class HTTP  < ::URI::HTTP  #:nodoc:
> +         include SandBox::FindProxy
> +         include SandBox::OpenURI::OpenRead
> +       end
> +       
> +       class FTP  < ::URI::FTP    #:nodoc:
> +         include SandBox::FindProxy
> +         include SandBox::OpenURI::OpenRead
> +       end
> +       
> +       # parse and new. internal use only.
> +       def self.__parse_and_new__(klass, uri) #:nodoc:
> +         scheme, userinfo, host, port,
> +         registry, path, opaque, query, fragment = ::URI.split(uri)
> +         klass.new(scheme, userinfo, host, port,
> +                   registry, path, opaque, query,
> +                   fragment)
> +       end
> +       private_class_method :__parse_and_new__
> +       
> +       # same as ::URI.parse. internal use only.
> +       def self.parse(uri) #:nodoc:
> +         r = ::URI.parse(uri)
> +         case r
> +         when ::URI::HTTPS
> +           __parse_and_new__(HTTPS, uri)
> +         when ::URI::HTTP
> +           __parse_and_new__(HTTP, uri)
> +         when ::URI::FTP
> +           __parse_and_new__(FTP, uri)
> +         else
> +           r
> +         end
> +       end
> +     end #module URI
> +     
> +     @load_openuri = nil
> +     # load open-uri.rb in SandBox module.
> +     def self.load_openuri_in_sandbox #:nodoc:
> +       return if @load_openuri
> +       fn = nil
> +       unless $:.find do |x|
> +           fn = File.join(x, 'open-uri.rb')
> +           FileTest.exist?(fn)
> +         end then
> +         warn('Warning: cannot find open-uri.rb in $LOAD_PATH')
> +       else
> +         # reading open-uri.rb
> +         str = File.read(fn)
> +         # eval open-uri.rb contents in SandBox module
> +         module_eval(str)
> +         
> +         # finds 'find_proxy' method
> +         find_proxy_lines = nil
> +         flag = nil
> +         endstr = nil
> +         str.each do |line|
> +           if flag then
> +             find_proxy_lines << line
> +             if endstr == line[0, endstr.length] and
> +                 /^\s+end(\s+.*)?$/ =~ line then
> +               break
> +             end
> +           elsif /^(\s+)def\s+find_proxy(\s+.*)?$/ =~ line then
> +             flag = true
> +             endstr = "#{$1}end"
> +             find_proxy_lines = line 
> +           end
> +         end
> +         if find_proxy_lines
> +           module_eval("module FindProxy;\n#{find_proxy_lines}\n;end\n")
> +         else
> +           warn('Warning: cannot find find_proxy method in open-uri.rb.')
> +         end
> +         @load_openuri = true
> +       end
> +     end
> +   end #module SandBox
> + end #module NetTools
> + 
>   end # module Command
>   end # module Bio
>
> _______________________________________________
> bioruby-cvs mailing list
> bioruby-cvs at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/bioruby-cvs




More information about the BioRuby mailing list