Commits

Larry Hastings  committed afad4c3

Improved --converters a thousand times; it now shows the relevant signature.

  • Participants
  • Parent commits c7ea31b

Comments (0)

Files changed (1)

+#!/usr/bin/env python3
 import abc
 import ast
 import atexit
     type = 'PyObject *'
     default = None
 
-    def __init__(self, *, doc_default=None):
+    def __init__(self, *, doc_default=None, **kwargs):
         self.doc_default = doc_default
+        self.return_converter_init(**kwargs)
+
+    def return_converter_init():
+        pass
 
     def declare(self, data, name="_return_value"):
         line = []
     cmdline = argparse.ArgumentParser()
     cmdline.add_argument("-f", "--force", action='store_true')
     cmdline.add_argument("-o", "--output", type=str)
-    cmdline.add_argument("filename", type=str, nargs="+")
+    cmdline.add_argument("--converters", action='store_true')
+    cmdline.add_argument("filename", type=str, nargs="*")
     ns = cmdline.parse_args(argv)
+
+    if ns.converters:
+        if ns.filename:
+            print("Usage error: can't specify --converters and a filename at the same time.")
+            print()
+            cmdline.print_usage()
+            sys.exit(-1)
+        converters = []
+        return_converters = []
+        ignored = set("""
+            add_c_converter
+            add_c_return_converter
+            add_default_legacy_c_converter
+            add_legacy_c_converter
+            """.strip().split())
+        module = globals()
+        for name in module:
+            for suffix, ids in (
+                ("_return_converter", return_converters),
+                ("_converter", converters),
+            ):
+                if name in ignored:
+                    continue
+                if name.endswith(suffix):
+                    ids.append((name, name[:-len(suffix)]))
+                    break
+        print()
+        for title, attribute, ids in (
+            ("Converters", 'converter_init', converters),
+            ("Return converters", 'return_converter_init', return_converters),
+        ):
+            print(title + ":")
+            longest = -1
+            for name, short_name in ids:
+                longest = max(longest, len(short_name))
+            for name, short_name in sorted(ids, key=lambda x: x[1].lower()):
+                cls = module[name]
+                callable = getattr(cls, attribute, None)
+                if not callable:
+                    continue
+                signature = inspect.signature(callable)
+                parameters = ['(']
+                add_comma = False
+                for parameter_name, parameter in signature.parameters.items():
+                    if parameter.kind == inspect.Parameter.KEYWORD_ONLY:
+                        if add_comma:
+                            parameters.append(', ')
+                        else:
+                            add_comma = True
+                        s = parameter_name
+                        if parameter.default != inspect.Parameter.empty:
+                            s += '=' + repr(parameter.default)
+                        parameters.append(s)
+                parameters.append(')')
+
+                print("   ", short_name + "".join(parameters))
+            print()
+        print("All converters also accept (doc_default=None, required=False).")
+        print("All return converters also accept (doc_default=None).")
+        sys.exit(0)
+
+    if not ns.filename:
+        cmdline.print_usage()
+        sys.exit(-1)
+
     if ns.output and len(ns.filename) > 1:
         print("Usage error: can't use -o with multiple filenames.")
         print()