Revision 8751c041 autotools/convert-constants
b/autotools/convert-constants | ||
---|---|---|
26 | 26 |
|
27 | 27 |
from ganeti import constants |
28 | 28 |
|
29 |
CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_]+$") |
|
29 |
CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_-]+$")
|
|
30 | 30 |
|
31 | 31 |
|
32 | 32 |
def NameRules(name): |
33 | 33 |
"""Converts the upper-cased Python name to Haskell camelCase. |
34 | 34 |
|
35 | 35 |
""" |
36 |
name = name.replace("-", "_") |
|
36 | 37 |
elems = name.split("_") |
37 | 38 |
return elems[0].lower() + "".join(e.capitalize() for e in elems[1:]) |
38 | 39 |
|
... | ... | |
46 | 47 |
return value |
47 | 48 |
|
48 | 49 |
|
50 |
def DictKeyName(dict_name, key_name): |
|
51 |
"""Converts a dict plus key name to a full name. |
|
52 |
|
|
53 |
""" |
|
54 |
return"%s_%s" % (dict_name, str(key_name).upper()) |
|
55 |
|
|
56 |
|
|
57 |
def ConvertVariable(name, value): |
|
58 |
"""Converts a given variable to Haskell code. |
|
59 |
|
|
60 |
@param name: the Python name |
|
61 |
@param value: the value |
|
62 |
@return: a list of Haskell code lines |
|
63 |
|
|
64 |
""" |
|
65 |
lines = [] |
|
66 |
hs_name = NameRules(name) |
|
67 |
if not CONSTANT_RE.match(name): |
|
68 |
lines.append("-- Skipped %s, not constant" % name) |
|
69 |
elif isinstance(value, basestring): |
|
70 |
lines.append("-- | Converted from Python constant %s" % name) |
|
71 |
lines.append("%s :: String" % hs_name) |
|
72 |
lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value))) |
|
73 |
elif isinstance(value, int): |
|
74 |
lines.append("-- | Converted from Python constant %s" % name) |
|
75 |
lines.append("%s :: Int" % hs_name) |
|
76 |
lines.append("%s = %d" % (hs_name, value)) |
|
77 |
elif isinstance(value, long): |
|
78 |
lines.append("-- | Converted from Python constant %s" % name) |
|
79 |
lines.append("%s :: Integer" % hs_name) |
|
80 |
lines.append("%s = %d" % (hs_name, value)) |
|
81 |
elif isinstance(value, float): |
|
82 |
lines.append("-- | Converted from Python constant %s" % name) |
|
83 |
lines.append("%s :: Double" % hs_name) |
|
84 |
lines.append("%s = %f" % (hs_name, value)) |
|
85 |
elif isinstance(value, dict): |
|
86 |
if value: |
|
87 |
lines.append("-- Following lines come from dictionary %s" % name) |
|
88 |
for k in sorted(value.keys()): |
|
89 |
lines.extend(ConvertVariable(DictKeyName(name, k), value[k])) |
|
90 |
else: |
|
91 |
lines.append("-- Skipped %s, %s not handled" % (name, type(value))) |
|
92 |
return lines |
|
93 |
|
|
94 |
|
|
49 | 95 |
def Convert(): |
50 | 96 |
"""Converts the constants to Haskell. |
51 | 97 |
|
... | ... | |
56 | 102 |
|
57 | 103 |
for name in all_names: |
58 | 104 |
value = getattr(constants, name) |
59 |
hs_name = NameRules(name) |
|
60 |
if not CONSTANT_RE.match(name): |
|
61 |
lines.append("-- Skipped %s, not constant" % name) |
|
62 |
elif isinstance(value, basestring): |
|
63 |
lines.append("-- | Converted from Python constant %s" % name) |
|
64 |
lines.append("%s :: String" % hs_name) |
|
65 |
lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value))) |
|
66 |
elif isinstance(value, int): |
|
67 |
lines.append("-- | Converted from Python constant %s" % name) |
|
68 |
lines.append("%s :: Int" % hs_name) |
|
69 |
lines.append("%s = %d" % (hs_name, value)) |
|
70 |
elif isinstance(value, long): |
|
71 |
lines.append("-- | Converted from Python constant %s" % name) |
|
72 |
lines.append("%s :: Integer" % hs_name) |
|
73 |
lines.append("%s = %d" % (hs_name, value)) |
|
74 |
elif isinstance(value, float): |
|
75 |
lines.append("-- | Converted from Python constant %s" % name) |
|
76 |
lines.append("%s :: Double" % hs_name) |
|
77 |
lines.append("%s = %f" % (hs_name, value)) |
|
78 |
else: |
|
79 |
lines.append("-- Skipped %s, %s not handled" % (name, type(value))) |
|
105 |
lines.extend(ConvertVariable(name, value)) |
|
80 | 106 |
lines.append("") |
81 | 107 |
|
82 | 108 |
return "\n".join(lines) |
Also available in: Unified diff