Class: Cosmos::LauncherConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/tools/launcher/launcher_config.rb

Overview

Reads and interprets the Launcher configuration file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ LauncherConfig

Processes a file and adds in the configuration defined in the file

Parameters:

  • filename (String)

    Name of the configuration file to parse



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 40

def initialize(filename)
  @title = 'COSMOS Launcher'
  @tool_font_settings  = ['Arial', 12]
  @label_font_settings = ['Arial', 16]
  @num_columns = 4
  @items = []

  if File.exist?(filename.to_s)
    parse_file(filename)
  else
    raise "Launcher configuration file does not exist: #{filename}"
  end
end

Instance Attribute Details

#itemsObject (readonly)

Array of [[item_type, text, shell command, icon filename, variable params], …] Where variable params is nil or an array of [[parameter name, parameter value], …]



32
33
34
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 32

def items
  @items
end

#label_font_settingsObject (readonly)

Array for label font settings [font_name, font_size]



28
29
30
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 28

def label_font_settings
  @label_font_settings
end

#num_columnsObject (readonly)

Number of columns per row



35
36
37
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 35

def num_columns
  @num_columns
end

#titleObject (readonly)

Launcher title



22
23
24
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 22

def title
  @title
end

#tool_font_settingsObject (readonly)

Array for tool font settings [font_name, font_size]



25
26
27
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 25

def tool_font_settings
  @tool_font_settings
end

Instance Method Details

#parse_file(filename) ⇒ Object

Create a ConfigParser and parse all the lines in the configuration file

Parameters:

  • filename (String)

    Name of the configuration file to parse



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cosmos/tools/launcher/launcher_config.rb', line 57

def parse_file(filename)
  multitool = nil

  # Loop over each line of the configuration file
  parser = ConfigParser.new
  parser.parse_file(filename) do |keyword, params|
    # Handle each keyword
    case keyword

    when 'AUTO_GEM_TOOLS'
      parse_gem_tool(parser)

    when 'TOOL'
      parse_tool(parser, params, multitool)

    when 'MULTITOOL_START'
      parser.verify_num_parameters(1, 2, "MULTITOOL_START <Button Text> <Icon Filename (optional)>")
      multitool = OpenStruct.new
      multitool.text = params[0]
      multitool.icon_filename = ConfigParser.handle_nil(params[1])
      multitool.icon_filename = 'multi.png' unless multitool.icon_filename
      multitool.settings = []

    when 'MULTITOOL_END'
      parser.verify_num_parameters(0, 0, "MULTITOOL_END")
      raise parser.error("No TOOLs defined within the MULTITOOL") if multitool.settings.select {|setting| setting[0] == :TOOL }.empty?
      @items << [:MULTITOOL, multitool.text, multitool.settings, true, multitool.icon_filename, nil]
      multitool = nil

    when 'DELAY'
      if multitool
        parser.verify_num_parameters(1, 1, "DELAY <Delay in seconds>")
        multitool.settings << [:DELAY, Float(params[0]), true]
      else
        raise parser.error("DELAY keyword only valid within MULTITOOL")
      end

    when 'DIVIDER'
      parser.verify_num_parameters(0, 0, "DIVIDER")
      @items << [:DIVIDER, nil, nil, nil, nil]

    when 'LABEL'
      parser.verify_num_parameters(1, 1, "LABEL <Label Text>")
      @items << [:LABEL, params[0], nil, nil, nil]

    when 'TOOL_FONT', 'LABEL_FONT'
      usage = "#{keyword} <Font Name> <Font Size>"
      parser.verify_num_parameters(2, 2, usage)
      begin
        @tool_font_settings = [params[0], Integer(params[1])] if keyword == 'TOOL_FONT'
        @label_font_settings = [params[0], Integer(params[1])] if keyword == 'LABEL_FONT'
      rescue ArgumentError
        raise parser.error("#{usage} passed '#{params[0]} #{params[1]}'")
      end

    when 'TITLE'
      parser.verify_num_parameters(1, 1, "TITLE <Title Text>")
      @title = params[0]

    when 'NUM_COLUMNS'
      usage = "NUM_COLUMNS <Num Columns>"
      parser.verify_num_parameters(1, 1, usage)
      begin
        @num_columns = Integer(params[0])
      rescue ArgumentError
        raise parser.error("#{usage} passed '#{params[0]}'")
      end

    when 'DONT_CAPTURE_IO'
      parser.verify_num_parameters(0, 0, "DONT_CAPTURE_IO")
      if multitool
        if multitool.settings[-1].nil? || multitool.settings[-1][0] != :TOOL
          raise parser.error("DONT_CAPTURE_IO must follow a TOOL")
        end
        multitool.settings[-1][2] = false
      else
        if @items[-1].nil? || @items[-1][0] != :TOOL
          raise parser.error("DONT_CAPTURE_IO must follow a TOOL")
        end
        @items[-1][3] = false
      end

    else # UNKNOWN
      raise parser.error("Unknown keyword '#{keyword}'.") if keyword
    end # case keyword
  end # parser.parse_file
end