Class: Cosmos::ScriptRunnerFrame

Inherits:
Qt::Widget show all
Defined in:
lib/cosmos/tools/script_runner/script_runner_frame.rb

Overview

class ScriptRunnerDialog

Constant Summary collapse

CMD_KEYWORDS =
%w(cmd cmd_no_range_check cmd_no_hazardous_check cmd_no_checks)
TLM_KEYWORDS =
%w(tlm tlm_raw tlm_formatted tlm_with_units limits_enabled? \
enable_limits disable_limits wait_tolerance wait_tolerance_raw \
check_tolerance check_tolerance_raw wait_check_tolerance \
wait_check_tolerance_raw)
SET_TLM_KEYWORDS =
%w(set_tlm set_tlm_raw)
CHECK_KEYWORDS =
%w(check check_raw wait wait_raw wait_check wait_check_raw)
@@instance =
nil
@@run_thread =
nil
@@breakpoints =
{}
@@step_mode =
false
@@line_delay =
0.1
@@instrumented_cache =
{}
@@file_cache =
{}
@@output_thread =
nil
@@limits_monitor_thread =
nil
@@pause_on_error =
true
@@monitor_limits =
false
@@pause_on_red =
false
@@show_backtrace =
false
@@error =
nil
@@output_sleeper =
Sleeper.new
@@limits_sleeper =
Sleeper.new
@@cancel_output =
false
@@cancel_limits =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, default_tab_text = 'Untitled') ⇒ ScriptRunnerFrame

Returns a new instance of ScriptRunnerFrame.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 124

def initialize(parent, default_tab_text = 'Untitled')
  super(parent)
  @default_tab_text = '  ' + default_tab_text + '  '
  # Keep track of whether this frame has been fully initialized
  @initialized = false
  @debug_frame = nil

  @layout = Qt::VBoxLayout.new
  @layout.setContentsMargins(0,0,0,0)

  # Add Realtime Button Bar
  @realtime_button_bar = RealtimeButtonBar.new(self)
  @realtime_button_bar.state = 'Stopped'
  @realtime_button_bar.start_callback = method(:handle_start_go_button)
  @realtime_button_bar.pause_callback = method(:handle_pause_retry_button)
  @realtime_button_bar.stop_callback  = method(:handle_stop_button)
  @layout.addWidget(@realtime_button_bar)

  # Create a splitter to hold the script text area and the script output text area.
  @splitter = Qt::Splitter.new(Qt::Vertical, self)
  @layout.addWidget(@splitter)
  @top_widget = Qt::Widget.new(@splitter)
  @top_widget.setContentsMargins(0,0,0,0)
  @top_frame = Qt::VBoxLayout.new(@top_widget)
  @top_frame.setContentsMargins(0,0,0,0)

  # Add Initial Text Window
  @script = create_ruby_editor()
  @script.connect(SIGNAL('modificationChanged(bool)')) do |changed|
    emit modificationChanged(changed)
  end
  @top_frame.addWidget(@script)

  # Set self as the gui window to allow prompts and other popups to appear
  set_cmd_tlm_gui_window(self)

  # Add change handlers
  connect(@script,
          SIGNAL('undoAvailable(bool)'),
          self,
          SLOT('undo_available(bool)'))

  # Add Output Text
  @bottom_frame = Qt::Widget.new
  @bottom_layout = Qt::VBoxLayout.new
  @bottom_layout.setContentsMargins(0,0,0,0)
  @bottom_layout_label = Qt::Label.new("Script Output:")
  @bottom_layout.addWidget(@bottom_layout_label)
  @output = Qt::PlainTextEdit.new
  @output.setReadOnly(true)
  @output.setMaximumBlockCount(100)
  @bottom_layout.addWidget(@output)
  @bottom_frame.setLayout(@bottom_layout)
  @splitter.addWidget(@bottom_frame)
  @splitter.setStretchFactor(0,10)
  @splitter.setStretchFactor(1,1)

  setLayout(@layout)

  # Configure Variables
  @line_offset = 0
  @filename = ''
  @output_io = StringIO.new('', 'r+')
  @output_io_mutex = Mutex.new
  @change_callback = nil
  @run_callback = nil
  @stop_callback = nil
  @error_callback = nil
  @pause_callback = nil
  @key_press_callback = nil
  @allow_start = true
  @continue_after_error = true
  @debug_text = nil
  @debug_history = []
  @debug_code_completion = nil
  @top_level_instrumented_cache = nil
  @output_time = Time.now
  initialize_variables()

  # Redirect $stdout and $stderr
  redirect_io()

  # Create Tabbook
  @tab_book = Qt::TabWidget.new
  @tab_book_shown = false

  @find_dialog = nil
  @replace_dialog = nil

  mark_breakpoints('')
end

Instance Attribute Details

#change_callbackObject

Returns the value of attribute change_callback.



88
89
90
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 88

def change_callback
  @change_callback
end

#continue_after_errorObject

Returns the value of attribute continue_after_error.



94
95
96
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 94

def continue_after_error
  @continue_after_error
end

#error_callbackObject

Returns the value of attribute error_callback.



91
92
93
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 91

def error_callback
  @error_callback
end

#exceptionsObject

Returns the value of attribute exceptions.



95
96
97
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 95

def exceptions
  @exceptions
end

#filenameObject

Returns the value of attribute filename.



93
94
95
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 93

def filename
  @filename
end

#inline_returnObject

Returns the value of attribute inline_return.



97
98
99
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 97

def inline_return
  @inline_return
end

#inline_return_paramsObject

Returns the value of attribute inline_return_params.



98
99
100
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 98

def inline_return_params
  @inline_return_params
end

#message_logObject (readonly)

Returns the value of attribute message_log.



99
100
101
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 99

def message_log
  @message_log
end

#pause_callbackObject

Returns the value of attribute pause_callback.



92
93
94
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 92

def pause_callback
  @pause_callback
end

#run_callbackObject

Returns the value of attribute run_callback.



89
90
91
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 89

def run_callback
  @run_callback
end

#scriptObject (readonly)

Returns the value of attribute script.



103
104
105
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 103

def script
  @script
end

#script_bindingObject

Returns the value of attribute script_binding.



96
97
98
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 96

def script_binding
  @script_binding
end

#script_classObject (readonly)

Returns the value of attribute script_class.



100
101
102
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 100

def script_class
  @script_class
end

#stdout_max_linesObject

Returns the value of attribute stdout_max_lines.



102
103
104
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 102

def stdout_max_lines
  @stdout_max_lines
end

#stop_callbackObject

Returns the value of attribute stop_callback.



90
91
92
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 90

def stop_callback
  @stop_callback
end

#top_level_instrumented_cacheObject (readonly)

Returns the value of attribute top_level_instrumented_cache.



101
102
103
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 101

def top_level_instrumented_cache
  @top_level_instrumented_cache
end

#use_instrumentationObject

Returns the value of attribute use_instrumentation.



87
88
89
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 87

def use_instrumentation
  @use_instrumentation
end

Class Method Details

.clear_breakpoint(filename, line_number) ⇒ Object



968
969
970
971
972
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 968

def self.clear_breakpoint(filename, line_number)
  filename = File.basename(filename)
  @@breakpoints[filename] ||= {}
  @@breakpoints[filename].delete(line_number) if @@breakpoints[filename][line_number]
end

.clear_breakpoints(filename = nil) ⇒ Object



974
975
976
977
978
979
980
981
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 974

def self.clear_breakpoints(filename = nil)
  filename = File.basename(filename) unless filename.nil?
  if filename == nil or filename.empty?
    @@breakpoints = {}
  else
    @@breakpoints[filename] = {}
  end
end

.instanceObject



304
305
306
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 304

def self.instance
  @@instance
end

.instance=(value) ⇒ Object



308
309
310
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 308

def self.instance=(value)
  @@instance = value
end

.instrument_script(text, filename, mark_private = false) ⇒ Object



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 466

def self.instrument_script(text, filename, mark_private = false)
  if filename and !filename.empty?
    @@file_cache[filename] = text.clone
  end

  ruby_lex_utils = RubyLexUtils.new
  instrumented_text = ''

  Qt.execute_in_main_thread(true) do
    window = Qt::CoreApplication.instance.activeWindow
    @cancel_instrumentation = false
    ProgressDialog.execute(window, # parent
                           "Instrumenting: #{File.basename(filename.to_s)}",
                           500,    # width
                           100,    # height
                           true,   # show overall progress
                           false,  # don't show step progress
                           false,  # don't show text
                           false,  # don't show done
                           true) do |progress_dialog| # show cancel
      progress_dialog.cancel_callback = lambda {|dialog| @cancel_instrumentation = true; [true, false]}
      progress_dialog.enable_cancel_button
      comments_removed_text = ruby_lex_utils.remove_comments(text)
      num_lines = comments_removed_text.num_lines.to_f
      num_lines = 1 if num_lines < 1
      instrumented_text =
        instrument_script_implementation(ruby_lex_utils,
                                         comments_removed_text,
                                         num_lines,
                                         progress_dialog,
                                         filename,
                                         mark_private)
      progress_dialog.close_done
    end
  end

  Kernel.raise StopScript if @cancel_instrumentation or ProgressDialog.canceled?
  instrumented_text
end

.instrument_script_implementation(ruby_lex_utils, comments_removed_text, num_lines, progress_dialog, filename, mark_private = false) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 506

def self.instrument_script_implementation(ruby_lex_utils,
                                          comments_removed_text,
                                          num_lines,
                                          progress_dialog,
                                          filename,
                                          mark_private = false)
  if mark_private
    instrumented_text = 'private; '
  else
    instrumented_text = ''
  end

  ruby_lex_utils.each_lexed_segment(comments_removed_text) do |segment, instrumentable, inside_begin, line_no|
    return nil if @cancel_instrumentation
    instrumented_line = ''
    if instrumentable
      # If not inside a begin block then create one to catch exceptions
      unless inside_begin
        instrumented_line << 'begin; '
      end

      # Add preline instrumentation
      instrumented_line << "ScriptRunnerFrame.instance.script_binding = binding(); if ScriptRunnerFrame.instance.inline_return then ScriptRunnerFrame.instance.inline_return = nil; return ScriptRunnerFrame.instance.inline_return_params; end; ScriptRunnerFrame.instance.pre_line_instrumentation('#{filename}', #{line_no}); "

      # Add the actual line
      instrumented_line << segment
      instrumented_line.chomp!

      # Add postline instrumentation
      instrumented_line << "; ScriptRunnerFrame.instance.post_line_instrumentation('#{filename}', #{line_no})"

      # Complete begin block to catch exceptions
      unless inside_begin
        instrumented_line << "; rescue Exception => eval_error; retry if ScriptRunnerFrame.instance.exception_instrumentation(eval_error, '#{filename}', #{line_no}); end"
      end

      instrumented_line << "\n"
    else
      unless segment =~ /^\s*end\s*$/ or segment =~ /^\s*when .*$/
        num_left_brackets = segment.count('{')
        num_right_brackets = segment.count('}')
        num_left_square_brackets = segment.count('[')
        num_right_square_brackets = segment.count(']')

        if (num_right_brackets > num_left_brackets) ||
          (num_right_square_brackets > num_left_square_brackets)
          instrumented_line = segment
        else
          instrumented_line = "ScriptRunnerFrame.instance.pre_line_instrumentation('#{filename}', #{line_no}); " + segment
        end
      else
        instrumented_line = segment
      end
    end

    instrumented_text << instrumented_line

    progress_dialog.set_overall_progress(line_no / num_lines) if progress_dialog and line_no
  end
  instrumented_text
end

.instrumented_cacheObject



335
336
337
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 335

def self.instrumented_cache
  @@instrumented_cache
end

.instrumented_cache=(value) ⇒ Object



339
340
341
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 339

def self.instrumented_cache=(value)
  @@instrumented_cache = value
end

.line_delayObject



327
328
329
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 327

def self.line_delay
  @@line_delay
end

.line_delay=(value) ⇒ Object



331
332
333
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 331

def self.line_delay=(value)
  @@line_delay = value
end

.monitor_limitsObject



351
352
353
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 351

def self.monitor_limits
  @@monitor_limits
end

.monitor_limits=(value) ⇒ Object



355
356
357
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 355

def self.monitor_limits=(value)
  @@monitor_limits = value
end

.pause_on_errorObject



343
344
345
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 343

def self.pause_on_error
  @@pause_on_error
end

.pause_on_error=(value) ⇒ Object



347
348
349
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 347

def self.pause_on_error=(value)
  @@pause_on_error = value
end

.pause_on_redObject



359
360
361
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 359

def self.pause_on_red
  @@pause_on_red
end

.pause_on_red=(value) ⇒ Object



363
364
365
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 363

def self.pause_on_red=(value)
  @@pause_on_red = value
end

.running?Boolean

Returns:

  • (Boolean)


399
400
401
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 399

def self.running?
  if @@run_thread then true else false end
end

.set_breakpoint(filename, line_number) ⇒ Object



962
963
964
965
966
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 962

def self.set_breakpoint(filename, line_number)
  filename = File.basename(filename)
  @@breakpoints[filename] ||= {}
  @@breakpoints[filename][line_number] = true
end

.show_backtraceObject



367
368
369
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 367

def self.show_backtrace
  @@show_backtrace
end

.show_backtrace=(value) ⇒ Object



371
372
373
374
375
376
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 371

def self.show_backtrace=(value)
  @@show_backtrace = value
  if @@show_backtrace and @@error
    puts Time.now.formatted + " (SCRIPTRUNNER): "  + "Most recent exception:\n" + @@error.formatted
  end
end

.step_modeObject



312
313
314
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 312

def self.step_mode
  @@step_mode
end

.step_mode=(value) ⇒ Object



316
317
318
319
320
321
322
323
324
325
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 316

def self.step_mode=(value)
  @@step_mode = value
  if self.instance
    if value
      self.instance.pause
    else
      self.instance.go
    end
  end
end

.stop!Object



427
428
429
430
431
432
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 427

def self.stop!
  if @@run_thread
    Cosmos.kill_thread(nil, @@run_thread)
    @@run_thread = nil
  end
end

Instance Method Details

#allow_start=(value) ⇒ Object



288
289
290
291
292
293
294
295
296
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 288

def allow_start=(value)
  @allow_start = value
  if @allow_start
    @realtime_button_bar.start_button.setEnabled(true)
  elsif not running?
    @realtime_button_bar.start_button.setEnabled(false)
    @script.setReadOnly(true)
  end
end

#breakpoint_cleared(line) ⇒ Object



655
656
657
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 655

def breakpoint_cleared(line)
  ScriptRunnerFrame.clear_breakpoint(current_tab_filename(), line)
end

#breakpoint_set(line) ⇒ Object

Implement the breakpoint callbacks from the RubyEditor



651
652
653
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 651

def breakpoint_set(line)
  ScriptRunnerFrame.set_breakpoint(current_tab_filename(), line)
end

#breakpoints_clearedObject



659
660
661
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 659

def breakpoints_cleared
  ScriptRunnerFrame.clear_breakpoints(current_tab_filename())
end

#clearObject



298
299
300
301
302
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 298

def clear
  self.set_text('')
  self.filename = ''
  self.modified = false
end

#comment_or_uncomment_linesObject



690
691
692
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 690

def comment_or_uncomment_lines
  @script.comment_or_uncomment_lines unless running?()
end

#copyObject



678
679
680
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 678

def copy
  @script.copy unless running?()
end

#create_ruby_editorObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 229

def create_ruby_editor
  # Add Initial Text Window
  script = RubyEditor.new(self)
  script.enable_breakpoints = true if @debug_frame
  connect(script,
          SIGNAL('breakpoint_set(int)'),
          self,
          SLOT('breakpoint_set(int)'))
  connect(script,
          SIGNAL('breakpoint_cleared(int)'),
          self,
          SLOT('breakpoint_cleared(int)'))
  connect(script,
          SIGNAL('breakpoints_cleared()'),
          self,
          SLOT('breakpoints_cleared()'))

  # Add right click menu
  script.setContextMenuPolicy(Qt::CustomContextMenu)
  connect(script,
          SIGNAL('customContextMenuRequested(const QPoint&)'),
          self,
          SLOT('context_menu(const QPoint&)'))

  return script
end

#current_backtraceObject



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 1050

def current_backtrace
  trace = []
  Qt.execute_in_main_thread(true) do
    if @@run_thread
      temp_trace = @@run_thread.backtrace
      cosmos_lib = Regexp.new(File.join(Cosmos::PATH, 'lib'))
      temp_trace.each do |line|
        next if line =~ cosmos_lib
        trace << line
      end
    end
  end
  trace
end

#current_tab_filenameObject



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 216

def current_tab_filename
  if @tab_book_shown
    filename = @tab_book.tabText(@tab_book.currentIndex)
    if filename == @default_tab_text
      return ''
    else
      return filename.strip
    end
  else
    return @filename
  end
end

#cutObject



674
675
676
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 674

def cut
  @script.cut unless running?()
end

#disable_retryObject



446
447
448
449
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 446

def disable_retry
  @realtime_button_bar.start_button.setText('Skip')
  @realtime_button_bar.pause_button.setDisabled(true)
end

#enable_retryObject



451
452
453
454
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 451

def enable_retry
  @realtime_button_bar.start_button.setText('Go')
  @realtime_button_bar.pause_button.setDisabled(false)
end

#exception_instrumentation(error, filename, line_number) ⇒ Object



607
608
609
610
611
612
613
614
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 607

def exception_instrumentation(error, filename, line_number)
  if error.class == StopScript || error.class == SkipTestCase || !@use_instrumentation
    Kernel.raise error
  else
    line_number = line_number + @line_offset if @active_script.object_id == @script.object_id
    handle_exception(error, false, filename, line_number)
  end
end

#goObject



407
408
409
410
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 407

def go
  @go    = true
  @pause = false unless @@step_mode
end

#go?Boolean

Returns:

  • (Boolean)


412
413
414
415
416
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 412

def go?
  temp = @go
  @go = false
  temp
end

#graceful_killObject



1125
1126
1127
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 1125

def graceful_kill
  # Just to avoid warning
end

#handle_output_io(filename = @current_filename, line_number = @current_line_number) ⇒ Object



1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 1069

