blob: 7b8e8f372e727e839c8885fb755c0d6ca8c60309 [file] [log] [blame]
Jack Jansen317b2a62000-05-09 08:38:20 +00001"""icopen patch
2
3OVERVIEW
4
5icopen patches MacOS Python to use the Internet Config file mappings to select
6the type and creator for a file.
7
8Version 1 released to the public domain 3 November 1999
9by Oliver Steele (steele@cs.brandeis.edu).
10
11DETAILS
12
13This patch causes files created by Python's open(filename, 'w') command (and
14by functions and scripts that call it) to set the type and creator of the file
15to the type and creator associated with filename's extension (the
16portion of the filename after the last period), according to Internet Config.
17Thus, a script that creates a file foo.html will create one that opens in whatever
18browser you've set to handle *.html files, and so on.
19
20Python IDE uses its own algorithm to select the type and creator for saved
21editor windows, so this patch won't effect their types.
22
23As of System 8.6 at least, Internet Config is built into the system, and the
24file mappings are accessed from the Advanced pane of the Internet control
25panel. User Mode (in the Edit menu) needs to be set to Advanced in order to
26access this pane.
27
28INSTALLATION
29
30Put this file in your Python path, and create a file named {Python}:sitecustomize.py
31that contains:
Raymond Hettingerff41c482003-04-06 09:01:11 +000032 import icopen
Jack Jansen317b2a62000-05-09 08:38:20 +000033
34(If {Python}:sitecustomizer.py already exists, just add the 'import' line to it.)
35
36The next time you launch PythonInterpreter or Python IDE, the patch will take
37effect.
38"""
39
Benjamin Peterson23681932008-05-12 21:42:13 +000040from warnings import warnpy3k
41warnpy3k("In 3.x, the icopen module is removed.")
42
Jack Jansen317b2a62000-05-09 08:38:20 +000043import __builtin__
44
45_builtin_open = globals().get('_builtin_open', __builtin__.open)
46
47def _open_with_typer(*args):
Tim Peters182b5ac2004-07-18 06:16:08 +000048 file = _builtin_open(*args)
49 filename = args[0]
50 mode = 'r'
51 if args[1:]:
52 mode = args[1]
53 if mode[0] == 'w':
54 from ic import error, settypecreator
55 try:
56 settypecreator(filename)
57 except error:
58 pass
59 return file
Jack Jansen317b2a62000-05-09 08:38:20 +000060
61__builtin__.open = _open_with_typer
62
63"""
64open('test.py')
65_open_with_typer('test.py', 'w')
66_open_with_typer('test.txt', 'w')
67_open_with_typer('test.html', 'w')
68_open_with_typer('test.foo', 'w')
Raymond Hettingerff41c482003-04-06 09:01:11 +000069"""