ptools 1.2.7 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19b48052d410d086d24c61ef25da0b3e87b32c00
4
- data.tar.gz: 49b572916684c747255775890a6e7188f286eced
3
+ metadata.gz: 57f08c106fbf7ec7ec69af176de55bed6738c146
4
+ data.tar.gz: bba434e438133b637255424a4d0192f550ac8f10
5
5
  SHA512:
6
- metadata.gz: 70977a568a82dd6885804f7b47391f096da6b66b965944838c85cb47b6eba501bec70e0bbe69ea65a02621707532a3bf24846dbd4011a9ef66b8dc5a03b63f58
7
- data.tar.gz: f81220180763b1fd1157712b16942c0fc5e588d3795b6be1fbaf7945201677333593f45dce298156e947434f18e1cfe7c990bbccd5edc3eac83a30fce56fe491
6
+ metadata.gz: 90c01dc5d9bba802ee72da662bcc8f68924899effe6cca6139521bd78664dc2e28a222d66198705076cf534cb21481413d01323821d8c5f02729e45c047a5c80
7
+ data.tar.gz: 8fbb9f1eb7794aa24a27dec7cc3ef0cd47d5d07338ea54996f1ec350950e0b9fcccf1059dc156318812d261db807223ddf54cde868ecfb533bcb6674032949ee
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
@@ -7,7 +7,6 @@
7
7
  * test/test_constants.rb
8
8
  * test/test_head.rb
9
9
  * test/test_image.rb
10
- * test/test_middle.rb
11
10
  * test/test_nlconvert.rb
12
11
  * test/test_null.rb
13
12
  * test/test_tail.rb
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
 
@@ -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.2.7'
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
- IO.readlines(filename).reverse[0..num_lines-1].reverse.each{ |line|
265
- yield line
266
- }
276
+ buf.split(line_sep)[-num_lines..-1].each{ |line| yield line }
267
277
  else
268
- IO.readlines(filename).reverse[0..num_lines-1].reverse
278
+ buf.split(line_sep)[-num_lines..-1]
269
279
  end
270
280
  end
271
281
 
@@ -3,7 +3,7 @@ require 'rbconfig'
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.name = 'ptools'
6
- gem.version = '1.2.7'
6
+ gem.version = '1.3.0'
7
7
  gem.license = 'Artistic 2.0'
8
8
  gem.author = 'Daniel J. Berger'
9
9
  gem.email = 'djberg96@gmail.com'
@@ -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.2.7', File::PTOOLS_VERSION)
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
@@ -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\n","line17\n","line18\n","line19\n"]
20
- @expected_tail1.push("line20\n","line21\n","line22\n", "line23\n")
21
- @expected_tail1.push("line24\n","line25\n")
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\n","line22\n","line23\n","line24\n","line25\n"]
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.2.7
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-06 00:00:00.000000000 Z
11
+ date: 2014-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -66,7 +66,6 @@ files:
66
66
  - test/test_head.rb
67
67
  - test/test_image.rb
68
68
  - test/test_is_sparse.rb
69
- - test/test_middle.rb
70
69
  - test/test_nlconvert.rb
71
70
  - test/test_null.rb
72
71
  - test/test_tail.rb
@@ -106,7 +105,6 @@ test_files:
106
105
  - test/test_head.rb
107
106
  - test/test_image.rb
108
107
  - test/test_is_sparse.rb
109
- - test/test_middle.rb
110
108
  - test/test_nlconvert.rb
111
109
  - test/test_null.rb
112
110
  - test/test_tail.rb
@@ -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