lxc-exporter/lib/jsonlio.rb

91 lines
1.8 KiB
Ruby
Raw Normal View History

# vim: set noet sw=2 ts=2 sts=2:
require 'json'
require 'socket'
class JSONLIO <IO
include Enumerable
extend Enumerable
def orig_io() @io end
def to_io() self end
def to_i() @io.to_i end
def fileno() @io.fileno end
def unpacker() @un end
def close() @io.close end
def close_write() @io.close_write end
def close_read() @io.close_read end
def write( obj) @io.write obj.to_json+"\n" end
def sync() @io.sync end
def sync=( v) @io.sync = v end
def inspect() "#<#{self.class.name} fd:#{@io.to_i} #{@mode}>" end
def initialize( io) @io = io end
class <<self
def stream() Socket.pair( :UNIX, :STREAM, 0).map {|s| self.new s, :rw } end
def pipe
a, b = IO.pipe
[self.new( a, :r), self.new( b, :w)]
end
def popen mode = nil, &exe
s1, s2 =
case mode
when :rw, 'rw', nil then self.stream
when :r, 'r' then self.pipe
when :w, 'w' then self.pipe.reverse
end
pid =
fork do
s1.close
yield s2
exit 0
end
s2.close
[pid, s1]
end
def open io, mode = :r, &exe
s = self.new io, mode
if block_given?
begin yield s
ensure s.close
end
else s
end
end
def each fn, &exe
open fn, 'r' do |f|
f.each &exe
end
end
end
attr_reader :lineno
def initialize io, mode = :rw
case io
when IO then @io = io
when String then @io = File.open io, mode
when Pathname then @io = io.open mode
else raise ArgumentError, "This seems not to be a IO/String/Pathname: #{io.inspect}"
end
@lineno, @mode, @io.sync = 0, mode, true
end
def read
JSON.parse( @io.readline.chomp).tap { @lineno += 1 }
rescue Exception
raise $!.class, $!.message + " (JSONL-line: #{@lineno})", $!.backtrace
end
def each &exe
return to_enum __method__ unless block_given?
loop { yield read }
self
rescue EOFError
self
end
end