epitools 0.4.35 → 0.4.36

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.35
1
+ 0.4.36
data/epitools.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{epitools}
8
- s.version = "0.4.35"
8
+ s.version = "0.4.36"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["epitron"]
12
- s.date = %q{2011-05-20}
12
+ s.date = %q{2011-05-23}
13
13
  s.description = %q{Miscellaneous utility libraries to make my life easier.}
14
14
  s.email = %q{chris@ill-logic.com}
15
15
  s.extra_rdoc_files = [
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "VERSION",
25
25
  "epitools.gemspec",
26
26
  "lib/epitools.rb",
27
+ "lib/epitools/autoloads.rb",
27
28
  "lib/epitools/basetypes.rb",
28
29
  "lib/epitools/browser.rb",
29
30
  "lib/epitools/browser/cache.rb",
@@ -48,6 +49,7 @@ Gem::Specification.new do |s|
48
49
  "spec/basetypes_spec.rb",
49
50
  "spec/browser_spec.rb",
50
51
  "spec/clitools_spec.rb",
52
+ "spec/colored_spec.rb",
51
53
  "spec/lcs_spec.rb",
52
54
  "spec/numwords_spec.rb",
53
55
  "spec/path_spec.rb",
@@ -62,7 +64,7 @@ Gem::Specification.new do |s|
62
64
  s.homepage = %q{http://github.com/epitron/epitools}
63
65
  s.licenses = ["WTFPL"]
64
66
  s.require_paths = ["lib"]
65
- s.rubygems_version = %q{1.7.2}
67
+ s.rubygems_version = %q{1.6.2}
66
68
  s.summary = %q{NOT UTILS... METILS!}
67
69
 
68
70
  if s.respond_to? :specification_version then
data/lib/epitools.rb CHANGED
@@ -1,11 +1,7 @@
1
- autoload :Path, 'epitools/path'
2
- autoload :Browser, 'epitools/browser'
3
- autoload :Rash, 'epitools/rash'
4
- autoload :Ratio, 'epitools/ratio'
5
- autoload :Sys, 'epitools/sys'
1
+ require 'pp'
2
+ require 'set'
6
3
 
7
4
  class Object
8
-
9
5
  unless defined?(__DIR__)
10
6
  #
11
7
  # This method is convenience for the `File.expand_path(File.dirname(__FILE__))` idiom.
@@ -17,9 +13,9 @@ class Object
17
13
  ::File.expand_path(::File.join(dir, *args.map{|a| a.to_s}))
18
14
  end
19
15
  end
20
-
21
16
  end
22
17
 
18
+ ## Pretty error messages
23
19
  require_wrapper = proc do |mod|
24
20
  #p [:loading, mod]
25
21
  begin
@@ -29,7 +25,9 @@ require_wrapper = proc do |mod|
29
25
  end
30
26
  end
31
27
 
28
+ ## Require the tools
32
29
  %w[
30
+ autoloads
33
31
  basetypes
34
32
  niceprint
35
33
  string_to_proc
@@ -0,0 +1,26 @@
1
+ ## Standard library
2
+ autoload :URI, 'uri'
3
+ autoload :CGI, 'cgi'
4
+ autoload :Base64, 'base64'
5
+ autoload :JSON, 'json'
6
+ autoload :Zlib, 'zlib'
7
+ autoload :FileUtils, 'fileutils'
8
+ autoload :Tempfile, 'tempfile'
9
+ autoload :BigDecimal, 'bigdecimal'
10
+
11
+ module Digest
12
+ autoload :SHA1, 'digest/sha1'
13
+ autoload :SHA2, 'digest/sha2'
14
+ autoload :MD5, 'digest/md5'
15
+ end
16
+
17
+ ## Nonstandard library
18
+ autoload :Path, 'epitools/path'
19
+ autoload :Browser, 'epitools/browser'
20
+ autoload :Rash, 'epitools/rash'
21
+ autoload :Ratio, 'epitools/ratio'
22
+ autoload :Sys, 'epitools/sys'
23
+ autoload :ProgressBar, 'epitools/progressbar'
24
+
25
+
26
+
@@ -1,14 +1,4 @@
1
- require 'pp'
2
-
3
- autoload :URI, 'uri'
4
- autoload :CGI, 'cgi'
5
- autoload :Base64, 'base64'
6
- module Digest
7
- autoload :MD5, 'digest/md5'
8
- autoload :SHA1, 'digest/sha1'
9
- end
10
- autoload :JSON, 'json'
11
-
1
+ require 'epitools'
12
2
 
13
3
  ## Alias "Enumerator" to "Enum"
14
4
 
@@ -1,5 +1,5 @@
1
+ require 'epitools'
1
2
  require 'mechanize'
2
- require 'epitools/basetypes'
3
3
  require 'epitools/browser/cache'
4
4
  require 'epitools/browser/mechanize_progressbar'
5
5
 
@@ -7,35 +7,9 @@ require 'epitools/browser/mechanize_progressbar'
7
7
  #require 'socksify'
8
8
  class SOCKSError < Exception; end # :nodoc:
9
9
 
10
- # TODO: Put options here.
11
- =begin
12
- class BrowserOptions < OpenStruct
13
-
14
- DEFAULTS = {
15
- :delay => 1,
16
- :delay_jitter => 0.2,
17
- :use_cache => true,
18
- :use_logs => false,
19
- :cookie_file => "cookies.txt"
20
- }
21
-
22
- def initialize(extra_opts)
23
-
24
- @opts = DEFAULTS.dup
25
-
26
- for key, val in opts
27
- if key.in? DEFAULTS
28
- @opts[key] = val
29
- else
30
- raise "Unknown option: #{key}"
31
- end
32
- end
33
- end
34
-
35
- end
36
- =end
37
-
38
- # Monkeypatches!
10
+ #
11
+ # Slightly more intelligent `Mechanize::File`s
12
+ #
39
13
  class Mechanize::File
40
14
  def content_type
41
15
  response['content-type']
@@ -78,7 +52,6 @@ class Browser
78
52
  init_agent!
79
53
  init_cache!
80
54
  end
81
-
82
55
 
83
56
  def init_agent!
84
57
  @agent = Mechanize.new do |a|
@@ -91,7 +64,6 @@ class Browser
91
64
  load_cookies!
92
65
  end
93
66
 
94
-
95
67
  def delay(override_delay=nil, override_jitter=nil)
96
68
  elapsed = Time.now - @last_get
97
69
  jitter = rand * (override_jitter || @delay_jitter)
@@ -103,13 +75,11 @@ class Browser
103
75
  end
104
76
  end
105
77
 
106
-
107
78
  def init_cache!
108
79
  # TODO: Rescue "couldn't load" exception and disable caching
109
80
  @cache = Cache.new(agent) if @use_cache
110
81
  end
111
82
 
112
-
113
83
  def load_cookies!
114
84
  if File.exists? @cookie_file
115
85
  agent.cookie_jar.load @cookie_file
@@ -118,19 +88,15 @@ class Browser
118
88
  false
119
89
  end
120
90
  end
121
-
122
91
 
123
92
  def save_cookies!
124
93
  agent.cookie_jar.save_as @cookie_file
125
94
  true
126
95
  end
127
-
128
-
129
96
 
130
97
  def relative?(url)
131
98
  not url[ %r{^https?://} ]
132
99
  end
133
-
134
100
 
135
101
  def cacheable?(page)
136
102
  case page.content_type
@@ -138,7 +104,6 @@ class Browser
138
104
  true
139
105
  end
140
106
  end
141
-
142
107
 
143
108
  def cache_put(page, url)
144
109
  if cache.valid_page?(page)
@@ -193,6 +158,7 @@ class Browser
193
158
  puts " |_ ERROR: #{e.inspect} -- retrying"
194
159
  delay(5)
195
160
  retry
161
+
196
162
  =begin
197
163
  rescue Mechanize::ResponseCodeError => e
198
164
 
@@ -231,3 +197,33 @@ class Browser
231
197
 
232
198
  end
233
199
 
200
+
201
+ # TODO: Put options here.
202
+ =begin
203
+ class BrowserOptions < OpenStruct
204
+
205
+ DEFAULTS = {
206
+ :delay => 1,
207
+ :delay_jitter => 0.2,
208
+ :use_cache => true,
209
+ :use_logs => false,
210
+ :cookie_file => "cookies.txt"
211
+ }
212
+
213
+ def initialize(extra_opts)
214
+
215
+ @opts = DEFAULTS.dup
216
+
217
+ for key, val in opts
218
+ if key.in? DEFAULTS
219
+ @opts[key] = val
220
+ else
221
+ raise "Unknown option: #{key}"
222
+ end
223
+ end
224
+ end
225
+
226
+ end
227
+ =end
228
+
229
+
File without changes
@@ -1,4 +1,4 @@
1
- require 'epitools/progressbar'
1
+ require 'epitools'
2
2
 
3
3
  #
4
4
  # Mechanize Progress Bar extension
@@ -1,4 +1,4 @@
1
- require 'epitools/colored'
1
+ require 'epitools'
2
2
 
3
3
  class String
4
4
 
@@ -35,6 +35,7 @@ module Colored
35
35
  'yellow' => 33,
36
36
  'blue' => 34,
37
37
  'magenta' => 35,
38
+ 'purple' => 35,
38
39
  'cyan' => 36,
39
40
  'white' => 37
40
41
  }
@@ -72,7 +73,8 @@ module Colored
72
73
  VALID_COLORS = Set.new(
73
74
  COLORS.keys +
74
75
  COLORS.map { |k,v| "light_#{k}" } +
75
- COLORS.map { |k,v| "bold_#{k}" }
76
+ COLORS.map { |k,v| "bright_#{k}" } +
77
+ ["grey", "gray"]
76
78
  )
77
79
 
78
80
  COLORS.each do |color, value|
@@ -88,6 +90,10 @@ module Colored
88
90
  colorize(self, :foreground => color, :extra => 'bold')
89
91
  end
90
92
 
93
+ define_method("bright_#{color}") do
94
+ colorize(self, :foreground => color, :extra => 'bold')
95
+ end
96
+
91
97
  COLORS.each do |highlight, value|
92
98
  next if color == highlight
93
99
 
@@ -102,6 +108,9 @@ module Colored
102
108
  end
103
109
  end
104
110
 
111
+ alias_method :gray, :light_black
112
+ alias_method :grey, :light_black
113
+
105
114
  EXTRAS.each do |extra, value|
106
115
  next if extra == 'clear'
107
116
  define_method(extra) do
@@ -210,7 +219,7 @@ module Colored
210
219
  def valid_tag?(tag)
211
220
  VALID_COLORS.include?(tag) or
212
221
  (
213
- string =~ /^\d+$/ and
222
+ tag =~ /^\d+$/ and
214
223
  BBS_COLOR_TABLE.include?(tag.to_i)
215
224
  )
216
225
  end
@@ -1,4 +1,4 @@
1
- require 'colored'
1
+ require 'epitools'
2
2
 
3
3
  ASCII_PRINTABLE = (33..126)
4
4
 
@@ -1,6 +1,4 @@
1
- require 'epitools/basetypes'
2
- require 'bigdecimal'
3
- require 'set'
1
+ require 'epitools'
4
2
 
5
3
  #
6
4
  # Allow numbers to be converted into words, or exprssed more easily with words.
@@ -118,7 +116,7 @@ class Numeric
118
116
  factor = 10**pow
119
117
 
120
118
  if is_a?(Float)
121
- (BigDecimal(to_s) * factor).to_i
119
+ (BigDecimal.new(to_s) * factor).to_i
122
120
  else
123
121
  self * factor
124
122
  end
data/lib/epitools/path.rb CHANGED
@@ -1,14 +1,4 @@
1
- require 'epitools/basetypes'
2
-
3
- autoload :FileUtils, 'fileutils'
4
- autoload :Tempfile, 'tempfile'
5
- autoload :URI, 'uri'
6
- module Digest
7
- autoload :SHA1, 'digest/sha1'
8
- autoload :SHA2, 'digest/sha2'
9
- autoload :MD5, 'digest/md5'
10
- end
11
-
1
+ require 'epitools'
12
2
 
13
3
  class Path
14
4
 
@@ -1,4 +1,4 @@
1
- require 'epitools/basetypes'
1
+ require 'epitools'
2
2
 
3
3
  class Array
4
4
 
@@ -1,6 +1,4 @@
1
- require 'pp'
2
- require 'rubygems'
3
- require 'colorize'
1
+ require 'epitools'
4
2
 
5
3
  # TODO: Pick a backtrace format. (Also, add a method to replace default backtracer.)
6
4
  # TODO: This chould be in a class.
@@ -71,7 +69,7 @@ end
71
69
 
72
70
  def color_backtrace_2(lines, options={})
73
71
 
74
- groups = lines.reverse.split_when { |line,nextline| line.path != nextline.path }
72
+ groups = lines.reverse.split_at { |line,nextline| line.path != nextline.path }
75
73
 
76
74
  if options[:no_gems]
77
75
  groups = groups.split_when { |a, nexta| a.first.gem? != nexta.first.gem? }
@@ -253,7 +251,13 @@ if $0 == __FILE__
253
251
  /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
254
252
  /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
255
253
  script/server:3
256
- }.split("\n").select{|line| line.any?}
254
+ }.lines
255
+
256
+ begin
257
+ String.new nil
258
+ rescue => e
259
+ backtrace = e.backtrace
260
+ end
257
261
 
258
262
  lines = parse_lines(backtrace)
259
263
  #debug_backtrace(lines)
@@ -1,3 +1,5 @@
1
+ require 'epitools'
2
+
1
3
  if defined? Rails
2
4
 
3
5
  class ActiveRecord::Base
@@ -1,4 +1,8 @@
1
-
1
+ #
2
+ # The ratio between two numbers (eg: 2:1, 3:4)
3
+ #
4
+ # Can be compared, added, "percent"ed, "to_f"ed, and displayed.
5
+ #
2
6
  class Ratio
3
7
 
4
8
  include Comparable
@@ -13,16 +17,25 @@ class Ratio
13
17
  new(*args)
14
18
  end
15
19
 
20
+ #
21
+ # `first` is the top part of the ratio, `last` is the bottom (eg: `first/last`)
22
+ #
16
23
  def initialize(first, last=1)
17
24
  @first = first
18
25
  @last = last
19
26
  end
20
27
 
28
+ #
29
+ # Returns a string representation: "a/b"
30
+ #
21
31
  def to_s
22
32
  "#{@first}/#{@last}"
23
33
  end
24
34
  alias_method :ratio, :to_s
25
35
 
36
+ #
37
+ # Returns the ratio as a float. (eg: Ratio[1,2].to_f == 0.5)
38
+ #
26
39
  def to_f
27
40
  if @last == 0
28
41
  0.0
@@ -31,18 +44,35 @@ class Ratio
31
44
  end
32
45
  end
33
46
 
47
+ #
48
+ # Returns a string representing the number in percent
49
+ #
34
50
  def percent
35
51
  "%0.1f%" % (to_f * 100)
36
52
  end
37
53
  alias_method :to_percent, :percent
38
54
 
55
+ #
56
+ # "#<Ratio: 1/2>"
57
+ #
39
58
  def inspect
40
59
  "#<Ratio: #{to_s}>"
41
60
  end
42
61
 
62
+ #
63
+ # Adds together the tops and bottoms of the ratios.
64
+ #
65
+ # Example: For the ratios `a/c` and `b/d`, returns `a+b/c+d`
66
+ #
43
67
  def +(other)
44
68
  Ratio.new( first+other.first, last+other.last)
45
69
  end
46
70
 
47
71
  end
48
72
 
73
+ #
74
+ # Function-style wrapper
75
+ #
76
+ def Ratio(*args)
77
+ Ratio.new(*args)
78
+ end
data/lib/epitools/sys.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'epitools/basetypes'
1
+ require 'epitools'
2
2
 
3
3
  #
4
4
  # Cross-platform operating system functions.
@@ -1,10 +1,10 @@
1
- require 'zlib'
1
+ require 'epitools'
2
2
 
3
3
  #
4
4
  # A mutation of "open" that lets you read/write gzip files, as well as
5
5
  # regular files.
6
6
  #
7
- # (NOTE: gzip detection is purely based on the filename.)
7
+ # (NOTE: gzip detection is based on the filename, not the contents.)
8
8
  #
9
9
  # It accepts a block just like open()!
10
10
  #
@@ -12,7 +12,7 @@ require 'zlib'
12
12
  # zopen("test.txt") #=> #<File:test.txt>
13
13
  # zopen("test.txt.gz") #=> #<Zlib::GzipReader:0xb6c79424>
14
14
  # zopen("otherfile.gz", "w") #=> #<Zlib::GzipWriter:0x7fe30448>>
15
- # zopen("test.txt.gz") { |f| f.read } # reads the contents of the .gz file, then closes the file automatically.
15
+ # zopen("test.txt.gz") { |f| f.read } # read the contents of the .gz file, then close the file handle automatically.
16
16
  #
17
17
  def zopen(filename, mode="r")
18
18
 
@@ -1,4 +1,4 @@
1
- require 'epitools/basetypes'
1
+ require 'epitools'
2
2
 
3
3
  describe Object do
4
4
 
data/spec/browser_spec.rb CHANGED
File without changes
@@ -0,0 +1,29 @@
1
+ require 'epitools'
2
+
3
+ describe "Colored strings" do
4
+
5
+ it "has string methods" do
6
+ s = "string"
7
+ s.should respond_to(:blue)
8
+ s.should respond_to(:light_blue)
9
+ s.should respond_to(:bright_blue)
10
+ s.should respond_to(:grey)
11
+ s.should respond_to(:gray)
12
+ s.should respond_to(:purple)
13
+ s.should respond_to(:magenta)
14
+ lambda { s.light_blue }.should_not raise_error
15
+ end
16
+
17
+ it "can do bbs colors" do
18
+ "<5>yay".colorize.should == "yay".magenta
19
+ end
20
+
21
+ it "can do tagged colors" do
22
+ "<blue>hello".colorize.should == "hello".blue
23
+ "<magenta>hello".colorize.should == "<purple>hello".colorize
24
+ "<gray>hello".colorize.should == "<light_black>hello".colorize
25
+ lambda { "</blue>".colorize }.should raise_error
26
+ end
27
+
28
+ end
29
+
@@ -1,4 +1,4 @@
1
- require 'epitools/numwords'
1
+ require 'epitools'
2
2
 
3
3
  describe Numeric do
4
4
 
data/spec/path_spec.rb CHANGED
File without changes
data/spec/ratio_spec.rb CHANGED
@@ -21,4 +21,9 @@ describe Ratio do
21
21
  @b.percent.should == "50.0%"
22
22
  end
23
23
 
24
+ it "uses the function-style wrapper" do
25
+ Ratio(1,2).should == Ratio[1,2]
26
+ Ratio(1,2).should == Ratio.new(1,2)
27
+ end
28
+
24
29
  end
data/spec/sys_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'epitools/sys'
1
+ require 'epitools'
2
2
 
3
3
  describe Sys::ProcessInfo do
4
4
 
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- hash: 73
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 4
9
- - 35
10
- version: 0.4.35
5
+ version: 0.4.36
11
6
  platform: ruby
12
7
  authors:
13
8
  - epitron
@@ -15,7 +10,8 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-05-20 00:00:00 Z
13
+ date: 2011-05-23 00:00:00 -04:00
14
+ default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
21
17
  name: rspec
@@ -25,11 +21,6 @@ dependencies:
25
21
  requirements:
26
22
  - - ~>
27
23
  - !ruby/object:Gem::Version
28
- hash: 7
29
- segments:
30
- - 2
31
- - 2
32
- - 0
33
24
  version: 2.2.0
34
25
  type: :development
35
26
  version_requirements: *id001
@@ -41,11 +32,6 @@ dependencies:
41
32
  requirements:
42
33
  - - ~>
43
34
  - !ruby/object:Gem::Version
44
- hash: 23
45
- segments:
46
- - 1
47
- - 0
48
- - 0
49
35
  version: 1.0.0
50
36
  type: :development
51
37
  version_requirements: *id002
@@ -57,9 +43,6 @@ dependencies:
57
43
  requirements:
58
44
  - - ">="
59
45
  - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
46
  version: "0"
64
47
  type: :development
65
48
  version_requirements: *id003
@@ -80,6 +63,7 @@ files:
80
63
  - VERSION
81
64
  - epitools.gemspec
82
65
  - lib/epitools.rb
66
+ - lib/epitools/autoloads.rb
83
67
  - lib/epitools/basetypes.rb
84
68
  - lib/epitools/browser.rb
85
69
  - lib/epitools/browser/cache.rb
@@ -104,6 +88,7 @@ files:
104
88
  - spec/basetypes_spec.rb
105
89
  - spec/browser_spec.rb
106
90
  - spec/clitools_spec.rb
91
+ - spec/colored_spec.rb
107
92
  - spec/lcs_spec.rb
108
93
  - spec/numwords_spec.rb
109
94
  - spec/path_spec.rb
@@ -114,6 +99,7 @@ files:
114
99
  - spec/spec_helper.rb
115
100
  - spec/sys_spec.rb
116
101
  - spec/zopen_spec.rb
102
+ has_rdoc: true
117
103
  homepage: http://github.com/epitron/epitools
118
104
  licenses:
119
105
  - WTFPL
@@ -127,23 +113,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
113
  requirements:
128
114
  - - ">="
129
115
  - !ruby/object:Gem::Version
130
- hash: 3
131
- segments:
132
- - 0
133
116
  version: "0"
134
117
  required_rubygems_version: !ruby/object:Gem::Requirement
135
118
  none: false
136
119
  requirements:
137
120
  - - ">="
138
121
  - !ruby/object:Gem::Version
139
- hash: 3
140
- segments:
141
- - 0
142
122
  version: "0"
143
123
  requirements: []
144
124
 
145
125
  rubyforge_project:
146
- rubygems_version: 1.7.2
126
+ rubygems_version: 1.6.2
147
127
  signing_key:
148
128
  specification_version: 3
149
129
  summary: NOT UTILS... METILS!