darkhelmet-darkext 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.9.5 2009-3-2
2
+
3
+ * add fibers
4
+ * add string#ends_with? and string#starts_with?
5
+ * make beagle not suck as much, but still suck a bit
6
+ * don't load sitemap_generator on include
7
+
1
8
  == 0.9.4 2009-2-27
2
9
 
3
10
  * Speed up beagle a bit...
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 9
3
- :patch: 4
3
+ :patch: 5
4
4
  :major: 0
@@ -1,11 +1,51 @@
1
- require 'darkext/io'
1
+ require 'colored'
2
+ require 'darkext/fiber'
3
+ require 'darkext/string'
2
4
  require 'darkext/symbol'
3
- require 'extensions/string'
5
+
6
+ "Beagle.query is not secure! Use with caution".red.printn
4
7
 
5
8
  module Beagle
6
- class BeagleError < RuntimeError
7
- def initialize(msg)
8
- super(msg)
9
+ class BeagleError < RuntimeError; end
10
+
11
+ class BeagleResultsHelper
12
+ def initialize(io)
13
+ @io = io
14
+ @fiber = Fiber.new do
15
+ while !@io.eof?
16
+ result = Hash.new
17
+ loop do
18
+ line = @io.gets
19
+ break if line.nil?
20
+ if !line.include?('=') || line.starts_with?(' Snip')
21
+ parts = line.split(':')
22
+ k = parts.shift.strip
23
+ break if result.keys.include?(k)
24
+ v = parts.join(':').strip
25
+ result[k] = v unless k.empty? || v.empty?
26
+ elsif line.include?('=')
27
+ k,v = line.split('=')
28
+ k = k.split(':').last.strip
29
+ break if result.keys.include?(k)
30
+ v = v.gsub("'",'').strip
31
+ result[k] = v unless k.empty? || v.empty?
32
+ end
33
+ end
34
+ Fiber.yield(result)
35
+ end
36
+ end
37
+ end
38
+
39
+ def next
40
+ begin
41
+ return @fiber.resume
42
+ rescue FiberError
43
+ return nil
44
+ end
45
+ end
46
+
47
+ def close
48
+ @io.close
9
49
  end
10
50
  end
11
51
 
@@ -27,17 +67,17 @@ module Beagle
27
67
  system('beagle-shutdown')
28
68
  end
29
69
 
30
- def self.query(query, verbose = false, max_hits = 40)
70
+ def self.query(query, max_hits = 100)
31
71
  raise BeagleError, "Beagle.home (BEAGLE_HOME) not set!" if home.nil?
32
72
  args = Array.new
33
73
  args << 'beagle-query'
34
- args << '--verbose' if verbose
74
+ args << '--verbose'
35
75
  args << '--max-hits'
36
76
  args << max_hits.to_s
37
77
  args << query
38
- output = DarkIO.capture_output(:stderr => false) { system(*args) }
39
- parser = verbose ? verbose_parser : regular_parser
40
- parser.call(output)
78
+ cmd = args.join(' ')
79
+ cmd.printn
80
+ return BeagleResultsHelper.new(IO.popen(cmd))
41
81
  end
42
82
 
43
83
  def self.running?
@@ -50,37 +90,4 @@ module Beagle
50
90
  raise BeagleError, "Beagle.home (BEAGLE_HOME) not set!" if home.nil?
51
91
  DarkIO.capture_output(:stderr => false) { system('beagle-info','--status') }.strip
52
92
  end
53
-
54
- private
55
-
56
- def self.verbose_parser
57
- lambda do |output|
58
- sections = output.strip.split("\n\n")
59
- results = sections.map do |section|
60
- section_lines = section.split("\n")
61
- section_hash = Hash.new
62
- section_lines.each do |line|
63
- if !line.include?('=') || line.starts_with?(' Snip')
64
- parts = line.split(':')
65
- k = parts.shift
66
- v = parts.join(':')
67
- section_hash[k.strip] = v.strip
68
- elsif line.include?('=')
69
- k,v = line.split('=')
70
- k = k.split(':').last
71
- v.gsub!("'",'')
72
- section_hash[k.strip] = v.strip
73
- end
74
- end
75
- section_hash
76
- end
77
- results.sort { |l,r| l['Score'].to_f <=> r['Score'].to_f }
78
- end
79
- end
80
-
81
- def self.regular_parser
82
- lambda do |output|
83
- output.split("\n").map(&:strip)
84
- end
85
- end
86
93
  end
