config: Add check for disk's “iv_name”
[ganeti-local] / autotools / convert-constants
index 3f2b362..ae36774 100755 (executable)
 import re
 
 from ganeti import constants
+from ganeti import compat
 
-CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_]+$")
+#: Constant name regex
+CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_-]+$")
+
+#: The type of regex objects
+RE_TYPE = type(CONSTANT_RE)
 
 
 def NameRules(name):
   """Converts the upper-cased Python name to Haskell camelCase.
 
   """
+  name = name.replace("-", "_")
   elems = name.split("_")
   return elems[0].lower() + "".join(e.capitalize() for e in elems[1:])
 
@@ -46,6 +52,98 @@ def StringValueRules(value):
   return value
 
 
+def DictKeyName(dict_name, key_name):
+  """Converts a dict plus key name to a full name.
+
+  """
+  return"%s_%s" % (dict_name, str(key_name).upper())
+
+
+def HaskellTypeVal(value):
+  """Returns the Haskell type and value for a Python value.
+
+  Note that this only work for 'plain' Python types.
+
+  @returns: (string, string) or None, if we can't determine the type.
+
+  """
+  if isinstance(value, basestring):
+    return ("String", "\"%s\"" % StringValueRules(value))
+  elif isinstance(value, int):
+    return ("Int", "%d" % value)
+  elif isinstance(value, long):
+    return ("Integer", "%d" % value)
+  elif isinstance(value, float):
+    return ("Double", "%f" % value)
+  else:
+    return None
+
+
+def ConvertVariable(name, value):
+  """Converts a given variable to Haskell code.
+
+  @param name: the Python name
+  @param value: the value
+  @return: a list of Haskell code lines
+
+  """
+  lines = []
+  hs_name = NameRules(name)
+  hs_typeval = HaskellTypeVal(value)
+  if not CONSTANT_RE.match(name):
+    lines.append("-- Skipped %s, not constant" % name)
+  elif hs_typeval is not None:
+    # this is a simple value
+    (hs_type, hs_val) = hs_typeval
+    lines.append("-- | Converted from Python constant %s" % name)
+    lines.append("%s :: %s" % (hs_name, hs_type))
+    lines.append("%s = %s" % (hs_name, hs_val))
+  elif isinstance(value, dict):
+    if value:
+      lines.append("-- Following lines come from dictionary %s" % name)
+      for k in sorted(value.keys()):
+        lines.extend(ConvertVariable(DictKeyName(name, k), value[k]))
+  elif isinstance(value, tuple):
+    tvs = [HaskellTypeVal(elem) for elem in value]
+    if compat.all(e is not None for e in tvs):
+      ttypes = ", ".join(e[0] for e in tvs)
+      tvals = ", ".join(e[1] for e in tvs)
+      lines.append("-- | Converted from Python tuple %s" % name)
+      lines.append("%s :: (%s)" % (hs_name, ttypes))
+      lines.append("%s = (%s)" % (hs_name, tvals))
+    else:
+      lines.append("-- Skipped tuple %s, cannot convert all elements" % name)
+  elif isinstance(value, (list, set, frozenset)):
+    # Lists and frozensets are handled the same in Haskell: as lists,
+    # since lists are immutable and we don't need for constants the
+    # high-speed of an actual Set type. However, we can only convert
+    # them if they have the same type for all elements (which is a
+    # normal expectation for constants, our code should be well
+    # behaved); note that this is different from the tuples case,
+    # where we always (for some values of always) can convert
+    tvs = [HaskellTypeVal(elem) for elem in value]
+    if compat.all(e is not None for e in tvs):
+      ttypes, tvals = zip(*tvs)
+      uniq_types = set(ttypes)
+      if len(uniq_types) == 1:
+        lines.append("-- | Converted from Python list or set %s" % name)
+        lines.append("%s :: [%s]" % (hs_name, uniq_types.pop()))
+        lines.append("%s = [%s]" % (hs_name, ", ".join(tvals)))
+      else:
+        lines.append("-- | Skipped list/set %s, is not homogeneous" % name)
+    else:
+      lines.append("-- | Skipped list/set %s, cannot convert all elems" % name)
+  elif isinstance(value, RE_TYPE):
+    tvs = HaskellTypeVal(value.pattern)
+    assert tvs is not None
+    lines.append("-- | Converted from Python RE object %s" % name)
+    lines.append("%s :: %s" % (hs_name, tvs[0]))
+    lines.append("%s = %s" % (hs_name, tvs[1]))
+  else:
+    lines.append("-- Skipped %s, %s not handled" % (name, type(value)))
+  return lines
+
+
 def Convert():
   """Converts the constants to Haskell.
 
@@ -56,27 +154,7 @@ def Convert():
 
   for name in all_names:
     value = getattr(constants, name)
-    hs_name = NameRules(name)
-    if not CONSTANT_RE.match(name):
-      lines.append("-- Skipped %s, not constant" % name)
-    elif isinstance(value, basestring):
-      lines.append("-- | Converted from Python constant %s" % name)
-      lines.append("%s :: String" % hs_name)
-      lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value)))
-    elif isinstance(value, int):
-      lines.append("-- | Converted from Python constant %s" % name)
-      lines.append("%s :: Int" % hs_name)
-      lines.append("%s = %d" % (hs_name, value))
-    elif isinstance(value, long):
-      lines.append("-- | Converted from Python constant %s" % name)
-      lines.append("%s :: Integer" % hs_name)
-      lines.append("%s = %d" % (hs_name, value))
-    elif isinstance(value, float):
-      lines.append("-- | Converted from Python constant %s" % name)
-      lines.append("%s :: Double" % hs_name)
-      lines.append("%s = %f" % (hs_name, value))
-    else:
-      lines.append("-- Skipped %s, %s not handled" % (name, type(value)))
+    lines.extend(ConvertVariable(name, value))
     lines.append("")
 
   return "\n".join(lines)