Class: Yast::DualMultiSelectionBoxClass

Inherits:
Module
  • Object
show all
Defined in:
../../src/modules/DualMultiSelectionBox.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) ClassifyPopup(selected)



298
299
300
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File '../../src/modules/DualMultiSelectionBox.rb', line 298

def ClassifyPopup(selected)
  selected = deep_copy(selected)
  # button text
  txt_sort = "Sorted"
  # button text
  txt_inter = "Interleaved"
  # button text
  txt_pat = "Pattern File"
  _SymToLetter = lambda do |sym|
    lbl = Builtins.tostring(sym)
    Builtins.substring(lbl, Ops.subtract(Builtins.size(lbl), 1))
  end
  _SymToLabel = lambda do |sym, hint|
    # button text
    Ops.add(_("Class") + (hint ? " &" : ""), _SymToLetter.call(sym))
  end
  _ClassifyHelpText = lambda do
    # dialog help text
    txt = _(
      "<p>This dialog is for defining classes for the raid devices\n" +
        "contained in the raid. Available classes are A, B, C, D and E but for many cases\n" +
        "fewer classes are needed (e.g. only A and B). </p>"
    )
    # dialog help text
    txt = Ops.add(
      txt,
      Builtins.sformat(
        _(
          "<p>You can put a device into a class by right-clicking on the\n" +
            "device and choosing the appropriate class from context menu. By pressing the \n" +
            "Ctrl  or Shift key you can select multiple devices and put them into a class in\n" +
            "one step. One can also use the buttons labeled \"%1\" to \"%2\" to put currently \n" +
            "selected devices into this class.</p>"
        ),
        _SymToLabel.call(:class_A, false),
        _SymToLabel.call(:class_E, false)
      )
    )
    # dialog help text
    txt = Ops.add(
      txt,
      Builtins.sformat(
        _(
          "<p>After choosing classes for devices you can order the \ndevices by pressing one of the buttons labeled \"%1\" or \"%2\"."
        ),
        txt_sort,
        txt_inter
      )
    )
    txt = Ops.add(txt, " ")
    # dialog help text
    txt = Ops.add(
      txt,
      _(
        "<b>Sorted</b> puts all devices of class A before all devices\nof class B and so on."
      )
    )
    txt = Ops.add(txt, " ")
    # dialog help text
    txt = Ops.add(
      txt,
      _(
        "<b>Interleaved</b> uses first device of class A, then first device of \n" +
          "class B, then all the following classes with assigned devices. Then the \n" +
          "second device of class A, the second device of class B, and so on will follow."
      )
    )
    txt = Ops.add(txt, " ")
    # dialog help text
    txt = Ops.add(
      txt,
      _(
        "All devices without a class are sorted to the end of devices list.\n" +
          "When you leave the pop-up the current order of the devices is used as the \n" +
          "order in the RAID to be created.</p>"
      )
    )
    # dialog help text
    txt = Ops.add(
      txt,
      Builtins.sformat(
        _(
          "By pressing button \"<b>%1</b>\" you can select a file that contains\n" +
            "lines with a regular expression and a class name (e.g. \"sda.*  A\"). All devices that match \n" +
            "the regular expression will be put into the class on this line. The regular expression is \n" +
            "matched against the kernel name (e.g. /dev/sda1), \n" +
            "the udev path name (e.g. /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0-part1) and the\n" +
            "the udev id (e.g. /dev/disk/by-id/ata-ST3500418AS_9VMN8X8L-part1). \n" +
            "The first match finally determines the class if a devices name matches more then one\n" +
            "regular expression.</p>"
        ),
        txt_pat
      )
    )
    txt
  end

  if Builtins.isempty(@classified)
    @classified = Builtins.listmap(
      Convert.convert(selected, :from => "list", :to => "list <string>")
    ) { |s| { s => "" } }
  end
  Builtins.y2milestone("ClassifyPopup select:%1", selected)
  Builtins.y2milestone("ClassifyPopup classified:%1", @classified)
  itl = Builtins.maplist(
    Convert.convert(selected, :from => "list", :to => "list <string>")
  ) { |s| Item(Id(s), s, Ops.get(@classified, s, "")) }
  Builtins.y2milestone("ClassifyPopup items:%1", itl)
  classes = [:class_A, :class_B, :class_C, :class_D, :class_E]
  cb = HBox()
  Builtins.foreach(classes) do |s|
    lbl = Builtins.tostring(s)
    lbl = Builtins.substring(lbl, Ops.subtract(Builtins.size(lbl), 1))
    cb = Builtins.add(cb, PushButton(Id(s), _SymToLabel.call(s, true)))
  end
  UI.OpenDialog(
    Opt(:decorated),
    VBox(
      MinHeight(
        15,
        Table(
          Id(:classtab),
          Opt(
            :keepSorting,
            :immediate,
            :notify,
            :multiSelection,
            :notifyContextMenu
          ),
          # headline text
          Header(_("Device"), Center(_("Class"))),
          itl
        )
      ),
      cb,
      HBox(
        PushButton(Id(:help), Opt(:helpButton), Label.HelpButton),
        PushButton(Id(:sorted), Ops.add(txt_sort, " (AAABBBCCC)")),
        PushButton(Id(:interleaved), Ops.add(txt_inter, " (ABCABCABC)")),
        PushButton(Id(:pattern), txt_pat)
      ),
      VSpacing(0.5),
      ButtonBox(
        PushButton(Id(:cancel), Opt(:cancelButton), Label.CancelButton),
        PushButton(Id(:ok), Opt(:okButton), Label.OKButton)
      )
    )
  )
  UI.ChangeWidget(:help, :HelpText, _ClassifyHelpText.call)
  ret = nil
  ctx = Builtins.maplist(classes) do |s|
    Item(Id(s), _SymToLabel.call(s, false))
  end
  begin
    ev = UI.WaitForEvent
    ret = Event.IsWidgetActivatedOrSelectionChanged(ev)
    ret = Event.IsWidgetValueChanged(ev) if ret == nil
    ret = Event.IsWidgetContextMenuActivated(ev) if ret == nil
    Builtins.y2milestone("ClassifyPopup event:%1 ret:%2", ev, ret)
    if ret == :classtab
      if Event.IsWidgetContextMenuActivated(ev) != nil
        UI.OpenContextMenu(term(:menu, ctx))
        value = UI.UserInput
        ret = Convert.to_symbol(value) if Convert.to_symbol(value) != nil
        Builtins.y2milestone("ClassifyPopup value:%1", value)
      end
    end
    if Builtins.contains(classes, ret)
      ls = Convert.convert(
        UI.QueryWidget(Id(:classtab), :SelectedItems),
        :from => "any",
        :to   => "list <string>"
      )
      Builtins.y2milestone("ClassifyPopup ls:%1", ls)
      let = _SymToLetter.call(ret)
      Builtins.y2milestone("ClassifyPopup class:%1", let)
      itl = Convert.convert(
        UI.QueryWidget(Id(:classtab), :Items),
        :from => "any",
        :to   => "list <term>"
      )
      itl = Builtins.maplist(itl) do |t|
        if Builtins.contains(ls, Ops.get_string(t, 1, ""))
          Ops.set(t, 2, let)
        end
        deep_copy(t)
      end
      UI.ChangeWidget(Id(:classtab), :Items, itl)
      UI.ChangeWidget(Id(:classtab), :SelectedItems, ls)
    elsif ret == :interleaved
      itl = Convert.convert(
        UI.QueryWidget(Id(:classtab), :Items),
        :from => "any",
        :to   => "list <term>"
      )
      ll = Builtins.filter(itl) do |a|
        Ops.less_than(Ops.get_string(a, 2, ""), "A")
      end
      itl = Builtins.filter(itl) do |a|
        Ops.greater_or_equal(Ops.get_string(a, 2, ""), "A")
      end
      sl = []
      tll = []
      cidx = 0
      while Ops.less_than(cidx, Builtins.size(classes))
        let = _SymToLetter.call(Ops.get(classes, cidx, :none))
        sl = Builtins.filter(itl) { |t| Ops.get_string(t, 2, "") == let }
        tll = Builtins.add(tll, sl) if !Builtins.isempty(sl)
        cidx = Ops.add(cidx, 1)
      end
      sl = []
      mpty = Item(Id(0), "-")
      while !Builtins.isempty(tll)
        cidx = 0
        while Ops.less_than(cidx, Builtins.size(tll))
          sl = Builtins.add(sl, Ops.get(tll, [cidx, 0], mpty))
          Ops.set(tll, cidx, Builtins.remove(Ops.get(tll, cidx, []), 0))
          if !Builtins.isempty(Ops.get(tll, cidx, []))
            cidx = Ops.add(cidx, 1)
          else
            tll = Builtins.remove(tll, cidx)
          end
        end
      end
      itl = Convert.convert(
        Builtins.union(sl, ll),
        :from => "list",
        :to   => "list <term>"
      )
      UI.ChangeWidget(Id(:classtab), :Items, itl)
    elsif ret == :sorted
      itl = Convert.convert(
        UI.QueryWidget(Id(:classtab), :Items),
        :from => "any",
        :to   => "list <term>"
      )
      ll = Builtins.filter(itl) do |a|
        Ops.less_than(Ops.get_string(a, 2, ""), "A")
      end
      itl = Builtins.sort(Builtins.filter(itl) do |t|
        Ops.greater_or_equal(Ops.get_string(t, 2, ""), "A")
      end) do |a, b|
        Ops.less_than(Ops.get_string(a, 2, " "), Ops.get_string(b, 2, " "))
      end
      itl = Convert.convert(
        Builtins.union(itl, ll),
        :from => "list",
        :to   => "list <term>"
      )
      UI.ChangeWidget(Id(:classtab), :Items, itl)
    elsif ret == :pattern
      # headline text
      fname = UI.AskForExistingFile(".", "", _("Pattern File"))
      Builtins.y2milestone("ClassifyPopup file:%1", fname)
      plst = []
      plst = ScanPatternFile(fname) if fname != nil
      if !Builtins.isempty(plst)
        itl = Convert.convert(
          UI.QueryWidget(Id(:classtab), :Items),
          :from => "any",
          :to   => "list <term>"
        )
        dc = {}
        dc = Builtins.listmap(itl) { |t| { Ops.get_string(t, 1, "") => "" } }
        Builtins.y2milestone("ClassifyPopup dc:%1", dc)
        dc_ref = arg_ref(dc)
        FindDeviceMatches(dc_ref, plst)
        dc = dc_ref.value
        Builtins.y2milestone("ClassifyPopup dc:%1", dc)
        itl = Builtins.maplist(itl) do |t|
          Ops.set(t, 2, Ops.get(dc, Ops.get_string(t, 1, ""), ""))
          deep_copy(t)
        end
        UI.ChangeWidget(Id(:classtab), :Items, itl)
      end
    end
  end while !Builtins.contains([:ok, :cancel], ret)
  if ret == :cancel
    selected = nil
  else
    itl = Convert.convert(
      UI.QueryWidget(Id(:classtab), :Items),
      :from => "any",
      :to   => "list <term>"
    )
    selected = Builtins.maplist(itl) { |t| Ops.get_string(t, 1, "") }
    Builtins.foreach(itl) do |t|
      Ops.set(
        @classified,
        Ops.get_string(t, 1, ""),
        Ops.get_string(t, 2, "")
      )
    end
    Builtins.y2milestone("ClassifyPopup classified:%1", @classified)
  end
  UI.CloseDialog
  Builtins.y2milestone("ClassifyPopup return:%1", selected)
  deep_copy(selected)