@@ -0,0 +1,48 @@
1
+ # Poor Man's Fiber (API compatible Thread based Fiber implementation for Ruby 1.8)
2
+ # (c) 2008 Aman Gupta (tmm1)
3
+
4
+ unless defined? Fiber
5
+ require 'thread'
6
+
7
+ class FiberError < StandardError; end
8
+
9
+ class Fiber
10
+ def initialize
11
+ raise ArgumentError, 'new Fiber requires a block' unless block_given?
12
+
13
+ @yield = Queue.new
14
+ @resume = Queue.new
15
+
16
+ @thread = Thread.new{ @yield.push [ *yield(*@resume.pop) ] }
17
+ @thread.abort_on_exception = true
18
+ @thread[:fiber] = self
19
+ end
20
+ attr_reader :thread
21
+
22
+ def resume *args
23
+ raise FiberError, 'dead fiber called' unless @thread.alive?
24
+ @resume.push(args)
25
+ result = @yield.pop
26
+ result.size > 1 ? result : result.first
27
+ end
28
+
29
+ def yield *args
30
+ @yield.push(args)
31
+ result = @resume.pop
32
+ result.size > 1 ? result : result.first
33
+ end
34
+
35
+ def self.yield *args
36
+ raise FiberError, "can't yield from root fiber" unless fiber = Thread.current[:fiber]
37
+ fiber.yield(*args)
38
+ end
39
+
40
+ def self.current
41
+ Thread.current[:fiber] or raise FiberError, 'not inside a fiber'
42
+ end
43
+
44
+ def inspect
45
+ "#<#{self.class}:0x#{self.object_id.to_s(16)}>"
46
+ end
47
+ end
48
+ end
data/lib/darkext/io.rb CHANGED
@@ -3,7 +3,7 @@ require 'darkext/hash'
3
3
  module DarkIO
4
4
  # Runs a block and captures the output it generates
5
5
  def self.capture_output(opts = { }) # yield e
6
- opts.with_defaults!(:stdout => true, :stderr => true)
6
+ opts.with_defaults!(:stdout => true, :stderr => false)
7
7
  cout,cerr = opts[:stdout],opts[:stderr]
8
8
 
9
9
  yield and return if !cout && !cerr
@@ -51,5 +51,15 @@ class String
51
51
  self.downcase == 'false'
52
52
  end
53
53
 
54
+ def starts_with?(str)
55
+ str = str.to_s
56
+ str == self[0, str.length]
57
+ end
58
+
59
+ def ends_with?(str)
60
+ str = str.to_s
61
+ str == self[-str.length, str.length]
62
+ end
63
+
54
64
  alias :/ :split
55
65
  end
data/lib/darkext.rb CHANGED
@@ -2,5 +2,6 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  Dir[File.join(File.join(File.dirname(__FILE__), 'darkext'), '*.rb')].sort.each do |lib|
5
- require lib unless lib.include?('sinatra') # don't include sinatra stuff by defaults
5
+ # don't include sinatra stuff by defaults
6
+ require lib unless lib.include?('sinatra') || lib.include?('sitemap')
6
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darkhelmet-darkext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Huckstep
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-27 00:00:00 -08:00
12
+ date: 2009-03-02 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -29,6 +29,7 @@ files:
29
29
  - lib/darkext/numeric.rb
30
30
  - lib/darkext/string.rb
31
31
  - lib/darkext/io.rb
32
+ - lib/darkext/fiber.rb
32
33
  - lib/darkext/symbol.rb
33
34
  - lib/darkext/statistics.rb
34
35
  - lib/darkext/sinatra.rb