Class: Yast::SequencerClass

Inherits:
Module
  • Object
show all
Defined in:
library/sequencer/src/modules/Sequencer.rb

Instance Method Summary collapse

Instance Method Details

#mainObject


39
40
41
42
43
# File 'library/sequencer/src/modules/Sequencer.rb', line 39

def main
  textdomain "base"

  @docheck = true
end

#Run(aliases, sequence) ⇒ Object

The Wizard Sequencer

Parameters:

  • aliases (Hash)

    the map of aliases

  • sequence (Hash)

    the sequence of dialogs

Returns:

  • final symbol or nil, if error (see the y2log)


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'library/sequencer/src/modules/Sequencer.rb', line 301

def Run(aliases, sequence)
  aliases = deep_copy(aliases)
  sequence = deep_copy(sequence)
  # Check aliases and sequence correctness
  return Convert.to_symbol(WS_error("CHECK FAILED")) if @docheck && WS_check(aliases, sequence) != true

  stack = []
  # string|symbol current
  current = Ops.get(sequence, "ws_start")
  return Convert.to_symbol(WS_error("Starting dialog not found")) if current.nil?

  loop do
    if Ops.is_symbol?(current)
      Builtins.y2debug("Finished")
      return Convert.to_symbol(current)
    end

    stack = WS_push(stack, current)
    Builtins.y2debug("stack=%1", stack)
    ret = WS_run(aliases, Convert.to_string(current))

    if ret.nil? || !Ops.is_symbol?(ret)
      return Convert.to_symbol(
        WS_error(Builtins.sformat("Invalid ret: %1", ret))
      )
    elsif ret == :back
      Builtins.y2debug("Back")
      poped = []
      special = true
      loop do
        return :back if Ops.less_than(Builtins.size(stack), 2)

        poped = WS_pop(stack)
        Builtins.y2debug("poped=%1", poped)
        current = Ops.get(poped, 1)
        stack = Ops.get_list(poped, 0)
        special = WS_special(aliases, Convert.to_string(current))
        Builtins.y2debug("special=%1", special)
        break if !special
      end
    else
      Builtins.y2debug("ret=%1", ret)
      current = WS_next(
        sequence,
        Convert.to_string(current),
        Convert.to_symbol(ret)
      )
      Builtins.y2debug("current=%1", current)
      if current.nil?
        return Convert.to_symbol(
          WS_error(Builtins.sformat("Next not found: %1", current))
        )
      end
    end
  end

  # Not reached
  nil
end

#WS_alias(aliases, alias_) ⇒ Yast::Term

Find an aliases in the aliases map

Parameters:

  • aliases (Hash)

    map of aliases

  • alias (String)

    given alias

Returns:

  • (Yast::Term)

    belonging to the given alias or nil, if error


187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'library/sequencer/src/modules/Sequencer.rb', line 187

def WS_alias(aliases, alias_)
  aliases = deep_copy(aliases)
  found = Ops.get(aliases, alias_)
  return WS_error(Builtins.sformat("Alias not found: %1", alias_)) if found.nil?

  if Ops.is_list?(found)
    return WS_error(Builtins.sformat("Invalid alias: %1", found)) if Ops.less_or_equal(Builtins.size(Convert.to_list(found)), 0)

    found = Ops.get(Convert.to_list(found), 0)
  end
  return WS_error(Builtins.sformat("Invalid alias: %1", found)) if found.nil?

  deep_copy(found)
end

#WS_check(aliases, sequence) ⇒ Object

Check correct types in maps and alias presence for sequence.

Parameters:

  • aliases (Hash)

    the map of aliases

  • sequence (Hash)

    the sequence of dialogs

Returns:

  • check passed?


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
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
# File 'library/sequencer/src/modules/Sequencer.rb', line 58

