Statistics
| Branch: | Tag: | Revision:

root / autotools / convert-constants @ 8751c041

History | View | Annotate | Download (3.2 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 8751c041 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 8751c041 Iustin Pop
  name = name.replace("-", "_")
37 d99d1e36 Iustin Pop
  elems = name.split("_")
38 d99d1e36 Iustin Pop
  return elems[0].lower() + "".join(e.capitalize() for e in elems[1:])
39 d99d1e36 Iustin Pop
40 d99d1e36 Iustin Pop
41 d99d1e36 Iustin Pop
def StringValueRules(value):
42 d99d1e36 Iustin Pop
  """Converts a string value from Python to Haskell.
43 d99d1e36 Iustin Pop
44 d99d1e36 Iustin Pop
  """
45 d99d1e36 Iustin Pop
  value = value.encode("string_escape") # escapes backslashes
46 d99d1e36 Iustin Pop
  value = value.replace("\"", "\\\"")
47 d99d1e36 Iustin Pop
  return value
48 d99d1e36 Iustin Pop
49 d99d1e36 Iustin Pop
50 8751c041 Iustin Pop
def DictKeyName(dict_name, key_name):
51 8751c041 Iustin Pop
  """Converts a dict plus key name to a full name.
52 8751c041 Iustin Pop
53 8751c041 Iustin Pop
  """
54 8751c041 Iustin Pop
  return"%s_%s" % (dict_name, str(key_name).upper())
55 8751c041 Iustin Pop
56 8751c041 Iustin Pop
57 8751c041 Iustin Pop
def ConvertVariable(name, value):
58 8751c041 Iustin Pop
  """Converts a given variable to Haskell code.
59 8751c041 Iustin Pop
60 8751c041 Iustin Pop
  @param name: the Python name
61 8751c041 Iustin Pop
  @param value: the value
62 8751c041 Iustin Pop
  @return: a list of Haskell code lines
63 8751c041 Iustin Pop
64 8751c041 Iustin Pop
  """
65 8751c041 Iustin Pop
  lines = []
66 8751c041 Iustin Pop
  hs_name = NameRules(name)
67 8751c041 Iustin Pop
  if not CONSTANT_RE.match(name):
68 8751c041 Iustin Pop
    lines.append("-- Skipped %s, not constant" % name)
69 8751c041 Iustin Pop
  elif isinstance(value, basestring):
70 8751c041 Iustin Pop
    lines.append("-- | Converted from Python constant %s" % name)
71 8751c041 Iustin Pop
    lines.append("%s :: String" % hs_name)
72 8751c041 Iustin Pop
    lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value)))
73 8751c041 Iustin Pop
  elif isinstance(value, int):
74 8751c041 Iustin Pop
    lines.append("-- | Converted from Python constant %s" % name)
75 8751c041 Iustin Pop
    lines.append("%s :: Int" % hs_name)
76 8751c041 Iustin Pop
    lines.append("%s = %d" % (hs_name, value))
77 8751c041 Iustin Pop
  elif isinstance(value, long):
78 8751c041 Iustin Pop
    lines.append("-- | Converted from Python constant %s" % name)
79 8751c041 Iustin Pop
    lines.append("%s :: Integer" % hs_name)
80 8751c041 Iustin Pop
    lines.append("%s = %d" % (hs_name, value))
81 8751c041 Iustin Pop
  elif isinstance(value, float):
82 8751c041 Iustin Pop
    lines.append("-- | Converted from Python constant %s" % name)
83 8751c041 Iustin Pop
    lines.append("%s :: Double" % hs_name)
84 8751c041 Iustin Pop
    lines.append("%s = %f" % (hs_name, value))
85 8751c041 Iustin Pop
  elif isinstance(value, dict):
86 8751c041 Iustin Pop
    if value:
87 8751c041 Iustin Pop
      lines.append("-- Following lines come from dictionary %s" % name)
88 8751c041 Iustin Pop
      for k in sorted(value.keys()):
89 8751c041 Iustin Pop
        lines.extend(ConvertVariable(DictKeyName(name, k), value[k]))
90 8751c041 Iustin Pop
  else:
91 8751c041 Iustin Pop
    lines.append("-- Skipped %s, %s not handled" % (name, type(value)))
92 8751c041 Iustin Pop
  return lines
93 8751c041 Iustin Pop
94 8751c041 Iustin Pop
95 d99d1e36 Iustin Pop
def Convert():
96 d99d1e36 Iustin Pop
  """Converts the constants to Haskell.
97 d99d1e36 Iustin Pop
98 d99d1e36 Iustin Pop
  """
99 d99d1e36 Iustin Pop
  lines = [""]
100 d99d1e36 Iustin Pop
101 d99d1e36 Iustin Pop
  all_names = dir(constants)
102 d99d1e36 Iustin Pop
103 d99d1e36 Iustin Pop
  for name in all_names:
104 d99d1e36 Iustin Pop
    value = getattr(constants, name)
105 8751c041 Iustin Pop
    lines.extend(ConvertVariable(name, value))
106 d99d1e36 Iustin Pop
    lines.append("")
107 d99d1e36 Iustin Pop
108 d99d1e36 Iustin Pop
  return "\n".join(lines)
109 d99d1e36 Iustin Pop
110 d99d1e36 Iustin Pop
111 d99d1e36 Iustin Pop
def main():
112 d99d1e36 Iustin Pop
  print Convert()
113 d99d1e36 Iustin Pop
114 d99d1e36 Iustin Pop
115 d99d1e36 Iustin Pop
if __name__ == "__main__":
116 d99d1e36 Iustin Pop
  main()