end

- (Object) Create(header, new_items, new_selected, unselected_label, selected_label, unselected_rp, selected_rp, can_change_order)



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
# File '../../src/modules/DualMultiSelectionBox.rb', line 75

def Create(header, new_items, new_selected, unselected_label, selected_label, unselected_rp, selected_rp, can_change_order)
  header = deep_copy(header)
  new_items = deep_copy(new_items)
  new_selected = deep_copy(new_selected)
  unselected_rp = deep_copy(unselected_rp)
  selected_rp = deep_copy(selected_rp)
  @items = deep_copy(new_items)
  @selected = deep_copy(new_selected)
  @item_map = Builtins.listmap(new_items) do |item|
    { Ops.get_string(item, [0, 0], "") => item }
  end
  @keep_order = can_change_order
  sel_header = deep_copy(header)
  if can_change_order
    sel_header = Builtins.add(sel_header, Center(_("Class")))
  end
  sel_term = Table(
    Id(:selected),
    Opt(:keepSorting, :multiSelection, :notify),
    sel_header,
    GetSelectedItems()
  )
  if can_change_order
    order_buttons = VBox(
      PushButton(Id(:top), _("Top")),
      VSpacing(0.5),
      PushButton(Id(:up), _("Up")),
      VSpacing(0.5),
      PushButton(Id(:down), _("Down")),
      VSpacing(0.5),
      PushButton(Id(:bottom), _("Bottom")),
      VSpacing(1.5),
      PushButton(Id(:classify), _("Classify"))
    )
    sel_term = HBox(sel_term, order_buttons)
  end
  HBox(
    HWeight(
      1,
      VBox(
        Left(Label(unselected_label)),
        Table(
          Id(:unselected),
          Opt(:keepSorting, :multiSelection, :notify),
          header,
          GetUnselectedItems()
        ),
        ReplacePoint(Id(:unselected_rp), unselected_rp)
      )
    ),
    MarginBox(
      1,
      1,
      HSquash(
        VBox(
          # push button text
          PushButton(
            Id(:add),
            Opt(:hstretch),
            Ops.add(_("Add") + " ", UI.Glyph(:ArrowRight))
          ),
          # push button text
          PushButton(
            Id(:add_all),
            Opt(:hstretch),
            Ops.add(_("Add All") + " ", UI.Glyph(:ArrowRight))
          ),
          VSpacing(1),
          # push button text
          PushButton(
            Id(:remove),
            Opt(:hstretch),
            Ops.add(Ops.add(UI.Glyph(:ArrowLeft), " "), _("Remove"))
          ),
          # push button text
          PushButton(
            Id(:remove_all),
            Opt(:hstretch),
            Ops.add(Ops.add(UI.Glyph(:ArrowLeft), " "), _("Remove All"))
          )
        )
      )
    ),
    HWeight(
      1,
      VBox(
        Left(Label(selected_label)),
        sel_term,
        ReplacePoint(Id(:selected_rp), selected_rp)
      )
    )
  )
