/Lib/plat-mac/icopen.py

http://unladen-swallow.googlecode.com/ · Python · 69 lines · 56 code · 1 blank · 12 comment · 7 complexity · 20486d2731ba0681612d9fca4c210bf0 MD5 · raw file

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