skylight 0.3.7 → 0.3.8

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.
@@ -0,0 +1,9 @@
1
+ module Skylight
2
+ module Util
3
+ module Conversions
4
+ def secs_to_nanos(secs)
5
+ secs * 1_000_000_000
6
+ end
7
+ end
8
+ end
9
+ end
@@ -20,6 +20,7 @@ module Skylight
20
20
  include Logging
21
21
 
22
22
  attr_accessor :authentication, :config
23
+ attr_reader :host, :port
23
24
 
24
25
  def initialize(config, service = :report)
25
26
  @config = config
@@ -93,7 +94,8 @@ module Skylight
93
94
 
94
95
  http = Net::HTTP.new(@host, @port, @proxy_addr, @proxy_port, @proxy_user, @proxy_pass)
95
96
 
96
- # Default is 60, but that's also the Heroku boot timeout and we don't want to slow boot
97
+ # Default is 60, but we don't want to wait that long.
98
+ # This path is also used on app boot and Heroku will timeout at 60
97
99
  http.read_timeout = 15
98
100
 
99
101
  if @ssl
@@ -113,9 +115,9 @@ module Skylight
113
115
  Response.new(res.code.to_i, res, res.body)
114
116
  end
115
117
  rescue Exception => e
116
- error "http %s failed; error=%s; msg=%s", req.method, e.class, e.message
118
+ error "http %s %s failed; error=%s; msg=%s", req.method, req.path, e.class, e.message
117
119
  t { e.backtrace.join("\n") }
118
- ErrorResponse.new(req.method, e)
120
+ ErrorResponse.new(req, e)
119
121
  end
120
122
 
121
123
  class Response
@@ -167,7 +169,7 @@ module Skylight
167
169
  end
168
170
  end
169
171
 
170
- class ErrorResponse < Struct.new(:request_method, :exception)
172
+ class ErrorResponse < Struct.new(:request, :exception)
171
173
  def status
172
174
  nil
173
175
  end
@@ -169,7 +169,7 @@ module Skylight
169
169
  end
170
170
 
171
171
  def source_uri
172
- "#{@source}/#{@version}/libskylight.#{@arch}.a.gz"
172
+ "#{@source}/#{@version}/libskylight.#{@version}.#{@arch}.a.gz"
173
173
  end
174
174
 
175
175
  def deconstruct_uri(uri)
@@ -7,13 +7,17 @@ module Skylight
7
7
 
8
8
  include Util::Logging
9
9
 
10
+ attr_reader :queue_depth_metric
11
+
10
12
  def initialize(size, timeout = 0.1, &blk)
11
- @pid = Process.pid
12
- @thread = nil
13
- @size = size
14
- @lock = Mutex.new
13
+ @pid = Process.pid
14
+ @thread = nil
15
+ @size = size
16
+ @lock = Mutex.new
15
17
  @timeout = timeout
16
- @blk = blk
18
+ @blk = blk
19
+
20
+ @queue_depth_metric = build_queue_depth_metric
17
21
  end
18
22
 
19
23
  def submit(msg, pid = Process.pid)
@@ -84,6 +88,8 @@ module Skylight
84
88
  @queue = Util::Queue.new(@size)
85
89
  @thread = Thread.new do
86
90
  begin
91
+ prepare
92
+
87
93
  unless work
88
94
  @queue = nil
89
95
  end
@@ -147,9 +153,19 @@ module Skylight
147
153
  @blk.call(msg)
148
154
  end
149
155
 
156
+ def prepare
157
+ end
158
+
150
159
  def finish
151
160
  end
152
161
 
162
+ def build_queue_depth_metric
163
+ lambda do
164
+ q = @queue
165
+ q ? @queue.length : 0
166
+ end
167
+ end
168
+
153
169
  end
154
170
  end
155
171
  end
@@ -28,7 +28,7 @@ require "highline/style"
28
28
  #
