Class: Cosmos::SystemConfigDialog

Inherits:
Qt::Dialog
  • Object
show all
Defined in:
lib/cosmos/tools/config_editor/system_config_dialog.rb

Overview

Creates a dialog asking to create a new system configuration.

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ SystemConfigDialog

Returns a new instance of SystemConfigDialog.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cosmos/tools/config_editor/system_config_dialog.rb', line 17

def initialize(parent)
  super(parent) # MUST BE FIRST
  @parent = parent
  Cosmos.load_cosmos_icon

  self.window_title = 'Create System Configuration'
  layout = Qt::VBoxLayout.new
  self.layout = layout

  description = Qt::Label.new("Creating a new COSMOS system configuration allows you to reuse "\
    "an existing COSMOS configuration but include different targets and/or change configuration details.")
  description.setWordWrap(true)
  layout.addWidget(description)

  select = Qt::Label.new("Select an existing system.txt file to base the new configuration on:")
  select.setWordWrap(true)
  layout.addWidget(select)
  @system_combo = Qt::ComboBox.new
  Dir[File.join(::Cosmos::USERPATH, 'config', 'system', '*.txt')].each do |system|
    @system_combo.addItem(File.basename(system))
  end
  layout.addWidget(@system_combo)
  layout.addSpacing(10)
  
  name_label = Qt::Label.new("Enter a name for the new system configuration that is descriptive "\
    " but relatively short. For example, 'EMI', 'SW Test', etc:")
  name_label.setWordWrap(true)
  layout.addWidget(name_label)
  @system_name = Qt::LineEdit.new
  layout.addWidget(@system_name)
  layout.addSpacing(10)

  info = Qt::Label.new("This action will create a new COSMOS system.txt, "\
    "cmd_tlm_server.txt, launcher.txt, and Windows Batch file appended with the specified name. "\
    "For example, system_emi.txt, system_sw_test.txt, launcher_emi.txt, launcher_sw_test.txt, etc.")
  info.setWordWrap(true)
  layout.addWidget(info)

  ok_button = Qt::PushButton.new('Ok')
  connect(ok_button, SIGNAL('clicked()'), self, SLOT('accept()'))
  cancel_button = Qt::PushButton.new('Cancel')
  connect(cancel_button, SIGNAL('clicked()'), self, SLOT('reject()'))

  hlayout = Qt::HBoxLayout.new
  hlayout.addWidget(ok_button, 0, Qt::AlignLeft)
  hlayout.addWidget(cancel_button, 0, Qt::AlignRight)
  layout.addLayout(hlayout)

  resize(500, 300)

  self.show()
  self.raise()
  if self.exec() == Qt::Dialog::Accepted
    build_system_config()
  end
  self.dispose()
end

Instance Method Details

#build_system_configObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cosmos/tools/config_editor/system_config_dialog.rb', line 75

def build_system_config
new_suffix = @system_name.text.downcase.gsub(' ','_')
existing_system = @system_combo.text
# Look for system_xxx.txt where we find the 'xxx' as the existing suffix
if existing = existing_system.scan(/.*?_(.*)\.txt/)[0]
  existing_suffix = existing[0]
else
  existing_suffix = nil
end

# First determine all the new files and make sure they don't already exist
new_system = File.join(Cosmos::USERPATH, 'config', 'system', "system_#{new_suffix}.txt")
return if file_exist?(new_system)
cmd_tlm_server_path = File.join('config', 'tools', 'cmd_tlm_server')
new_cmd_tlm_server = File.join(Cosmos::USERPATH, cmd_tlm_server_path, "cmd_tlm_server_#{new_suffix}.txt")
return if file_exist?(new_cmd_tlm_server)
launcher_path = File.join('config', 'tools', 'launcher')
new_launcher = File.join(Cosmos::USERPATH, launcher_path, "launcher_#{new_suffix}.txt")
return if file_exist?(new_launcher)
new_batch = File.join(Cosmos::USERPATH, "Launcher#{@system_name.text.gsub(' ','')}.bat")
return if file_exist?(new_batch)

# Create the new system.txt. We know the existing exists so simply copy it.
File.open(new_system, 'w') do |file|
  file.puts File.read(File.join(::Cosmos::USERPATH, 'config', 'system', existing_system))
end

# Create the new cmd_tlm_server config and update the TITLE
data = get_config_contents(existing_suffix, cmd_tlm_server_path, 'cmd_tlm_server')
data.sub!(/\s*TITLE.*/, "TITLE 'COSMOS Command and Telemetry Server - #{@system_name.text} Configuration'")
File.open(new_cmd_tlm_server, 'w') {|file| file.puts data }

# Create the new launcher config and update the TITLE and Server LAUNCH commands
data = get_config_contents(existing_suffix, launcher_path, 'launcher')
data.sub!(/\s*TITLE.*/, "TITLE 'Launcher - #{@system_name.text} Configuration'")
data.gsub!(/LAUNCH\s+(\w+)/, "LAUNCH \\1 --system system_#{new_suffix}.txt")
# Convert all --config to -c to make it easier to replace in the next step
data.gsub!(/(.*LAUNCH\s+CmdTlmServer.*)(--config)(.*)/, "\\1-c\\3")
data.gsub!(/(.*LAUNCH\s+CmdTlmServer.*)-c\s+(\w+)(.*)/, "\\1-c cmd_tlm_server_#{new_suffix}\\3")
File.open(new_launcher, 'w') {|file| file.puts data }

File.open(new_batch, 'w') do |file|
  file.puts "call tools\\Launcher.bat --config launcher_#{new_suffix}.txt --system system_#{new_suffix}.txt"
end

@parent.file_open(new_batch)
@parent.file_open(new_launcher)
@parent.file_open(new_cmd_tlm_server)
@parent.file_open(new_system)
Qt::MessageBox.information(self, "System Config Creation Success",
  "The new system configuration was successfully created.\n\n"\
  "The newly created files have been opened for further customization.")
end

#file_exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
# File 'lib/cosmos/tools/config_editor/system_config_dialog.rb', line 129

def file_exist?(path)
  if File.exist?(path)
    Qt::MessageBox.warning(self, "Config file exists!", "#{path} already exists!")
    return true
  else
    return false
  end
end

#get_config_contents(existing_suffix, base_path, file_name) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/cosmos/tools/config_editor/system_config_dialog.rb', line 138

def get_config_contents(existing_suffix, base_path, file_name)
  contents = ''
  if existing_suffix
    existing_file = File.join(Cosmos::USERPATH, base_path, "#{file_name}_#{existing_suffix}.txt")
    if File.exist?(existing_file)
      contents = File.read(existing_file)
    end
  else
    # Otherwise see if there is a basic one we can copy
    basic_config = File.join(Cosmos::USERPATH, base_path, "#{file_name}.txt")
    if File.exist?(basic_config)
      contents = File.read(basic_config)
    else
      # Otherwise use the install config
      contents = File.read(File.join(Cosmos::PATH, 'install', base_path, "#{file_name}.txt"))
    end
  end
  return contents
end