convert-constants 2.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/python
#

# Copyright (C) 2011 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

"""Script for converting Python constants to Haskell code fragments.

"""

import re

from ganeti import constants

CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_]+$")


def NameRules(name):
  """Converts the upper-cased Python name to Haskell camelCase.

  """
  elems = name.split("_")
  return elems[0].lower() + "".join(e.capitalize() for e in elems[1:])


def StringValueRules(value):
  """Converts a string value from Python to Haskell.

  """
  value = value.encode("string_escape") # escapes backslashes
  value = value.replace("\"", "\\\"")
  return value


def Convert():
  """Converts the constants to Haskell.

  """
  lines = [""]

  all_names = dir(constants)

  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):
63
      lines.append("-- | Converted from Python constant %s" % name)
64
65
66
      lines.append("%s :: String" % hs_name)
      lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value)))
    elif isinstance(value, int):
67
      lines.append("-- | Converted from Python constant %s" % name)
68
69
70
      lines.append("%s :: Int" % hs_name)
      lines.append("%s = %d" % (hs_name, value))
    elif isinstance(value, long):
71
      lines.append("-- | Converted from Python constant %s" % name)
72
73
74
      lines.append("%s :: Integer" % hs_name)
      lines.append("%s = %d" % (hs_name, value))
    elif isinstance(value, float):
75
      lines.append("-- | Converted from Python constant %s" % name)
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
      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.append("")

  return "\n".join(lines)


def main():
  print Convert()


if __name__ == "__main__":
  main()