epitools 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/epitools.gemspec +5 -2
- data/lib/epitools.rb +1 -0
- data/lib/epitools/basetypes.rb +13 -1
- data/lib/epitools/browser/browser_cache.rb +7 -2
- data/lib/epitools/clitools.rb +10 -1
- data/lib/epitools/path.rb +131 -0
- data/spec/basetypes_spec.rb +5 -1
- data/spec/browser_spec.rb +63 -6
- data/spec/path_spec.rb +61 -0
- data/spec/spec_helper.rb +1 -0
- metadata +6 -9
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
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.
|
8
|
+
s.version = "0.4.0"
|
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{
|
12
|
+
s.date = %q{2011-01-06}
|
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 = [
|
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
|
|
35
35
|
"lib/epitools/lcs.rb",
|
36
36
|
"lib/epitools/metaclass.rb",
|
37
37
|
"lib/epitools/niceprint.rb",
|
38
|
+
"lib/epitools/path.rb",
|
38
39
|
"lib/epitools/permutations.rb",
|
39
40
|
"lib/epitools/pretty_backtrace.rb",
|
40
41
|
"lib/epitools/progressbar.rb",
|
@@ -49,6 +50,7 @@ Gem::Specification.new do |s|
|
|
49
50
|
"spec/clitools_spec.rb",
|
50
51
|
"spec/lcs_spec.rb",
|
51
52
|
"spec/metaclass_spec.rb",
|
53
|
+
"spec/path_spec.rb",
|
52
54
|
"spec/permutations_spec.rb",
|
53
55
|
"spec/rash_spec.rb",
|
54
56
|
"spec/ratio_spec.rb",
|
@@ -68,6 +70,7 @@ Gem::Specification.new do |s|
|
|
68
70
|
"spec/clitools_spec.rb",
|
69
71
|
"spec/lcs_spec.rb",
|
70
72
|
"spec/metaclass_spec.rb",
|
73
|
+
"spec/path_spec.rb",
|
71
74
|
"spec/permutations_spec.rb",
|
72
75
|
"spec/rash_spec.rb",
|
73
76
|
"spec/ratio_spec.rb",
|
data/lib/epitools.rb
CHANGED
data/lib/epitools/basetypes.rb
CHANGED
@@ -101,7 +101,7 @@ class Integer
|
|
101
101
|
# Convert the number to an array of bits (least significant digit first).
|
102
102
|
#
|
103
103
|
def to_bits
|
104
|
-
("%b" % self).chars.reverse.map(&:to_i)
|
104
|
+
("%b" % self).chars.to_a.reverse.map(&:to_i)
|
105
105
|
end
|
106
106
|
|
107
107
|
alias_method :bits, :to_bits
|
@@ -145,6 +145,18 @@ class Array
|
|
145
145
|
removed
|
146
146
|
end
|
147
147
|
|
148
|
+
#
|
149
|
+
# zip from the right (or reversed zip.)
|
150
|
+
#
|
151
|
+
# eg:
|
152
|
+
# >> [5,39].rzip([:hours, :mins, :secs])
|
153
|
+
# => [ [:mins, 5], [:secs, 39] ]
|
154
|
+
#
|
155
|
+
def rzip(other)
|
156
|
+
# That's a lotta reverses!
|
157
|
+
reverse.zip(other.reverse).reverse
|
158
|
+
end
|
159
|
+
|
148
160
|
end
|
149
161
|
|
150
162
|
|
@@ -35,6 +35,7 @@ class CacheDB
|
|
35
35
|
url = page.uri.to_s
|
36
36
|
|
37
37
|
p [:page_uri, url]
|
38
|
+
p [:original_url, url]
|
38
39
|
|
39
40
|
if url != original_url
|
40
41
|
# redirect original_url to url
|
@@ -48,7 +49,7 @@ class CacheDB
|
|
48
49
|
)
|
49
50
|
end
|
50
51
|
|
51
|
-
compressed_body = Zlib::Deflate.deflate(page.body)
|
52
|
+
compressed_body = page.body #Zlib::Deflate.deflate(page.body)
|
52
53
|
expire(url) if options[:overwrite]
|
53
54
|
db.execute(
|
54
55
|
"INSERT INTO cache VALUES ( ?, ?, ?, ? )",
|
@@ -71,7 +72,7 @@ class CacheDB
|
|
71
72
|
if redirect
|
72
73
|
get(redirect)
|
73
74
|
else
|
74
|
-
body = Zlib::Inflate.inflate(compressed_body)
|
75
|
+
body = compressed_body #Zlib::Inflate.inflate(compressed_body)
|
75
76
|
|
76
77
|
Mechanize::Page.new(
|
77
78
|
URI.parse(url),
|
@@ -150,6 +151,10 @@ class CacheDB
|
|
150
151
|
create_tables
|
151
152
|
end
|
152
153
|
|
154
|
+
def delete!
|
155
|
+
File.unlink @filename
|
156
|
+
end
|
157
|
+
|
153
158
|
private
|
154
159
|
|
155
160
|
def list_tables
|
data/lib/epitools/clitools.rb
CHANGED
@@ -18,11 +18,20 @@ end
|
|
18
18
|
#
|
19
19
|
# Output to less.
|
20
20
|
#
|
21
|
-
def lesspipe(
|
21
|
+
def lesspipe(*args)
|
22
|
+
if args.any? and args.last.is_a?(Hash)
|
23
|
+
options = args.pop
|
24
|
+
else
|
25
|
+
options = {}
|
26
|
+
end
|
27
|
+
|
28
|
+
output = args.first if args.any?
|
29
|
+
|
22
30
|
params = []
|
23
31
|
params << "-R" unless options[:color] == false
|
24
32
|
params << "-S" unless options[:wrap] == true
|
25
33
|
params << "-X"
|
34
|
+
|
26
35
|
IO.popen("less #{params * ' '}", "w") do |less|
|
27
36
|
if output
|
28
37
|
less.puts output
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'epitools/basetypes'
|
2
|
+
|
3
|
+
class Path
|
4
|
+
|
5
|
+
## initializers
|
6
|
+
|
7
|
+
def initialize(newpath)
|
8
|
+
self.path = newpath
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.[](newpath)
|
12
|
+
new(newpath)
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
## setters
|
17
|
+
|
18
|
+
def path=(newpath)
|
19
|
+
if File.exists? newpath
|
20
|
+
if File.directory? newpath
|
21
|
+
self.dir = newpath
|
22
|
+
else
|
23
|
+
self.dir, self.filename = File.split(newpath)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
if path[-1..-1] == File::SEPARATOR
|
27
|
+
self.dir = newpath
|
28
|
+
else
|
29
|
+
self.dir, self.filename = File.split(newpath)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def filename=(newfilename)
|
35
|
+
if newfilename.nil?
|
36
|
+
@ext, @base = nil, nil
|
37
|
+
else
|
38
|
+
ext = File.extname(newfilename)
|
39
|
+
|
40
|
+
if ext.blank?
|
41
|
+
@ext = nil
|
42
|
+
@base = newfilename
|
43
|
+
else
|
44
|
+
@ext = ext
|
45
|
+
if pos = newfilename.rindex(ext)
|
46
|
+
@base = newfilename[0...pos]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def dir=(newdir)
|
53
|
+
@dirs = File.absolute_path(newdir).split(File::SEPARATOR)[1..-1]
|
54
|
+
end
|
55
|
+
|
56
|
+
def ext=(newext)
|
57
|
+
if newext.nil? or newext[0] == ?.
|
58
|
+
@ext = newext
|
59
|
+
else
|
60
|
+
@ext = "." + newext
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
## readers
|
65
|
+
|
66
|
+
attr_reader :dirs, :base, :ext
|
67
|
+
|
68
|
+
def path
|
69
|
+
d = dir
|
70
|
+
f = filename
|
71
|
+
if d
|
72
|
+
File.join(d, (f || "") )
|
73
|
+
else
|
74
|
+
""
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def dir
|
79
|
+
if dirs
|
80
|
+
File::SEPARATOR + File.join(*dirs)
|
81
|
+
else
|
82
|
+
nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def filename
|
87
|
+
if base
|
88
|
+
base + (ext || "")
|
89
|
+
else
|
90
|
+
nil
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
## fstat info
|
96
|
+
|
97
|
+
def exists?
|
98
|
+
File.exists? path
|
99
|
+
end
|
100
|
+
|
101
|
+
def size
|
102
|
+
File.size path
|
103
|
+
end
|
104
|
+
|
105
|
+
def mtime
|
106
|
+
File.mtime path
|
107
|
+
end
|
108
|
+
|
109
|
+
def dir?
|
110
|
+
File.directory? path
|
111
|
+
end
|
112
|
+
|
113
|
+
def file?
|
114
|
+
File.file? path
|
115
|
+
end
|
116
|
+
|
117
|
+
def symlink?
|
118
|
+
File.symlink? path
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
## aliases
|
123
|
+
|
124
|
+
alias_method :basename, :base
|
125
|
+
alias_method :extname, :ext
|
126
|
+
alias_method :dirname, :dir
|
127
|
+
alias_method :pathname, :path
|
128
|
+
alias_method :to_s, :path
|
129
|
+
alias_method :directory?, :dir?
|
130
|
+
|
131
|
+
end
|
data/spec/basetypes_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe Object do
|
|
18
18
|
5.in?(20..30).should == false
|
19
19
|
"butt".in?("butts!!!").should == true
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it "benches" do
|
23
23
|
lambda {
|
24
24
|
bench("benchmark test") { x = 10 }
|
@@ -123,6 +123,10 @@ describe Array do
|
|
123
123
|
odd.should == [1,3,5,7,9,11]
|
124
124
|
end
|
125
125
|
|
126
|
+
it "rzips" do
|
127
|
+
[5,39].rzip([:hours, :mins, :secs]).should == [ [5, :mins], [39, :secs] ]
|
128
|
+
end
|
129
|
+
|
126
130
|
end
|
127
131
|
|
128
132
|
|
data/spec/browser_spec.rb
CHANGED
@@ -1,14 +1,71 @@
|
|
1
1
|
require 'epitools/browser'
|
2
2
|
|
3
|
-
describe Browser do
|
3
|
+
#describe Browser do
|
4
|
+
#
|
5
|
+
# before :all do
|
6
|
+
# @browser = Browser.new
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# after :all do
|
10
|
+
# @browser.cache.delete!
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# it "googles" do
|
14
|
+
# page = @browser.get("http://google.com")
|
15
|
+
# page.body["Feeling Lucky"].should_not be_empty
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
#end
|
19
|
+
|
20
|
+
class Mechanize::Page
|
21
|
+
def url
|
22
|
+
uri.to_s
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe CacheDB do
|
4
27
|
|
5
28
|
before :all do
|
6
|
-
@
|
29
|
+
@agent = Mechanize.new
|
30
|
+
CacheDB.new(@agent).delete!
|
31
|
+
@cache = CacheDB.new(@agent)
|
32
|
+
end
|
33
|
+
|
34
|
+
def new_page(body, url)
|
35
|
+
Mechanize::Page.new(
|
36
|
+
URI.parse(url),
|
37
|
+
{'content-type'=>'text/html'},
|
38
|
+
body,
|
39
|
+
nil,
|
40
|
+
@agent
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
after :all do
|
45
|
+
#@cache.delete!
|
7
46
|
end
|
8
|
-
|
9
|
-
it "
|
10
|
-
|
11
|
-
|
47
|
+
|
48
|
+
it "writes and reads" do
|
49
|
+
body = "Blah blah blah."
|
50
|
+
url = "http://example.com/url.html"
|
51
|
+
|
52
|
+
page = new_page(body, url)
|
53
|
+
|
54
|
+
page.body.should == body
|
55
|
+
page.url.should == url
|
56
|
+
|
57
|
+
@cache.put page, url
|
58
|
+
|
59
|
+
p @cache.urls
|
60
|
+
|
61
|
+
@cache.includes?(url).should == true
|
62
|
+
|
63
|
+
result = @cache.get url
|
64
|
+
|
65
|
+
body.should == page.body
|
66
|
+
body.should == result.body
|
67
|
+
url.should == page.url
|
68
|
+
url.should == result.url
|
12
69
|
end
|
13
70
|
|
14
71
|
end
|
data/spec/path_spec.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'epitools/path'
|
2
|
+
|
3
|
+
describe Path do
|
4
|
+
|
5
|
+
it "initializes and accesses everything" do
|
6
|
+
path = Path.new("/blah/what.mp4/.mp3/hello.avi")
|
7
|
+
|
8
|
+
path.dirs.should == %w[ blah what.mp4 .mp3 ]
|
9
|
+
path.dir.should == "/blah/what.mp4/.mp3"
|
10
|
+
path.filename.should == "hello.avi"
|
11
|
+
path.ext.should == ".avi"
|
12
|
+
path.base.should == "hello"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "works with relative paths" do
|
16
|
+
path = Path.new("../hello.mp3/blah")
|
17
|
+
|
18
|
+
path.filename.should == "blah"
|
19
|
+
path.ext.should == nil
|
20
|
+
|
21
|
+
abs_path = File.join(File.absolute_path(".."), "hello.mp3")
|
22
|
+
path.dir.should == abs_path
|
23
|
+
end
|
24
|
+
|
25
|
+
it "handles directories" do
|
26
|
+
path = Path.new("/etc/")
|
27
|
+
|
28
|
+
path.dirs.should_not == nil
|
29
|
+
path.dir.should == "/etc"
|
30
|
+
path.filename.should == nil
|
31
|
+
end
|
32
|
+
|
33
|
+
it "replaces ext" do
|
34
|
+
path = Path.new("/blah/what.mp4/.mp3/hello.avi")
|
35
|
+
|
36
|
+
path.ext.should == ".avi"
|
37
|
+
path.ext = ".mkv"
|
38
|
+
path.ext.should == ".mkv"
|
39
|
+
|
40
|
+
path.ext = "mkv"
|
41
|
+
path.ext.should == ".mkv"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "replaces filename" do
|
45
|
+
path = Path.new(__FILE__)
|
46
|
+
path.dir?.should == false
|
47
|
+
path.filename = nil
|
48
|
+
path.dir?.should == true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "fstats" do
|
52
|
+
path = Path.new(__FILE__)
|
53
|
+
|
54
|
+
path.exists?.should == true
|
55
|
+
path.dir?.should == false
|
56
|
+
path.file?.should == true
|
57
|
+
path.symlink?.should == false
|
58
|
+
path.mtime.class.should == Time
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: epitools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash: 27
|
5
4
|
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
8
|
-
- 3
|
9
7
|
- 4
|
10
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- epitron
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date:
|
17
|
+
date: 2011-01-06 00:00:00 -05:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -26,7 +25,6 @@ dependencies:
|
|
26
25
|
requirements:
|
27
26
|
- - ~>
|
28
27
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 7
|
30
28
|
segments:
|
31
29
|
- 2
|
32
30
|
- 2
|
@@ -42,7 +40,6 @@ dependencies:
|
|
42
40
|
requirements:
|
43
41
|
- - ~>
|
44
42
|
- !ruby/object:Gem::Version
|
45
|
-
hash: 23
|
46
43
|
segments:
|
47
44
|
- 1
|
48
45
|
- 0
|
@@ -58,7 +55,6 @@ dependencies:
|
|
58
55
|
requirements:
|
59
56
|
- - ">="
|
60
57
|
- !ruby/object:Gem::Version
|
61
|
-
hash: 3
|
62
58
|
segments:
|
63
59
|
- 0
|
64
60
|
version: "0"
|
@@ -92,6 +88,7 @@ files:
|
|
92
88
|
- lib/epitools/lcs.rb
|
93
89
|
- lib/epitools/metaclass.rb
|
94
90
|
- lib/epitools/niceprint.rb
|
91
|
+
- lib/epitools/path.rb
|
95
92
|
- lib/epitools/permutations.rb
|
96
93
|
- lib/epitools/pretty_backtrace.rb
|
97
94
|
- lib/epitools/progressbar.rb
|
@@ -106,6 +103,7 @@ files:
|
|
106
103
|
- spec/clitools_spec.rb
|
107
104
|
- spec/lcs_spec.rb
|
108
105
|
- spec/metaclass_spec.rb
|
106
|
+
- spec/path_spec.rb
|
109
107
|
- spec/permutations_spec.rb
|
110
108
|
- spec/rash_spec.rb
|
111
109
|
- spec/ratio_spec.rb
|
@@ -127,7 +125,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
127
125
|
requirements:
|
128
126
|
- - ">="
|
129
127
|
- !ruby/object:Gem::Version
|
130
|
-
hash: 3
|
131
128
|
segments:
|
132
129
|
- 0
|
133
130
|
version: "0"
|
@@ -136,7 +133,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
136
133
|
requirements:
|
137
134
|
- - ">="
|
138
135
|
- !ruby/object:Gem::Version
|
139
|
-
hash: 3
|
140
136
|
segments:
|
141
137
|
- 0
|
142
138
|
version: "0"
|
@@ -153,6 +149,7 @@ test_files:
|
|
153
149
|
- spec/clitools_spec.rb
|
154
150
|
- spec/lcs_spec.rb
|
155
151
|
- spec/metaclass_spec.rb
|
152
|
+
- spec/path_spec.rb
|
156
153
|
- spec/permutations_spec.rb
|
157
154
|
- spec/rash_spec.rb
|
158
155
|
- spec/ratio_spec.rb
|