def handle_output_io(filename = @current_filename, line_number = @current_line_number)
  @output_time = Time.now
  Qt.execute_in_main_thread(true) do
    if @output_io.string[-1..-1] == "\n"
      time_formatted = Time.now.formatted
      lines_to_write = ''
      out_line_number = line_number.to_s
      out_filename = File.basename(filename) if filename

      # Build each line to write
      string = @output_io.string.clone
      @output_io.string = @output_io.string[string.length..-1]
      line_count = 0
      string.each_line do |out_line|
        color = nil
        if out_line[0..1] == '20' and out_line[10] == ' ' and out_line[23..24] == ' ('
          line_to_write = out_line
        else
          if filename
            line_to_write = time_formatted + " (#{out_filename}:#{out_line_number}): "  + out_line
          else
            line_to_write = time_formatted + " (SCRIPTRUNNER): "  + out_line
            color = Cosmos::BLUE
          end
        end
        @output.add_formatted_text(line_to_write, color)
        lines_to_write << line_to_write

        line_count += 1
        if line_count > @stdout_max_lines
          out_line = "ERROR: Too much written to stdout.  Truncating output to #{@stdout_max_lines} lines.\n"
          if filename
            line_to_write = time_formatted + " (#{out_filename}:#{out_line_number}): "  + out_line
          else
            line_to_write = time_formatted + " (SCRIPTRUNNER): "  + out_line
          end
          @output.addText(line_to_write, Cosmos::RED)
          lines_to_write << line_to_write
          break
        end
      end # string.each_line

      # Actually add to the GUI
      @output.flush

      # Add to the message log
      if @filename.empty?
        @message_log ||= MessageLog.new("sr_untitled")
      else
        @message_log ||= MessageLog.new("sr_#{File.basename(@filename).split('.')[0]}")
      end
      @message_log.write(lines_to_write)
    end
  end
end

#hide_debugObject



943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 943

def hide_debug
  # Since we're disabling debug, clear the breakpoints and disable them
  ScriptRunnerFrame.clear_breakpoints()
  @script.clear_breakpoints
  @script.enable_breakpoints = false
  if @tab_book_shown
    if @tab_book.count > 0
      (0..(@tab_book.count - 1)).each do |index|
        @tab_book.tab(index).enable_breakpoints = false
      end
    end
  end
  # Remove the debug frame
  @bottom_frame.layout.takeAt(@bottom_frame.layout.count - 1) if @debug_frame
  @debug_frame.removeAll
  @debug_frame.dispose
  @debug_frame = nil
end

#key_press_callback=(callback) ⇒ Object



280
281
282
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 280

def key_press_callback=(callback)
  @script.keyPressCallback = callback
end

#mnemonic_check_selectionObject



769
770
771
772
773
774
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 769

def mnemonic_check_selection
  unless self.class.running?()
    selection = @script.selected_lines
    mnemonic_check_text(selection, @script.selection_start_line+1) if selection
  end
end

#mnemonic_check_text(text, start_line_number = 1) ⇒ Object



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 776

