Skip to content

Commit

Permalink
service: add support to read AY l10n settings
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed May 21, 2024
1 parent 2f0819a commit b060422
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 3 deletions.
31 changes: 28 additions & 3 deletions doc/autoyast.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,28 @@ For the first user, the following elements are supported:
| user_password | Yes | password | |
| username | Yes | userName | |

### `keyboard`, `language` and `timezone`
### `keyboard`

These sections are rather simple, but we need to do some mapping between AutoYaST and Agama values.
Additionally, the `hwclock` element is not present in Agama.
Only the `keymap` element is translated. The rest of options are ignored in YaST and they are not
even documented in the AutoYaST handbook.

| AutoYaST | Supported | Agama | Comment |
| -------- | ---------- | ------------- | ------------------------------ |
| keymap | Yes | l10n.keyboard | Should we rename it to keymap? |
| capslock | Deprecated | | |
| delay | Deprecated | | |
| discaps | Deprecated | | |
| numlock | Deprecated | | |
| rate | Deprecated | | |
| scrlock | Deprecated | | |
| tty | Deprecated | | |

### `language`

| AutoYaST | Supported | Agama | Comment |
| --------- | --------- | -------------- | ------------------------- |
| language | Yes | l10n.languages | First element of the list |
| languages | Yes | l10n.languages | |

### `networking`

Expand Down Expand Up @@ -228,6 +246,13 @@ About the `slp_discoverty` element, Agama does not support [SLP] at all?

[SLP]: https://documentation.suse.com/sles/15-SP5/single-html/SLES-administration/#cha-slp

### `timezone`

| AutoYaST | Supported | Agama | Comment |
| -------- | --------- | ------------- | ------- |
| timezone | Yes | l10n.timezone | |
| hwclock | No | | |

## Unsupported sections

- `FCoE`
Expand Down
3 changes: 3 additions & 0 deletions service/lib/agama/autoyast/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require "yast"
require "autoinstall/script_runner"
require "autoinstall/script"
require "agama/autoyast/l10n_reader"
require "agama/autoyast/product_reader"
require "agama/autoyast/root_reader"
require "agama/autoyast/software_reader"
Expand Down Expand Up @@ -119,13 +120,15 @@ def export_profile(profile)
root = Agama::AutoYaST::RootReader.new(profile)
software = Agama::AutoYaST::SoftwareReader.new(profile)
product = Agama::AutoYaST::ProductReader.new(profile)
l10n = Agama::AutoYaST::L10nReader.new(profile)
storage = Agama::AutoYaST::StorageReader.new(profile)

user.read
.merge(root.read)
.merge(software.read)
.merge(product.read)
.merge(storage.read)
.merge(l10n.read)
end

def import_yast
Expand Down
79 changes: 79 additions & 0 deletions service/lib/agama/autoyast/l10n_reader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "yast"

# :nodoc:
module Agama
module AutoYaST
class L10nReader
# @param profile [ProfileHash] AutoYaST profile
def initialize(profile)
@profile = profile
end

# @return [Hash] Agama "l10n" section
def read
l10n = keyboard
.merge(languages)
.merge(timezone)
l10n.empty? ? {} : { "l10n" => l10n }
end

private

attr_reader :profile

def keyboard
section = profile.fetch_as_hash("keyboard")
keymap = section["keymap"]
return {} if keymap.nil?

{ "keyboard" => keymap.to_s }
end

def languages
section = profile.fetch_as_hash("language")
primary = section["language"]
secondary = section["languages"].to_s.split(",").map(&:strip)

languages = []
languages.push(primary.to_s) unless primary.nil?
languages.concat(secondary)

with_encoding = languages.map do |lang|
lang.include?(".") ? lang : "#{lang}.UTF-8"
end

with_encoding.empty? ? {} : { "languages" => with_encoding.uniq }
end

def timezone
section = profile.fetch_as_hash("timezone")
timezone = section["timezone"]
return {} if timezone.nil?

{ "timezone" => timezone.to_s }
end
end
end
end
9 changes: 9 additions & 0 deletions service/test/agama/autoyast/converter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@
"password" => "12345678", "fullName" => "Jane Doe")
end
end

it "exports l10n settings" do
subject.to_agama(workdir)
expect(result["l10n"]).to include(
"languages" => ["en_US.UTF-8"],
"timezone" => "Atlantic/Canary",
"keyboard" => "us"
)
end
end

context "when an invalid profile is given" do
Expand Down
102 changes: 102 additions & 0 deletions service/test/agama/autoyast/l10n_reader_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: true

# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../test_helper"
require "yast"
require "agama/autoyast/l10n_reader"

Yast.import "Profile"

describe Agama::AutoYaST::L10nReader do
let(:profile) do
{}
end

subject do
described_class.new(Yast::ProfileHash.new(profile))
end

describe "#read" do
context "when there is no l10n-related sections" do
let(:profile) { {} }

it "returns an empty hash" do
expect(subject.read).to be_empty
end
end

context "when a keymap is defined" do
let(:profile) do
{ "keyboard" => { "keymap" => "us" } }
end

it "includes a 'keyboard' key with its value" do
l10n = subject.read["l10n"]
expect(l10n["keyboard"]).to eq("us")
end
end

context "when there are primary and secondary languages" do
let(:profile) do
{
"language" => {
"language" => "en_US.UTF-8",
"languages" => "es_ES.UTF-8, cs_CZ.UTF-8"
}
}
end

it "includes a 'languages' key with all the languages" do
l10n = subject.read["l10n"]
expect(l10n["languages"]).to eq(["en_US.UTF-8", "es_ES.UTF-8", "cs_CZ.UTF-8"])
end

context "when the encoding is not included" do
let(:profile) do
{
"language" => {
"language" => "en_US",
"languages" => "es_ES"
}
}
end

it "uses the UTF-8 encoding" do
l10n = subject.read["l10n"]
expect(l10n["languages"]).to eq(["en_US.UTF-8", "es_ES.UTF-8"])
end
end
end

context "when a timezone is defined" do
let(:profile) do
{ "timezone" => { "timezone" => "Europe/Berlin" } }
end

it "includes a 'keyboard' key with its value" do
l10n = subject.read["l10n"]
expect(l10n["timezone"]).to eq("Europe/Berlin")
end
end
end
end


8 changes: 8 additions & 0 deletions service/test/fixtures/profiles/simple.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
<languages>en_US</languages>
</language>

<timezone>
<timezone>Atlantic/Canary</timezone>
</timezone>

<keyboard>
<keymap>us</keymap>
</keyboard>

<partitioning config:type="list">
<drive>
<device>/dev/vda</device>
Expand Down

0 comments on commit b060422

Please sign in to comment.