lib/rio/scheme/temp.rb in rio-0.4.2 vs lib/rio/scheme/temp.rb in rio-0.5.1
- old
+ new
@@ -1,8 +1,8 @@
#--
-# ===============================================================================
-# Copyright (c) 2005,2006,2007,2008 Christopher Kleckner
+# ===========================================================================
+# Copyright (c) 2005-2012 Christopher Kleckner
# All rights reserved
#
# This file is part of the Rio library for ruby.
#
# Rio is free software; you can redistribute it and/or modify
@@ -16,125 +16,143 @@
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rio; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-# ===============================================================================
+# ===========================================================================
#++
#
-# To create the documentation for Rio run the command
-# ruby build_doc.rb
-# from the distribution directory.
-#
-# Suggested Reading
-# * RIO::Doc::SYNOPSIS
-# * RIO::Doc::INTRO
-# * RIO::Doc::HOWTO
-# * RIO::Doc::EXAMPLES
-# * RIO::Rio
-#
require 'tmpdir'
+require 'rio/rrl/base'
+
module RIO
module Temp #:nodoc: all
RESET_STATE = 'Temp::Reset'
- require 'rio/rl/base'
- class RL < RL::Base
+ class RRL < ::RIO::RRL::Base
RIOSCHEME = 'temp'
- RIOPATH = RIO::RL::CHMAP.invert[RIOSCHEME].to_s.freeze
+ RIOPATH = RIO::RRL::CHMAP.invert[RIOSCHEME].to_s.freeze
DFLT_PREFIX = 'rio'
DFLT_TMPDIR = ::Dir::tmpdir
- attr_reader :prefix,:tmpdir
- def initialize(file_prefix=DFLT_PREFIX,temp_dir=DFLT_TMPDIR)
- #puts "initialize(#{file_prefix.inspect},#{temp_dir.inspect})"
- @prefix = file_prefix || DFLT_PREFIX
- @tmpdir = temp_dir || DFLT_TMPDIR
- super
+ # p 'DFLT_TMPDIR=' + DFLT_TMPDIR
+ def initialize(u,file_prefix=DFLT_PREFIX,temp_dir=DFLT_TMPDIR)
+ # p "Temp::RRL::initialize u=#{u} file_prefix=#{file_prefix} temp_dir=#{temp_dir}"
+ # puts "Temp::RRL CALLER: ",caller[0..8]
+ alturi = case u
+ when ::Alt::URI::Base then u
+ else ::Alt::URI.parse(u.to_s)
+ end
+ prefix = alturi.query || file_prefix.to_s
+ tmpdir = (alturi.path.nil? || alturi.path.empty?) ?
+ temp_dir.to_s : alturi.path
+ #alturi = ::Alt::URI.create(:scheme => 'temp', :path => temp_dir,
+ # :query => file_prefix)
+ # p 'HERE'
+ super(alturi)
+ # p "alturi=#{alturi}"
+ self.path = tmpdir
+ self.query = prefix
end
- #def path() nil end
- def scheme() self.class.const_get(:RIOSCHEME) end
- def opaque()
- td = self.escape(@tmpdir.to_s)
- td += '/' unless td.nil? or td.empty? or (td != '/' and td[-1] == ?/)
- td + self.escape(@prefix)
- end
-
- SPLIT_RE = %r|(?:(.*)/)?([^/]*)$|.freeze
- def self.splitrl(s)
- sub,opq,whole = split_riorl(s)
- if opq.nil? or opq.empty?
- []
- elsif bm = SPLIT_RE.match(opq)
- tdir = bm[1] unless bm[1].nil? or bm[1].empty?
- tpfx = bm[2] unless bm[2].nil? or bm[2].empty?
- [tpfx,tdir]
- else
- []
- end
- end
+
+ extend Forwardable
+ def_delegators :uri, :path=, :path, :query, :query=, :scheme
+ alias :prefix :query
+ alias :tmpdir :path
+
end
module Dir
- require 'rio/rl/path'
- RESET_STATE = RIO::RL::PathBase::RESET_STATE
+ require 'rio/rrl/path'
+ RESET_STATE = RIO::RRL::PathBase::RESET_STATE
require 'tmpdir'
- class RL < RIO::RL::PathBase
+ class RRL < RIO::RRL::PathBase
RIOSCHEME = 'tempdir'
- DFLT_PREFIX = Temp::RL::DFLT_PREFIX
- DFLT_TMPDIR = Temp::RL::DFLT_TMPDIR
- attr_reader :prefix,:tmpdir,:tmprl
- def initialize(file_prefix=DFLT_PREFIX,temp_dir=DFLT_TMPDIR)
- #puts "initialize(#{file_prefix.inspect},#{temp_dir.inspect})"
- @prefix = file_prefix || DFLT_PREFIX
- @tmpdir = temp_dir || DFLT_TMPDIR
+ DFLT_PREFIX = Temp::RRL::DFLT_PREFIX
+ DFLT_TMPDIR = Temp::RRL::DFLT_TMPDIR
+
+ def initialize(u,file_prefix=DFLT_PREFIX,temp_dir=DFLT_TMPDIR)
require 'rio/tempdir'
- @td = ::Tempdir.new( @prefix.to_s, @tmpdir.to_s)
- super(@td.to_s)
+ # p "Temp::Dir u=#{u.inspect} file_prefix=#{file_prefix} temp_dir=#{temp_dir}"
+ alturi = case u
+ when ::Alt::URI::Base then u
+ else ::Alt::URI.parse(u.to_s)
+ end
+ prefix = alturi.query || file_prefix.to_s
+ tmpdir = (alturi.path.nil? || alturi.path.empty?) ?
+ temp_dir.to_s : alturi.path
+ #p "Temp::Dir prefix=#{prefix} tmpdir=#{tmpdir}"
+ td = ::Tempdir.new( prefix, tmpdir)
+ #td = "file:///" + td if td.to_s =~ /^[a-zA-Z]:/
+ #p "Temp::Dir td=#{td.to_s.inspect}"
+ td = "file:///" + td.to_s if td.to_s =~ /^[a-zA-Z]:/
+ super(td.to_s)
+ #self.query = file_prefix.to_s
+ #self.path = temp_dir.to_s
end
+ extend Forwardable
+ def_delegators :uri, :path=, :path, :query, :query=
+ alias :prefix :query
+ alias :tmpdir :path
+
def dir_rl()
- #p "temp:dir_rl: #{self.uri.inspect}"
- RIO::Dir::RL.new(self.uri, {:fs => self.fs})
- #self
+ RIO::Dir::RRL.new(self.uri, {:fs => self.fs})
end
- SPLIT_RE = Temp::RL::SPLIT_RE
- def self.splitrl(s)
- Temp::RL.splitrl(s)
- end
end
end
module File
- require 'rio/rl/path'
+ require 'rio/rrl/path'
RESET_STATE = 'Temp::Stream::Open'
- class RL < RIO::RL::PathBase
+ class RRL < RIO::RRL::PathBase
RIOSCHEME = 'tempfile'
- DFLT_PREFIX = Temp::RL::DFLT_PREFIX
- DFLT_TMPDIR = Temp::RL::DFLT_TMPDIR
- attr_reader :prefix,:tmpdir,:tmprl
- def initialize(file_prefix=DFLT_PREFIX,temp_dir=DFLT_TMPDIR)
- #puts "initialize(#{file_prefix.inspect},#{temp_dir.inspect})"
- @prefix = file_prefix || DFLT_PREFIX
- @tmpdir = temp_dir || DFLT_TMPDIR
+ DFLT_PREFIX = Temp::RRL::DFLT_PREFIX
+ DFLT_TMPDIR = Temp::RRL::DFLT_TMPDIR
+
+ def initialize(u,file_prefix=DFLT_PREFIX,temp_dir=DFLT_TMPDIR)
require 'tempfile'
- @tf = ::Tempfile.new( @prefix.to_s, @tmpdir.to_s)
- super(@tf.path)
+ #p "Temp::File::initialize u=#{u.inspect} file_prefix=#{file_prefix.inspect} temp_dir=#{temp_dir.inspect}"
+ # puts "Temp::File CALLER: ",caller[0..8]
+ # FIXME: Temporary fix for jruby 1.4 - make tmpdir absolute
+ #tmpdir_rio = rio(@tmpdir).abs
+ alturi = case u
+ when ::Alt::URI::Base then u
+ else ::Alt::URI.parse(u.to_s)
+ end
+ prefix = alturi.query || file_prefix.to_s
+ tmpdir = (alturi.path.nil? || alturi.path.empty?) ?
+ temp_dir.to_s : alturi.path
+ # p "Temp::File::initialize alturi=#{alturi} prefix=#{prefix} tmpdir=#{tmpdir}"
+
+ @tf = ::Tempfile.new( prefix, tmpdir)
+
+ #puts "TF=#{@tf.inspect}"
+ # @tf = ::Tempfile.new( @prefix.to_s, @tmpdir.to_s)
+
+ # FIXME: Temporary fix for jruby 1.4 - fix slashes
+ pth = @tf.path
+ pth.gsub!("\\","/")
+ pth = "file:///" + pth if pth =~ /^[a-zA-Z]:/
+
+ #p "PTH=#{pth}"
+ # @tf = "file:///" + @tf if @tf.to_s =~ /^[a-zA-Z]:/
+ #
+ #p "Temp::File pth=#{pth.inspect}"
+ super(::Alt::URI.parse(pth))
+ #self.query = file_prefix
+ #self.path = temp_dir
end
+ extend Forwardable
+ def_delegators :uri, :path=, :path, :query, :query=
def file_rl()
- RIO::File::RL.new(self.uri,{:fs => self.fs})
- #self
+ RIO::File::RRL.new(self.uri,{:fs => self.fs})
end
def open(mode='ignored')
- #p callstr('open',mode)
@tf
end
def close
super
@tf = nil
end
- SPLIT_RE = Temp::RL::SPLIT_RE
- def self.splitrl(s)
- Temp::RL.splitrl(s)
- end
end
end
require 'rio/state'
class Reset < State::Base
def initialize(*args)
@@ -146,21 +164,23 @@
Cx::Vars.new( { 'closeoneof' => false, 'closeoncopy' => false } )
end
def check?() true end
def mkdir(prefix=rl.prefix,tmpdir=rl.tmpdir)
- self.rl = RIO::Temp::Dir::RL.new(prefix, tmpdir)
+ # p "scheme/temp.rb mkdir(): prefix="+prefix+" tmpdir="+tmpdir
+ self.rl = RIO::Temp::Dir::RRL.new("",prefix, tmpdir)
become 'Dir::Existing'
end
# def mkdir()
# dir()
# end
def chdir(&block)
self.mkdir.chdir(&block)
end
def file(prefix=rl.prefix,tmpdir=rl.tmpdir)
- self.rl = RIO::Temp::File::RL.new(prefix, tmpdir)
+ # p "scheme/temp.rb file(): prefix="+prefix+" tmpdir="+tmpdir
+ self.rl = RIO::Temp::File::RRL.new("",prefix, tmpdir)
become 'Temp::Stream::Open'
end
def scheme() rl.scheme() end
def host() rl.host() end
def opaque() rl.opaque() end
@@ -169,11 +189,10 @@
def file?() false end
def dir?() false end
def open?() false end
def closed?() true end
def when_missing(sym,*args)
- #p @rl.scheme
if @tempobj.nil?
file()
else
gofigure(sym,*args)
end
@@ -183,18 +202,10 @@
module Stream
class Open < RIO::Stream::Open
def iostate(sym)
mode_('w+').open_.inout()
end
-# def inout() stream_state('Temp::Stream::InOut') end
end
-# require 'rio/stream'
-# class InOut < RIO::Stream::InOut
-# def base_state() 'Temp::Stream::Close' end
-# end
-# class Close < RIO::Stream::Close
-# def base_state() 'Temp::Reset' end
-# end
end
end
end