csvr 0.1.0
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.
- checksums.yaml +7 -0
- data/bin/csvr +4 -0
- data/lib/csvr/format.rb +19 -0
- data/lib/csvr/parse.rb +73 -0
- data/lib/csvr.rb +51 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f0dfd39ecb99d1a00937e827bf9f4a4961527f9
|
4
|
+
data.tar.gz: 466e8595dbbe844955da0b144914aaea8591b3e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e865b6bb93fd03e2d47f04cfb13698d5529e480acc59ce6cc816a7272ff672beb662dbedd6c9d48e24e17bd809c8cc4213c02f48e939d36e2ec45cbe62f57f6e
|
7
|
+
data.tar.gz: 87825343a9f054a393f76d4acf5ab49125ccfa65eae97817dc60946e266755e8f0dfb594339ee41c9f63c453c129228bdb34fc7d8c44a4e216d6b47b6e5b54f3
|
data/bin/csvr
ADDED
data/lib/csvr/format.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
module Format
|
3
|
+
|
4
|
+
module_function
|
5
|
+
|
6
|
+
def headers(headers)
|
7
|
+
headers = headers.map { |h| "#{h} TEXT"}
|
8
|
+
return headers.join(",")
|
9
|
+
end
|
10
|
+
|
11
|
+
def row(row)
|
12
|
+
|
13
|
+
if row.is_a? Hash
|
14
|
+
values = row.values.map { |r| "'#{r}'"}.join(",")
|
15
|
+
keys = row.keys.join(",")
|
16
|
+
end
|
17
|
+
return "(#{keys}) VALUES(#{values})"
|
18
|
+
end
|
19
|
+
end
|
data/lib/csvr/parse.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module CSVR
|
4
|
+
|
5
|
+
module Parse
|
6
|
+
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def headers(file)
|
10
|
+
|
11
|
+
File.open(file) do |fi|
|
12
|
+
1.times do
|
13
|
+
return CSV.parse_line(fi.readline)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def index(file, columns)
|
19
|
+
|
20
|
+
array = headers(file)
|
21
|
+
hash = {}
|
22
|
+
array.each{|value| hash[array.index(value)] = value if columns.include?(value) }
|
23
|
+
|
24
|
+
return hash
|
25
|
+
end
|
26
|
+
|
27
|
+
def rows(file, headers, filters = nil)
|
28
|
+
|
29
|
+
index = index(file, headers)
|
30
|
+
|
31
|
+
File.open(file) do |fi|
|
32
|
+
|
33
|
+
lines = fi.each_line
|
34
|
+
lines.next
|
35
|
+
|
36
|
+
values = []
|
37
|
+
|
38
|
+
maximum(file).times do
|
39
|
+
|
40
|
+
row = CSV.parse_line(fi.readline)
|
41
|
+
|
42
|
+
hash = {}
|
43
|
+
|
44
|
+
index.each do |k, v|
|
45
|
+
row[k].gsub!(/[']/, '') if row[k].respond_to? :gsub
|
46
|
+
hash[v] = row[k]
|
47
|
+
end
|
48
|
+
|
49
|
+
filters ? values << filter(hash, filters) : values << hash
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
return values.compact
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def filter(hash, filters)
|
58
|
+
|
59
|
+
hash.each do |key, value|
|
60
|
+
filters.each do |filter|
|
61
|
+
match = value.match(filter) if value.respond_to? :match
|
62
|
+
return hash if match
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
return nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def maximum(file)
|
70
|
+
return %x{wc -l < "#{file}"}.to_i
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/csvr.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require_relative 'csvr/parse'
|
2
|
+
require_relative 'csvr/format'
|
3
|
+
require_relative 'csvr/database'
|
4
|
+
require_relative 'csvr/cli'
|
5
|
+
|
6
|
+
module CSVR
|
7
|
+
|
8
|
+
module_function
|
9
|
+
|
10
|
+
def open(file)
|
11
|
+
return App.new(file)
|
12
|
+
end
|
13
|
+
|
14
|
+
class App
|
15
|
+
|
16
|
+
include CSVR::Parse
|
17
|
+
include Format
|
18
|
+
|
19
|
+
attr_reader :file, :rows
|
20
|
+
attr_accessor :headers, :filters
|
21
|
+
|
22
|
+
def initialize(file)
|
23
|
+
@file = file
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse
|
27
|
+
@headers ||= CSVR::Parse.headers(@file)
|
28
|
+
@rows = CSVR::Parse.rows(@file, @headers, @filters)
|
29
|
+
end
|
30
|
+
|
31
|
+
def format
|
32
|
+
@headers = Format.headers(@headers)
|
33
|
+
@rows = @rows.map{ |row| Format.row(row) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def create(db, table)
|
37
|
+
self.parse
|
38
|
+
self.format
|
39
|
+
db = Database.new(db, table, @headers, @rows)
|
40
|
+
db.create
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
# csvr = CSVR.open('test.csv')
|
47
|
+
# csvr.headers = ['header2', 'header4']
|
48
|
+
# csvr.filters = ['row1']
|
49
|
+
# csvr.create('new', 'table1')
|
50
|
+
|
51
|
+
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csvr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Parses CSV files to a custom SQLite Database, with custom headers and
|
14
|
+
filtering
|
15
|
+
email: a.smith@live.ca
|
16
|
+
executables:
|
17
|
+
- csvr
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/csvr
|
22
|
+
- lib/csvr.rb
|
23
|
+
- lib/csvr/format.rb
|
24
|
+
- lib/csvr/parse.rb
|
25
|
+
homepage: http://idonthaveone.ca
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.4.5
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Parsing CSV files to SQLite
|
49
|
+
test_files: []
|