def mnemonic_check_text(text, start_line_number = 1)
  results = []
  line_number = start_line_number
  text.each_line do |line|
    if line =~ /\(/
      result = nil
      keyword = line.split('(')[0].split[-1]
      if CMD_KEYWORDS.include? keyword
        result = mnemonic_check_cmd_line(keyword, line_number, line)
      elsif TLM_KEYWORDS.include? keyword
        result = mnemonic_check_tlm_line(keyword, line_number, line)
      elsif SET_TLM_KEYWORDS.include? keyword
        result = mnemonic_check_set_tlm_line(keyword, line_number, line)
      elsif CHECK_KEYWORDS.include? keyword
        result = mnemonic_check_check_line(keyword, line_number, line)
      end
      results << result if result
    end
    line_number += 1
  end

  if results.empty?
    Qt::MessageBox.information(self,
                               'Mnemonic Check Successful',
                               'Mnemonic Check Found No Errors')
  else
    dialog = Qt::Dialog.new(self) do |box|
      box.setWindowTitle('Mnemonic Check Failed')
      text = Qt::PlainTextEdit.new
      text.setReadOnly(true)
      text.setPlainText(results.join("\n"))
      frame = Qt::VBoxLayout.new(box)
      ok = Qt::PushButton.new('Ok')
      ok.setDefault(true)
      ok.connect(SIGNAL('clicked(bool)')) { box.accept }
      frame.addWidget(text)
      frame.addWidget(ok)
    end
    dialog.exec
    dialog.dispose
  end
end

#modifiedObject



268
269
270
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 268

def modified
  @script.document.isModified()
end

#modified=(bool) ⇒ Object



272
273
274
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 272

def modified=(bool)
  @script.document.setModified(bool)
end

#pasteObject



682
683
684
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 682

def paste
  @script.paste unless running?()
end

#pauseObject



418
419
420
421
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 418

def pause
  @pause = true
  @go    = false
end

#pause?Boolean

Returns:

  • (Boolean)


423
424
425
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 423

def pause?
  @pause
end

#perform_breakpoint(filename, line_number) ⇒ Object



621
622
623
624
625
626
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 621

def perform_breakpoint(filename, line_number)
  mark_breakpoint()
  scriptrunner_puts "Hit Breakpoint at #{filename}:#{line_number}"
  handle_output_io(filename, line_number)
  wait_for_go_or_stop()
end

#perform_pauseObject



616
617
618
619
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 616

def perform_pause
  mark_paused()
  wait_for_go_or_stop()
end

#post_line_instrumentation(filename, line_number) ⇒ Object



600
601
602
603
604
605
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 600

def post_line_instrumentation(filename, line_number)
  if @use_instrumentation
    line_number = line_number + @line_offset if @active_script.object_id == @script.object_id
    handle_output_io(filename, line_number)
  end
end

#pre_line_instrumentation(filename, line_number) ⇒ Object



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 568

def pre_line_instrumentation(filename, line_number)
  @current_filename = filename
  @current_line_number = line_number
  if @use_instrumentation
    # Clear go
    @go = false

    # Handle stopping mid-script if necessary
    Kernel.raise StopScript if @stop

    # Handle needing to change tabs
    handle_potential_tab_change(filename)

    # Adjust line number for offset in main script
    line_number = line_number + @line_offset if @active_script.object_id == @script.object_id
    detail_string = nil
    if filename
      detail_string = File.basename(filename) << ':' << line_number.to_s
    end
    Logger.detail_string = detail_string

    # Highlight the line that is about to run
    Qt.execute_in_main_thread(true) {@active_script.highlight_line(line_number)}

    # Handle pausing the script
    handle_pause(filename, line_number)

    # Handle delay between lines
    handle_line_delay()
  end
end

#prompt_if_running_on_closeObject

Prompts the user that a script is running before they close the app



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 629

def prompt_if_running_on_close
  safe_to_continue = true
  if running?
    case Qt::MessageBox.warning(
      self,      # parent
      'Warning', # title
      'A Script is Running! Close Anyways?', # text
      Qt::MessageBox::Yes | Qt::MessageBox::No, # buttons
      Qt::MessageBox::No) # default button
    when Qt::MessageBox::Yes
      safe_to_continue = true
      ScriptRunnerFrame.stop!
    else
      safe_to_continue = false
    end
  end
  return safe_to_continue
end

#redoObject



670
671
672
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 670

def redo
  @script.redo unless running?()
end

#retry_neededObject



442
443
444
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 442

def retry_needed
  @retry_needed = true
end

#retry_needed?Boolean

Returns:

  • (Boolean)


438
439
440
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 438

def retry_needed?
  @retry_needed
end

#ruby_syntax_check_selectionObject



734
735
736
737
738
739
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 734

def ruby_syntax_check_selection
  unless self.class.running?()
    selection = @script.selected_lines
    ruby_syntax_check_text(selection) if selection
  end
end

#ruby_syntax_check_text(selection = nil) ⇒ Object



741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 741

def ruby_syntax_check_text(selection = nil)
  unless self.class.running?()
    selection = text() unless selection
    check_process = IO.popen("ruby -c -rubygems 2>&1", 'r+')
    check_process.write("require 'cosmos'; require 'cosmos/script'; " + selection)
    check_process.close_write
    results = check_process.gets
    check_process.close
    if results
      if results =~ /Syntax OK/
        Qt::MessageBox.information(self, 'Syntax Check Successful', results)
      else
        # Results is a string like this: ":2: syntax error ..."
        # Normally the procedure comes before the first colon but since we
        # are writing to the process this is blank so we throw it away
        _, line_no, error = results.split(':')
        Qt::MessageBox.warning(self,
                               'Syntax Check Failed',
                               "Error on line #{line_no}: #{error.strip}")
      end
    else
      Qt::MessageBox.critical(self,
                              'Syntax Check Exception',
                              'Ruby syntax check unexpectedly returned nil')
    end
  end
end

#runObject



456
457
458
459
460
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 456

def run
  unless self.class.running?()
    run_text(@script.toPlainText)
  end
end

#run_and_close_on_complete(text_binding = nil) ⇒ Object



462
463
464
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 462

def run_and_close_on_complete(text_binding = nil)
  run_text(@script.toPlainText, 0, text_binding, true)
end

#run_from_cursorObject



724
725
726
727
728
729
730
731
732
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 724

def run_from_cursor
  unless self.class.running?()
    line_number = @script.selection_start_line
    text = @script.toPlainText.split("\n")[line_number..-1].join("\n")
    scriptrunner_puts "Running script from line #{line_number}: #{File.basename(@filename)}"
    handle_output_io()
    run_text(text, line_number)
  end
end

#run_selectionObject

Implement Script functionality in the frame (run selection, run from cursor, etc



697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 697

def run_selection
  unless self.class.running?()
    selection = @script.selected_lines
    if selection
      start_line_number = @script.selection_start_line
      end_line_number   = @script.selection_end_line
      scriptrunner_puts "Running script lines #{start_line_number+1}-#{end_line_number+1}: #{File.basename(@filename)}"
      handle_output_io()
      run_text(selection, start_line_number)
    end
  end
end

#run_selection_while_pausedObject



710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 710

def run_selection_while_paused
  current_script = @tab_book.tab(@tab_book.currentIndex)
  selection = current_script.selected_lines
  if selection
    start_line_number = current_script.selection_start_line
    end_line_number   = current_script.selection_end_line
    scriptrunner_puts "Debug: Running selected lines #{start_line_number+1}-#{end_line_number+1}: #{@tab_book.tabText(@tab_book.currentIndex)}"
    handle_output_io()
    dialog = ScriptRunnerDialog.new(self, 'Executing Selected Lines While Paused')
    dialog.execute_text_and_close_on_complete(selection, @script_binding)
    handle_output_io()
  end
end

#running?Boolean

Returns:

  • (Boolean)


403
404
405
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 403

def running?
  if @@instance == self and ScriptRunnerFrame.running?() then true else false end
end

#scriptrunner_puts(string) ⇒ Object



1065
1066
1067
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 1065

def scriptrunner_puts(string)
  puts Time.now.formatted + " (SCRIPTRUNNER): "  + string
end

#select_allObject



686
687
688
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 686

def select_all
  @script.select_all unless running?()
end

#select_tab_and_destroy_tabs_after_index(index) ⇒ Object



983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 983

def select_tab_and_destroy_tabs_after_index(index)
  Qt.execute_in_main_thread(true) do
    if @tab_book_shown
      @tab_book.setCurrentIndex(index)
      @active_script = @tab_book.tab(@tab_book.currentIndex)

      first_to_remove = index + 1
      last_to_remove  = @call_stack.length - 1

      last_to_remove.downto(first_to_remove) do |tab_index|
        tab = @tab_book.tab(tab_index)
        @tab_book.removeTab(tab_index)
        tab.dispose
      end

      @call_stack = @call_stack[0..index]
      @current_file = @call_stack[index]
    end
  end
end

#set_text(text, filename = '') ⇒ Object



382
383
384
385
386
387
388
389
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 382

def set_text(text, filename = '')
  unless running?()
    @script.setPlainText(text)
    @script.stop_highlight
    @filename = filename
    mark_breakpoints(filename)
  end
end

#set_text_from_file(filename) ⇒ Object



391
392
393
394
395
396
397
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 391

def set_text_from_file(filename)
  unless running?()
    @@file_cache[filename] = nil
    load_file_into_script(filename)
    @filename = filename
  end
end

#setFocusObject



284
285
286
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 284

def setFocus
  @script.setFocus
end

#show_debugObject



842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 842

def show_debug
  unless @debug_frame
    @script.enable_breakpoints = true
    if @tab_book_shown
      if @tab_book.count > 0
        (0..(@tab_book.count - 1)).each do |index|
          @tab_book.tab(index).enable_breakpoints = true
        end
      end
    end

    @debug_frame = Qt::HBoxLayout.new
    @debug_frame.setContentsMargins(0,0,0,0)
    @debug_frame_label = Qt::Label.new("Debug:")
    @debug_frame.addWidget(@debug_frame_label)
    @debug_text = CompletionLineEdit.new(self)
    @debug_text.setFocus(Qt::OtherFocusReason)
    @debug_text.keyPressCallback = lambda { |event|
      case event.key
      when Qt::Key_Return, Qt::Key_Enter
        begin
          debug_text = @debug_text.toPlainText
          @debug_history.unshift(debug_text)
          @debug_history_index = 0
          @debug_text.setPlainText('')
          scriptrunner_puts "Debug: #{debug_text}"
          handle_output_io()
          if not running?
            # Capture STDOUT and STDERR
            $stdout.add_stream(@output_io)
            $stderr.add_stream(@output_io)
          end

          if @script_binding
            eval(debug_text, @script_binding, 'debug', 1)
          else
            Object.class_eval(debug_text, 'debug', 1)
          end
          handle_output_io()
        rescue Exception => error
          if error.class == DRb::DRbConnError
            Logger.error("Error Connecting to Command and Telemetry Server")
          else
            Logger.error(error.class.to_s.split('::')[-1] + ' : ' + error.message)
          end
          handle_output_io()
        ensure
          if not running?
            # Capture STDOUT and STDERR
            $stdout.remove_stream(@output_io)
            $stderr.remove_stream(@output_io)
          end
        end
      when Qt::Key_Up
        if @debug_history.length > 0
          @debug_text.setPlainText(@debug_history[@debug_history_index])
          @debug_history_index += 1
          if @debug_history_index == @debug_history.length
            @debug_history_index = @debug_history.length-1
          end
        end
      when Qt::Key_Down
        if @debug_history.length > 0
          @debug_text.setPlainText(@debug_history[@debug_history_index])
          @debug_history_index -= 1
          @debug_history_index = 0 if @debug_history_index < 0
        end
      when Qt::Key_Escape
        @debug_text.setPlainText("")
      end
    }

    @debug_frame.addWidget(@debug_text)

    @toggle_button = Qt::PushButton.new('Toggle Run/Step')
    @debug_frame.addWidget(@toggle_button)
    @toggle_button.connect(SIGNAL('clicked(bool)')) do
      if @@step_mode
        scriptrunner_puts "Debug: run_mode"
        handle_output_io()
        self.class.step_mode = false
      else
        scriptrunner_puts "Debug: step_mode"
        handle_output_io()
        self.class.step_mode = true
      end
    end

    @return_button = Qt::PushButton.new('Insert Return')
    @debug_frame.addWidget(@return_button)
    @return_button.connect(SIGNAL('clicked(bool)')) do
      scriptrunner_puts "Debug: insert_return(); ScriptRunnerFrame.instance.go()"
      handle_output_io()
      insert_return()
      go()
    end

    @bottom_frame.layout.addLayout(@debug_frame)
  end
end

#stop?Boolean

Returns:

  • (Boolean)


434
435
436
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 434

def stop?
  @stop
end

#stop_message_logObject



256
257
258
259
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 256

def stop_message_log
  @message_log.stop if @message_log
  @message_log = nil
end

#textObject



378
379
380
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 378

def text
  @script.toPlainText.gsub("\r", '')
end

#toggle_debug(debug = nil) ⇒ Object

Implement the debug capability



822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 822

def toggle_debug(debug = nil)
  if debug.nil?
    if @debug_frame
      hide_debug()
    else
      show_debug()
    end
  else
    if debug
      if !@debug_frame
        show_debug()
      end
    else
      if @debug_frame
        hide_debug()
      end
    end
  end
end

#toggle_disconnect(config_file) ⇒ Object



1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 1004

def toggle_disconnect(config_file)
  if get_cmd_tlm_disconnect
    set_cmd_tlm_disconnect(false)
    self.parent.setPalette(Cosmos::DEFAULT_PALETTE)
  else
    dialog = Qt::Dialog.new(self, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
    dialog.setWindowTitle(tr("Server Config File"))
    dialog_layout = Qt::VBoxLayout.new

    chooser = FileChooser.new(self, "Config File", config_file, 'Select',
                              File.join('config', 'tools', 'cmd_tlm_server', config_file))
    chooser.callback = lambda do |filename|
      chooser.filename = File.basename(filename)
    end
    dialog_layout.addWidget(chooser)

    button_layout = Qt::HBoxLayout.new
    ok = Qt::PushButton.new("Ok")
    ok.setDefault(true)
    ok.connect(SIGNAL('clicked()')) do
      dialog.accept()
    end
    button_layout.addWidget(ok)
    cancel = Qt::PushButton.new("Cancel")
    cancel.connect(SIGNAL('clicked()')) do
      dialog.reject()
    end
    button_layout.addWidget(cancel)
    dialog_layout.addLayout(button_layout)

    dialog.setLayout(dialog_layout)
    if dialog.exec == Qt::Dialog::Accepted
      config_file = chooser.filename
      self.parent.setPalette(Cosmos::RED_PALETTE)
      Splash.execute(self) do |splash|
        ConfigParser.splash = splash
        splash.message = "Initializing Command and Telemetry Server"
        set_cmd_tlm_disconnect(true, config_file)
        ConfigParser.splash = nil
      end
    end
    dialog.dispose
  end
  config_file
end

#undoObject

Implement edit functionality in the frame (cut, copy, paste, etc)



666
667
668
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 666

def undo
  @script.undo unless running?()
end

#undo_available(bool) ⇒ Object



276
277
278
# File 'lib/cosmos/tools/script_runner/script_runner_frame.rb', line 276

def undo_available(bool)
  emit undoAvailable(bool)
end