diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-08-14 18:31:43 -0700 |
---|---|---|
committer | Jon Dufresne <jon.dufresne@gmail.com> | 2019-08-24 16:45:26 -0700 |
commit | 164eedbb8b67ab089c61261d44f83e8af6811adc (patch) | |
tree | 2c348238550eb7bef3ec9f42d2659094518e39a5 /scripts | |
parent | df443dfc7d4f02d31ba49682738e1bd07d723613 (diff) | |
download | isort-164eedbb8b67ab089c61261d44f83e8af6811adc.tar.gz |
Auto-generate the complete list of stdlib modules
The new script, mkstdlibs.py, uses intersphinx to dump a complete list
of Python stdlib modules to the files py3.txt/py27.txt. These files are
read by isort to decide if a module should be considered stdlib or not.
In the future, if the stdlib changes, the fix is to simply re-run the
script.
Fixes #985
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/mkstdlibs.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/scripts/mkstdlibs.py b/scripts/mkstdlibs.py new file mode 100755 index 00000000..4a72c75c --- /dev/null +++ b/scripts/mkstdlibs.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +from sphinx.ext.intersphinx import fetch_inventory + + +URL = "https://docs.python.org/{}/objects.inv" +PATH = "isort/stdlibs/py{}.py" +VERSIONS = [("2", "7"), ("3",)] + +DOCSTRING = """ +File contains the standard library of Python {}. + +DO NOT EDIT. If the standard library changes, a new list should be created +using the mkstdlibs.py script. +""" + + +class FakeConfig: + intersphinx_timeout = None + tls_verify = True + + +class FakeApp: + srcdir = "" + config = FakeConfig() + + +for version_info in VERSIONS: + version = ".".join(version_info) + url = URL.format(version) + invdata = fetch_inventory(FakeApp(), "", url) + + modules = set() + for module in invdata["py:module"]: + root, *_ = module.split(".") + if root not in ["__future__", "__main__"]: + modules.add(root) + + path = PATH.format("".join(version_info)) + with open(path, "w") as fp: + docstring = DOCSTRING.format(version) + fp.write('"""{}"""\n\n'.format(docstring)) + fp.write("stdlib = [\n") + for module in sorted(modules): + fp.write(' "{}",\n'.format(module)) + fp.write("]\n") |