Commits

Juri Pakaste committed 60b0b11

Code cleanup: shortening lines, fixing imports, etc.

Comments (0)

Files changed (1)

nsstringgen/NSStringFromEnumGenerator.py

-#!/usr/bin/python
+"""Generate NSStringFromEnumName functions for Objective-C given
+an enum input.
+
+The main entry point is the funs_from_file function.
+"""
 
 from collections import namedtuple, OrderedDict
 from functools import reduce
 from tempfile import NamedTemporaryFile
 import os.path
-import sys
 
-import clang.cindex
+from nsstringgen.clang import cindex
 
 class NSGException(Exception):
+    """Exception class for displaying errors to the user."""
     pass
 
 EnumValue = namedtuple("EnumValue", "name, value")
 
 class ParsedEnum(object):
+    """A parsed enumeration, with typedef_name telling typedeffed name if
+    one exists."""
     def __init__(self, name, typedef_name, values = None):
         self.name = name if name is not None else ""
         self.typedef_name = typedef_name if typedef_name is not None else ""
         return self.name
 
     def __repr__(self):
-        return "ParsedEnum({0}, {1}, {2})".format(repr(self.name), repr(self.typedef_name), self.values)
+        return "ParsedEnum({0}, {1}, {2})".format(
+            repr(self.name), repr(self.typedef_name), self.values)
 
 def print_tree(node, depth = 0):
+    """Stringify a cursor tree for debugging."""
     indent = depth * "  "
     defn = node.get_definition()
     r = ["{0}node:          {1}".format(indent, node),
          "{0}type:          {1}".format(indent, node.type.kind),
          "{0}is_definition: {1}".format(indent, node.is_definition()),
          "{0}extent:        {1}".format(indent, node.extent),
-         "{0}definition:    {1} {2} ({3})".format(indent, defn, defn.kind if defn else None, "Same" if defn is not None and node == defn else "Not same"),
+         "{0}definition:    {1} {2} ({3})".format(
+            indent,
+            defn,
+            defn.kind if defn else None,
+            "Same" if defn is not None and node == defn else "Not same"),
          "----------------------------------------"]
     for c in node.get_children():
         r.extend(print_tree(c, depth + 1))
 
     pe = ParsedEnum(node.spelling, typedef_name)
     for c in node.get_children():
-        if c.kind == clang.cindex.CursorKind.ENUM_CONSTANT_DECL:
+        if c.kind == cindex.CursorKind.ENUM_CONSTANT_DECL:
             name = c.spelling
             value = None
 
             childlist = list(c.get_children())
             if len(childlist) > 0:
-                vbytes = file_reader.read_extent(c.location.file.name, childlist[0].extent)
+                vbytes = file_reader.read_extent(
+                    c.location.file.name, childlist[0].extent)
                 value = vbytes
 
             pe.values.append(EnumValue(name, value))
         enums = []
 
         # we get nil files at the start for some reason
-        if node.location.file is not None and os.path.abspath(node.location.file.name) != ppath:
+        if (node.location.file is not None and
+            os.path.abspath(node.location.file.name) != ppath):
             return []
         try:
             kind = node.kind
         except ValueError:
             # a cursor kind not recognized by cindex.py
-            kind = clang.cindex.CursorKind.UNEXPOSED_DECL
+            kind = cindex.CursorKind.UNEXPOSED_DECL
         # plain enum. see if the parent was a typedef and if so, use its name.
-        if kind == clang.cindex.CursorKind.ENUM_DECL:
+        if kind == cindex.CursorKind.ENUM_DECL:
             typedef_name = None
-            if len(parents) and parents[-1].kind == clang.cindex.CursorKind.TYPEDEF_DECL:
+            if (len(parents) > 0 and
+                parents[-1].kind == cindex.CursorKind.TYPEDEF_DECL):
+
                 typedef_name = parents[-1].spelling
             enums.append(handle_enum(node, typedef_name, file_reader))