end

- (Object) FindDeviceMatches(dc, plst)



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File '../../src/modules/DualMultiSelectionBox.rb', line 259

def FindDeviceMatches(dc, plst)
  plst = deep_copy(plst)
  tg = Storage.GetTargetMap
  dc.value = Builtins.mapmap(dc.value) do |d, c|
    p = Storage.GetPartition(tg, d)
    Builtins.y2milestone("FindDeviceMatches %1 is %2", d, p)
    match = Builtins.find(plst) do |m|
      found = Builtins.regexpmatch(d, Ops.get_string(m, 0, ""))
      if !found
        found = Builtins.regexpmatch(
          Ops.get_string(p, "device", ""),
          Ops.get_string(m, 0, "")
        )
      end
      if !found
        id = Builtins.find(Ops.get_list(p, "udev_id", [])) do |s|
          Builtins.regexpmatch(
            Ops.add("/dev/disk/by-id/", s),
            Ops.get_string(m, 0, "")
          )
        end
        found = id != nil
      end
      if !found
        found = Builtins.regexpmatch(
          Ops.add("/dev/disk/by-path/", Ops.get_string(p, "udev_path", "")),
          Ops.get_string(m, 0, "")
        )
      end
      found
    end
    Builtins.y2milestone("FindDeviceMatches match %1 is %2", d, match)
    c = Ops.get_string(match, 1, "") if match != nil
    { d => c }
  end

  nil
