Statistics
| Branch: | Tag: | Revision:

root / autotools / convert-constants @ d72ff6c3

History | View | Annotate | Download (2.6 kB)

1 d99d1e36 Iustin Pop
#!/usr/bin/python
2 d99d1e36 Iustin Pop
#
3 d99d1e36 Iustin Pop
4 d99d1e36 Iustin Pop
# Copyright (C) 2011 Google Inc.
5 d99d1e36 Iustin Pop
#
6 d99d1e36 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 d99d1e36 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 d99d1e36 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 d99d1e36 Iustin Pop
# (at your option) any later version.
10 d99d1e36 Iustin Pop
#
11 d99d1e36 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 d99d1e36 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 d99d1e36 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 d99d1e36 Iustin Pop
# General Public License for more details.
15 d99d1e36 Iustin Pop
#
16 d99d1e36 Iustin Pop
# You should have received a copy of the GNU General Public License
17 d99d1e36 Iustin Pop
# along with this program; if not, write to the Free Software
18 d99d1e36 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 d99d1e36 Iustin Pop
# 02110-1301, USA.
20 d99d1e36 Iustin Pop
21 d99d1e36 Iustin Pop
"""Script for converting Python constants to Haskell code fragments.
22 d99d1e36 Iustin Pop
23 d99d1e36 Iustin Pop
"""
24 d99d1e36 Iustin Pop
25 d99d1e36 Iustin Pop
import re
26 d99d1e36 Iustin Pop
27 d99d1e36 Iustin Pop
from ganeti import constants
28 d99d1e36 Iustin Pop
29 d99d1e36 Iustin Pop
CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_]+$")
30 d99d1e36 Iustin Pop
31 d99d1e36 Iustin Pop
32 d99d1e36 Iustin Pop
def NameRules(name):
33 d99d1e36 Iustin Pop
  """Converts the upper-cased Python name to Haskell camelCase.
34 d99d1e36 Iustin Pop
35 d99d1e36 Iustin Pop
  """
36 d99d1e36 Iustin Pop
  elems = name.split("_")
37 d99d1e36 Iustin Pop
  return elems[0].lower() + "".join(e.capitalize() for e in elems[1:])
38 d99d1e36 Iustin Pop
39 d99d1e36 Iustin Pop
40 d99d1e36 Iustin Pop
def StringValueRules(value):
41 d99d1e36 Iustin Pop
  """Converts a string value from Python to Haskell.
42 d99d1e36 Iustin Pop
43 d99d1e36 Iustin Pop
  """
44 d99d1e36 Iustin Pop
  value = value.encode("string_escape") # escapes backslashes
45 d99d1e36 Iustin Pop
  value = value.replace("\"", "\\\"")
46 d99d1e36 Iustin Pop
  return value
47 d99d1e36 Iustin Pop
48 d99d1e36 Iustin Pop
49 d99d1e36 Iustin Pop
def Convert():
50 d99d1e36 Iustin Pop
  """Converts the constants to Haskell.
51 d99d1e36 Iustin Pop
52 d99d1e36 Iustin Pop
  """
53 d99d1e36 Iustin Pop
  lines = [""]
54 d99d1e36 Iustin Pop
55 d99d1e36 Iustin Pop
  all_names = dir(constants)
56 d99d1e36 Iustin Pop
57 d99d1e36 Iustin Pop
  for name in all_names:
58 d99d1e36 Iustin Pop
    value = getattr(constants, name)
59 d99d1e36 Iustin Pop
    hs_name = NameRules(name)
60 d99d1e36 Iustin Pop
    if not CONSTANT_RE.match(name):
61 d99d1e36 Iustin Pop
      lines.append("-- Skipped %s, not constant" % name)
62 d99d1e36 Iustin Pop
    elif isinstance(value, basestring):
63 606e71d3 Iustin Pop
      lines.append("-- | Converted from Python constant %s" % name)
64 d99d1e36 Iustin Pop
      lines.append("%s :: String" % hs_name)
65 d99d1e36 Iustin Pop
      lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value)))
66 d99d1e36 Iustin Pop
    elif isinstance(value, int):
67 606e71d3 Iustin Pop
      lines.append("-- | Converted from Python constant %s" % name)
68 d99d1e36 Iustin Pop
      lines.append("%s :: Int" % hs_name)
69 d99d1e36 Iustin Pop
      lines.append("%s = %d" % (hs_name, value))
70 d99d1e36 Iustin Pop
    elif isinstance(value, long):
71 606e71d3 Iustin Pop
      lines.append("-- | Converted from Python constant %s" % name)
72 d99d1e36 Iustin Pop
      lines.append("%s :: Integer" % hs_name)
73 d99d1e36 Iustin Pop
      lines.append("%s = %d" % (hs_name, value))
74 d99d1e36 Iustin Pop
    elif isinstance(value, float):
75 606e71d3 Iustin Pop
      lines.append("-- | Converted from Python constant %s" % name)
76 d99d1e36 Iustin Pop
      lines.append("%s :: Double" % hs_name)
77 d99d1e36 Iustin Pop
      lines.append("%s = %f" % (hs_name, value))
78 d99d1e36 Iustin Pop
    else:
79 d99d1e36 Iustin Pop
      lines.append("-- Skipped %s, %s not handled" % (name, type(value)))
80 d99d1e36 Iustin Pop
    lines.append("")
81 d99d1e36 Iustin Pop
82 d99d1e36 Iustin Pop
  return "\n".join(lines)
83 d99d1e36 Iustin Pop
84 d99d1e36 Iustin Pop
85 d99d1e36 Iustin Pop
def main():
86 d99d1e36 Iustin Pop
  print Convert()
87 d99d1e36 Iustin Pop
88 d99d1e36 Iustin Pop
89 d99d1e36 Iustin Pop
if __name__ == "__main__":
90 d99d1e36 Iustin Pop
  main()