Class: Cosmos::TableManagerCore

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/tools/table_manager/table_manager_core.rb

Overview

Provides the low level Table Manager methods which do not require a GUI.

Defined Under Namespace

Classes: CoreError, MismatchError, NoConfigError, NoTableError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTableManagerCore

Create the instance



40
41
42
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 40

def initialize
  reset()
end

Instance Attribute Details

#configTableConfig (readonly)

Returns Configuration instance.

Returns:



37
38
39
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 37

def config
  @config
end

Instance Method Details

#file_checkString

Returns Success string if parameters all check. Raises a CoreError if errors are found.

Returns:

  • (String)

    Success string if parameters all check. Raises a CoreError if errors are found.

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 93

def file_check
  raise NoConfigError unless @config
  result = ""
  @config.table_names.each do |name|
    table_result = table_check(name)
    unless table_result.empty?
      result << "Errors in #{name}:\n" + table_result
    end
  end
  raise CoreError, result unless result.empty?
  "All parameters are within their constraints."
end

#file_hexObject

Create a hex formatted string of all the file data

Raises:



162
163
164
165
166
167
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 162

def file_hex
  raise NoConfigError unless @config
  data = ""
  @config.tables.values.each {|table| data << table.buffer }
  "#{data.formatted}\n\nTotal Bytes Read: #{data.length}"
end

#file_new(def_path, output_dir) {|0.3| ... } ⇒ String

Returns Binary file path.

Parameters:

  • def_path (String)

    Definition file to process

  • output_dir (String)

    Output directory to create the new file

Yields:

  • (0.3)

Returns:

  • (String)

    Binary file path



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 58

def file_new(def_path, output_dir)
  process_definition(def_path)
  yield 0.3 if block_given?

  @config.table_names.each {|table_name| set_binary_data_to_default(table_name) }
  yield 0.7 if block_given?

  bin_path = File.join(output_dir, def_to_bin_filename(def_path))
  file_save(bin_path)
  bin_path
end

#file_open(bin_path, def_path) ⇒ Object

Parameters:

  • bin_path (String)

    Binary file to open

  • def_path (String)

    Definition file to use when opening



72
73
74
75
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 72

def file_open(bin_path, def_path)
  process_definition(def_path)
  open_and_load_binary_file(bin_path)
end

#file_report(bin_path, def_path) ⇒ String

Create a CSV report file based on the file contents.

Parameters:

  • bin_path (String)

    Binary filename currently open. Used to generate the report name such that it matches the binary filename.

  • def_path (String)

    Definition filename currently open

Returns:

  • (String)

    Report filename path

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
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
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 112

def file_report(bin_path, def_path)
  raise NoConfigError unless @config
  file_check()

  basename = File.basename(bin_path, ".dat")
  report_path = File.join(File.dirname(bin_path), "#{basename}.csv")
  File.open(report_path, 'w+') do |file|
    file.write("File Definition, #{def_path}\n")
    file.write("File Binary, #{bin_path}\n\n")
    @config.tables.values.each do |table|
      items = table.sorted_items
      file.puts(table.table_name)

      # Write the column headers
      if table.type == :TWO_DIMENSIONAL
        columns = ["Item"]
        # Remove the '0' from the 'itemname0'
        table.num_columns.times.each {|x| columns << items[x].name[0...-1] }
        file.puts columns.join(", ")
      else
        file.puts "Label, Value"
      end

      # Write the table item values
      (0...table.num_rows).each do |r|
        if table.type == :TWO_DIMENSIONAL
          rowtext = "#{r + 1}"
        else
          rowtext = items[r].name
        end

        file.write "#{rowtext}, "
        (0...table.num_columns).each do |c|
          if table.type == :TWO_DIMENSIONAL
            table_item = items[c + r * table.num_columns]
          else
            table_item = items[r]
          end

          file.write "#{table.read(table_item.name, :FORMATTED).to_s}, "
        end
        file.write("\n") # newline after each row
      end
      file.write("\n") # newline after each table
    end
  end
  report_path
end

#file_save(filename) ⇒ Object

Saves the current tables in the config instance to the given filename.

Parameters:

  • filename (String)

    Filename to write, overwritten if it exists.

Raises:



80
81
82
83
84
85
86
87
88
89
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 80

