root / autotools / convert-constants @ 8751c041
History | View | Annotate | Download (3.2 kB)
1 |
#!/usr/bin/python |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2011 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
"""Script for converting Python constants to Haskell code fragments. |
22 |
|
23 |
""" |
24 |
|
25 |
import re |
26 |
|
27 |
from ganeti import constants |
28 |
|
29 |
CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_-]+$") |
30 |
|
31 |
|
32 |
def NameRules(name): |
33 |
"""Converts the upper-cased Python name to Haskell camelCase. |
34 |
|
35 |
""" |
36 |
name = name.replace("-", "_") |
37 |
elems = name.split("_") |
38 |
return elems[0].lower() + "".join(e.capitalize() for e in elems[1:]) |
39 |
|
40 |
|
41 |
def StringValueRules(value): |
42 |
"""Converts a string value from Python to Haskell. |
43 |
|
44 |
""" |
45 |
value = value.encode("string_escape") # escapes backslashes |
46 |
value = value.replace("\"", "\\\"") |
47 |
return value |
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 |
|
95 |
def Convert(): |
96 |
"""Converts the constants to Haskell. |
97 |
|
98 |
""" |
99 |
lines = [""] |
100 |
|
101 |
all_names = dir(constants) |
102 |
|
103 |
for name in all_names: |
104 |
value = getattr(constants, name) |
105 |
lines.extend(ConvertVariable(name, value)) |
106 |
lines.append("") |
107 |
|
108 |
return "\n".join(lines) |
109 |
|
110 |
|
111 |
def main(): |
112 |
print Convert() |
113 |
|
114 |
|
115 |
if __name__ == "__main__": |
116 |
main() |