chunky_csv 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9c47bbbc2d1f0689b3ee2e04ab899bc748cad559
4
+ data.tar.gz: 2dbf03e1e8a7e5e8c014f147493d51000363ddcd
5
+ SHA512:
6
+ metadata.gz: 97ad66f3e2c286a30c74c983f55c4a5c54e77dc7ca782b9478fe23a65a31e43258077345f0fb6daf92206c1f3bf6881c9d9715e1d304d6284eb72dbd596ae993
7
+ data.tar.gz: deb8742db483f64515463205bb2b06847209bed8300701ed4ee1210713dff571dfc61911fe81805d7e0f0e37883fcfe453ea3fa97eaa17259e25eea69712f0eb
@@ -0,0 +1,18 @@
1
+ .ruby-*
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chunky_csv.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brandon Dewitt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # ChunkyCsv
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'chunky_csv'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install chunky_csv
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'chunky_csv'
4
+
5
+ module ChunkyCsv
6
+ class ChunkyCsvCli < Thor
7
+
8
+ desc "split", "split csv file <file> into multiple csv's"
9
+ option :chunk_size, :type => :numeric, :default => 1000
10
+ option :input_file, :type => :string, :required => true
11
+ option :output_directory, :type => :string, :default => "./chunky_csv"
12
+ def split()
13
+ splitter = ::ChunkyCsv::FileSplitter.new( options[:input_file], options[:chunk_size], options[:output_directory] )
14
+ splitter.split
15
+ end
16
+
17
+ end
18
+ end
19
+
20
+ ::ChunkyCsv::ChunkyCsvCli.start(ARGV)
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chunky_csv/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "chunky_csv"
8
+ gem.version = ChunkyCsv::VERSION
9
+ gem.authors = ["Brandon Dewitt"]
10
+ gem.email = ["brandonsdewitt@gmail.com"]
11
+ gem.description = %q{ Splits large csv files into smaller ones }
12
+ gem.summary = %q{ Use it to do things, like split large csv files into small ones }
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "thor"
21
+
22
+ gem.add_development_dependency "bundler"
23
+ gem.add_development_dependency "mocha"
24
+ gem.add_development_dependency "pry"
25
+ gem.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,67 @@
1
+ require 'thor'
2
+ require "chunky_csv/version"
3
+
4
+ module ChunkyCsv
5
+
6
+ class FileSplitter
7
+ attr_reader :chunk_number, :chunk_size, :input_filename, :output_directory
8
+
9
+ def initialize( input_filename, chunk_size, output_directory )
10
+ @chunk_number = 0
11
+ @input_filename = input_filename
12
+ @chunk_size = chunk_size
13
+ @output_directory = output_directory
14
+ end
15
+
16
+ def split()
17
+ batch = []
18
+ IO.foreach( input_filename ) do |line|
19
+ batch << line
20
+
21
+ if batch.size >= self.chunk_size
22
+ flush_batch( batch )
23
+ batch = []
24
+ end
25
+ end
26
+
27
+ flush_batch( batch )
28
+ end
29
+
30
+ private
31
+
32
+ def basename
33
+ @basename ||= File.basename(self.input_filename, extension)
34
+ end
35
+
36
+ def create_output_directory
37
+ FileUtils.mkdir_p(output_directory)
38
+ end
39
+
40
+ def extension
41
+ @extension ||= File.extname(self.input_filename)
42
+ end
43
+
44
+ def flush_batch( batch )
45
+ unless File.directory?( output_directory )
46
+ create_output_directory
47
+ end
48
+
49
+ if File.directory?( output_directory )
50
+ File.open( next_chunk_filename, "w" ) do |file|
51
+ batch.each do |line|
52
+ file.print( line )
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ def next_chunk_filename
59
+ @chunk_number = @chunk_number + 1
60
+ return File.join(output_directory, "#{output_basename}_#{chunk_number}#{extension}")
61
+ end
62
+
63
+ def output_basename
64
+ "#{basename}_chunk"
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module ChunkyCsv
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chunky_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Dewitt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mocha
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ' Splits large csv files into smaller ones '
84
+ email:
85
+ - brandonsdewitt@gmail.com
86
+ executables:
87
+ - chunky_csv
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/chunky_csv
97
+ - chunky_csv.gemspec
98
+ - lib/chunky_csv.rb
99
+ - lib/chunky_csv/version.rb
100
+ homepage: ''
101
+ licenses: []
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.1.11
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Use it to do things, like split large csv files into small ones
123
+ test_files: []