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