Merge lp:~ngompa13/switchboard-plug-about/rework-osrel-parsing into lp:~elementary-apps/switchboard-plug-about/trunk

Proposed by Neal Gompa
Status: Merged
Approved by: Cody Garver
Approved revision: 443
Merged at revision: 443
Proposed branch: lp:~ngompa13/switchboard-plug-about/rework-osrel-parsing
Merge into: lp:~elementary-apps/switchboard-plug-about/trunk
Diff against target: 57 lines (+10/-24)
1 file modified
src/Plug.vala (+10/-24)
To merge this branch: bzr merge lp:~ngompa13/switchboard-plug-about/rework-osrel-parsing
Reviewer Review Type Date Requested Status
elementary Apps team Pending
Review via email: mp+322261@code.launchpad.net

Commit message

Rework os-release(5) parsing to be less brittle.

Since /etc/os-release is effectively a set of key-value pairs, we'll just read the whole thing and import it into a HashMap.

This allows us to reference arbitrary entries from the file and set variables with that data accordingly.

Description of the change

This changes the parsing of os-release(5) to be less brittle.

Since /etc/os-release is effectively a set of key-value pairs, we'll just read the whole thing and import it into a HashMap.

This allows us to reference arbitrary entries from the file and set variables with that data accordingly.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/Plug.vala'
2--- src/Plug.vala 2017-03-19 18:14:15 +0000
3+++ src/Plug.vala 2017-04-09 21:11:35 +0000
4@@ -80,35 +80,21 @@
5
6 File file = File.new_for_path("/etc/os-release");
7 try {
8+ var osrel = new Gee.HashMap<string, string> ();
9 var dis = new DataInputStream (file.read ());
10 string line;
11 // Read lines until end of file (null) is reached
12 while ((line = dis.read_line (null)) != null) {
13- if ("PRETTY_NAME=" in line) {
14- os = line.replace ("PRETTY_NAME=", "");
15- if ("\"" in os) {
16- os = os.replace ("\"", "");
17- }
18- }
19- if ("HOME_URL=" in line) {
20- website_url = line.replace ("HOME_URL=", "");
21- if ("\"" in website_url) {
22- website_url = website_url.replace ("\"", "");
23- }
24- }
25- if ("BUG_REPORT_URL=" in line) {
26- bugtracker_url = line.replace ("BUG_REPORT_URL=", "");
27- if ("\"" in bugtracker_url) {
28- bugtracker_url = bugtracker_url.replace ("\"", "");
29- }
30- }
31- if ("SUPPORT_URL=" in line) {
32- support_url = line.replace ("SUPPORT_URL=", "");
33- if ("\"" in support_url) {
34- support_url = support_url.replace ("\"", "");
35- }
36+ var osrel_component = line.split ("=", 2);
37+ if ( osrel_component.length == 2 ) {
38+ osrel[osrel_component[0]] = osrel_component[1].replace ("\"", "");
39 }
40 }
41+
42+ os = osrel["PRETTY_NAME"];
43+ website_url = osrel["HOME_URL"];
44+ bugtracker_url = osrel["BUG_REPORT_URL"];
45+ support_url = osrel["SUPPORT_URL"];
46 } catch (Error e) {
47 warning("Couldn't read os-release file, assuming elementary OS");
48 os = "elementary OS";
49@@ -120,7 +106,7 @@
50
51 //Upstream distro version (for "Built on" text)
52 //FIXME: Add distro specific field to /etc/os-release and use that instead
53- // Like "ELEMENTARYOS_UPSTREAM_PRETTY_ID" or something
54+ // Like "ELEMENTARY_UPSTREAM_DISTRO_NAME" or something
55 file = File.new_for_path("/etc/upstream-release/lsb-release");
56 try {
57 var dis = new DataInputStream (file.read ());

Subscribers

People subscribed via source and target branches

to all changes: