ยปCore Development>Code coverage>Tools/pynche/Main.py

Python code coverage for Tools/pynche/Main.py

#countcontent
1n/a"""Pynche -- The PYthon Natural Color and Hue Editor.
2n/a
3n/aContact: %(AUTHNAME)s
4n/aEmail: %(AUTHEMAIL)s
5n/aVersion: %(__version__)s
6n/a
7n/aPynche is based largely on a similar color editor I wrote years ago for the
8n/aSunView window system. That editor was called ICE: the Interactive Color
9n/aEditor. I'd always wanted to port the editor to X but didn't feel like
10n/ahacking X and C code to do it. Fast forward many years, to where Python +
11n/aTkinter provides such a nice programming environment, with enough power, that
12n/aI finally buckled down and implemented it. I changed the name because these
13n/adays, too many other systems have the acronym `ICE'.
14n/a
15n/aThis program currently requires Python 2.2 with Tkinter.
16n/a
17n/aUsage: %(PROGRAM)s [-d file] [-i file] [-X] [-v] [-h] [initialcolor]
18n/a
19n/aWhere:
20n/a --database file
21n/a -d file
22n/a Alternate location of a color database file
23n/a
24n/a --initfile file
25n/a -i file
26n/a Alternate location of the initialization file. This file contains a
27n/a persistent database of the current Pynche options and color. This
28n/a means that Pynche restores its option settings and current color when
29n/a it restarts, using this file (unless the -X option is used). The
30n/a default is ~/.pynche
31n/a
32n/a --ignore
33n/a -X
34n/a Ignore the initialization file when starting up. Pynche will still
35n/a write the current option settings to this file when it quits.
36n/a
37n/a --version
38n/a -v
39n/a print the version number and exit
40n/a
41n/a --help
42n/a -h
43n/a print this message
44n/a
45n/a initialcolor
46n/a initial color, as a color name or #RRGGBB format
47n/a"""
48n/a
49n/a__version__ = '1.4.1'
50n/a
51n/aimport sys
52n/aimport os
53n/aimport getopt
54n/aimport ColorDB
55n/a
56n/afrom PyncheWidget import PyncheWidget
57n/afrom Switchboard import Switchboard
58n/afrom StripViewer import StripViewer
59n/afrom ChipViewer import ChipViewer
60n/afrom TypeinViewer import TypeinViewer
61n/a
62n/a
63n/a
64n/aPROGRAM = sys.argv[0]
65n/aAUTHNAME = 'Barry Warsaw'
66n/aAUTHEMAIL = 'barry@python.org'
67n/a
68n/a# Default locations of rgb.txt or other textual color database
69n/aRGB_TXT = [
70n/a # Solaris OpenWindows
71n/a '/usr/openwin/lib/rgb.txt',
72n/a # Linux
73n/a '/usr/lib/X11/rgb.txt',
74n/a # The X11R6.4 rgb.txt file
75n/a os.path.join(sys.path[0], 'X/rgb.txt'),
76n/a # add more here
77n/a ]
78n/a
79n/a
80n/a
81n/a# Do this because PyncheWidget.py wants to get at the interpolated docstring
82n/a# too, for its Help menu.
83n/adef docstring():
84n/a return __doc__ % globals()
85n/a
86n/a
87n/adef usage(code, msg=''):
88n/a print(docstring())
89n/a if msg:
90n/a print(msg)
91n/a sys.exit(code)
92n/a
93n/a
94n/a
95n/adef initial_color(s, colordb):
96n/a # function called on every color
97n/a def scan_color(s, colordb=colordb):
98n/a try:
99n/a r, g, b = colordb.find_byname(s)
100n/a except ColorDB.BadColor:
101n/a try:
102n/a r, g, b = ColorDB.rrggbb_to_triplet(s)
103n/a except ColorDB.BadColor:
104n/a return None, None, None
105n/a return r, g, b
106n/a #
107n/a # First try the passed in color
108n/a r, g, b = scan_color(s)
109n/a if r is None:
110n/a # try the same color with '#' prepended, since some shells require
111n/a # this to be escaped, which is a pain
112n/a r, g, b = scan_color('#' + s)
113n/a if r is None:
114n/a print('Bad initial color, using gray50:', s)
115n/a r, g, b = scan_color('gray50')
116n/a if r is None:
117n/a usage(1, 'Cannot find an initial color to use')
118n/a # does not return
119n/a return r, g, b
120n/a
121n/a
122n/a
123n/adef build(master=None, initialcolor=None, initfile=None, ignore=None,
124n/a dbfile=None):
125n/a # create all output widgets
126n/a s = Switchboard(not ignore and initfile)
127n/a # defer to the command line chosen color database, falling back to the one
128n/a # in the .pynche file.
129n/a if dbfile is None:
130n/a dbfile = s.optiondb().get('DBFILE')
131n/a # find a parseable color database
132n/a colordb = None
133n/a files = RGB_TXT[:]
134n/a if dbfile is None:
135n/a dbfile = files.pop()
136n/a while colordb is None:
137n/a try:
138n/a colordb = ColorDB.get_colordb(dbfile)
139n/a except (KeyError, IOError):
140n/a pass
141n/a if colordb is None:
142n/a if not files:
143n/a break
144n/a dbfile = files.pop(0)
145n/a if not colordb:
146n/a usage(1, 'No color database file found, see the -d option.')
147n/a s.set_colordb(colordb)
148n/a
149n/a # create the application window decorations
150n/a app = PyncheWidget(__version__, s, master=master)
151n/a w = app.window()
152n/a
153n/a # these built-in viewers live inside the main Pynche window
154n/a s.add_view(StripViewer(s, w))
155n/a s.add_view(ChipViewer(s, w))
156n/a s.add_view(TypeinViewer(s, w))
157n/a
158n/a # get the initial color as components and set the color on all views. if
159n/a # there was no initial color given on the command line, use the one that's
160n/a # stored in the option database
161n/a if initialcolor is None:
162n/a optiondb = s.optiondb()
163n/a red = optiondb.get('RED')
164n/a green = optiondb.get('GREEN')
165n/a blue = optiondb.get('BLUE')
166n/a # but if there wasn't any stored in the database, use grey50
167n/a if red is None or blue is None or green is None:
168n/a red, green, blue = initial_color('grey50', colordb)
169n/a else:
170n/a red, green, blue = initial_color(initialcolor, colordb)
171n/a s.update_views(red, green, blue)
172n/a return app, s
173n/a
174n/a
175n/adef run(app, s):
176n/a try:
177n/a app.start()
178n/a except KeyboardInterrupt:
179n/a pass
180n/a
181n/a
182n/a
183n/adef main():
184n/a try:
185n/a opts, args = getopt.getopt(
186n/a sys.argv[1:],
187n/a 'hd:i:Xv',
188n/a ['database=', 'initfile=', 'ignore', 'help', 'version'])
189n/a except getopt.error as msg:
190n/a usage(1, msg)
191n/a
192n/a if len(args) == 0:
193n/a initialcolor = None
194n/a elif len(args) == 1:
195n/a initialcolor = args[0]
196n/a else:
197n/a usage(1)
198n/a
199n/a ignore = False
200n/a dbfile = None
201n/a initfile = os.path.expanduser('~/.pynche')
202n/a for opt, arg in opts:
203n/a if opt in ('-h', '--help'):
204n/a usage(0)
205n/a elif opt in ('-v', '--version'):
206n/a print("""\
207n/aPynche -- The PYthon Natural Color and Hue Editor.
208n/aContact: %(AUTHNAME)s
209n/aEmail: %(AUTHEMAIL)s
210n/aVersion: %(__version__)s""" % globals())
211n/a sys.exit(0)
212n/a elif opt in ('-d', '--database'):
213n/a dbfile = arg
214n/a elif opt in ('-X', '--ignore'):
215n/a ignore = True
216n/a elif opt in ('-i', '--initfile'):
217n/a initfile = arg
218n/a
219n/a app, sb = build(initialcolor=initialcolor,
220n/a initfile=initfile,
221n/a ignore=ignore,
222n/a dbfile=dbfile)
223n/a run(app, sb)
224n/a sb.save_views()
225n/a
226n/a
227n/a
228n/aif __name__ == '__main__':
229n/a main()