Created
October 21, 2016 11:57
-
-
Save Dr-ZeeD/352f309b2660f3f34ba769233eb75686 to your computer and use it in GitHub Desktop.
Read Python Intersphinx inventories.
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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import re | |
| import warnings | |
| import click | |
| from sphinx.ext.intersphinx import fetch_inventory | |
| _inventory_uris = { | |
| 'python': 'https://docs.python.org/3/', | |
| 'numpy': 'https://docs.scipy.org/doc/numpy/', | |
| 'scipy': 'https://docs.scipy.org/doc/scipy/reference/', | |
| 'matplotlib': 'http://matplotlib.org/', | |
| 'h5py': 'http://docs.h5py.org/en/latest/' | |
| } | |
| def _heading(text): | |
| return '{}\n{}'.format(text, '='*len(text)) | |
| class RegexParam(click.ParamType): | |
| name = 'regular_expression' | |
| def convert(self, value, param, ctx): | |
| try: | |
| return re.compile(value) | |
| except Exception as e: | |
| self.fail('{} is not a valid regular expression"\n {}'.format( | |
| value, e)) | |
| @click.command() | |
| @click.option('--filter', 'filter_', type=RegexParam()) | |
| @click.argument('inventory', type=click.Choice(_inventory_uris.keys())) | |
| def main(inventory, filter_): | |
| uri = _inventory_uris[inventory] | |
| inv = fetch_inventory(warnings, uri, uri + 'objects.inv') | |
| for domain in sorted(inv): | |
| hits = [] | |
| max_hit_len = 0 | |
| for name in sorted(inv[domain]): | |
| url = inv[domain][name][2] | |
| if not filter_ or filter_.search(name) or filter_.search(url): | |
| max_hit_len = max(len(name), max_hit_len) | |
| hits.append((name, url)) | |
| if hits: | |
| template = '{{:{}}} {{}}'.format(max_hit_len) | |
| print('\n' + _heading(domain) + '\n') | |
| for name, url in hits: | |
| print(template.format(name, url)) | |
| print() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment