Module: Tb

Defined in:
lib/tb/pnm.rb,
lib/tb.rb,
lib/tb/csv.rb,
lib/tb/tsv.rb,
lib/tb/json.rb,
lib/tb/ltsv.rb,
lib/tb/ndjson.rb

Overview

NDJSON is Newline Delimited JSON ndjson.org/

Tb::NDJSONReader accepts empty lines.

Defined Under Namespace

Modules: CatReader, Enumerable, EnumerableWithEach, ExEnumerator, Func, Search Classes: Cmd, CustomCmp, CustomEq, Enumerator, FileEnumerator, FileHeaderEnumerator, HashReader, HashWriter, HeaderCSVReader, HeaderCSVWriter, HeaderReader, HeaderTSVReader, HeaderTSVWriter, HeaderWriter, JSONReader, JSONWriter, LTSVReader, LTSVWriter, NDJSONReader, NDJSONWriter, NumericCSVReader, NumericCSVWriter, NumericReader, NumericTSVReader, NumericTSVWriter, NumericWriter, PNMReader, PNMWriter, Pager, RevCmp, Yielder, Zipper

Constant Summary collapse

FormatHash =

lib/tb/ropen.rb - Tb.open_reader

Copyright © 2011-2014 Tanaka Akira <[email protected]>

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
   copyright notice, this list of conditions and the following
   disclaimer in the documentation and/or other materials provided
   with the distribution.
3. The name of the author may not be used to endorse or promote
   products derived from this software without specific prior
   written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

{
  'csv'    => { :reader => Tb::HeaderCSVReader,  :writer => Tb::HeaderCSVWriter},
  'ncsv'   => { :reader => Tb::NumericCSVReader, :writer => Tb::NumericCSVWriter},
  'tsv'    => { :reader => Tb::HeaderTSVReader,  :writer => Tb::HeaderTSVWriter},
  'ntsv'   => { :reader => Tb::NumericTSVReader, :writer => Tb::NumericTSVWriter},
  'ltsv'   => { :reader => Tb::LTSVReader,       :writer => Tb::LTSVWriter},
  'pnm'    => { :reader => Tb::PNMReader,        :writer => Tb::PNMWriter},
  'ppm'    => { :reader => Tb::PNMReader,        :writer => Tb::PNMWriter},
  'pgm'    => { :reader => Tb::PNMReader,        :writer => Tb::PNMWriter},
  'pbm'    => { :reader => Tb::PNMReader,        :writer => Tb::PNMWriter},
  'json'   => { :reader => Tb::JSONReader,       :writer => Tb::JSONWriter},
  'ndjson' => { :reader => Tb::NDJSONReader,     :writer => Tb::NDJSONWriter},
}

Class Method Summary collapse

Class Method Details

.csv_encode_row(ary) ⇒ Object



34
35
36
# File 'lib/tb/csv.rb', line 34

def Tb.csv_encode_row(ary)
  ary.to_csv
end

.ltsv_assoc_join(assoc) ⇒ Object



125
126
127
128
129
# File 'lib/tb/ltsv.rb', line 125

def Tb.ltsv_assoc_join(assoc)
  assoc.map {|key, val|
    Tb.ltsv_escape_key(key) + ':' + Tb.ltsv_escape_value(val)
  }.join("\t")
end

.ltsv_escape_key(str) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tb/ltsv.rb', line 34