def WS_check(aliases, sequence)
  aliases = deep_copy(aliases)
  sequence = deep_copy(sequence)
  ret = []

  # check if aliases is not a nil
  if aliases.nil?
    Builtins.y2error(2, "sequencer check: aliases is nil")
    return false
  end

  # check if sequence is not a nil
  if sequence.nil?
    Builtins.y2error(2, "sequencer check: sequence is nil")
    return false
  end

  # check if ws_start is in aliases
  if Ops.get(aliases, "ws_start")
    Builtins.y2error(2, "sequencer check: ws_start cannot be an alias name")
    ret = Builtins.add(ret, false)
  else
    ret = Builtins.add(ret, true)
  end

  # check aliases map types
  ret0 = Builtins.maplist(aliases) do |key, val|
    if !Ops.is_string?(key)
      Builtins.y2error(2, "sequencer check: not a string: %1", key)
      next false
    elsif Ops.is_list?(val)
      if Ops.less_than(Builtins.size(Convert.to_list(val)), 2)
        Builtins.y2error(
          2,
          "sequencer check: list size too small: %1 (key: %2)",
          Builtins.size(Convert.to_list(val)),
          key
        )
        next false
      # FIXME: use function pointers
      #             else if (!is(select((list) val, 0, nil), term)) {
      #                 y2error(2, "sequencer check: not a term: %1", select((list) val, 0, nil));
      #                 return false;
      #             }
      elsif !Ops.is_boolean?(Ops.get(Convert.to_list(val), 1))
        Builtins.y2error(
          2,
          "sequencer check: not a boolean: %1",
          Ops.get(Convert.to_list(val), 1)
        )
        next false
      else
        next true
      end
    else
      # FIXME: use function pointers
      #         else if (!is(val, term)) {
      #             y2error(2, "sequencer check: not a term: %1", val);
      #             return false;
      #         }
      next true
    end
  end
  ret = Builtins.flatten([ret, ret0])

  # check if ws_start is in sequence
  if Ops.get(sequence, "ws_start").nil?
    Builtins.y2error(2, "sequencer check: ws_start needs to be defined")
    ret = Builtins.add(ret, false)
  else
    ret = Builtins.add(ret, true)
  end

  # check all aliases in sequence
  ret0 = Builtins.maplist(sequence) do |key, val|
    if key == "ws_start"
      next true if Ops.is_symbol?(val) || !Ops.get(aliases, val).nil?

      Builtins.y2error(2, "sequencer check: alias not found: %1", val)
      false
    elsif Ops.get(aliases, key).nil?
      Builtins.y2error(2, "sequencer check: alias not found: %1", key)
      false
    elsif !Ops.is_map?(val)
      Builtins.y2error(2, "sequencer check: not a map: %1 %2", key, val)
      false
    else
      ret1 = Builtins.maplist(Convert.to_map(val)) do |k, v|
        if !Ops.is_symbol?(k)
          Builtins.y2error(2, "sequencer check: not a symbol: %1", k)
          false
        elsif !Ops.is_symbol?(v) && Ops.get(aliases, v).nil?
          Builtins.y2error(2, "sequencer check: alias not found: %1", v)
          false
        else
          true
        end
      end
      ret1.all?
    end
  end
  ret = Builtins.flatten([ret, ret0])

  # check that all aliases are used
  ret0 = Builtins.maplist(aliases) do |key, _val|
    if !Builtins.haskey(sequence, key)
      Builtins.y2warning(2, "sequencer check: alias not used: %1", key)
      # return false;
    end
    true
  end
  ret = Builtins.flatten([ret, ret0])

  ret.all? { |v| v }
end

#WS_error(error) ⇒ Object

Report error and return nil

Parameters:

  • error (String)

    the error message text

Returns:

  • always nil

See Also:

  • #6474

178
179
180
181
# File 'library/sequencer/src/modules/Sequencer.rb', line 178

def WS_error(error)
  Builtins.y2error(1, "sequencer: %1", error)
  nil
end

#WS_next(sequence, current, ret) ⇒ Object

Find a next item in the sequence

Parameters:

  • sequence (Hash)

    sequence of dialogs

  • current (String)

    current dialog

  • ret (Symbol)

    returned value (determines the next dialog)

Returns:

  • next dialog (symbol), WS action (string) or nil, if error (current or next not found)


