mymatrix 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/file_io.rb +34 -0
- data/lib/loader_csv.rb +44 -0
- data/lib/loader_factory.rb +32 -0
- data/lib/loader_txt.rb +40 -0
- data/lib/loader_xls.rb +41 -0
- data/lib/mymatrix.rb +5 -6
- data/lib/mymatrix/version.rb +1 -1
- metadata +8 -3
data/lib/file_io.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
class FileIO
|
3
|
+
# ファイルオープン時、パス文字列のエンコードを変換してシステムに返却するためのメソッド
|
4
|
+
def self.encodePath(path)
|
5
|
+
case self.filesystem
|
6
|
+
when 'u'
|
7
|
+
#utf8=>utf8なので何もしない
|
8
|
+
#path = MyMatrix.toutf8(path)
|
9
|
+
#path.encode('UTF-8')
|
10
|
+
path
|
11
|
+
when 's'
|
12
|
+
path = MyMatrix.tosjis(path)
|
13
|
+
#path.encode('Windows-31J')
|
14
|
+
when 'w'
|
15
|
+
path = MyMatrix.tosjis(path)
|
16
|
+
#path.encode('Windows-31J')
|
17
|
+
when 'm'
|
18
|
+
path = MyMatrix.toUtf8Mac(path)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
def self.filesystem
|
22
|
+
#platform check
|
23
|
+
if(RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/)
|
24
|
+
filesystem = 's'
|
25
|
+
elsif(RUBY_PLATFORM.downcase =~ /darwin/)
|
26
|
+
filesystem = 'm'
|
27
|
+
elsif(RUBY_PLATFORM.downcase =~ /linux/)
|
28
|
+
filesystem = 'u'
|
29
|
+
else
|
30
|
+
filesystem = 'u'
|
31
|
+
end
|
32
|
+
return filesystem
|
33
|
+
end
|
34
|
+
end
|
data/lib/loader_csv.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'file_io'
|
3
|
+
class LoaderCsv < FileIO
|
4
|
+
def self.makeMatrix(file, opts={:offset=>0})
|
5
|
+
#CSV読み込みメソッド
|
6
|
+
#1.9系ではFasterCSVを使えない
|
7
|
+
if(RUBY_VERSION =~ /1\.[^9]/)
|
8
|
+
#1.8以下の場合
|
9
|
+
require 'fastercsv'
|
10
|
+
csv = FasterCSV
|
11
|
+
else
|
12
|
+
#1.9以上の場合
|
13
|
+
require 'csv'
|
14
|
+
Encoding.default_external = 'Windows-31J'
|
15
|
+
csv = CSV
|
16
|
+
end
|
17
|
+
out = []
|
18
|
+
i= 0
|
19
|
+
syspath = self.encodePath(file)
|
20
|
+
csv.foreach(syspath, {:row_sep => "\r\n", :encoding => 'Shift_JIS'}) do |row|
|
21
|
+
if(opts[:offset])
|
22
|
+
if(opts[:offset] < i)
|
23
|
+
next
|
24
|
+
end
|
25
|
+
end
|
26
|
+
#「1,300台」などカンマが使われている場合、「"1,300台"」となってしまうので、カンマを無視する
|
27
|
+
newRow = []
|
28
|
+
row.each do |cell|
|
29
|
+
cell = cell.to_s
|
30
|
+
cell ||= ''
|
31
|
+
cell = MyMatrix.toutf8(cell)
|
32
|
+
#cell = cell.gsub(/^\"/, "")
|
33
|
+
#cell = cell.gsub(/\"$/, "")
|
34
|
+
#"
|
35
|
+
newRow << cell
|
36
|
+
end
|
37
|
+
out << newRow
|
38
|
+
i += 1
|
39
|
+
end
|
40
|
+
return out
|
41
|
+
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'loader_xls'
|
3
|
+
require 'loader_csv'
|
4
|
+
require 'loader_txt'
|
5
|
+
class LoaderFactory
|
6
|
+
def self.load(file, opts)
|
7
|
+
mx = []
|
8
|
+
if(file =~ /\.xls$/)
|
9
|
+
mx = LoaderXls.makeMatrix(file, opts)
|
10
|
+
elsif(@file =~ /(\.tsv|\.txt|\.TSV|\.TXT)/)
|
11
|
+
mx = LoaderTxt.makeMatrix(file, opts)
|
12
|
+
elsif(file =~ /(\.csv|\.CSV)/)
|
13
|
+
mx = LoaderCsv.makeMatrix(file, opts)
|
14
|
+
elsif(file == nil)
|
15
|
+
else
|
16
|
+
#デフォルトはTSVで読み込むようにする。
|
17
|
+
mx = LoaderTxt.makeMatrix(file, opts)
|
18
|
+
end
|
19
|
+
mx = self.clean(mx)
|
20
|
+
return mx
|
21
|
+
end
|
22
|
+
def self.clean(mx)
|
23
|
+
#@mxの末尾に空レコードが入っていたら、その空白を削除
|
24
|
+
while(mx[mx.size-1] && mx[mx.size-1].join == '')
|
25
|
+
mx.pop
|
26
|
+
end
|
27
|
+
if(mx.size == 0)
|
28
|
+
mx = []
|
29
|
+
end
|
30
|
+
return mx
|
31
|
+
end
|
32
|
+
end
|
data/lib/loader_txt.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'file_io'
|
3
|
+
class LoaderTxt < FileIO
|
4
|
+
def self.makeMatrix(file, opts={:sep=>"\t", :offset=>0})
|
5
|
+
#TSV: tab separated value 読み込みメソッド
|
6
|
+
|
7
|
+
out = []
|
8
|
+
epath = encodePath(file)
|
9
|
+
if(!File.exist?(epath))
|
10
|
+
open(epath, 'w') do |fo|
|
11
|
+
fo.print("\n\n")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
path = self.encodePath(file)
|
15
|
+
fi = open(path, "r:Windows-31J")
|
16
|
+
if(opts[:offset])
|
17
|
+
opts[:offset].times do |i|
|
18
|
+
fi.gets
|
19
|
+
end
|
20
|
+
end
|
21
|
+
opts[:sep]||="\t"
|
22
|
+
fi.each do |line|
|
23
|
+
row = MyMatrix.toutf8(line).chomp.split(/#{opts[:sep]}/)
|
24
|
+
#「1,300台」などカンマが使われている場合、「"1,300台"」となってしまうので、カンマを無視する
|
25
|
+
newRow = []
|
26
|
+
row.each do |cell|
|
27
|
+
stri = cell.dup
|
28
|
+
stri.gsub!(/^\"(.*)\"$/, '\1')
|
29
|
+
#"
|
30
|
+
stri.gsub!(/""/, '"')
|
31
|
+
newRow << stri
|
32
|
+
end
|
33
|
+
out << newRow
|
34
|
+
end
|
35
|
+
fi.close
|
36
|
+
return out
|
37
|
+
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
data/lib/loader_xls.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'file_io'
|
3
|
+
require 'spreadsheet'
|
4
|
+
class LoaderXls < FileIO
|
5
|
+
def self.makeMatrix(file, opts={ })
|
6
|
+
# xls読み込みメソッド
|
7
|
+
if(opts)
|
8
|
+
offset = opts[:offset]
|
9
|
+
sheet = opts[:sheet]
|
10
|
+
end
|
11
|
+
offset ||= 0
|
12
|
+
sheet ||= 0
|
13
|
+
|
14
|
+
out = []
|
15
|
+
#todo xlsFileがなかったら作成
|
16
|
+
path = self.encodePath(file)
|
17
|
+
xl = Spreadsheet.open(path, 'rb')
|
18
|
+
sheet = xl.worksheet(sheet)
|
19
|
+
rowsize = sheet.last_row_index
|
20
|
+
(rowsize+1-offset).times do |i|
|
21
|
+
row = sheet.row(i+offset)
|
22
|
+
orow = []
|
23
|
+
row.each do |ele|
|
24
|
+
#様々な型で値が入っている。改行も入っている
|
25
|
+
if(ele.class == Float)&&(ele.to_s =~ /(\d+)\.0/)
|
26
|
+
ele = $1
|
27
|
+
end
|
28
|
+
if(ele.class == Spreadsheet::Formula)
|
29
|
+
ele = ele.value
|
30
|
+
end
|
31
|
+
if(ele == nil)
|
32
|
+
ele = ''
|
33
|
+
end
|
34
|
+
ele = ele.to_s.gsub(/\n/, '<br>')
|
35
|
+
orow << ele
|
36
|
+
end
|
37
|
+
out << orow
|
38
|
+
end
|
39
|
+
return out
|
40
|
+
end
|
41
|
+
end
|
data/lib/mymatrix.rb
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
#!/usr/bin/ruby -Ku
|
2
2
|
# -*- encoding: utf-8 -*-
|
3
|
-
require "mymatrix/version"
|
4
|
-
require 'loader_factory'
|
5
|
-
|
6
|
-
|
7
3
|
$:.unshift(File.dirname(__FILE__)) unless
|
8
4
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
9
|
-
|
5
|
+
require "mymatrix/version"
|
10
6
|
require 'rubygems'
|
11
7
|
require 'nkf'
|
12
8
|
require 'logger'
|
13
9
|
require 'pp'
|
14
10
|
require 'enumerable_ex' #verbose_each
|
15
11
|
|
12
|
+
require 'loader_factory'
|
13
|
+
|
14
|
+
|
16
15
|
if(RUBY_VERSION =~ /1\.[^9]/)
|
17
16
|
$KCODE='UTF8'
|
18
17
|
end
|
@@ -42,7 +41,7 @@ class MyMatrix
|
|
42
41
|
@log.level = Logger::DEBUG
|
43
42
|
@file = file
|
44
43
|
|
45
|
-
|
44
|
+
|
46
45
|
@mx = LoaderFactory.load(@file, opts)
|
47
46
|
|
48
47
|
|
data/lib/mymatrix/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mymatrix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-03-27 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2160859920 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2160859920
|
25
25
|
description: mymatrix is a handling library for MS Excel and csv/tsv text.
|
26
26
|
email:
|
27
27
|
- yukihico@gmail.com
|
@@ -31,6 +31,11 @@ extra_rdoc_files: []
|
|
31
31
|
files:
|
32
32
|
- Gemfile
|
33
33
|
- Rakefile
|
34
|
+
- lib/file_io.rb
|
35
|
+
- lib/loader_csv.rb
|
36
|
+
- lib/loader_factory.rb
|
37
|
+
- lib/loader_txt.rb
|
38
|
+
- lib/loader_xls.rb
|
34
39
|
- lib/mymatrix.rb
|
35
40
|
- lib/mymatrix/version.rb
|
36
41
|
- mymatrix.gemspec
|