def Tb.ltsv_escape_key(str)
  if /[\0-\x1f":\\\x7f]/ =~ str
    '"' +
    str.gsub(/[\0-\x1f":\\\x7f]/) {
      ch = $&
      case ch
      when "\0"; '\0'
      when "\a"; '\a'
      when "\b"; '\b'
      when "\f"; '\f'
      when "\n"; '\n'
      when "\r"; '\r'
      when "\t"; '\t'
      when "\v"; '\v'
      when "\e"; '\e'
      else
        "\\x%02X" % ch.ord
      end
    } +
    '"'
  else
    str
  end
end

.ltsv_escape_value(str) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tb/ltsv.rb', line 59

def Tb.ltsv_escape_value(str)
  if /[\0-\x1f"\\\x7f]/ =~ str
    '"' +
    str.gsub(/[\0-\x1f"\\\x7f]/) {
      ch = $&
      case ch
      when "\0"; '\0'
      when "\a"; '\a'
      when "\b"; '\b'
      when "\f"; '\f'
      when "\n"; '\n'
      when "\r"; '\r'
      when "\t"; '\t'
      when "\v"; '\v'
      when "\e"; '\e'
      else
        "\\x%02X" % ch.ord
      end
    } +
    '"'
  else
    str
  end
end

.ltsv_split_line(line) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/tb/ltsv.rb', line 110

def Tb.ltsv_split_line(line)
  line = line.chomp("\n")
  line = line.chomp("\r")
  ary = line.split(/\t/, -1)
  assoc = ary.map {|str|
    /:/ =~ str
    key = $`
    val = $'
    key = Tb.ltsv_unescape_string(key)
    val = Tb.ltsv_unescape_string(val)
    [key, val]
  }
  assoc
end

.ltsv_unescape_string(str) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tb/ltsv.rb', line 84

def Tb.ltsv_unescape_string(str)
  if /\A\s*"(.*)"\s*\z/ =~ str
    $1.gsub(/\\([0abfnrtve]|x([0-9A-Fa-f][0-9A-Fa-f]))/) {
      if $2
        [$2].pack("H2")
      else
        case $1
        when "0"; "\0"
        when "a"; "\a"
        when "b"; "\b"
        when "f"; "\f"
        when "n"; "\n"
        when "r"; "\r"
        when "t"; "\t"
        when "v"; "\v"
        when "e"; "\e"
        else
          raise "[bug] unexpected escape"
        end
      end
    }
  else
    str
  end
end

.open_reader(filename, numeric = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/tb/ropen.rb', line 72

def Tb.open_reader(filename, numeric=false)
  filename, fmt = Tb.undecorate_filename(filename, numeric)
  factory = Tb::FormatHash.fetch(fmt)[:reader]
  io_opened = nil
  if filename == '-'
    reader = factory.new($stdin)
  else
    io_opened = File.open(filename)
    reader = factory.new(io_opened)
  end
  if block_given?
    begin
      yield reader
    ensure
      if io_opened && !io_opened.closed?
        io_opened.close
      end
    end
  else
    reader
  end
end

.tsv_fields_join(values) ⇒ Object



34
35
36
# File 'lib/tb/tsv.rb', line 34

def Tb.tsv_fields_join(values)
  values.map {|v| v.to_s.gsub(/[\t\r\n]/, ' ') }.join("\t") + "\n"
end

.tsv_fields_split(line) ⇒ Object



38
39
40
41
42
# File 'lib/tb/tsv.rb', line 38

def Tb.tsv_fields_split(line)
  line = line.chomp("\n")
  line = line.chomp("\r")
  line.split(/\t/, -1)
end

.undecorate_filename(filename, numeric) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tb/ropen.rb', line 45

def Tb.undecorate_filename(filename, numeric)
  if filename.respond_to?(:to_str)
    filename = filename.to_str
  elsif filename.respond_to?(:to_path)
    filename = filename.to_path
  else
    raise ArgumentError, "unexpected filename: #{filename.inspect}"
  end
  if /\A([a-z0-9]{2,}):/ =~ filename
    fmt = $1
    filename = $'
    err("unexpected format: #{fmt.inspect}") if !Tb::FormatHash.has_key?(fmt)
  elsif /\.([a-z0-9]+{2,})\z/ =~ filename
    fmt = $1
    fmt = 'csv' if !Tb::FormatHash.has_key?(fmt)
  else
    fmt = 'csv'
  end
  if numeric
    case fmt
    when 'csv' then fmt = 'ncsv'
    when 'tsv' then fmt = 'ntsv'
    end
  end
  return filename, fmt
end