226
227
228
229
230
231
232
233
234
235
236
# File 'library/sequencer/src/modules/Sequencer.rb', line 226

def WS_next(sequence, current, ret)
  sequence = deep_copy(sequence)
  found = Ops.get_map(sequence, current)
  return WS_error(Builtins.sformat("Current not found: %1", current)) if found.nil?

  # string|symbol next
  next_ = Ops.get(found, ret)
  return WS_error(Builtins.sformat("Symbol not found: %1", ret)) if next_.nil?

  deep_copy(next_)
end

#WS_pop(stack) ⇒ new stack, poped value

Pop one item from the stack (remove an item and return the stack top item)

Parameters:

  • stack (Array)

    stack of previously run dialogsk

Returns:

  • (new stack, poped value)

    or nil if the stack is empty or nil


285
286
287
288
289
290
291
292
293
294
295
# File 'library/sequencer/src/modules/Sequencer.rb', line 285

def WS_pop(stack)
  stack = deep_copy(stack)
  return nil if stack.nil?

  num = Builtins.size(stack)
  return nil if Ops.less_than(num, 2)

  newstack = Builtins.remove(stack, Ops.subtract(num, 1))
  poped = Ops.get(stack, Ops.subtract(num, 2))
  [newstack, poped]
end

#WS_push(stack, item) ⇒ Object

Push one item to the stack

Parameters:

  • stack (Array)

    stack of previously run dialogs

  • item (Object)

    item to be pushed

Returns:

  • the new stack or nil, if the stack is nil


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'library/sequencer/src/modules/Sequencer.rb', line 264

def WS_push(stack, item)
  stack = deep_copy(stack)
  item = deep_copy(item)
  return nil if stack.nil?

  return Builtins.add(stack, item) if !Builtins.contains(stack, item)

  found = false
  newstack = Builtins.filter(stack) do |v|
    next false if found

    found = true if v == item
    true
  end

  deep_copy(newstack)
end

#WS_run(aliases, id) ⇒ Object

Run a function from the aliases map

Parameters:

  • aliases (Hash)

    map of aliases

  • id (String)

    function to run

Returns:

  • returned value from function or nil, if function is nil or returned something else than symbol


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'library/sequencer/src/modules/Sequencer.rb', line 242

def WS_run(aliases, id)
  aliases = deep_copy(aliases)
  Builtins.y2debug("Running: %1", id)

  function = WS_alias(aliases, id)
  return Convert.to_symbol(WS_error(Builtins.sformat("Bad id: %1", id))) if function.nil?

  ret = Builtins.eval(function)

  if !Ops.is_symbol?(ret)
    return Convert.to_symbol(
      WS_error(Builtins.sformat("Returned value not symbol: %1", ret))
    )
  end

  Convert.to_symbol(ret)
end

#WS_special(aliases, alias_) ⇒ Object

Decide if an alias is special

Parameters:

  • aliases (Hash)

    map of aliases

  • alias (String)

    given alias

Returns:

  • true if the given alias is special or nil, if not found


206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'library/sequencer/src/modules/Sequencer.rb', line 206

def WS_special(aliases, alias_)
  aliases = deep_copy(aliases)
  found = Ops.get(aliases, alias_)
  if found.nil?
    return Convert.to_boolean(
      WS_error(Builtins.sformat("Alias not found: %1", alias_))
    )
  end
  ret = false
  if Ops.is_list?(found)
    ret = Ops.get_boolean(Convert.to_list(found), 1) if Ops.greater_than(Builtins.size(Convert.to_list(found)), 1)
  end
  ret
end

#WS_testall(aliases) ⇒ Object

Test (run) all dialogs in the aliases map

Parameters:

  • aliases (Hash)

    the map of aliases

Returns:

  • returned values of tested dialogs

See Also:

  • documentation for the format of aliases map

49
50
51
52
# File 'library/sequencer/src/modules/Sequencer.rb', line 49

def WS_testall(aliases)
  aliases = deep_copy(aliases)
  Builtins.maplist(aliases) { |_id, func| Builtins.eval(func) }
end