end

- (Object) GetSelected



170
171
172
# File '../../src/modules/DualMultiSelectionBox.rb', line 170

def GetSelected
  deep_copy(@selected)
end

- (Object) GetSelectedItems



59
60
61
62
63
64
65
66
67
68
69
70
71
# File '../../src/modules/DualMultiSelectionBox.rb', line 59

def GetSelectedItems
  Builtins.y2milestone("selected:%1", @selected)
  if @keep_order
    return Builtins.maplist(@selected) do |id|
      Ops.get(@item_map, id, Empty())
    end
  else
    return Builtins.filter(@items) do |item|
      id = Ops.get(item, [0, 0])
      Builtins.contains(@selected, id)
    end
  end
end

- (Object) GetUnselectedItems



51
52
53
54
55
56
# File '../../src/modules/DualMultiSelectionBox.rb', line 51

def GetUnselectedItems
  Builtins.filter(@items) do |item|
    id = Ops.get(item, [0, 0])
    !Builtins.contains(@selected, id)
  end
end

- (Object) Handle(widget)



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File '../../src/modules/DualMultiSelectionBox.rb', line 610

def Handle(widget)
  sel = []
  case widget
    when :add, :unselected
      tmp1 = Convert.to_list(
        UI.QueryWidget(Id(:unselected), :SelectedItems)
      )
      @selected = Builtins.flatten([@selected, tmp1])
      sel = Convert.convert(
        UI.QueryWidget(Id(:selected), :SelectedItems),
        :from => "any",
        :to   => "list <string>"
      )
      Builtins.y2milestone("selected:%1", @selected)
    when :remove, :selected
      tmp1 = Convert.to_list(UI.QueryWidget(Id(:selected), :SelectedItems))
      @selected = Builtins.filter(@selected) do |tmp2|
        !Builtins.contains(tmp1, tmp2)
      end
      sel = Convert.convert(
        UI.QueryWidget(Id(:selected), :SelectedItems),
        :from => "any",
        :to   => "list <string>"
      )
      sel = Convert.convert(
        Builtins.filter(sel) { |tmp2| !Builtins.contains(tmp1, tmp2) },
        :from => "list",
        :to   => "list <string>"
      )
      Builtins.y2milestone("selected:%1", @selected)
    when :add_all
      tmp1 = Builtins.maplist(@items) do |item|
        id = Ops.get(item, [0, 0])
        deep_copy(id)
      end
      tmp1 = Builtins.filter(
        Convert.convert(tmp1, :from => "list", :to => "list <string>")
      ) { |s| !Builtins.contains(@selected, s) }
      @selected = Builtins.merge(@selected, tmp1)
      sel = Convert.convert(
        UI.QueryWidget(Id(:selected), :SelectedItems),
        :from => "any",
        :to   => "list <string>"
      )
      Builtins.y2milestone("selected:%1", @selected)
    when :remove_all
      @selected = []
      @classified = {}
      @items = Builtins.maplist(@items) do |t|
        Ops.set(t, 5, "")
        Ops.set(@item_map, Ops.get_string(t, 1, ""), t)
        deep_copy(t)
      end
      Builtins.y2milestone("selected:%1", @selected)
    when :classify
      Builtins.y2milestone("selected:%1", @selected)
      l = ClassifyPopup(@selected)
      if l != nil
        @selected = deep_copy(l)
        Builtins.y2milestone("selected:%1", @selected)
        @items = Builtins.maplist(@items) do |t|
          Ops.set(t, 5, Ops.get(@classified, Ops.get_string(t, 1, ""), ""))
          Ops.set(@item_map, Ops.get_string(t, 1, ""), t)
          deep_copy(t)
        end
      end
    when :down, :up
      up = widget == :up
      sel = Convert.convert(
        UI.QueryWidget(Id(:selected), :SelectedItems),
        :from => "any",
        :to   => "list <string>"
      )
      cnt = -1
      siz = Ops.subtract(Builtins.size(@selected), 1)
      sm = Builtins.listmap(
        Convert.convert(@selected, :from => "list", :to => "list <string>")
      ) do |s|
        cnt = Ops.add(cnt, 1)
        { s => cnt }
      end
      cnt = 0
      diff = up ? -1 : 1
      sel = reverse(sel) if !up
      Builtins.foreach(sel) do |s|
        idx = Ops.get(sm, s, 0)
        if up && Ops.greater_than(idx, cnt) ||
            !up && Ops.less_than(idx, Ops.subtract(siz, cnt))
          @selected = Builtins::List.swap(
            @selected,
            Ops.add(idx, diff),
            idx
          )
        end
        cnt = Ops.add(cnt, 1)
      end
      Builtins.y2milestone("change:%1 selected:%2", sel, @selected)
    when :bottom, :top
      up = widget == :top
      sel = Convert.convert(
        UI.QueryWidget(Id(:selected), :SelectedItems),
        :from => "any",
        :to   => "list <string>"
      )
      @selected = Builtins.filter(
        Convert.convert(@selected, :from => "list", :to => "list <string>")
      ) { |s| !Builtins.contains(sel, s) }
      if up
        @selected = Builtins.merge(sel, @selected)
      else
        @selected = Builtins.merge(@selected, sel)
      end
      Builtins.y2milestone("change:%1 selected:%2", sel, @selected)
  end

  if Builtins.contains(
      [
        :unselected,
        :selected,
        :add,
        :add_all,
        :remove,
        :remove_all,
        :up,
        :down,
        :top,
        :bottom,
        :classify
      ],
      widget
    )
    UI.ChangeWidget(Id(:unselected), :Items, GetUnselectedItems())
    UI.ChangeWidget(Id(:selected), :Items, GetSelectedItems())
    UI.ChangeWidget(Id(:selected), :SelectedItems, sel)
  end

  nil