def file_save(filename)
  raise NoConfigError unless @config
  file_check()
  File.open(filename, "wb") do |file|
    @config.tables.each do |table_name, table|
      file.write(table.buffer)
    end
  end
  file_report(filename, @config.filename)
end

#process_definition(filename) ⇒ Object

Parameters:

  • filename (String)

    Create a new TableConfig instance and process the filename



50
51
52
53
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 50

def process_definition(filename)
  @config = TableConfig.new()
  @config.process_file(filename)
end

#resetObject

Clears the configuration



45
46
47
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 45

def reset
  @config = nil
end

#table_check(table_name) ⇒ Object

Parameters:

  • table_name (String)

    Name of the table to check for out of range values

Raises:



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
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 170

def table_check(table_name)
  raise NoConfigError unless @config
  table = @config.table(table_name)
  raise NoTableError unless table

  result = ""
  table_items = table.sorted_items

  # Check the ranges and constraints for each item in the table
  # We go through it this way (by row and columns) so we can grab the actual
  # user input when we display any errors found
  (0...table.num_rows).each do |r|
    (0...table.num_columns).each do |c|
      # get the table item definition so we know how to save it
      table_item = table_items[r * table.num_columns + c]

      value = table.read(table_item.name)
      unless table_item.range.nil?
        # If the item has states which include the value, then convert
        # the state back to the numeric value for range checking
        if table_item.states && table_item.states.include?(value)
          value = table_item.states[value]
        end
        # check to see if the value lies within its valid range
        unless table_item.range.include?(value)
          if table_item.format_string
            value = table.read(table_item.name, :FORMATTED)
            range_first = sprintf(table_item.format_string, table_item.range.first)
            range_last = sprintf(table_item.format_string, table_item.range.last)
          else
            range_first = table_item.range.first
            range_last = table_item.range.last
          end
          result << "  #{table_item.name}: #{value} outside valid range of #{range_first}..#{range_last}\n"
        end
      end
    end # end each column
  end # end each row
  result
end

#table_commit(table_name, bin_file, def_file) ⇒ Object

Commit a table from the current configuration into a new binary

Parameters:

  • table_name (String)

    Table name to commit to an existing binary

  • bin_file (String)

    Binary file to open

  • def_file (String)

    Definition file to use when opening

Raises:



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 240

def table_commit(table_name, bin_file, def_file)
  raise NoConfigError unless @config
  save_table = @config.table(table_name)
  raise NoTableError unless save_table

  result = table_check(table_name)
  raise CoreError, "Errors in #{table_name}:\n#{result}" unless result.empty?

  config = TableConfig.new
  begin
    config.process_file(def_file)
  rescue => err
    raise CoreError, "The table definition file:#{def_file} has the following errors:\n#{err}"
  end

  if !config.table_names.include?(table_name.upcase)
    raise NoTableError, "#{table_name} not found in #{def_file} table definition file."
  end

  saved_config = @config
  @config = config
  open_and_load_binary_file(bin_file)

  # Store the saved table data in the new table definition
  table = config.table(save_table.table_name)
  table.buffer = save_table.buffer[0...table.length]
  file_save(bin_file)
  @config = saved_config
end

#table_default(table_name) ⇒ Object

Parameters:

  • table_name (String)

    Name of the table to revert all values to default

Raises:



212
213
214
215
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 212

def table_default(table_name)
  raise NoConfigError unless @config
  set_binary_data_to_default(table_name)
end

#table_hex(table_name) ⇒ Object

Parameters:

  • table_name (String)

    Create a hex formatted string of the given table data

Raises:



218
219
220
221
222
223
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 218

def table_hex(table_name)
  raise NoConfigError unless @config
  table = @config.table(table_name)
  raise NoTableError unless table
  "#{table.buffer.formatted}\n\nTotal Bytes Read: #{table.buffer.length}"
end

#table_save(table_name, filename) ⇒ Object

Parameters:

  • table_name (String)

    Table name to write as a stand alone file

  • filename (String)

    Filename to write the table data to. Existing files will be overwritten.

Raises:



228
229
230
231
232
233
# File 'lib/cosmos/tools/table_manager/table_manager_core.rb', line 228

def table_save(table_name, filename)
  raise NoConfigError unless @config
  result = table_check(table_name)
  raise CoreError, "Errors in #{table_name}:\n#{result}" unless result.empty?
  File.open(filename, 'wb') {|file| file.write(@config.table(table_name).buffer) }
end