29
29
  class HighLine
30
30
  # The version of the installed library.
31
- VERSION = "1.6.18".freeze
31
+ VERSION = "1.6.21".freeze
32
32
 
33
33
  # An internal HighLine error. User code does not need to trap this.
34
34
  class QuestionError < StandardError
@@ -255,6 +255,7 @@ class HighLine
255
255
  # Also, JRuby-1.7's ConsoleReader.readLine() needs to be passed the prompt
256
256
  # to handle line editing properly.
257
257
  say(@question) unless ((JRUBY or @question.readline) and @question.echo == true)
258
+
258
259
  begin
259
260
  @answer = @question.answer_or_default(get_response)
260
261
  unless @question.valid_answer?(@answer)
@@ -613,22 +614,10 @@ class HighLine
613
614
  # and the HighLine.color() method.
614
615
  #
615
616
  def say( statement )
616
- statement = statement.to_str
617
+ statement = format_statement(statement)
617
618
  return unless statement.length > 0
618
619
 
619
- # Allow non-ascii menu prompts in ruby > 1.9.2. ERB eval the menu statement
620
- # with the environment's default encoding(usually utf8)
621
- statement.force_encoding(Encoding.default_external) if defined?(Encoding) && Encoding.default_external
622
-
623
- template = ERB.new(statement, nil, "%")
624
- statement = template.result(binding)
625
-
626
- statement = wrap(statement) unless @wrap_at.nil?
627
- statement = page_print(statement) unless @page_at.nil?
628
-
629
- statement = statement.gsub(/\n(?!$)/,"\n#{indentation}") if @multi_indent
630
-
631
- # Don't add a newline if statement ends with whitespace, OR
620
+ # Don't add a newline if statement ends with whitespace, OR
632
621
  # if statement ends with whitespace before a color escape code.
