Last active
July 5, 2022 01:22
-
-
Save internetimagery/e7fb249a0783800d2ec8100dd9c0d12a to your computer and use it in GitHub Desktop.
Generate apipkg setups for existing modules. Simple way to transition to the apipkg setup which can then be maually tweaked to suit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without | |
# fee is hereby granted. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
# THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE | |
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER | |
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE | |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
# Example usage: python /path/to/this/file/apipkg_gen.py _modulename -o /path/to/modulename.py | |
import os | |
import re | |
from pprint import pformat | |
from importlib import import_module | |
def main(module_name, include_private, output): | |
structure = _import(module_name, include_private) | |
result = "import apipkg\n\napipkg.initpkg(\n __name__,\n{}\n)".format( | |
"\n".join(" " + line for line in pformat(structure, indent=4).split("\n")), | |
) | |
if output: | |
with open(output, "w") as h: | |
h.write(result) | |
else: | |
print(output) | |
_is_magic = re.compile(r"^__\w+__$") | |
def _import(module_name, include_private): | |
# Attempt to import the module | |
attributes = {} | |
try: | |
module = import_module(module_name) | |
except Exception: | |
print("Failed to import {}".format(module_name)) | |
return attributes | |
# Get the contents of the module | |
for attr_name in dir(module): | |
if _is_magic.match(attr_name): | |
continue | |
if not include_private and attr_name.startswith("_"): | |
continue | |
attr = getattr(module, attr_name) | |
attributes[attr_name] = "{}:{}".format(module_name, attr_name) | |
# Search deeper for submodules | |
try: | |
module_path = module.__file__ | |
except AttributeError: | |
pass | |
else: | |
basename = module_path and os.path.basename(os.path.splitext(module_path)[0]) | |
if basename == "__init__": | |
for script in os.listdir(os.path.dirname(module_path)): | |
basename = os.path.splitext(script)[0] | |
if _is_magic.match(basename): | |
continue | |
if not include_private and basename.startswith("_"): | |
continue | |
submodule = "{}.{}".format(module_name, basename) | |
subattrs = _import(submodule, include_private) | |
if subattrs: | |
attributes[basename] = subattrs | |
return attributes | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser( | |
description="Generate apipkg setup for existing module" | |
) | |
parser.add_argument("module") | |
parser.add_argument("--include-private", action="store_true", default=False) | |
parser.add_argument("--output", "-o") | |
args = parser.parse_args() | |
main(args.module, args.include_private, args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment