[virt-tools-list] [libosinfo PATCHv2 10/11] Automatically remap OsinfoInstallConfig values if needed

Christophe Fergeau cfergeau at redhat.com
Tue Dec 11 18:20:14 UTC 2012


Now that OsinfoInstallConfig has access to the
OsinfoInstallConfigParamList for the OsinfoInstallScript that is
being configured, we can use the OsinfoDatamap that is optionally
set on a given parameter to automatically translate a value for
this parameter from a generic libosinfo value to an OS-specific one.
---
 osinfo/osinfo_install_config.c | 204 ++++++++++++++++++++++++++++++-----------
 1 file changed, 152 insertions(+), 52 deletions(-)

diff --git a/osinfo/osinfo_install_config.c b/osinfo/osinfo_install_config.c
index 52eb062..f2e3746 100644
--- a/osinfo/osinfo_install_config.c
+++ b/osinfo/osinfo_install_config.c
@@ -30,6 +30,11 @@ G_DEFINE_TYPE (OsinfoInstallConfig, osinfo_install_config, OSINFO_TYPE_ENTITY);
 
 #define OSINFO_INSTALL_CONFIG_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), OSINFO_TYPE_INSTALL_CONFIG, OsinfoInstallConfigPrivate))
 
+static void osinfo_install_config_set_param(OsinfoInstallConfig *config,
+                                            const gchar *key,
+                                            const gchar *value);
+static const gchar *osinfo_install_config_get_param_value(OsinfoInstallConfig *config,
+                                                          const gchar *key);
 /**
  * SECTION:osinfo_install_config
  * @short_description: OS install configuration
@@ -106,6 +111,50 @@ osinfo_install_config_finalize (GObject *object)
 
 
 /* Init functions */
+static const gchar valid[] = {
+    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'X', 'W', 'X', 'Y', 'Z',
+    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_', '+',
+    '=', '!', '@', '#', '%', '^', '&', ',', '(', ')', '[', '{', '}',
+    '[', ']', ';', ':', '<', '>', ',', '.', '?', '/', '~',
+};
+
+static void osinfo_install_config_constructed(GObject *object)
+{
+    OsinfoInstallConfig *config = OSINFO_INSTALL_CONFIG(object);
+    gchar pass[9];
+    gsize i;
+
+    G_OBJECT_CLASS(osinfo_install_config_parent_class)->constructed(object);
+
+    /* osinfo_install_config_set_param() needs the GObject properties
+     * to be set as we will get the needed datamaps from the 'valid-params'
+     * property
+     */
+    osinfo_install_config_set_param(config,
+                                    OSINFO_INSTALL_CONFIG_PROP_L10N_KEYBOARD,
+                                    "us");
+    osinfo_install_config_set_param(config,
+                                    OSINFO_INSTALL_CONFIG_PROP_L10N_TIMEZONE,
+                                    "America/New_York");
+    osinfo_install_config_set_param(config,
+                                    OSINFO_INSTALL_CONFIG_PROP_L10N_LANGUAGE,
+                                    "en_US.UTF-8");
+
+    for (i = 0 ; i < sizeof(pass)-1 ; i++) {
+        gint val = g_random_int_range(0, sizeof(valid));
+        pass[i] = valid[val];
+    }
+    pass[sizeof(pass)-1] = '\0';
+
+    osinfo_entity_set_param(OSINFO_ENTITY(config),
+                            OSINFO_INSTALL_CONFIG_PROP_ADMIN_PASSWORD,
+                            pass);
+}
+
+
 static void
 osinfo_install_config_class_init (OsinfoInstallConfigClass *klass)
 {
@@ -114,6 +163,7 @@ osinfo_install_config_class_init (OsinfoInstallConfigClass *klass)
 
     g_klass->get_property = osinfo_install_config_get_property;
     g_klass->set_property = osinfo_install_config_set_property;
+    g_klass->constructed = osinfo_install_config_constructed;
     g_klass->finalize = osinfo_install_config_finalize;
 
     pspec = g_param_spec_object("valid-params",
@@ -130,45 +180,13 @@ osinfo_install_config_class_init (OsinfoInstallConfigClass *klass)
     g_type_class_add_private (klass, sizeof (OsinfoInstallConfigPrivate));
 }
 
-static const gchar valid[] = {
-    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'X', 'W', 'X', 'Y', 'Z',
-    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_', '+',
-    '=', '!', '@', '#', '%', '^', '&', ',', '(', ')', '[', '{', '}',
-    '[', ']', ';', ':', '<', '>', ',', '.', '?', '/', '~',
-};
-
 
 static void
 osinfo_install_config_init (OsinfoInstallConfig *config)
 {
     OsinfoInstallConfigPrivate *priv;
-    gchar pass[9];
-    gsize i;
 
     config->priv = priv = OSINFO_INSTALL_CONFIG_GET_PRIVATE(config);
-
-    osinfo_entity_set_param(OSINFO_ENTITY(config),
-                            OSINFO_INSTALL_CONFIG_PROP_L10N_KEYBOARD,
-                            "us");
-    osinfo_entity_set_param(OSINFO_ENTITY(config),
-                            OSINFO_INSTALL_CONFIG_PROP_L10N_TIMEZONE,
-                            "America/New_York");
-    osinfo_entity_set_param(OSINFO_ENTITY(config),
-                            OSINFO_INSTALL_CONFIG_PROP_L10N_LANGUAGE,
-                            "en_US.UTF-8");
-
-    for (i = 0 ; i < sizeof(pass)-1 ; i++) {
-        gint val = g_random_int_range(0, sizeof(valid));
-        pass[i] = valid[val];
-    }
-    pass[sizeof(pass)-1] = '\0';
-
-    osinfo_entity_set_param(OSINFO_ENTITY(config),
-                            OSINFO_INSTALL_CONFIG_PROP_ADMIN_PASSWORD,
-                            pass);
 }
 
 
@@ -206,16 +224,16 @@ OsinfoInstallConfig *osinfo_install_config_new_for_script(const gchar *id,
 void osinfo_install_config_set_hardware_arch(OsinfoInstallConfig *config,
                                              const gchar *arch)
 {
-    osinfo_entity_set_param(OSINFO_ENTITY(config),
-                            OSINFO_INSTALL_CONFIG_PROP_HARDWARE_ARCH,
-                            arch);
+    osinfo_install_config_set_param(config,
+                                    OSINFO_INSTALL_CONFIG_PROP_HARDWARE_ARCH,
+                                    arch);
 }
 
 
 const gchar *osinfo_install_config_get_hardware_arch(OsinfoInstallConfig *config)
 {
-    return osinfo_entity_get_param_value(OSINFO_ENTITY(config),
-                                         OSINFO_INSTALL_CONFIG_PROP_HARDWARE_ARCH);
+    return osinfo_install_config_get_param_value(config,
+                                                 OSINFO_INSTALL_CONFIG_PROP_HARDWARE_ARCH);
 }
 
 
@@ -232,16 +250,16 @@ const gchar *osinfo_install_config_get_hardware_arch(OsinfoInstallConfig *config
 void osinfo_install_config_set_l10n_keyboard(OsinfoInstallConfig *config,
                                              const gchar *keyboard)
 {
-    osinfo_entity_set_param(OSINFO_ENTITY(config),
-                            OSINFO_INSTALL_CONFIG_PROP_L10N_KEYBOARD,
-                            keyboard);
+    osinfo_install_config_set_param(config,
+                                    OSINFO_INSTALL_CONFIG_PROP_L10N_KEYBOARD,
+                                    keyboard);
 }
 
 
 const gchar *osinfo_install_config_get_l10n_keyboard(OsinfoInstallConfig *config)
 {
-    return osinfo_entity_get_param_value(OSINFO_ENTITY(config),
-                                         OSINFO_INSTALL_CONFIG_PROP_L10N_KEYBOARD);
+    return osinfo_install_config_get_param_value(config,
+                                                 OSINFO_INSTALL_CONFIG_PROP_L10N_KEYBOARD);
 }
 
 /**
@@ -261,32 +279,32 @@ const gchar *osinfo_install_config_get_l10n_keyboard(OsinfoInstallConfig *config
 void osinfo_install_config_set_l10n_language(OsinfoInstallConfig *config,
                                              const gchar *language)
 {
-    osinfo_entity_set_param(OSINFO_ENTITY(config),
-                            OSINFO_INSTALL_CONFIG_PROP_L10N_LANGUAGE,
-                            language);
+    osinfo_install_config_set_param(config,
+                                    OSINFO_INSTALL_CONFIG_PROP_L10N_LANGUAGE,
+                                    language);
 }
 
 
 const gchar *osinfo_install_config_get_l10n_language(OsinfoInstallConfig *config)
 {
-    return osinfo_entity_get_param_value(OSINFO_ENTITY(config),
-                                         OSINFO_INSTALL_CONFIG_PROP_L10N_LANGUAGE);
+    return osinfo_install_config_get_param_value(config,
+                                                 OSINFO_INSTALL_CONFIG_PROP_L10N_LANGUAGE);
 }
 
 
 void osinfo_install_config_set_l10n_timezone(OsinfoInstallConfig *config,
                                              const gchar *tz)
 {
-    osinfo_entity_set_param(OSINFO_ENTITY(config),
-                            OSINFO_INSTALL_CONFIG_PROP_L10N_TIMEZONE,
-                            tz);
+    osinfo_install_config_set_param(config,
+                                    OSINFO_INSTALL_CONFIG_PROP_L10N_TIMEZONE,
+                                    tz);
 }
 
 
 const gchar *osinfo_install_config_get_l10n_timezone(OsinfoInstallConfig *config)
 {
-    return osinfo_entity_get_param_value(OSINFO_ENTITY(config),
-                                         OSINFO_INSTALL_CONFIG_PROP_L10N_TIMEZONE);
+    return osinfo_install_config_get_param_value(config,
+                                                 OSINFO_INSTALL_CONFIG_PROP_L10N_TIMEZONE);
 }
 
 
@@ -746,6 +764,88 @@ OsinfoInstallConfigParamList *osinfo_install_config_get_valid_params(OsinfoInsta
     return config->priv->valid_params;
 }
 
+
+static const gchar *
+osinfo_install_config_transform_value_generic(OsinfoInstallConfig *config,
+                                              const gchar *(*mapping_func)(OsinfoDatamap *map, const gchar *val),
+                                              const gchar *key,
+                                              const gchar *value)
+{
+    OsinfoDatamap *map;
+    OsinfoEntity *entity;
+    OsinfoInstallConfigParam *param;
+    const gchar *transformed_value;
+
+    if (!config->priv->valid_params)
+        return value;
+
+    entity = osinfo_list_find_by_id(OSINFO_LIST(config->priv->valid_params),
+                                    key);
+    if (entity == NULL) {
+        g_warning("%s is not a known parameter for this config", key);
+        return value;
+    }
+
+    param = OSINFO_INSTALL_CONFIG_PARAM(entity);;
+    map = osinfo_install_config_param_get_value_map(param);
+    if (map == NULL) {
+        g_debug("no remapping to be done for %s", key);
+        return value;
+    }
+    transformed_value = mapping_func(map, value);
+    if (transformed_value == NULL) {
+        g_warning("value not present in %s datamap: %s", key, value);
+        return value;
+    }
+
+    return transformed_value;
+}
+
+
+static const gchar *
+osinfo_install_config_transform_value(OsinfoInstallConfig *config,
+                                      const gchar *key,
+                                      const gchar *value)
+{
+    return osinfo_install_config_transform_value_generic(config,
+                                                         osinfo_datamap_lookup,
+                                                         key, value);
+}
+
+
+static const gchar *
+osinfo_install_config_untransform_value(OsinfoInstallConfig *config,
+                                        const gchar *key,
+                                        const gchar *value)
+{
+    return osinfo_install_config_transform_value_generic(config,
+                                                         osinfo_datamap_reverse_lookup,
+                                                         key, value);
+}
+
+
+static void
+osinfo_install_config_set_param(OsinfoInstallConfig *config,
+                                const gchar *key,
+                                const gchar *value)
+{
+    value = osinfo_install_config_transform_value(config, key, value);
+    osinfo_entity_set_param(OSINFO_ENTITY(config), key, value);
+}
+
+
+static const gchar *
+osinfo_install_config_get_param_value(OsinfoInstallConfig *config, const gchar *key)
+{
+    const gchar *value;
+
+    value = osinfo_entity_get_param_value(OSINFO_ENTITY(config), key);
+    if (value == NULL)
+        return NULL;
+
+    return osinfo_install_config_untransform_value(config, key, value);
+}
+
 /*
  * Local variables:
  *  indent-tabs-mode: nil
-- 
1.8.0.1




More information about the virt-tools-list mailing list