|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | +A script to convert the version.json file into a pretty markdown doc. |
| 4 | +
|
| 5 | +Public domain where applicable, MIT license everywhere else. |
| 6 | +""" |
| 7 | +from __future__ import print_function |
| 8 | + |
| 9 | +from itertools import chain |
| 10 | +import json |
| 11 | +import operator |
| 12 | + |
| 13 | +with open("version.json", "r") as fp: |
| 14 | + versions = json.load(fp) |
| 15 | + |
| 16 | +engine = versions["VERSION_ENGINE"] |
| 17 | +bugs = versions["VERSION_BUGS"] |
| 18 | + |
| 19 | +unfixed_bugs = [] |
| 20 | +fixed_bugs = [] |
| 21 | + |
| 22 | +for bug in bugs: |
| 23 | + (fixed_bugs if bug["fix"] else unfixed_bugs).append(bug) |
| 24 | + |
| 25 | +# Sort fixed bugs, latest first |
| 26 | +fixed_bugs.sort(key=operator.itemgetter("fix"), reverse=True) |
| 27 | + |
| 28 | +# Sort unfixed bugs, latest first (for consistency, and ease of tracking down) |
| 29 | +unfixed_bugs.sort(key=operator.itemgetter("intro"), reverse=True) |
| 30 | + |
| 31 | + |
| 32 | +def version_string(n): |
| 33 | + z = n & ((1<<10)-1); n >>= 10 |
| 34 | + a = n & ((1<<5)-1); n >>= 5 |
| 35 | + y = n & ((1<<7)-1); n >>= 7 |
| 36 | + x = n & ((1<<5)-1); n >>= 5 |
| 37 | + w = n |
| 38 | + |
| 39 | + s = "%i.%i" % (w, x) |
| 40 | + if y > 0: |
| 41 | + s += ".%i" % y |
| 42 | + if a > 0: |
| 43 | + s += chr(ord('a')+a-1) |
| 44 | + if z > 0: |
| 45 | + s += "-%i" % z |
| 46 | + |
| 47 | + return s |
| 48 | + |
| 49 | + |
| 50 | +for bug in chain(fixed_bugs, unfixed_bugs): |
| 51 | + bug["intro"] = version_string(bug["intro"]) |
| 52 | + bug["fix"] = version_string(bug["fix"]) |
| 53 | + |
| 54 | + |
| 55 | +with open("../version.md", "w") as fp: |
| 56 | + write = fp.write |
| 57 | + |
| 58 | + def writeln(b=""): |
| 59 | + write(b + "\n") |
| 60 | + |
| 61 | + def table(data, columns=None, padding=1): |
| 62 | + if len(data) == 0: |
| 63 | + return |
| 64 | + |
| 65 | + if columns is None: |
| 66 | + columns = [(k, k.title()) for k in data[0].keys()] |
| 67 | + else: |
| 68 | + new_columns = [] |
| 69 | + for column in columns: |
| 70 | + if isinstance(column, basestring): |
| 71 | + new_columns.append((column, column.title())) |
| 72 | + else: |
| 73 | + new_columns.append(column) |
| 74 | + columns = new_columns |
| 75 | + del new_columns |
| 76 | + |
| 77 | + # Determine widths based on longest item (including headers) |
| 78 | + widths = [len(str(k[1])) for k in columns] |
| 79 | + |
| 80 | + for item in data: |
| 81 | + for x, column in enumerate(columns): |
| 82 | + width = len(str(item[column[0]])) |
| 83 | + if width > widths[x]: |
| 84 | + widths[x] = width |
| 85 | + |
| 86 | + # for x, width in enumerate(widths): |
| 87 | + # widths[x] = width + (padding * 2) |
| 88 | + |
| 89 | + # Write table |
| 90 | + padding_template = " " * padding |
| 91 | + section_template = padding_template + "%-{0}s" + padding_template |
| 92 | + line_template = "|".join(section_template.format(width) for width in widths) |
| 93 | + line_template = "|" + line_template + "|" |
| 94 | + |
| 95 | + writeln(line_template % tuple(c[1] for c in columns)) |
| 96 | + writeln(line_template % tuple("-" * width for width in widths)) |
| 97 | + |
| 98 | + for item in data: |
| 99 | + writeln(line_template % tuple(item[c[0]] for c in columns)) |
| 100 | + |
| 101 | + writeln("# Versions") |
| 102 | + writeln("----------") |
| 103 | + writeln() |
| 104 | + writeln("*Current version:* %s" % engine["str"]) |
| 105 | + writeln() |
| 106 | + writeln("## Bugs") |
| 107 | + writeln("### Unfixed") |
| 108 | + writeln() |
| 109 | + table(unfixed_bugs, ["msg", "intro"]) |
| 110 | + writeln() |
| 111 | + writeln("### Fixed") |
| 112 | + writeln() |
| 113 | + table(fixed_bugs) |
0 commit comments