""" Given a package name (E.g. numpy), write a file which contains a list of all possible function and ufunc imports. """ import inspect import os import sys import importlib MY_NAME = "pkgfuncs" REPORT_FILE = "pkgfunc_report.py" OUTPUT_HANDLE = -1 count = 0 def command_line_error(arg_text): """ The specified command line is in error. Exit to the O/S after printing arg_text. """ print("\n*** Command line error: {}\n".format(arg_text)) print("Command line should be: {} PACKAGE-NAME\n".format(MY_NAME)) sys.exit(86) def oops(arg_text): """ Exit to the O/S after printing arg_text. """ print("\n*** Oops, {}\n".format(arg_text)) sys.exit(86) def spew_one(arg_name, arg_strung): """ Write one line: from PKG_NAME import UFUNC-or-FUNCTION """ try: OUTPUT_HANDLE.write("from {} import {} # {}\n".format(PKG_NAME, arg_name, arg_strung)) except IOError as exc: oops("Cannot write {}, reason: {}".format(REPORT_FILE, exc.strerror)) #========================================================================# NARGS = len(sys.argv) MY_NAME = sys.argv[0] if NARGS != 2: command_line_error("Exactly 1 argument should be specified") PKG_NAME = sys.argv[1] REPORT_FILE = PKG_NAME + "_list.py" try: PKG = importlib.import_module(PKG_NAME) except ModuleNotFoundError: oops("Package {} not found".format(PKG_NAME)) if os.path.exists(REPORT_FILE): os.remove(REPORT_FILE) try: OUTPUT_HANDLE = open(REPORT_FILE, "wt") except IOError as exc: oops("Cannot open {}, reason: {}".format(REPORT_FILE, exc.strerror)) EXPORTABLES = inspect.getmembers(PKG) for currow in EXPORTABLES: name = str(currow[0]).strip() strung = str(currow[1]).strip() if len(strung) < 6: continue if strung[:6] == "