File: gendocs.py

package info (click to toggle)
palettable 3.3.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 772 kB
  • sloc: python: 25,326; makefile: 9
file content (115 lines) | stat: -rw-r--r-- 3,536 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
Generate color map images and fill in the docs to point to them.

"""
from __future__ import print_function

import argparse
import os
import sys
from importlib import import_module

from jinja2 import Template
from palettable.palette import Palette

MODULES = {
    'palettable.colorbrewer.diverging': './colorbrewer/diverging',
    'palettable.colorbrewer.qualitative': './colorbrewer/qualitative',
    'palettable.colorbrewer.sequential': './colorbrewer/sequential',
    'palettable.tableau': './tableau',
    'palettable.wesanderson': './wesanderson',
    'palettable.cubehelix': './cubehelix',
    'palettable.matplotlib': './matplotlib',
    'palettable.mycarta': './mycarta',
    'palettable.cmocean.diverging': './cmocean/diverging',
    'palettable.cmocean.sequential': './cmocean/sequential',
    'palettable.cartocolors.diverging': './cartocolors/diverging',
    'palettable.cartocolors.qualitative': './cartocolors/qualitative',
    'palettable.cartocolors.sequential': './cartocolors/sequential',
    'palettable.lightbartlein.diverging': './lightbartlein/diverging',
    'palettable.lightbartlein.sequential': './lightbartlein/sequential',
    'palettable.scientific.diverging': './scientific/diverging',
    'palettable.scientific.sequential': './scientific/sequential',
}


def find_palettes(mod):
    """
    Find all Palette instances in mod.

    """
    return {
        k: v for k, v in vars(mod).items()
        if isinstance(v, Palette) and not k.endswith('_r')}


def gen_images(palettes, dir_):
    """
    Create images for each palette in the palettes dict.
    For qualitative palettes only the discrete images is made.

    """
    img_dir = os.path.join(dir_, 'img')
    os.makedirs(img_dir, exist_ok=True)

    discrete_fmt = '{}_discrete.png'.format
    continuous_fmt = '{}_continuous.png'.format

    img_size = (6, 0.5)

    for name, p in palettes.items():
        print('Making discrete image for palette {}'.format(name))
        p.save_discrete_image(
            os.path.join(img_dir, discrete_fmt(name)), size=img_size)

        if p.type != 'qualitative':
            print('Making continuous image for palette {}'.format(name))
            p.save_continuous_image(
                os.path.join(img_dir, continuous_fmt(name)), size=img_size)


def render_doc_page(dir_, palette_names, palette_dict):
    """
    Render the documentation page in a given directory.

    """
    print('Rendering index in dir {}'.format(dir_))

    with open(os.path.join(dir_, 'index.md.tpl')) as f:
        tpl = Template(f.read())

    with open(os.path.join(dir_, 'index.md'), 'w') as f:
        f.write(tpl.render(palettes=palette_names, palette_dict=palette_dict))


def palette_name_sort_key(name):
    base, length = name.rsplit('_', maxsplit=1)
    return base, int(length)


def mkdocs(mod, dir_, images=False):
    palettes = find_palettes(mod)
    if images:
        gen_images(palettes, dir_)
    render_doc_page(
        dir_, sorted(palettes.keys(), key=palette_name_sort_key), palettes)


def parse_args(args=None):
    parser = argparse.ArgumentParser(
        description='Build Palettable documentation.')
    parser.add_argument(
        '-i', '--images', action='store_true', help='force rebuild images')
    return parser.parse_args(args)


def main(args=None):
    args = parse_args(args)

    for mod, dir_ in MODULES.items():
        print('Running module {}'.format(mod))
        mkdocs(import_module(mod), dir_, images=args.images)


if __name__ == '__main__':
    sys.exit(main())