Jack Jansen | 54b9ce1 | 1999-12-18 16:57:33 +0000 | [diff] [blame^] | 1 | """StandardFile compatability module: implement macfs StandardFile |
| 2 | API calls with Navigation Services""" |
| 3 | import macfs |
| 4 | import struct |
| 5 | import Res |
| 6 | try: |
| 7 | import Nav |
| 8 | except ImportError: |
| 9 | Nav = None |
| 10 | |
| 11 | _curfolder = None |
| 12 | _movablemodal = 1 |
| 13 | |
| 14 | def _mktypelist(typelist): |
| 15 | if not typelist: |
| 16 | return None |
| 17 | data = 'Pyth' + struct.pack("hh", 0, len(typelist)) |
| 18 | for type in typelist: |
| 19 | data = data+type |
| 20 | return Res.Resource(data) |
| 21 | |
| 22 | def _StandardGetFile(*typelist): |
| 23 | return apply(_PromptGetFile, (None,)+typelist) |
| 24 | |
| 25 | def _PromptGetFile(prompt, *typelist): |
| 26 | args = {} |
| 27 | flags = 0x56 |
| 28 | typehandle = _mktypelist(typelist) |
| 29 | if typehandle: |
| 30 | args['typeList'] = typehandle |
| 31 | else: |
| 32 | flags = flags | 0x01 |
| 33 | if prompt: |
| 34 | args['message'] = prompt |
| 35 | args['preferenceKey'] = 'PyMC' |
| 36 | if _movablemodal: |
| 37 | args['eventProc'] = None |
| 38 | try: |
| 39 | rr = Nav.NavChooseFile(args) |
| 40 | good = 1 |
| 41 | except Nav.error, arg: |
| 42 | good = 0 |
| 43 | fss = macfs.FSSpec(':cancelled') |
| 44 | else: |
| 45 | fss = rr.selection[0] |
| 46 | ## if typehandle: |
| 47 | ## typehandle.DisposeHandle() |
| 48 | return fss, good |
| 49 | |
| 50 | def _StandardPutFile(prompt, default=None): |
| 51 | args = {} |
| 52 | flags = 0x57 |
| 53 | if prompt: |
| 54 | args['message'] = prompt |
| 55 | args['preferenceKey'] = 'PyMC' |
| 56 | if _movablemodal: |
| 57 | args['eventProc'] = None |
| 58 | try: |
| 59 | rr = Nav.NavPutFile(args) |
| 60 | good = 1 |
| 61 | except Nav.error, arg: |
| 62 | good = 0 |
| 63 | fss = macfs.FSSpec(':cancelled') |
| 64 | else: |
| 65 | fss = rr.selection[0] |
| 66 | return fss, good |
| 67 | |
| 68 | def _SetFolder(folder): |
| 69 | global _curfolder |
| 70 | if _curfolder: |
| 71 | rv = _curfolder |
| 72 | else: |
| 73 | _curfolder = macfs.FSSpec(":") |
| 74 | _curfolder = macfs.FSSpec(folder) |
| 75 | return rv |
| 76 | |
| 77 | def _GetDirectory(prompt=None): |
| 78 | args = {} |
| 79 | flags = 0x57 |
| 80 | if prompt: |
| 81 | args['message'] = prompt |
| 82 | args['preferenceKey'] = 'PyMC' |
| 83 | if _movablemodal: |
| 84 | args['eventProc'] = None |
| 85 | try: |
| 86 | rr = Nav.NavChooseFolder(args) |
| 87 | good = 1 |
| 88 | except Nav.error, arg: |
| 89 | good = 0 |
| 90 | fss = macfs.FSSpec(':cancelled') |
| 91 | else: |
| 92 | fss = rr.selection[0] |
| 93 | return fss, good |
| 94 | |
| 95 | def _install(): |
| 96 | macfs.StandardGetFile = StandardGetFile |
| 97 | macfs.PromptGetFile = PromptGetFile |
| 98 | macfs.StandardPutFile = StandardPutFile |
| 99 | macfs.SetFolder = SetFolder |
| 100 | macfs.GetDirectory = GetDirectory |
| 101 | |
| 102 | if Nav and Nav.NavServicesAvailable(): |
| 103 | StandardGetFile = _StandardGetFile |
| 104 | PromptGetFile = _PromptGetFile |
| 105 | StandardPutFile = _StandardPutFile |
| 106 | SetFolder = _SetFolder |
| 107 | GetDirectory = _GetDirectory |
| 108 | else: |
| 109 | from macfs import StandardGetFile, PromptGetFile, StandardPutFile, SetFolder, GetDirectory |
| 110 | |
| 111 | |
| 112 | if __name__ == '__main__': |
| 113 | print 'Testing StandardGetFile' |
| 114 | fss, ok = StandardGetFile() |
| 115 | print '->', fss, ok |
| 116 | print 'Testing StandardGetFile("TEXT")' |
| 117 | fss, ok = StandardGetFile("TEXT") |
| 118 | print '->', fss, ok |
| 119 | print 'Testing PromptGetFile' |
| 120 | fss, ok = PromptGetFile("prompt") |
| 121 | print '->', fss, ok |
| 122 | print 'Testing StandardPutFile("the prompt", "default")' |
| 123 | fss, ok = StandardPutFile("the prompt", "default") |
| 124 | print '->', fss, ok |
| 125 | print 'Testing GetDirectory("another prompt")' |
| 126 | fss, ok = GetDirectory("Another prompt") |
| 127 | print '->', fss, ok |
| 128 | import sys |
| 129 | sys.exit(1) |
| 130 | |