-        # typeref inside a typedecl pointing to an enum we can find with get_definition()
-        elif kind == clang.cindex.CursorKind.TYPE_REF and node.type.kind == clang.cindex.TypeKind.ENUM and len(parents) > 0 and parents[-1].kind == clang.cindex.CursorKind.TYPEDEF_DECL:
-            enums.append(handle_enum(node.get_definition(), parents[-1].spelling, file_reader))
+        # typeref inside a typedecl pointing to an enum we can find
+        # with get_definition()
+        elif (kind == cindex.CursorKind.TYPE_REF and
+              node.type.kind == cindex.TypeKind.ENUM and
+              len(parents) > 0
+              and parents[-1].kind == cindex.CursorKind.TYPEDEF_DECL):
+            enums.append(handle_enum(node.get_definition(),
+                                     parents[-1].spelling,
+                                     file_reader))
         else:
             for c in node.get_children():
                 enums.extend(find_enums(c, parents + (node,)))
     if enumvalue.value is None:
         return "case {0}: return @\"{0}\";".format(enumvalue.name)
     else:
-        return "case {0}: return [NSString stringWithFormat:@\"{0} (%d)\", {1}];".format(enumvalue.name, enumvalue.value)
+        return ("case {0}: return [NSString stringWithFormat:@\""
+                "{0} (%d)\", {1}];".format(enumvalue.name, enumvalue.value))
 
 def create_fun(parsed_enum):
     name = parsed_enum.preferred_name
     cases = ["    {0}".format(create_case(ev)) for ev in parsed_enum.values]
     switchend = "  }"
     funend = "}"
-    return "\n".join((ext_decl, defn, switchstart, "\n".join(cases), switchend, funend))
+    return "\n".join(
+        (ext_decl, defn, switchstart, "\n".join(cases), switchend, funend))
 
 def create_mask_fun(parsed_enum):
     name = parsed_enum.preferred_name
     lines.append("}")
     return "\n".join(lines)
 
+def create_includeargs(includes):
+    includepairs = [["-include", inc] for inc in includes] if includes else []
+    includeargs = sum(includepairs, [])
+    return includeargs
 
-
-def funs_from_file(f, includes, mask, contents = None):
-    index = clang.cindex.Index.create()
-    includeargs = reduce(lambda acc, e: acc + e, [["-include", inc] for inc in includes], []) if includes else []
+def funs_from_file(fname, includes, mask, contents = None):
+    index = cindex.Index.create()
+    includeargs = create_includeargs(includes)
     unsaved = []
     if contents:
-        # create a dummy file if we get contents, because seems libclang is not happy with a name like "-"
+        # create a dummy file if we get contents, because seems
+        # libclang is not happy with a name like "-"
         ntemp = NamedTemporaryFile(suffix = ".h")
-        f = ntemp.name
-        unsaved = [[f, contents]]
-    tu = index.parse(f, args = ["-x", "objective-c"] + includeargs, unsaved_files = unsaved)
+        fname = ntemp.name
+        unsaved = [[fname, contents]]
+    tu = index.parse(fname,
+                     args = ["-x", "objective-c"] + includeargs,
+                     unsaved_files = unsaved)
     if not tu:
-        raise NSGException("Unable to parse input {0} for unknown reason".format(f))
+        raise NSGException(
+            "Unable to parse input {0} for unknown reason".format(fname))
     if tu.diagnostics:
         msg = "\n".join([str(d) for d in tu.diagnostics])
         raise NSGException(msg)
 
     file_reader = FileReader(10)
-    for fname, content in unsaved:
-        file_reader.insert_content(fname, content)
+    for ufname, content in unsaved:
+        file_reader.insert_content(ufname, content)
 
-    finder = create_finder(f, file_reader)
+    finder = create_finder(fname, file_reader)
     enums = finder(tu.cursor)
     creator = create_mask_fun if mask else create_fun
     return [creator(e) for e in enums if e.preferred_name]
 
-if __name__ == '__main__':
-    for fun in funs_from_file(sys.argv[1]):
-        sys.stdout.write(fun)
-        sys.stdout.write("\n")