Restore settings to default on error

In the event that the value read from a persistence file is
invalid, currently a ValueError exception is thrown and the
settingsd fails to run.  Instead, catch this condition and
restore the setting to default.

Fixes openbmc/openbmc#1442.

Change-Id: I22ce8837d9c48b21bab050f1de9b730ec219bc4b
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/settings_manager.py b/settings_manager.py
index 9c797d9..134bc6d 100644
--- a/settings_manager.py
+++ b/settings_manager.py
@@ -132,15 +132,26 @@
     # or the default file value if the BMC value
     # does not exist.
     def set_settings_property(self, attr_name, attr_type, value, fname):
+        default = value
+
+        # Read from file.
         bmcv = self.get_bmc_value(attr_name, fname)
         if bmcv:
             value = bmcv
-        if attr_type == "i":
-            self.Set(DBUS_NAME, attr_name, int(value))
-        elif attr_type == "s":
-            self.Set(DBUS_NAME, attr_name, str(value))
-        elif attr_type == "b":
-            self.Set(DBUS_NAME, attr_name, bool(value))
+
+        # Perform type mapping.
+        type_map = {"i": int, "s": str, "b": bool}[attr_type]
+        real_value = type_map(value)
+        real_default = type_map(default)
+
+        try:
+            self.Set(DBUS_NAME, attr_name, real_value)
+        except ValueError:
+            print("Persistent value for {} is invalid: {}{} had '{}', "
+                  "using '{}'.".format(attr_name, fname, attr_name,
+                                       value, default))
+            self.Set(DBUS_NAME, attr_name, real_default)
+            self.set_system_settings(attr_name, real_default, fname)
 
     # Save the settings to the BMC. This will write the settings value in
     # individual files named by the property name to the BMC.