Class: WSK::Launcher

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/WSK/Launcher.rb

Instance Method Summary collapse

Methods included from Common

#accessInputWaveFile, #accessOutputWaveFile, #getWaveFileAccesses, #parsePlugins, #readDuration, #readFFTProfile, #readThresholds, #val2db

Constructor Details

#initializeLauncher

Constructor



16
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
# File 'lib/WSK/Launcher.rb', line 16

def initialize
  # Options set by the command line parser
  @InputFileName = nil
  @OutputFileName = nil
  @Action = nil
  @DisplayHelp = false
  @Debug = false
  parsePlugins

  # The command line parser
  @Options = OptionParser.new
  @Options.banner = 'WSK.rb [--help] [--debug] --input <InputFile> --output <OutputFile> --action <ActionName> -- <ActionOptions>'
  @Options.on( '--input <InputFile>', String,
    '<InputFile>: WAVE file name to use as input',
    'Specify input file name') do |iArg|
    @InputFileName = iArg
  end
  @Options.on( '--output <OutputFile>', String,
    '<OutputFile>: WAVE file name to use as output',
    'Specify output file name') do |iArg|
    @OutputFileName = iArg
  end
  @Options.on( '--action <ActionName>', String,
    "<ActionName>: Name of the action to process. Available Actions: #{get_plugins_names('Actions').sort.join(', ')}",
    'Specify the Action to execute') do |iArg|
    @Action = iArg
  end
  @Options.on( '--help',
    'Display help') do
    @DisplayHelp = true
  end
  @Options.on( '--debug',
    'Activate debug logs') do
    @Debug = true
  end
end

Instance Method Details

#execute(iArgs) ⇒ Object

Execute command line arguments

Parameters
  • iArgs (list<String>): Command line arguments

Return
  • Integer: The error code to return to the terminal



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
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
# File 'lib/WSK/Launcher.rb', line 59

def execute(iArgs)
  rResult = 0

  lBeginTime = DateTime.now
  lError = nil
  lActionArgs = nil
  begin
    # Split the arguments
    lMainArgs, lActionArgs = splitParameters(iArgs)
    lRemainingArgs = @Options.parse(lMainArgs)
    if (!lRemainingArgs.empty?)
      lError = RuntimeError.new("Unknown arguments: #{lRemainingArgs.join(' ')}")
    end
  rescue Exception
    lError = $!
  end
  if (lError == nil)
    if (@DisplayHelp)
      puts @Options
    else
      if (@Debug)
        activate_log_debug(true)
      end
      # Check mandatory arguments were given
      if (@InputFileName == nil)
        lError = RuntimeError.new('Missing --input option. Please specify an input file.')
      elsif (@OutputFileName == nil)
        lError = RuntimeError.new('Missing --output option. Please specify an output file.')
      elsif (@Action == nil)
        lError = RuntimeError.new('Missing --action option. Please specify an action to perform.')
      elsif (!File.exists?(@InputFileName))
        lError = RuntimeError.new("Missing input file #{@InputFileName}")
      elsif (File.exists?(@OutputFileName))
        lError = RuntimeError.new("Output file #{@OutputFileName} already exists.")
      else
        # Access the Action
        access_plugin('Actions', @Action) do |ioActionPlugin|
          lDesc = ioActionPlugin.pluginDescription
          # Check the output interface required by this plugin
          lOutputInterfaceName = lDesc[:OutputInterface]
          if (lOutputInterfaceName == nil)
            lOutputInterfaceName = 'DirectStream'
          end
          # Initialize the variables if options are specified
          if (lDesc[:Options] == nil)
            if (!lActionArgs.empty?)
              lError = RuntimeError.new("Unknown Action arguments: #{lActionArgs.join(' ')}. Normally no parameter was expected.")
            end
          else
            # Check options
            lPluginOptions = OptionParser.new
            # Variables to instantiate
            lVariables = {}
            lDesc[:Options].each do |iVariable, iOptionInfo|
              # Set the variable correctly when the option is encountered
              lPluginOptions.on(*iOptionInfo) do |iArg|
                lVariables[iVariable] = iArg
              end
            end
            if (lActionArgs.empty?)
              lError = RuntimeError.new("Action was expecting arguments: #{lPluginOptions.to_s}. Please specify them after -- separator.")
            else
              # Parse Action's options
              begin
                lRemainingActionArgs = lPluginOptions.parse(lActionArgs)
                if (!lRemainingActionArgs.empty?)
                  lError = RuntimeError.new("Unknown Action arguments: #{lRemainingActionArgs.join(' ')}. Expected signature: #{lPluginOptions.to_s}")
                end
              rescue Exception
                lError = $!
              end
              # Instantiate variables if needed
              if (lError == nil)
                instantiateVars(ioActionPlugin, lVariables)
              end
            end
          end
          # Plugin is initialized
          if (lError == nil)
            # Access the output interface plugin
            access_plugin('OutputInterfaces', lOutputInterfaceName) do |ioOutputPlugin|
              # Access the input file
              lError = accessInputWaveFile(@InputFileName) do |iInputHeader, iInputData|
                lInputSubError = nil

                # Get the maximal output data samples
                lNbrOutputDataSamples = ioActionPlugin.get_nbr_samples(iInputData)
                log_debug "Number of samples to be written: #{lNbrOutputDataSamples}"

                # Access the output file
                lInputSubError = accessOutputWaveFile(@OutputFileName, iInputHeader, ioOutputPlugin, lNbrOutputDataSamples) do
                  # Execute
                  log_info "Execute Action #{@Action}, reading #{@InputFileName} and writing #{@OutputFileName} using #{lOutputInterfaceName} output interface."
                  next ioActionPlugin.execute(iInputData, ioOutputPlugin)
                end

                next lInputSubError
              end
            end
          end
        end
      end
    end
  end

  if (lError != nil)
    log_err "Error encountered: #{lError}"
    rResult = 1
  end
  log_info "Elapsed milliseconds: #{((DateTime.now-lBeginTime)*86400000).to_i}"

  return rResult
end