linesource 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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +10 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/linesource.rb +112 -0
- data/test/helper.rb +9 -0
- data/test/test_linesource.rb +7 -0
- metadata +65 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Daniel Polfer
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
= linesource
|
2
|
+
|
3
|
+
linesource walks through one or more text files, which may be compressed,
|
4
|
+
and allows each of the lines of text within that file to be processed in
|
5
|
+
one or more ways. The simplest is to walk over each line much like you
|
6
|
+
would a normal file.
|
7
|
+
|
8
|
+
== Copyright
|
9
|
+
|
10
|
+
Copyright (c) 2009 Daniel Polfer. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "linesource"
|
8
|
+
gem.summary = %Q{Allows fast line-by-line processing of one or more text files}
|
9
|
+
gem.description = %Q{Walks one or more text files, which may be gzipped, allowing line-by-line processing of the contents}
|
10
|
+
gem.email = "daniel@thepolfers.com"
|
11
|
+
gem.homepage = "http://github.com/polfer/linesource"
|
12
|
+
gem.authors = ["Daniel Polfer"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "linesource #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/linesource.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'zlib'
|
3
|
+
|
4
|
+
# Need to add documenation
|
5
|
+
# Need to have different versions of initialize
|
6
|
+
# Need to be able to pass in multiple patterns (an array of patterns)
|
7
|
+
# Needs to be a gem
|
8
|
+
|
9
|
+
class LineSource
|
10
|
+
|
11
|
+
include Enumerable
|
12
|
+
|
13
|
+
attr_reader :linenum
|
14
|
+
|
15
|
+
def initialize( pattern, skiplines = 0, filenew = nil, filedone = nil )
|
16
|
+
@skiplines = skiplines
|
17
|
+
@filenames = Dir.glob(pattern).sort!.freeze
|
18
|
+
@filenames.each { |fn| fn.freeze }
|
19
|
+
@index = -1
|
20
|
+
@content = nil
|
21
|
+
@lastline = nil
|
22
|
+
@filenew = filenew
|
23
|
+
@filedone = filedone
|
24
|
+
nextfile
|
25
|
+
end
|
26
|
+
|
27
|
+
def setfilenew( filenew )
|
28
|
+
@filenew = filenew
|
29
|
+
end
|
30
|
+
|
31
|
+
def setfiledone( filedone )
|
32
|
+
@filedone = filedone
|
33
|
+
end
|
34
|
+
|
35
|
+
def eof?
|
36
|
+
@content ? @content.eof? : true
|
37
|
+
end
|
38
|
+
|
39
|
+
def close
|
40
|
+
if @content
|
41
|
+
@filedone.call(self) if @filedone
|
42
|
+
@content.close
|
43
|
+
@content = nil
|
44
|
+
end
|
45
|
+
@lastline = nil
|
46
|
+
@index = -1
|
47
|
+
@linenum = -1
|
48
|
+
end
|
49
|
+
|
50
|
+
def nextfile
|
51
|
+
begin
|
52
|
+
if @content
|
53
|
+
@filedone.call(self) if @filedone
|
54
|
+
@content.close
|
55
|
+
@content = nil
|
56
|
+
end
|
57
|
+
|
58
|
+
@linenum = @skiplines
|
59
|
+
@index += 1 if @index < @filenames.length
|
60
|
+
if @filenames[@index] =~ /\.gz$/
|
61
|
+
@content = Zlib::GzipReader.open(@filenames[@index])
|
62
|
+
else
|
63
|
+
@content = File.open(@filenames[@index],"r")
|
64
|
+
end
|
65
|
+
@skiplines.times do
|
66
|
+
@content.gets
|
67
|
+
end
|
68
|
+
|
69
|
+
@filenew.call(self) if @filenew
|
70
|
+
rescue
|
71
|
+
if @index < @filenames.length
|
72
|
+
puts "\nERROR! Could not open #{@filenames[@index]}... skipping file!"
|
73
|
+
retry
|
74
|
+
end
|
75
|
+
nil
|
76
|
+
else
|
77
|
+
@filenames[@index]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def gets
|
82
|
+
@lastline = nil
|
83
|
+
if @content
|
84
|
+
while (!(@lastline = @content.gets) && nextfile) do
|
85
|
+
end
|
86
|
+
if @lastline
|
87
|
+
@linenum += 1
|
88
|
+
@lastline.freeze
|
89
|
+
end
|
90
|
+
end
|
91
|
+
@lastline
|
92
|
+
end
|
93
|
+
|
94
|
+
def lastline
|
95
|
+
@lastline
|
96
|
+
end
|
97
|
+
|
98
|
+
def filename
|
99
|
+
@index >= 0 ? @filenames[@index] : nil
|
100
|
+
end
|
101
|
+
|
102
|
+
def filesize
|
103
|
+
(@index >= 0 && @filenames[@index]) ? File.stat(@filenames[@index]).size : -1
|
104
|
+
end
|
105
|
+
|
106
|
+
def each
|
107
|
+
while gets
|
108
|
+
yield @lastline
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: linesource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Polfer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-06 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Walks one or more text files, which may be gzipped, allowing line-by-line processing of the contents
|
17
|
+
email: daniel@thepolfers.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- lib/linesource.rb
|
33
|
+
- test/helper.rb
|
34
|
+
- test/test_linesource.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/polfer/linesource
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options:
|
41
|
+
- --charset=UTF-8
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
version:
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.5
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Allows fast line-by-line processing of one or more text files
|
63
|
+
test_files:
|
64
|
+
- test/helper.rb
|
65
|
+
- test/test_linesource.rb
|