home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / scripts / EditPythonPrefs.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  4.3 KB  |  179 lines

  1. """Edit the Python Preferences file."""
  2. #
  3. # This program is getting more and more clunky. It should really
  4. # be rewritten in a modeless way some time soon.
  5.  
  6. from Dlg import *
  7. from Events import *
  8. from Res import *
  9. import string
  10. import struct
  11. import macfs
  12. import MacOS
  13. import os
  14. import sys
  15. import Res # For Res.Error
  16. import pythonprefs
  17. import EasyDialogs
  18. import Help
  19.  
  20. # resource IDs in our own resources (dialogs, etc)
  21. MESSAGE_ID = 256
  22.  
  23. DIALOG_ID = 511
  24. TEXT_ITEM = 1
  25. OK_ITEM = 2
  26. CANCEL_ITEM = 3
  27. DIR_ITEM = 4
  28. TITLE_ITEM = 5
  29. OPTIONS_ITEM = 7
  30. HELP_ITEM = 9
  31.  
  32. # The options dialog. There is a correspondence between
  33. # the dialog item numbers and the option.
  34. OPT_DIALOG_ID = 510
  35.  
  36. # Map dialog item numbers to option names (and the reverse)
  37. opt_dialog_map = [
  38.     None,
  39.     "inspect",
  40.     "verbose",
  41.     "optimize",
  42.     "unbuffered",
  43.     "debugging",
  44.     "keepopen",
  45.     "keeperror",
  46.     "nointopt",
  47.     "noargs",
  48.     "delayconsole",
  49.     None, None, None, None, None, None, None, None, # 11-18 are different
  50.     "oldexc",
  51.     "nosite"]
  52. opt_dialog_dict = {}
  53. for i in range(len(opt_dialog_map)):
  54.     if opt_dialog_map[i]:
  55.         opt_dialog_dict[opt_dialog_map[i]] = i
  56. # 1 thru 10 are the options
  57. # The GUSI creator/type and delay-console
  58. OD_CREATOR_ITEM = 11
  59. OD_TYPE_ITEM = 12
  60. OD_OK_ITEM = 13
  61. OD_CANCEL_ITEM = 14
  62. OD_HELP_ITEM = 22
  63.  
  64. def optinteract(options):
  65.     """Let the user interact with the options dialog"""
  66.     d = GetNewDialog(OPT_DIALOG_ID, -1)
  67.     tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
  68.     SetDialogItemText(h, options['creator'])
  69.     tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
  70.     SetDialogItemText(h, options['type'])
  71.     d.SetDialogDefaultItem(OD_OK_ITEM)
  72.     d.SetDialogCancelItem(OD_CANCEL_ITEM)
  73.     
  74.     while 1:
  75.         for name in opt_dialog_dict.keys():
  76.             num = opt_dialog_dict[name]
  77.             tp, h, rect = d.GetDialogItem(num)
  78.             h.as_Control().SetControlValue(options[name])
  79.         n = ModalDialog(None)
  80.         if n == OD_OK_ITEM:
  81.             tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
  82.             ncreator = GetDialogItemText(h)
  83.             tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
  84.             ntype = GetDialogItemText(h)
  85.             if len(ncreator) == 4 and len(ntype) == 4:
  86.                 options['creator'] = ncreator
  87.                 options['type'] = ntype
  88.                 return options
  89.             else:
  90.                 MacOS.SysBeep()
  91.         elif n == OD_CANCEL_ITEM:
  92.             return
  93.         elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
  94.             pass
  95.         elif n == OD_HELP_ITEM:
  96.             onoff = Help.HMGetBalloons()
  97.             Help.HMSetBalloons(not onoff)
  98.         elif 1 <= n <= len(opt_dialog_map):
  99.             options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
  100.  
  101.             
  102. def interact(options, title):
  103.     """Let the user interact with the dialog"""
  104.     try:
  105.         # Try to go to the "correct" dir for GetDirectory
  106.         os.chdir(options['dir'].as_pathname())
  107.     except os.error:
  108.         pass
  109.     d = GetNewDialog(DIALOG_ID, -1)
  110.     tp, h, rect = d.GetDialogItem(TITLE_ITEM)
  111.     SetDialogItemText(h, title)
  112.     tp, h, rect = d.GetDialogItem(TEXT_ITEM)
  113. ##    SetDialogItemText(h, string.joinfields(list, '\r'))
  114.     h.data = string.joinfields(options['path'], '\r')
  115.     d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
  116.     d.SelectDialogItemText(TEXT_ITEM, 0, 0)
  117. ##    d.SetDialogDefaultItem(OK_ITEM)
  118.     d.SetDialogCancelItem(CANCEL_ITEM)
  119.     d.GetDialogWindow().ShowWindow()
  120.     d.DrawDialog()
  121.     while 1:
  122.         n = ModalDialog(None)
  123.         if n == OK_ITEM:
  124.             break
  125.         if n == CANCEL_ITEM:
  126.             return None
  127. ##        if n == REVERT_ITEM:
  128. ##            return [], pythondir
  129.         if n == DIR_ITEM:
  130.             fss, ok = macfs.GetDirectory('Select python home folder:')
  131.             if ok:
  132.                 options['dir'] = fss
  133.         elif n == HELP_ITEM:
  134.             onoff = Help.HMGetBalloons()
  135.             Help.HMSetBalloons(not onoff)
  136.         if n == OPTIONS_ITEM:
  137.             noptions = options
  138.             for k in options.keys():
  139.                 noptions[k] = options[k]
  140.             noptions = optinteract(noptions)
  141.             if noptions:
  142.                 options = noptions
  143.     tmp = string.splitfields(h.data, '\r')
  144.     newpath = []
  145.     for i in tmp:
  146.         if i:
  147.             newpath.append(i)
  148.     options['path'] = newpath
  149.     return options
  150.     
  151.     
  152. def edit_preferences():
  153.     handler = pythonprefs.PythonOptions()
  154.     result = interact(handler.load(), 'System-wide preferences')
  155.     if result:
  156.         handler.save(result)
  157.     
  158. def edit_applet(name):
  159.     handler = pythonprefs.AppletOptions(name)
  160.     result = interact(handler.load(), os.path.split(name)[1])
  161.     if result:
  162.         handler.save(result)
  163.  
  164. def main():
  165.     try:
  166.         h = OpenResFile('EditPythonPrefs.rsrc')
  167.     except Res.Error:
  168.         pass    # Assume we already have acces to our own resource
  169.     
  170.     if len(sys.argv) <= 1:
  171.         edit_preferences()
  172.     else:
  173.         for appl in sys.argv[1:]:
  174.             edit_applet(appl)
  175.         
  176.  
  177. if __name__ == '__main__':
  178.     main()
  179.