|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# List all available versions of the documentation |
| 4 | + |
| 5 | +from urllib.request import urlopen |
| 6 | +import json |
| 7 | +import re |
| 8 | + |
| 9 | +from distutils.version import LooseVersion |
| 10 | + |
| 11 | + |
| 12 | +def json_urlread(url): |
| 13 | + return json.loads(urlopen(url).read().decode('utf8')) |
| 14 | + |
| 15 | + |
| 16 | +def human_readable_data_quantity(quantity, multiple=1024): |
| 17 | + # https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size |
| 18 | + if quantity == 0: |
| 19 | + quantity = +0 |
| 20 | + SUFFIXES = ["B"] + [i + {1000: "B", 1024: "iB"}[multiple] |
| 21 | + for i in "KMGTPEZY"] |
| 22 | + for suffix in SUFFIXES: |
| 23 | + if quantity < multiple or suffix == SUFFIXES[-1]: |
| 24 | + if suffix == SUFFIXES[0]: |
| 25 | + return "%d %s" % (quantity, suffix) |
| 26 | + else: |
| 27 | + return "%.1f %s" % (quantity, suffix) |
| 28 | + else: |
| 29 | + quantity /= multiple |
| 30 | + |
| 31 | + |
| 32 | +def get_pdf_size(version): |
| 33 | + api_url = ROOT_URL + '%s/_downloads' % version |
| 34 | + for path_details in json_urlread(api_url): |
| 35 | + if path_details['name'] == 'scikit-learn-docs.pdf': |
| 36 | + return human_readable_data_quantity(path_details['size'], 1000) |
| 37 | + |
| 38 | + |
| 39 | +heading = 'Available documentation for Scikit-learn' |
| 40 | +print(heading) |
| 41 | +print('=' * len(heading)) |
| 42 | +print() |
| 43 | +print('Web-based documentation is available for versions listed below:') |
| 44 | +print() |
| 45 | + |
| 46 | +ROOT_URL = 'https://api.github.com/repos/scikit-learn/scikit-learn.github.io/contents/' # noqa |
| 47 | +RAW_FMT = 'https://raw.githubusercontent.com/scikit-learn/scikit-learn.github.io/master/%s/documentation.html' # noqa |
| 48 | +VERSION_RE = re.compile(r"\bVERSION:\s*'([^']+)'") |
| 49 | +NAMED_DIRS = ['dev', 'stable'] |
| 50 | + |
| 51 | +# Gather data for each version directory, including symlinks |
| 52 | +dirs = {} |
| 53 | +symlinks = {} |
| 54 | +root_listing = json_urlread(ROOT_URL) |
| 55 | +for path_details in root_listing: |
| 56 | + name = path_details['name'] |
| 57 | + if not (name[:1].isdigit() or name in NAMED_DIRS): |
| 58 | + continue |
| 59 | + if path_details['type'] == 'dir': |
| 60 | + html = urlopen(RAW_FMT % name).read().decode('utf8') |
| 61 | + version_num = VERSION_RE.search(html).group(1) |
| 62 | + pdf_size = get_pdf_size(name) |
| 63 | + dirs[name] = (version_num, pdf_size) |
| 64 | + |
| 65 | + if
E294
path_details['type'] == 'symlink': |
| 66 | + symlinks[name] = json_urlread(path_details['_links']['self'])['target'] |
| 67 | + |
| 68 | + |
| 69 | +# Symlinks should have same data as target |
| 70 | +for src, dst in symlinks.items(): |
| 71 | + if dst in dirs: |
| 72 | + dirs[src] = dirs[dst] |
| 73 | + |
| 74 | +# Output in order: dev, stable, decreasing other version |
| 75 | +seen = set() |
| 76 | +for name in (NAMED_DIRS + |
| 77 | + sorted((k for k in dirs if k[:1].isdigit()), |
| 78 | + key=LooseVersion, reverse=True)): |
| 79 | + version_num, pdf_size = dirs[name] |
| 80 | + if version_num in seen: |
| 81 | + # symlink came first |
| 82 | + continue |
| 83 | + else: |
| 84 | + seen.add(version_num) |
| 85 | + name_display = '' if name[:1].isdigit() else ' (%s)' % name |
| 86 | + path = 'http://scikit-learn.org/%s' % name |
| 87 | + out = ('* `Scikit-learn %s%s documentation <%s/documentation.html>`_' |
| 88 | + % (version_num, name_display, path)) |
| 89 | + if pdf_size is not None: |
| 90 | + out += (' (`PDF %s <%s/_downloads/scikit-learn-docs.pdf>`_)' |
| 91 | + % (pdf_size, path)) |
| 92 | + print(out) |
0 commit comments