Skip to content

Commit ca5a028

Browse files
committed
Extract CLI into new module
1 parent 1df5838 commit ca5a028

File tree

4 files changed

+115
-108
lines changed

4 files changed

+115
-108
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ text = "MIT"
3232
files = ["LICENSE"]
3333

3434
[project.scripts]
35-
viscm = "viscm.gui:main"
35+
viscm = "viscm.cli:cli"
3636

3737

3838
[build-system]

viscm/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
# Copyright (C) 2015 Stefan van der Walt <stefanv@berkeley.edu>
44
# See file LICENSE.txt for license information.
55

6-
from .gui import main
7-
main()
6+
from .cli import cli
7+
cli()

viscm/cli.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import sys
2+
3+
import matplotlib.pyplot as plt
4+
5+
from . import gui
6+
7+
8+
def cli():
9+
import argparse
10+
argv = sys.argv[1:]
11+
12+
# Usage:
13+
# python -m viscm
14+
# python -m viscm edit
15+
# python -m viscm edit <file.py>
16+
# (file.py must define some appropriate globals)
17+
# python -m viscm view <file.py>
18+
# (file.py must define a global named "test_cm")
19+
# python -m viscm view "matplotlib builtin colormap"
20+
# python -m viscm view --save=foo.png ...
21+
22+
parser = argparse.ArgumentParser(
23+
prog="python -m viscm",
24+
description="A colormap tool.",
25+
)
26+
parser.add_argument("action", metavar="ACTION",
27+
help="'edit' or 'view' (or 'show', same as 'view')",
28+
choices=["edit", "view", "show"],
29+
default="edit",
30+
nargs="?")
31+
parser.add_argument("colormap", metavar="COLORMAP",
32+
default=None,
33+
help="A .json file saved from the editor, or "
34+
"the name of a matplotlib builtin colormap",
35+
nargs="?")
36+
parser.add_argument("--uniform-space", metavar="SPACE",
37+
default="CAM02-UCS",
38+
dest="uniform_space",
39+
help="The perceptually uniform space to use. Usually "
40+
"you should leave this alone. You can pass 'CIELab' "
41+
"if you're curious how uniform some colormap is in "
42+
"CIELab space. You can pass 'buggy-CAM02-UCS' if "
43+
"you're trying to reproduce the matplotlib colormaps "
44+
"(which turn out to have had a small bug in the "
45+
"assumed sRGB viewing conditions) from their bezier "
46+
"curves.")
47+
parser.add_argument("-t", "--type", type=str,
48+
default="linear", choices=["linear", "diverging", "diverging-continuous"],
49+
help="Choose a colormap type. Supported options are 'linear', 'diverging', and 'diverging-continuous")
50+
parser.add_argument("-m", "--method", type=str,
51+
default="CatmulClark", choices=["Bezier", "CatmulClark"],
52+
help="Choose a spline construction method. 'CatmulClark' is the default, but you may choose the legacy option 'Bezier'")
53+
parser.add_argument("--save", metavar="FILE",
54+
default=None,
55+
help="Immediately save visualization to a file "
56+
"(view-mode only).")
57+
parser.add_argument("--quit", default=False, action="store_true",
58+
help="Quit immediately after starting "
59+
"(useful with --save).")
60+
args = parser.parse_args(argv)
61+
62+
cm = gui.Colormap(args.type, args.method, args.uniform_space)
63+
app = gui.QtWidgets.QApplication([])
64+
65+
if args.colormap:
66+
cm.load(args.colormap)
67+
68+
69+
# Easter egg! I keep typing 'show' instead of 'view' so accept both
70+
if args.action in ("view", "show"):
71+
if cm is None:
72+
sys.exit("Please specify a colormap")
73+
fig = plt.figure()
74+
figureCanvas = gui.FigureCanvas(fig)
75+
v = gui.viscm(cm.cmap, name=cm.name, figure=fig, uniform_space=cm.uniform_space)
76+
mainwindow = gui.ViewerWindow(figureCanvas, v, cm.name)
77+
if args.save is not None:
78+
v.figure.set_size_inches(20, 12)
79+
v.figure.savefig(args.save)
80+
elif args.action == "edit":
81+
if not cm.can_edit:
82+
sys.exit("Sorry, I don't know how to edit the specified colormap")
83+
# Hold a reference so it doesn't get GC'ed
84+
fig = plt.figure()
85+
figureCanvas = gui.FigureCanvas(fig)
86+
v = gui.viscm_editor(figure=fig, uniform_space=cm.uniform_space, cmtype=cm.cmtype, method=cm.method, **cm.params)
87+
mainwindow = gui.EditorWindow(figureCanvas, v)
88+
else:
89+
raise RuntimeError("can't happen")
90+
91+
if args.quit:
92+
sys.exit()
93+
94+
figureCanvas.setSizePolicy(gui.QtWidgets.QSizePolicy.Expanding,
95+
gui.QtWidgets.QSizePolicy.Expanding)
96+
figureCanvas.updateGeometry()
97+
98+
mainwindow.resize(800, 600)
99+
mainwindow.show()
100+
101+
# PyQt messes up signal handling by default. Python signal handlers (e.g.,
102+
# the default handler for SIGINT that raises KeyboardInterrupt) can only
103+
# run when we enter the Python interpreter, which doesn't happen while
104+
# idling in the Qt mainloop. (Unless we register a timer to poll
105+
# explicitly.) So here we unregister Python's default signal handler and
106+
# replace it with... the *operating system's* default signal handler, so
107+
# instead of a KeyboardInterrupt our process just exits.
108+
import signal
109+
signal.signal(signal.SIGINT, signal.SIG_DFL)
110+
111+
app.exec_()

viscm/gui.py

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -948,117 +948,13 @@ def load(self, path):
948948
self.name = path
949949

950950

951-
def main():
952-
import argparse
953-
argv = sys.argv[1:]
954-
955-
# Usage:
956-
# python -m viscm
957-
# python -m viscm edit
958-
# python -m viscm edit <file.py>
959-
# (file.py must define some appropriate globals)
960-
# python -m viscm view <file.py>
961-
# (file.py must define a global named "test_cm")
962-
# python -m viscm view "matplotlib builtin colormap"
963-
# python -m viscm view --save=foo.png ...
964-
965-
parser = argparse.ArgumentParser(
966-
prog="python -m viscm",
967-
description="A colormap tool.",
968-
)
969-
parser.add_argument("action", metavar="ACTION",
970-
help="'edit' or 'view' (or 'show', same as 'view')",
971-
choices=["edit", "view", "show"],
972-
default="edit",
973-
nargs="?")
974-
parser.add_argument("colormap", metavar="COLORMAP",
975-
default=None,
976-
help="A .json file saved from the editor, or "
977-
"the name of a matplotlib builtin colormap",
978-
nargs="?")
979-
parser.add_argument("--uniform-space", metavar="SPACE",
980-
default="CAM02-UCS",
981-
dest="uniform_space",
982-
help="The perceptually uniform space to use. Usually "
983-
"you should leave this alone. You can pass 'CIELab' "
984-
"if you're curious how uniform some colormap is in "
985-
"CIELab space. You can pass 'buggy-CAM02-UCS' if "
986-
"you're trying to reproduce the matplotlib colormaps "
987-
"(which turn out to have had a small bug in the "
988-
"assumed sRGB viewing conditions) from their bezier "
989-
"curves.")
990-
parser.add_argument("-t", "--type", type=str,
991-
default="linear", choices=["linear", "diverging", "diverging-continuous"],
992-
help="Choose a colormap type. Supported options are 'linear', 'diverging', and 'diverging-continuous")
993-
parser.add_argument("-m", "--method", type=str,
994-
default="CatmulClark", choices=["Bezier", "CatmulClark"],
995-
help="Choose a spline construction method. 'CatmulClark' is the default, but you may choose the legacy option 'Bezier'")
996-
parser.add_argument("--save", metavar="FILE",
997-
default=None,
998-
help="Immediately save visualization to a file "
999-
"(view-mode only).")
1000-
parser.add_argument("--quit", default=False, action="store_true",
1001-
help="Quit immediately after starting "
1002-
"(useful with --save).")
1003-
args = parser.parse_args(argv)
1004-
1005-
cm = Colormap(args.type, args.method, args.uniform_space)
1006-
app = QtWidgets.QApplication([])
1007-
1008-
if args.colormap:
1009-
cm.load(args.colormap)
1010-
1011-
1012-
# Easter egg! I keep typing 'show' instead of 'view' so accept both
1013-
if args.action in ("view", "show"):
1014-
if cm is None:
1015-
sys.exit("Please specify a colormap")
1016-
fig = plt.figure()
1017-
figureCanvas = FigureCanvas(fig)
1018-
v = viscm(cm.cmap, name=cm.name, figure=fig, uniform_space=cm.uniform_space)
1019-
mainwindow = ViewerWindow(figureCanvas, v, cm.name)
1020-
if args.save is not None:
1021-
v.figure.set_size_inches(20, 12)
1022-
v.figure.savefig(args.save)
1023-
elif args.action == "edit":
1024-
if not cm.can_edit:
1025-
sys.exit("Sorry, I don't know how to edit the specified colormap")
1026-
# Hold a reference so it doesn't get GC'ed
1027-
fig = plt.figure()
1028-
figureCanvas = FigureCanvas(fig)
1029-
v = viscm_editor(figure=fig, uniform_space=cm.uniform_space, cmtype=cm.cmtype, method=cm.method, **cm.params)
1030-
mainwindow = EditorWindow(figureCanvas, v)
1031-
else:
1032-
raise RuntimeError("can't happen")
1033-
1034-
if args.quit:
1035-
sys.exit()
1036-
1037-
figureCanvas.setSizePolicy(QtWidgets.QSizePolicy.Expanding,
1038-
QtWidgets.QSizePolicy.Expanding)
1039-
figureCanvas.updateGeometry()
1040-
1041-
mainwindow.resize(800, 600)
1042-
mainwindow.show()
1043-
1044-
# PyQt messes up signal handling by default. Python signal handlers (e.g.,
1045-
# the default handler for SIGINT that raises KeyboardInterrupt) can only
1046-
# run when we enter the Python interpreter, which doesn't happen while
1047-
# idling in the Qt mainloop. (Unless we register a timer to poll
1048-
# explicitly.) So here we unregister Python's default signal handler and
1049-
# replace it with... the *operating system's* default signal handler, so
1050-
# instead of a KeyboardInterrupt our process just exits.
1051-
import signal
1052-
signal.signal(signal.SIGINT, signal.SIG_DFL)
1053-
1054-
app.exec_()
1055-
1056951
def about():
1057952
QtWidgets.QMessageBox.about(None, "VISCM",
1058953
"Copyright (C) 2015-2016 Nathaniel Smith\n" +
1059954
"Copyright (C) 2015-2016 Stéfan van der Walt\n"
1060955
"Copyright (C) 2016 Hankun Zhao")
1061956

957+
1062958
class ViewerWindow(QtWidgets.QMainWindow):
1063959
def __init__(self, figurecanvas, viscm, cmapname, parent=None):
1064960
QtWidgets.QMainWindow.__init__(self, parent)

0 commit comments

Comments
 (0)