633
622
  if /[ \t](\e\[\d+(;\d+)*m)?\Z/ =~ statement
634
623
  @output.print(indentation+statement)
@@ -672,10 +661,16 @@ class HighLine
672
661
  @indent_level += increase
673
662
  multi = @multi_indent
674
663
  @multi_indent = multiline unless multiline.nil?
675
- if block_given?
676
- yield self
677
- else
678
- say(statement)
664
+ begin
665
+ if block_given?
666
+ yield self
667
+ else
668
+ say(statement)
669
+ end
670
+ rescue
671
+ @multi_indent = multi
672
+ @indent_level -= increase
673
+ raise
679
674
  end
680
675
  @multi_indent = multi
681
676
  @indent_level -= increase
@@ -712,6 +707,25 @@ class HighLine
712
707
 
713
708
  private
714
709
 
710
+ def format_statement statement
711
+ statement = statement.dup.to_str
712
+ return statement unless statement.length > 0
713
+
714
+ # Allow non-ascii menu prompts in ruby > 1.9.2. ERB eval the menu statement
715
+ # with the environment's default encoding(usually utf8)
716
+ statement.force_encoding(Encoding.default_external) if defined?(Encoding) && Encoding.default_external
717
+
718
+ template = ERB.new(statement, nil, "%")
719
+ statement = template.result(binding)
720
+
721
+ statement = wrap(statement) unless @wrap_at.nil?
722
+ statement = page_print(statement) unless @page_at.nil?
723
+
724
+ statement = statement.gsub(/\n(?!$)/,"\n#{indentation}") if @multi_indent
725
+
726
+ statement
727
+ end
728
+
715
729
  #
716
730
  # A helper method for sending the output stream and error and repeat
717
731
  # of the question.
@@ -839,7 +853,11 @@ class HighLine
839
853
  answer
840
854
  else
841
855
  if JRUBY
842
- raw_answer = @java_console.readLine(@question.question, nil)
856
+ statement = format_statement(@question)
857
+ raw_answer = @java_console.readLine(statement, nil)
858
+
859
+ raise EOFError, "The input stream is exhausted." if raw_answer.nil? and
860
+ @@track_eof
843
861
  else
844
862
  raise EOFError, "The input stream is exhausted." if @@track_eof and
845
863
  @input.eof?
@@ -917,6 +935,10 @@ class HighLine
917
935
  @question.change_case(@question.remove_whitespace(line))
918
936
  end
919
937
  else
938
+ if JRUBY #prompt has not been shown
939
+ say @question
940
+ end
941
+
920
942
  raw_no_echo_mode
921
943
  begin
922
944
  if @question.character == :getc
@@ -368,31 +368,14 @@ class HighLine
368
368
 
369
369
  #
370
370
  # This method will update the intelligent responses to account for
371
- # Menu specific differences. This overrides the work done by
372
- # Question.build_responses().
371
+ # Menu specific differences. Calls the superclass' (Question's)
372
+ # build_responses method, overriding its default arguments to specify
373
+ # 'options' will be used to populate choice lists, and that
374
+ # the newly built hash will predominate over the preexisting hash
375
+ # for any keys that are the same.
373
376
  #
374
377
  def update_responses( )
375
- append_default unless default.nil?
376
- @responses = @responses.merge(
377
- :ambiguous_completion =>
378
- "Ambiguous choice. " +
379
- "Please choose one of #{options.inspect}.",
380
- :ask_on_error =>
381
- "? ",
382
- :invalid_type =>
383
- "You must enter a valid #{options}.",
384
- :no_completion =>
385
- "You must choose one of " +
386
- "#{options.inspect}.",
387
- :not_in_range =>
388
- "Your answer isn't within the expected range " +
389
- "(#{expected_range}).",
390
- :mismatch =>
391
- "Your entries didn't match.",
392
- :not_valid =>
393
- "Your answer isn't valid (must match " +
394
- "#{@validate.inspect})."
395
- )
378
+ build_responses(options, true)
396
379
  end
397
380
  end
398
381
  end
@@ -225,22 +225,29 @@ class HighLine
225
225
  #
226
226
  # Called late in the initialization process to build intelligent
227
227
  # responses based on the details of this Question object.
228
+ # Also used by Menu#update_responses.
228
229
  #
229
- def build_responses( )
230
- ### WARNING: This code is quasi-duplicated in ###
231
- ### Menu.update_responses(). Check there too when ###
232
- ### making changes! ###
230
+ def build_responses(message_source = answer_type, new_hash_wins = false)
231
+
233
232
  append_default unless default.nil?
234
- @responses = { :ambiguous_completion =>
235
- "Ambiguous choice. " +
236
- "Please choose one of #{@answer_type.inspect}.",
233
+
234
+ choice_error_str_func = lambda do
235
+ message_source.is_a?(Array) \
236
+ ? '[' + message_source.map { |s| "#{s}" }.join(', ') + ']' \
237
+ : message_source.inspect
238
+ end
239
+
240
+ old_hash = @responses
241
+
242
+ new_hash = { :ambiguous_completion =>
243
+ "Ambiguous choice. Please choose one of " +
244
+ choice_error_str_func.call + '.',
237
245
  :ask_on_error =>
238
246
  "? ",
239
247
  :invalid_type =>
240
- "You must enter a valid #{@answer_type}.",
248
+ "You must enter a valid #{message_source}.",
241
249
  :no_completion =>
242
- "You must choose one of " +
243
- "#{@answer_type.inspect}.",
250
+ "You must choose one of " + choice_error_str_func.call + '.',
244
251
  :not_in_range =>
245
252
  "Your answer isn't within the expected range " +
246
253
  "(#{expected_range}).",
@@ -248,10 +255,9 @@ class HighLine
248
255
  "Your entries didn't match.",
249
256
  :not_valid =>
250
257
  "Your answer isn't valid (must match " +
251
- "#{@validate.inspect})." }.merge(@responses)
252
- ### WARNING: This code is quasi-duplicated in ###
253
- ### Menu.update_responses(). Check there too when ###
254
- ### making changes! ###
258
+ "#{@validate.inspect})." }
259
+
260
+ @responses = new_hash_wins ? old_hash.merge(new_hash) : new_hash.merge(old_hash)
255
261
  end
256
262
 
257
263
  #
@@ -31,85 +31,65 @@ class HighLine
31
31
  module StringExtensions
32
32
  def self.included(base)
33
33
  HighLine::COLORS.each do |color|
34
+ color = color.downcase
34
35
  base.class_eval <<-END
35
- if public_instance_methods.map { |m| m.to_s }.
36
- include? "#{color.downcase}"
37
- undef :#{color.downcase}
38
- end
39
- def #{color.downcase}
40
- color(:#{color.downcase})
36
+ undef :#{color} if method_defined? :#{color}
37
+ def #{color}
38
+ color(:#{color})
41
39
  end
42
40
  END
41
+
43
42
  base.class_eval <<-END
44
- if public_instance_methods.map { |m| m.to_s }.
45
- include? "on_#{color.downcase}"
46
- undef :on_#{color.downcase}
47
- end
48
- def on_#{color.downcase}
49
- on(:#{color.downcase})
43
+ undef :on_#{color} if method_defined? :on_#{color}
44
+ def on_#{color}
45
+ on(:#{color})
50
46
  end
51
47
  END
52
48
  HighLine::STYLES.each do |style|
49
+ style = style.downcase
53
50
  base.class_eval <<-END
54
- if public_instance_methods.map { |m| m.to_s }.
55
- include? "#{style.downcase}"
56
- undef :#{style.downcase}
57
- end
58
- def #{style.downcase}
59
- color(:#{style.downcase})
51
+ undef :#{style} if method_defined? :#{style}
52
+ def #{style}
53
+ color(:#{style})
60
54
  end
61
55
  END
62
56
  end
63
57
  end
64
58
 
65
59
  base.class_eval do
66
- if public_instance_methods.map { |m| m.to_s }.include? "color"
67
- undef :color
68
- end
69
- if public_instance_methods.map { |m| m.to_s }.include? "foreground"
70
- undef :foreground
71
- end
60
+ undef :color if method_defined? :color
61
+ undef :foreground if method_defined? :foreground
72
62
  def color(*args)
73
63
  self.class.new(HighLine.color(self, *args))
74
64
  end
75
65
  alias_method :foreground, :color
76
66
 
77
- if public_instance_methods.map { |m| m.to_s }.include? "on"
78
- undef :on
79
- end
67
+ undef :on if method_defined? :on
80
68
  def on(arg)
81
69
  color(('on_' + arg.to_s).to_sym)
82
70
  end
83
71
 
84
- if public_instance_methods.map { |m| m.to_s }.include? "uncolor"
85
- undef :uncolor
86
- end
72
+ undef :uncolor if method_defined? :uncolor
87
73
  def uncolor
88
74
  self.class.new(HighLine.uncolor(self))
89
75
  end
90
76
 
91
- if public_instance_methods.map { |m| m.to_s }.include? "rgb"
92
- undef :rgb
93
- end
77
+ undef :rgb if method_defined? :rgb
94
78
  def rgb(*colors)
95
79
  color_code = colors.map{|color| color.is_a?(Numeric) ? '%02x'%color : color.to_s}.join
96
80
  raise "Bad RGB color #{colors.inspect}" unless color_code =~ /^[a-fA-F0-9]{6}/
97
81
  color("rgb_#{color_code}".to_sym)
98
82
  end
99
83
 
100
- if public_instance_methods.map { |m| m.to_s }.include? "on_rgb"
101
- undef :on_rgb
102
- end
84
+ undef :on_rgb if method_defined? :on_rgb
103
85
  def on_rgb(*colors)
104
86
  color_code = colors.map{|color| color.is_a?(Numeric) ? '%02x'%color : color.to_s}.join
105
87
  raise "Bad RGB color #{colors.inspect}" unless color_code =~ /^[a-fA-F0-9]{6}/
106
88
  color("on_rgb_#{color_code}".to_sym)
107
89
  end
108
90
 
109
- if public_instance_methods.map { |m| m.to_s }.include? "method_missing"
110
- undef :method_missing
111
- end
112
91
  # TODO Chain existing method_missing
92
+ undef :method_missing if method_defined? :method_missing
113
93
  def method_missing(method, *args, &blk)
114
94
  if method.to_s =~ /^(on_)?rgb_([0-9a-fA-F]{6})$/
115
95
  color(method)
@@ -14,10 +14,14 @@ class HighLine
14
14
  if JRUBY
15
15
  def initialize_system_extensions
16
16
  require 'java'
17
+ require 'readline'
17
18
  if JRUBY_VERSION =~ /^1.7/
18
19
  java_import 'jline.console.ConsoleReader'
19
20
 
20
- @java_console = ConsoleReader.new(@input.to_inputstream, @output.to_outputstream)
21
+ input = @input && @input.to_inputstream
22
+ output = @output && @output.to_outputstream
23
+
24
+ @java_console = ConsoleReader.new(input, output)
21
25
  @java_console.set_history_enabled(false)
22
26
  @java_console.set_bell_enabled(true)
23
27
  @java_console.set_pagination_enabled(false)
@@ -76,7 +80,13 @@ class HighLine
76
80
  require "dl/import"
77
81
 
78
82
  module WinAPI
79
- extend DL::Importer rescue DL::Importable
83
+ if defined?(DL::Importer)
84
+ # Ruby 1.9
85
+ extend DL::Importer
86
+ else
87
+ # Ruby 1.8
88
+ extend DL::Importable
89
+ end
80
90
  begin
81
91
  dlload "msvcrt", "kernel32"
82
92
  rescue DL::DLError
@@ -85,6 +95,16 @@ class HighLine
85
95
  extern "unsigned long _getch()"
86
96
  extern "unsigned long GetConsoleScreenBufferInfo(unsigned long, void*)"
87
97
  extern "unsigned long GetStdHandle(unsigned long)"
98
+
99
+ # Ruby 1.8 DL::Importable.import does mname[0,1].downcase so FooBar becomes fooBar
100
+ if defined?(getConsoleScreenBufferInfo)
101
+ alias_method :GetConsoleScreenBufferInfo, :getConsoleScreenBufferInfo
102
+ module_function :GetConsoleScreenBufferInfo
103
+ end
104
+ if defined?(getStdHandle)
105
+ alias_method :GetStdHandle, :getStdHandle
106
+ module_function :GetStdHandle
107
+ end
88
108
  end
89
109
  end
90
110
 
@@ -140,7 +160,11 @@ class HighLine
140
160
  CHARACTER_MODE = "jline" # For Debugging purposes only.
141
161
 
142
162
  def terminal_size
143
- [ @java_terminal.getTerminalWidth, @java_terminal.getTerminalHeight ]
163
+ if JRUBY_VERSION =~ /^1.7/
164
+ [ @java_terminal.get_width, @java_terminal.get_height ]
165
+ else
166
+ [ @java_terminal.getTerminalWidth, @java_terminal.getTerminalHeight ]
167
+ end
144
168
  end
145
169
 
146
170
  def raw_no_echo_mode
@@ -172,7 +196,7 @@ class HighLine
172
196
  size = [80, 40]
173
197
  FFI::NCurses.initscr
174
198
  begin
175
- size = FFI::NCurses.getmaxyx(stdscr).reverse
199
+ size = FFI::NCurses.getmaxyx(FFI::NCurses.stdscr).reverse
176
200
  ensure
177
201
  FFI::NCurses.endwin
178
202
  end