ptools 1.2.7-universal-mingw32 → 1.3.0-universal-mingw32
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 +8 -8
- data/CHANGES +5 -0
- data/MANIFEST +0 -1
- data/README +0 -3
- data/lib/ptools.rb +30 -20
- data/ptools.gemspec +1 -1
- data/test/test_constants.rb +1 -1
- data/test/test_tail.rb +4 -4
- metadata +2 -4
- data/test/test_middle.rb +0 -58
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ODRjNGZmYTAzYTU4ZjE4NDA0OWVlZWZiZDlmMDNlNjExZWM4YWY3ZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjVkYjQwMTQ0YWViOTJlMDI4NTFmYTYyYTc3MGJhNTQyNGM3NTU5Ng==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YjcxOWU1MmNkZTBlZjg5YTIzMTU0NWZmNjM5ZjBlMmU4NGUyZTM0MTdjYzNk
|
10
|
+
NWFmNmRmZTYzZTUzYmE3N2U4NjMwZmM0MWEzZjc3M2EwNjUzMmY1NWM4NDc2
|
11
|
+
NjUxODhkZDczNWVhZjIzZTRjNWRmMDNiMzc0Zjc4YTUzZWJkMjY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YTUyMzY2MGRjNzM4YzE5ZDhkMWQ3MWYyNjgwNDBkYjFmMzA4NjQyYmJmYjg5
|
14
|
+
ODdhMzFlNTZmZmQ4MzE0YzRiNGIwNGE5MjI2Y2MzZjdjNGYwYjkzNWI1NjMy
|
15
|
+
OTJiMmNjMjE2ZTY3M2M4MDhhMzQ5ZjhmMGUwM2M3ZmJjMDIxOGY=
|
data/CHANGES
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 1.3.0 - 8-Dec-2014
|
2
|
+
* Made the File.tail method efficient. It's no longer slurpy, and it also no
|
3
|
+
longer includes line endings in the result.
|
4
|
+
* Removed the File.middle method. I don't think it's useful.
|
5
|
+
|
1
6
|
== 1.2.7 - 6-Dec-2014
|
2
7
|
* File.which now expands ~ for paths. Thanks go to dg-vrnetze for the patch.
|
3
8
|
* Use /dev/null for cygwin instead of NUL.
|
data/MANIFEST
CHANGED
data/README
CHANGED
@@ -15,7 +15,6 @@
|
|
15
15
|
File.whereis("ruby") # ['/usr/local/bin/ruby','/opt/bin/ruby']
|
16
16
|
|
17
17
|
File.head("myfile") # Returns first 10 lines of 'myfile'
|
18
|
-
File.middle("myfile",8,12) # Returns lines 8-12 of 'myfile'
|
19
18
|
File.tail("myfile",3) # Returns last 3 lines of 'myfile'
|
20
19
|
File.wc("myfile",'words') # Returns the number of words in 'myfile'
|
21
20
|
|
@@ -40,8 +39,6 @@
|
|
40
39
|
The File.nl_convert method is based on the nlcvt program found at
|
41
40
|
http://www.perl.com/language/ppt/src/nlcvt/nlcvt, written by Tom Christiansen.
|
42
41
|
|
43
|
-
The middle() method was provided by Shashank Date.
|
44
|
-
|
45
42
|
The binary?() method was based almost entirely on a blog post by Ryan
|
46
43
|
Davis (who, in turn, based his code on Perl's -B switch).
|
47
44
|
|
data/lib/ptools.rb
CHANGED
@@ -3,7 +3,7 @@ require 'win32/file' if File::ALT_SEPARATOR
|
|
3
3
|
|
4
4
|
class File
|
5
5
|
# The version of the ptools library.
|
6
|
-
PTOOLS_VERSION = '1.
|
6
|
+
PTOOLS_VERSION = '1.3.0'
|
7
7
|
|
8
8
|
# :stopdoc:
|
9
9
|
|
@@ -237,35 +237,45 @@ class File
|
|
237
237
|
return a.empty? ? nil : a # Return nil in block form
|
238
238
|
end
|
239
239
|
|
240
|
-
# In block form, yields line +from+ up to line +to+. In non-block form
|
241
|
-
# returns an Array of lines from +from+ to +to+.
|
242
|
-
#
|
243
|
-
def self.middle(filename, from=10, to=20)
|
244
|
-
if block_given?
|
245
|
-
IO.readlines(filename)[from-1..to-1].each{ |line| yield line }
|
246
|
-
else
|
247
|
-
IO.readlines(filename)[from-1..to-1]
|
248
|
-
end
|
249
|
-
end
|
250
|
-
|
251
240
|
# In block form, yields the last +num_lines+ of file +filename+.
|
252
241
|
# In non-block form, it returns the lines as an array.
|
253
242
|
#
|
254
|
-
# Note that this method slurps the entire file, so I don't recommend it
|
255
|
-
# for very large files. Also note that 'tail -f' functionality is not
|
256
|
-
# present. See the 'file-tail' library for that.
|
257
|
-
#
|
258
243
|
# Example:
|
259
244
|
#
|
260
245
|
# File.tail('somefile.txt') # => ['This is line7', 'This is line8', ...]
|
261
246
|
#
|
247
|
+
# If you're looking for tail -f functionality, please use the file-tail
|
248
|
+
# gem instead.
|
249
|
+
#
|
262
250
|
def self.tail(filename, num_lines=10)
|
251
|
+
tail_size = 2**16 # 64k chunks
|
252
|
+
|
253
|
+
# MS Windows gets unhappy if you try to seek backwards past the
|
254
|
+
# end of the file, so we have some extra checks here and later.
|
255
|
+
file_size = File.size(filename)
|
256
|
+
tail_size = file_size if file_size <= tail_size
|
257
|
+
line_sep = File::ALT_SEPARATOR ? "\r\n" : "\n"
|
258
|
+
|
259
|
+
buf = ''
|
260
|
+
|
261
|
+
File.open(filename){ |fh|
|
262
|
+
fh.seek(-tail_size, File::SEEK_END)
|
263
|
+
|
264
|
+
while buf.count(line_sep) <= num_lines
|
265
|
+
buf = fh.read(tail_size) + buf
|
266
|
+
break if buf.count(line_sep) >= num_lines
|
267
|
+
if tail_size * 2 < file_size
|
268
|
+
fh.seek(-tail_size * 2, File::SEEK_CUR)
|
269
|
+
else
|
270
|
+
fh.seek(-file_size, File::SEEK_CUR)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
}
|
274
|
+
|
263
275
|
if block_given?
|
264
|
-
|
265
|
-
yield line
|
266
|
-
}
|
276
|
+
buf.split(line_sep)[-num_lines..-1].each{ |line| yield line }
|
267
277
|
else
|
268
|
-
|
278
|
+
buf.split(line_sep)[-num_lines..-1]
|
269
279
|
end
|
270
280
|
end
|
271
281
|
|
data/ptools.gemspec
CHANGED
data/test/test_constants.rb
CHANGED
@@ -15,7 +15,7 @@ class TC_Ptools_Constants < Test::Unit::TestCase
|
|
15
15
|
end
|
16
16
|
|
17
17
|
test "PTOOLS_VERSION constant is set to expected value" do
|
18
|
-
assert_equal('1.
|
18
|
+
assert_equal('1.3.0', File::PTOOLS_VERSION)
|
19
19
|
end
|
20
20
|
|
21
21
|
test "IMAGE_EXT constant is set to array of values" do
|
data/test/test_tail.rb
CHANGED
@@ -16,11 +16,11 @@ class TC_FileTail < Test::Unit::TestCase
|
|
16
16
|
def setup
|
17
17
|
@test_file = 'test_file1.txt'
|
18
18
|
|
19
|
-
@expected_tail1 = ["line16
|
20
|
-
@expected_tail1.push("line20
|
21
|
-
@expected_tail1.push("line24
|
19
|
+
@expected_tail1 = ["line16","line17","line18","line19"]
|
20
|
+
@expected_tail1.push("line20","line21","line22", "line23")
|
21
|
+
@expected_tail1.push("line24","line25")
|
22
22
|
|
23
|
-
@expected_tail2 = ["line21
|
23
|
+
@expected_tail2 = ["line21","line22","line23","line24","line25"]
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_tail_basic
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ptools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: universal-mingw32
|
6
6
|
authors:
|
7
7
|
- Daniel J. Berger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -79,7 +79,6 @@ files:
|
|
79
79
|
- test/test_head.rb
|
80
80
|
- test/test_image.rb
|
81
81
|
- test/test_is_sparse.rb
|
82
|
-
- test/test_middle.rb
|
83
82
|
- test/test_nlconvert.rb
|
84
83
|
- test/test_null.rb
|
85
84
|
- test/test_tail.rb
|
@@ -119,7 +118,6 @@ test_files:
|
|
119
118
|
- test/test_head.rb
|
120
119
|
- test/test_image.rb
|
121
120
|
- test/test_is_sparse.rb
|
122
|
-
- test/test_middle.rb
|
123
121
|
- test/test_nlconvert.rb
|
124
122
|
- test/test_null.rb
|
125
123
|
- test/test_tail.rb
|
data/test/test_middle.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
#####################################################################
|
2
|
-
# test_middle.rb
|
3
|
-
#
|
4
|
-
# Test case for the File.middle method. You should run this test
|
5
|
-
# via the 'rake test_middle' task.
|
6
|
-
#####################################################################
|
7
|
-
require 'test-unit'
|
8
|
-
require 'ptools'
|
9
|
-
|
10
|
-
class TC_FileMiddle < Test::Unit::TestCase
|
11
|
-
def self.startup
|
12
|
-
Dir.chdir('test') if File.exist?('test')
|
13
|
-
File.open('test_file1.txt', 'w'){ |fh| 25.times{ |n| fh.puts "line#{n+1}" } }
|
14
|
-
end
|
15
|
-
|
16
|
-
def setup
|
17
|
-
@test_file = 'test_file1.txt'
|
18
|
-
|
19
|
-
@expected_middle1 = ["line10\n", "line11\n", "line12\n", "line13\n", "line14\n"]
|
20
|
-
@expected_middle1.push("line15\n","line16\n", "line17\n", "line18\n")
|
21
|
-
@expected_middle1.push("line19\n","line20\n")
|
22
|
-
|
23
|
-
@expected_middle2 = ["line14\n","line15\n","line16\n","line17\n"]
|
24
|
-
@expected_middle2.push("line18\n","line19\n","line20\n")
|
25
|
-
|
26
|
-
@expected_middle3 = ["line5\n","line6\n","line7\n"]
|
27
|
-
@expected_middle3.push("line8\n","line9\n","line10\n")
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_method_basic
|
31
|
-
assert_respond_to(File, :middle)
|
32
|
-
assert_nothing_raised{ File.middle(@test_file) }
|
33
|
-
assert_nothing_raised{ File.middle(@test_file, 14) }
|
34
|
-
assert_nothing_raised{ File.middle(@test_file, 5, 10) }
|
35
|
-
assert_nothing_raised{ File.middle(@test_file){} }
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_middle_expected_results
|
39
|
-
assert_kind_of(Array, File.middle(@test_file))
|
40
|
-
assert_equal(@expected_middle1, File.middle(@test_file))
|
41
|
-
assert_equal(@expected_middle2, File.middle(@test_file, 14))
|
42
|
-
assert_equal(@expected_middle3, File.middle(@test_file, 5, 10))
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_middle_expected_errors
|
46
|
-
assert_raises(ArgumentError){ File.middle }
|
47
|
-
assert_raises(ArgumentError){ File.middle(@test_file, 5, 10, 15) }
|
48
|
-
assert_raises(NoMethodError){ File.middle(@test_file, "foo") }
|
49
|
-
assert_raises(Errno::ENOENT){ File.middle("bogus") }
|
50
|
-
end
|
51
|
-
|
52
|
-
def teardown
|
53
|
-
@test_file = nil
|
54
|
-
@expected_middle1 = nil
|
55
|
-
@expected_middle2 = nil
|
56
|
-
@expected_middle3 = nil
|
57
|
-
end
|
58
|
-
end
|