end

- (Object) main



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File '../../src/modules/DualMultiSelectionBox.rb', line 32

def main
  Yast.import "UI"

  Yast.import "Label"
  Yast.import "Event"
  Yast.import "Popup"
  Yast.import "Storage"

  textdomain "storage"


  @items = []

  @selected = []
  @item_map = {}
  @classified = {}
  @keep_order = false
end

- (Object) reverse(v)



599
600
601
602
603
604
605
606
607
608
# File '../../src/modules/DualMultiSelectionBox.rb', line 599

def reverse(v)
  v = deep_copy(v)
  siz = Builtins.size(v)
  i = 0
  while Ops.less_than(i, Ops.divide(siz, 2))
    v = Builtins::List.swap(v, i, Ops.subtract(Ops.subtract(siz, 1), i))
    i = Ops.add(i, 1)
  end
  deep_copy(v)
end

- (Object) ScanPatternFile(fname)



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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File '../../src/modules/DualMultiSelectionBox.rb', line 174

def ScanPatternFile(fname)
  ret = []
  txt = ""
  Builtins.y2milestone("ScanPatternFile fname:%1", fname)
  d = Convert.to_map(SCR.Read(path(".target.stat"), fname))
  Builtins.y2milestone("ScanPatternFile stat:%1", d)
  ok = true
  if !Ops.get_boolean(d, "isreg", false)
    # error popup text
    txt = Builtins.sformat(_("File %1 is not a regular file!"), fname)
    Popup.Error(txt)
    ok = false
  elsif Ops.greater_than(Ops.get_integer(d, "size", 0), 1024 * 1024)
    # error popup text
    txt = Builtins.sformat(_("File %1 is too big!"), fname)
    Popup.Error(txt)
    ok = false
  end
  sl = []
  fl = []
  if ok
    bo = Convert.to_map(
      SCR.Execute(path(".target.bash_output"), Ops.add("cat ", fname))
    )
    sl = Builtins.filter(
      Builtins.splitstring(Ops.get_string(bo, "stdout", ""), "\n")
    ) { |s| !Builtins.isempty(s) }
    fl = Builtins.filter(Builtins.splitstring(Ops.get(sl, 0, ""), " \t")) do |s|
      !Builtins.isempty(s)
    end
    Builtins.y2milestone(
      "ScanPatternFile fline:%1 size:%2",
      fl,
      Builtins.size(fl)
    )
    if Builtins.isempty(sl) || Builtins.size(fl) != 2 ||
        Builtins.size(Ops.get(fl, 1, "")) != 1
      # error popup text
      txt = _(
        "Pattern file has invalid format!\n" +
          "\n" +
          "The file needs to contain lines with a regular expression and a class name\n" +
          "per line. Example:"
      )
      txt = Ops.add(txt, "\nsda.* A\nsdb.* B")
      Popup.Error(txt)
      ok = false
    end
  end
  if ok
    Builtins.foreach(sl) do |s|
      fl = Builtins.filter(Builtins.splitstring(s, " \t")) do |f|
        !Builtins.isempty(f)
      end
      if !Builtins.isempty(Ops.get(fl, 0, "")) &&
          Builtins.size(Ops.get(fl, 1, "")) == 1
        ret = Builtins.add(
          ret,
          [Ops.get(fl, 0, ""), Builtins.toupper(Ops.get(fl, 1, ""))]
        )
      end
    end
    # popup text
    txt = _("Detected following pattern lines:") + "\n"
    Builtins.foreach(ret) do |l|
      txt = Ops.add(
        Ops.add(
          Ops.add(Ops.add(txt, "\n"), Ops.get_string(l, 0, "")),
          " : "
        ),
        Ops.get_string(l, 1, "")
      )
    end
    txt = Ops.add(txt, "\n\n")
    txt = Ops.add(
      txt,
      _("Ok to match devices to classes with these patterns?")
    )
    ok = Popup.YesNo(txt)
    ret = [] if !ok
  end
  Builtins.y2milestone("ScanPatternFile ret:%1", ret)
  deep_copy(ret)
end