[Libosinfo] [PATCHv4 07/11] Automatically remap OsinfoInstallConfig values if needed

Christophe Fergeau cfergeau at redhat.com
Mon Dec 17 21:07:50 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/Makefile.am                     |  1 +
 osinfo/osinfo_install_config.c         | 85 ++++++++++++++++++++++++++++++++++
 osinfo/osinfo_install_config_private.h | 38 +++++++++++++++
 3 files changed, 124 insertions(+)
 create mode 100644 osinfo/osinfo_install_config_private.h

diff --git a/osinfo/Makefile.am b/osinfo/Makefile.am
index 9d03a34..9f3c20d 100644
--- a/osinfo/Makefile.am
+++ b/osinfo/Makefile.am
@@ -116,6 +116,7 @@ libosinfo_1_0_la_SOURCES =		\
   osinfo_install_config.c		\
   osinfo_install_config_param.c		\
   osinfo_install_config_paramlist.c	\
+  osinfo_install_config_private.h	\
   osinfo_install_script.c		\
   osinfo_install_script_private.h	\
   osinfo_install_scriptlist.c		\
diff --git a/osinfo/osinfo_install_config.c b/osinfo/osinfo_install_config.c
index 8078f8c..7f75841 100644
--- a/osinfo/osinfo_install_config.c
+++ b/osinfo/osinfo_install_config.c
@@ -24,6 +24,7 @@
 #include <config.h>
 
 #include <osinfo/osinfo.h>
+#include "osinfo/osinfo_install_config_private.h"
 #include <glib/gi18n-lib.h>
 
 G_DEFINE_TYPE (OsinfoInstallConfig, osinfo_install_config, OSINFO_TYPE_ENTITY);
@@ -730,6 +731,90 @@ OsinfoInstallConfigParamList *osinfo_install_config_get_config_params(OsinfoInst
     return config->priv->config_params;
 }
 
+
+static const gchar *
+osinfo_install_config_transform_value(OsinfoInstallConfig *config,
+                                      const gchar *key,
+                                      const gchar *value)
+{
+    OsinfoDatamap *map;
+    OsinfoEntity *entity;
+    OsinfoInstallConfigParam *param;
+    const gchar *transformed_value;
+
+    if (!config->priv->config_params)
+        return value;
+
+    entity = osinfo_list_find_by_id(OSINFO_LIST(config->priv->config_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 = osinfo_datamap_lookup(map, value);
+    if (transformed_value == NULL) {
+        g_warning("value not present in %s datamap: %s", key, value);
+        return value;
+    }
+
+    return transformed_value;
+}
+
+static GHashTable *get_remapped_keys_once(void)
+{
+    const char *remapped_properties[] = {
+        OSINFO_INSTALL_CONFIG_PROP_HARDWARE_ARCH,
+        OSINFO_INSTALL_CONFIG_PROP_L10N_KEYBOARD,
+        OSINFO_INSTALL_CONFIG_PROP_L10N_LANGUAGE,
+        OSINFO_INSTALL_CONFIG_PROP_L10N_TIMEZONE,
+        NULL
+    };
+    const char **it;
+    GHashTable *remapped_keys;
+
+    remapped_keys = g_hash_table_new(g_str_hash, g_str_equal);
+    for (it = remapped_properties; *it != NULL; it++) {
+        g_hash_table_add(remapped_keys, (gpointer)*it);
+    }
+
+    return remapped_keys;
+}
+
+GList *
+osinfo_install_config_get_param_value_list(OsinfoInstallConfig *config,
+                                           const gchar *key)
+{
+    GList *values;
+    GList *it;
+    static GOnce remapped_keys_once = G_ONCE_INIT;
+    GHashTable *remapped_keys;
+
+    values = osinfo_entity_get_param_value_list(OSINFO_ENTITY(config), key);
+    if (values == NULL)
+        return NULL;
+
+    remapped_keys = g_once(&remapped_keys_once,
+                           (GThreadFunc)get_remapped_keys_once,
+                           NULL);
+    if (!g_hash_table_contains(remapped_keys, key))
+        return values;
+
+    for (it = values; it != NULL; it = it->next) {
+        it->data = (gpointer)osinfo_install_config_transform_value(config,
+                                                                   key,
+                                                                   it->data);
+    }
+
+    return values;
+}
+
 /*
  * Local variables:
  *  indent-tabs-mode: nil
diff --git a/osinfo/osinfo_install_config_private.h b/osinfo/osinfo_install_config_private.h
new file mode 100644
index 0000000..e08f863
--- /dev/null
+++ b/osinfo/osinfo_install_config_private.h
@@ -0,0 +1,38 @@
+/*
+ * libosinfo: OS installation config
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Christophe Fergeau <cfergeau at redhat.com>
+ */
+
+#include <osinfo/osinfo_install_config.h>
+
+#ifndef __OSINFO_INSTALL_CONFIG_PRIVATE_H__
+#define __OSINFO_INSTALL_CONFIG_PRIVATE_H__
+
+GList *osinfo_install_config_get_param_value_list(OsinfoInstallConfig *config, const gchar *key);
+
+#endif /* __OSINFO_INSTALL_CONFIG_PRIVATE_H__ */
+/*
+ * Local variables:
+ *  indent-tabs-mode: nil
+ *  c-indent-level: 4
+ *  c-basic-offset: 4
+ * End:
+ */
-- 
1.8.0.2




More information about the Libosinfo mailing list