ยปCore Development>Code coverage>Lib/plat-mac/icopen.py

Python code coverage for Lib/plat-mac/icopen.py

#countcontent
1n/a"""icopen patch
2n/a
3n/aOVERVIEW
4n/a
5n/aicopen patches MacOS Python to use the Internet Config file mappings to select
6n/athe type and creator for a file.
7n/a
8n/aVersion 1 released to the public domain 3 November 1999
9n/aby Oliver Steele (steele@cs.brandeis.edu).
10n/a
11n/aDETAILS
12n/a
13n/aThis patch causes files created by Python's open(filename, 'w') command (and
14n/aby functions and scripts that call it) to set the type and creator of the file
15n/ato the type and creator associated with filename's extension (the
16n/aportion of the filename after the last period), according to Internet Config.
17n/aThus, a script that creates a file foo.html will create one that opens in whatever
18n/abrowser you've set to handle *.html files, and so on.
19n/a
20n/aPython IDE uses its own algorithm to select the type and creator for saved
21n/aeditor windows, so this patch won't effect their types.
22n/a
23n/aAs of System 8.6 at least, Internet Config is built into the system, and the
24n/afile mappings are accessed from the Advanced pane of the Internet control
25n/apanel. User Mode (in the Edit menu) needs to be set to Advanced in order to
26n/aaccess this pane.
27n/a
28n/aINSTALLATION
29n/a
30n/aPut this file in your Python path, and create a file named {Python}:sitecustomize.py
31n/athat contains:
32n/a import icopen
33n/a
34n/a(If {Python}:sitecustomizer.py already exists, just add the 'import' line to it.)
35n/a
36n/aThe next time you launch PythonInterpreter or Python IDE, the patch will take
37n/aeffect.
38n/a"""
39n/a
40n/afrom warnings import warnpy3k
41n/awarnpy3k("In 3.x, the icopen module is removed.", stacklevel=2)
42n/a
43n/aimport __builtin__
44n/a
45n/a_builtin_open = globals().get('_builtin_open', __builtin__.open)
46n/a
47n/adef _open_with_typer(*args):
48n/a file = _builtin_open(*args)
49n/a filename = args[0]
50n/a mode = 'r'
51n/a if args[1:]:
52n/a mode = args[1]
53n/a if mode[0] == 'w':
54n/a from ic import error, settypecreator
55n/a try:
56n/a settypecreator(filename)
57n/a except error:
58n/a pass
59n/a return file
60n/a
61n/a__builtin__.open = _open_with_typer
62n/a
63n/a"""
64n/aopen('test.py')
65n/a_open_with_typer('test.py', 'w')
66n/a_open_with_typer('test.txt', 'w')
67n/a_open_with_typer('test.html', 'w')
68n/a_open_with_typer('test.foo', 'w')
69n/a"""