Class: IO

Inherits:
Object
  • Object
show all
Defined in:
lib/processpilot/io.rb

Overview

Define some helpers that can be handy in case of process piloting from IO

Instance Method Summary collapse

Instance Method Details

#gets(*iArgs) ⇒ Object

Add the possibility to gets to take an optional Hash: :time_out_secs (Integer): Timeout in seconds to read data. nil means no timeout (regular gets) [optional = nil].



12
13
14
15
16
17
18
19
20
21
# File 'lib/processpilot/io.rb', line 12

def gets(*iArgs)
  if (iArgs[-1].is_a?(Hash))
    lOptions = iArgs[-1]
    return protect_with_timeout(lOptions[:time_out_secs]) do
      next gets_ORG_ProcessPilot(*iArgs[0..-2])
    end
  else
    return gets_ORG_ProcessPilot(*iArgs)
  end
end

#gets_ORG_ProcessPilotObject



9
# File 'lib/processpilot/io.rb', line 9

alias :gets_ORG_ProcessPilot :gets

#gets_until(iPattern, iOptions = {}) ⇒ Object

Read lines until a given pattern is recognized

Parameters
  • iPattern (Regexp): The pattern to match. Can also be a String for exact match (don’t forget n).

  • iOptions (map<Symbol,Object>): Additional options [optional = {}]:

    • :time_out_secs (Integer): Timeout in seconds to read data and find the pattern. nil means no timeout. [optional = nil]

Return
  • list<String>: The list of lines read



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/processpilot/io.rb', line 45

def gets_until(iPattern, iOptions = {})
  rLstLines = []

  lTimeOutSecs = iOptions[:time_out_secs]
  protect_with_timeout(lTimeOutSecs) do
    if (iPattern.is_a?(Regexp))
      while ((rLstLines << self.gets(:time_out_secs => lTimeOutSecs))[-1].match(iPattern) == nil)
        #$stdout.puts "Read from IO: #{rLstLines[-1].inspect}"
        raise(IOError, 'End of stream') if (rLstLines[-1] == nil)
      end
    else
      while ((rLstLines << self.gets(:time_out_secs => lTimeOutSecs))[-1] != iPattern)
        #$stdout.puts "Read from IO: #{rLstLines[-1].inspect}"
        raise(IOError, 'End of stream') if (rLstLines[-1] == nil)
      end
    end
    #$stdout.puts "Read from IO: #{rLstLines[-1].inspect}"
  end

  return rLstLines
end

#read(*iArgs) ⇒ Object

Add the possibility to gets to take an optional Hash: :time_out_secs (Integer): Timeout in seconds to read data. nil means no timeout (regular gets) [optional = nil].



26
27
28
29
30
31
32
33
34
35
# File 'lib/processpilot/io.rb', line 26

def read(*iArgs)
  if (iArgs[-1].is_a?(Hash))
    lOptions = iArgs[-1]
    return protect_with_timeout(lOptions[:time_out_secs]) do
      next read_ORG_ProcessPilot(*iArgs[0..-2])
    end
  else
    return read_ORG_ProcessPilot(*iArgs)
  end
end

#read_ORG_ProcessPilotObject



23
# File 'lib/processpilot/io.rb', line 23

alias :read_ORG_ProcessPilot :read