main()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
c 2
b 0
f 0
dl 0
loc 33
rs 7.5384
1
"""
2
Module that contains the command line app.
3
4
Why does this file exist, and why not put this in __main__?
5
6
  You might be tempted to import things from __main__ later,
7
  but that will cause problems:
8
  the code will get executed twice:
9
10
  - When you run `python -mreplotlib` python will execute
11
    ``__main__.py`` as a script. That means there won't be any
12
    ``replotlib.__main__`` in ``sys.modules``.
13
  - When you import __main__ it will get executed again (as a module) because
14
    there's no ``replotlib.__main__`` in ``sys.modules``.
15
16
  Also see (1) from http://click.pocoo.org/5/setuptools/#setuptools-integration
17
"""
18
import json
19
from collections import OrderedDict
20
21
import click
22
import matplotlib.pyplot as plt
23
24
from . import Axes
25
26
27
def replot(file_name, file_type, style=None, savefig=False, **kargs):
28
    plt.figure()
29
    Axes(file_name, file_type=file_type, style=style,
30
         erase=False).replot()
31
    if savefig:
32
        plt.savefig(savefig, **kargs)
33
    else:
34
        plt.show()
35
36
37
@click.command()
38
@click.argument('file_name', nargs=1)
39
@click.option('--savefig', '-s', help="save the figure to 'savefig'.")
40
@click.option('--bb', is_flag=True,
41
              help="make figure compatible with black background")
42
@click.option('--file_type', '-t', default='json',
43
              help="file type 'json' or 'hdf5'")
44
@click.option('--style_file', default=None,
45
              help="Style file in json format")
46
def main(file_name, savefig, file_type, style_file, bb):
47
    if style_file:
48
        with open(style_file) as f:
49
            style = json.load(f, object_pairs_hook=OrderedDict)
50
    else:
51
        style = {}
52
53
    if bb:
54
        from matplotlib.colors import colorConverter
55
        colorConverter.cache = {}
56
        colorConverter.colors['w'], colorConverter.colors['k']\
57
            = (colorConverter.colors['k'], colorConverter.colors['w'])
58
        colorConverter.cache['black'] = colorConverter.colors['k']
59
        colorConverter.cache['wight'] = colorConverter.colors['w']
60
        plt.rcParams['savefig.transparent'] = True
61
    try:
62
        replot(file_name=file_name, file_type=file_type,
63
               savefig=savefig, style=style)
64
    except IOError as err:
65
        print("Cannot read '{}': {}"
66
              .format(file_name, err))
67
    except ValueError:
68
        replot(file_name=file_name, file_type='hdf5',
69
               savefig=savefig, style=style)
70