On Thu, May 19, 2011 at 06:44:36PM +0100, Michael Hanselmann wrote:
> Am 19. Mai 2011 17:56 schrieb Iustin Pop <[email protected]>:
> > --- a/Makefile.am
> > +++ b/Makefile.am
> > +htools/Ganeti/Constants.hs: htools/Ganeti/Constants.hs.in \
> > +       lib/constants.py lib/_autoconf.py $(CONVERT_CONSTANTS)
> > +       set -e; \
> > +       cat $< > $@; \
> > +       $(CONVERT_CONSTANTS) >> $@
> 
> Can you change this to “{ cat $<; $(CONVERT…); } > $@” to match the
> generation of .rst files?
> 
> > +
> > +
> >  lib/_autoconf.py: Makefile vcs-version | lib/.dir
> 
> One empty line only, please.
> 
> > --- /dev/null
> > +++ b/autotools/convert-constants
> > @@ -0,0 +1,78 @@
> > +import re
> > +
> > +from ganeti import constants
> > +
> > +CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_]+$")
> > +
> > +def nameRules(name):
> 
> Please change the names to match Ganeti's Python style (“NameRules”),
> here and elsewhere.
> 
> > +  """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):
> 
> Two empty lines at module level. Here and elsewhere.
> 
> > +  """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 = constants.__getattribute__(name)
> 
> Just wondering, why __getattribute__ instead of getattr()?
> 
> > +    hs_name = nameRules(name)
> > +    if not CONSTANT_RE.match(name):
> > +      lines.append("-- Skipped %s, not constant" % name)
> > +    elif isinstance(value, basestring):
> > +      lines.append("%s :: String" % hs_name)
> > +      lines.append("%s = \"%s\"" % (hs_name, stringValueRules(value)))
> > +    elif isinstance(value, int):
> 
> (int, long)?
> 
> > +      lines.append("%s :: Int" % hs_name)
> > +      lines.append("%s = %d" % (hs_name, value))
> > +    elif isinstance(value, float):
> > +      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)

Interdiff:


diff --git a/Makefile.am b/Makefile.am
index eff6d9a..fa773c9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -818,10 +818,7 @@ htools/Ganeti/HTools/Version.hs: 
htools/Ganeti/HTools/Version.hs.in vcs-version
 
 htools/Ganeti/Constants.hs: htools/Ganeti/Constants.hs.in \
        lib/constants.py lib/_autoconf.py $(CONVERT_CONSTANTS)
-       set -e; \
-       cat $< > $@; \
-       $(CONVERT_CONSTANTS) >> $@
-
+       set -e; { cat $< ; $(CONVERT_CONSTANTS); } >> $@
 
 lib/_autoconf.py: Makefile vcs-version | lib/.dir
        set -e; \
diff --git a/autotools/convert-constants b/autotools/convert-constants
index 2650854..ddca1fc 100755
--- a/autotools/convert-constants
+++ b/autotools/convert-constants
@@ -28,14 +28,16 @@ from ganeti import constants
 
 CONSTANT_RE = re.compile("^[A-Z][A-Z0-9_]+$")
 
-def nameRules(name):
+
+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):
+
+def StringValueRules(value):
   """Converts a string value from Python to Haskell.
 
   """
@@ -43,7 +45,8 @@ def stringValueRules(value):
   value = value.replace("\"", "\\\"")
   return value
 
-def convert():
+
+def Convert():
   """Converts the constants to Haskell.
 
   """
@@ -52,16 +55,19 @@ def convert():
   all_names = dir(constants)
 
   for name in all_names:
-    value = constants.__getattribute__(name)
-    hs_name = nameRules(name)
+    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):
       lines.append("%s :: String" % hs_name)
-      lines.append("%s = \"%s\"" % (hs_name, stringValueRules(value)))
+      lines.append("%s = \"%s\"" % (hs_name, StringValueRules(value)))
     elif isinstance(value, int):
       lines.append("%s :: Int" % hs_name)
       lines.append("%s = %d" % (hs_name, value))
+    elif isinstance(value, long):
+      lines.append("%s :: Integer" % hs_name)
+      lines.append("%s = %d" % (hs_name, value))
     elif isinstance(value, float):
       lines.append("%s :: Double" % hs_name)
       lines.append("%s = %f" % (hs_name, value))
@@ -71,8 +77,10 @@ def convert():
 
   return "\n".join(lines)
 
+
 def main():
-  print convert()
+  print Convert()
+
 
 if __name__ == "__main__":
   main()


-- 
iustin

Reply via email to