csv_to_sqlite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011, A.C. Wright Design
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a
4
+ copy of this software and associated documentation files (the "Software"),
5
+ to deal in the Software without restriction, including without limitation
6
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
+ and/or sell copies of the Software, and to permit persons to whom the
8
+ Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
+ DEALINGS IN THE SOFTWARE.
data/README.md ADDED
File without changes
data/bin/csv_to_sqlite ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/ruby
2
+
3
+ path = File.expand_path(File.join(File.dirname(__FILE__), '../lib'))
4
+ $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
5
+ require 'csv_to_sqlite'
6
+
7
+ CSVToSqlite::Runner.new(ARGV).run
@@ -0,0 +1,9 @@
1
+ module CSVToSqlite
2
+
3
+ VERSION = '0.0.1'
4
+ ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../'))
5
+
6
+ autoload :Runner, "#{ROOT}/lib/csv_to_sqlite/runner"
7
+ autoload :Parser, "#{ROOT}/lib/csv_to_sqlite/parser"
8
+
9
+ end
@@ -0,0 +1,75 @@
1
+ require 'rubygems'
2
+ require 'fastercsv'
3
+ require 'dm-core'
4
+ require 'dm-migrations'
5
+
6
+ module CSVToSqlite
7
+
8
+ class Parser
9
+
10
+ attr_reader :db
11
+ attr_reader :klass
12
+ attr_reader :column_names
13
+
14
+ def initialize(source, target, table_name=nil, column_names=nil)
15
+ if File.exists?(source)
16
+ @source = IO.readlines(source)
17
+ else
18
+ @source = source
19
+ end
20
+
21
+ @target = target
22
+ @table_name = table_name || 'item'
23
+ @column_names = column_names || _name_columns(@source)
24
+
25
+ _connect(@target)
26
+ _create_model(@table_name, @column_names)
27
+
28
+ DataMapper.finalize
29
+ DataMapper.auto_migrate!
30
+ end
31
+
32
+ def parse!
33
+ _parse(@source)
34
+ end
35
+
36
+ private
37
+
38
+ def _connect(target)
39
+ @db = DataMapper.setup(:default, "sqlite:///#{File.expand_path(target)}")
40
+ end
41
+
42
+ def _create_model(table_name, column_names)
43
+ @klass = Object.const_set(table_name.capitalize, Class.new)
44
+ @klass.class_eval do
45
+ include DataMapper::Resource
46
+ property :id, DataMapper::Property::Serial
47
+ column_names.each do |column|
48
+ property column.to_s.to_sym, DataMapper::Property::String
49
+ end
50
+ end
51
+ end
52
+
53
+ def _name_columns(source)
54
+ columns = []
55
+ columns_count = 0
56
+ FasterCSV.parse(source) do |row|
57
+ columns_count = row.length if row.length > columns_count
58
+ end
59
+ (0...columns_count).each {|i| columns.push("col_#{i.to_s}") }
60
+ columns
61
+ end
62
+
63
+ def _parse(source)
64
+ FasterCSV.parse(source) do |row|
65
+ item = @klass.new()
66
+ row.each_with_index do |column, index|
67
+ item.send("#{@column_names[index]}=", column)
68
+ end
69
+ item.save
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,65 @@
1
+ require 'optparse'
2
+
3
+ module CSVToSqlite
4
+
5
+ class Runner
6
+
7
+ attr_reader :argv
8
+ attr_accessor :parser
9
+ attr_accessor :options
10
+ attr_accessor :command
11
+ attr_accessor :arguments
12
+
13
+ def initialize(argv)
14
+ @argv = argv
15
+ @options = {
16
+ :verbose => true
17
+ }
18
+ init_parser
19
+ end
20
+
21
+ def run
22
+ parse!
23
+ CSVToSqlite::Parser.new(@source, @target, @options[:table], @options[:columns]).parse!
24
+ end
25
+
26
+ protected
27
+
28
+ def init_parser
29
+
30
+ @parser ||= OptionParser.new do |opts|
31
+ opts.banner = "Usage: csv_to_sqlite [OPTIONS] source target"
32
+ opts.separator ""
33
+ opts.on('-t', '--table TABLE_NAME', 'The name of the table to create in the database.') { |table| @options[:table] = table }
34
+ opts.on('-c', '--columns COLUMN_NAMES', 'Comma separated list of column names. Ex: one,two,three...') { |columns| @options[:columns] = columns.split(',') }
35
+ opts.on('-q', '--quiet') { |quiet| @options[:verbose] = false }
36
+ opts.on('-v', '--version') { puts version ; exit }
37
+ opts.on('-h', '--help') { puts opts ; exit }
38
+ opts.on("-D", "--debug", "Set debugging on") { @options[:debug] = true }
39
+ opts.separator ""
40
+ end
41
+
42
+ end
43
+
44
+ def parse!
45
+
46
+ begin
47
+ @parser.parse!(@argv)
48
+ rescue
49
+ puts @parser
50
+ exit 1
51
+ end
52
+
53
+ @source = @argv.shift
54
+ @target = @argv.shift
55
+ @arguments = @argv
56
+
57
+ end
58
+
59
+ def version
60
+ "csv_to_sqlite #{GPC::VERSION}"
61
+ end
62
+
63
+ end
64
+
65
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_to_sqlite
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - A.C. Wright
14
+ - A.C. Wright Design
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-02-17 00:00:00 -06:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: fastercsv
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 15
45
+ segments:
46
+ - 2
47
+ - 0
48
+ - 0
49
+ version: 2.0.0
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: This is a tool to create SQlite database from a CSV file.
53
+ email: acwright@acwrightdesign.com
54
+ executables:
55
+ - csv_to_sqlite
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - bin/csv_to_sqlite
62
+ - lib/csv_to_sqlite/parser.rb
63
+ - lib/csv_to_sqlite/runner.rb
64
+ - lib/csv_to_sqlite.rb
65
+ - LICENSE.txt
66
+ - README.md
67
+ has_rdoc: true
68
+ homepage: http://github.com/acwright/csv_to_sqlite
69
+ licenses:
70
+ - MIT
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.4.2
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Convert CSV file to an SQlite database
101
+ test_files: []
102
+