Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 1 | """IC wrapper module, based on Internet Config 1.3""" |
| 2 | |
| 3 | import icglue |
| 4 | import string |
| 5 | import sys |
Jack Jansen | cf0b2e8 | 2003-02-05 15:49:19 +0000 | [diff] [blame] | 6 | import os |
Jack Jansen | 5a6fdcd | 2001-08-25 12:15:04 +0000 | [diff] [blame] | 7 | from Carbon import Res |
Jack Jansen | cf0b2e8 | 2003-02-05 15:49:19 +0000 | [diff] [blame] | 8 | import Carbon.File |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 9 | import macostools |
| 10 | |
| 11 | error=icglue.error |
| 12 | |
| 13 | # From ictypes.h: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 14 | icPrefNotFoundErr = -666 # preference not found (duh!) |
| 15 | icPermErr = -667 # cannot set preference |
| 16 | icPrefDataErr = -668 # problem with preference data |
| 17 | icInternalErr = -669 # hmm, this is not good |
| 18 | icTruncatedErr = -670 # more data was present than was returned |
| 19 | icNoMoreWritersErr = -671 # you cannot begin a write session because someone else is already doing it */ |
| 20 | icNothingToOverrideErr = -672 # no component for the override component to capture |
| 21 | icNoURLErr = -673 # no URL found |
| 22 | icConfigNotFoundErr = -674 # no configuration was found |
| 23 | icConfigInappropriateErr = -675 # incorrect manufacturer code |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 24 | |
| 25 | ICattr_no_change = -1 |
| 26 | |
| 27 | icNoPerm = 0 |
| 28 | icReadOnlyPerm = 1 |
| 29 | icReadWritePerm = 2 |
| 30 | # End of ictypes.h |
| 31 | |
Jack Jansen | 8dc797d | 1997-01-10 15:25:47 +0000 | [diff] [blame] | 32 | class ICOpaqueData: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 33 | """An unparseable IC entry""" |
| 34 | def __init__(self, data): |
| 35 | self.data = data |
Jack Jansen | 8dc797d | 1997-01-10 15:25:47 +0000 | [diff] [blame] | 36 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 37 | def __repr__(self): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 38 | return "ICOpaqueData(%r)"%(self.data,) |
Jack Jansen | 8dc797d | 1997-01-10 15:25:47 +0000 | [diff] [blame] | 39 | |
| 40 | _ICOpaqueDataType=type(ICOpaqueData('')) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 41 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 42 | def _decode_default(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 43 | if len(data) == 0: |
| 44 | return data |
| 45 | if ord(data[0]) == len(data)-1: |
| 46 | # Assume Pstring |
| 47 | return data[1:] |
| 48 | return ICOpaqueData(data) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 49 | |
| 50 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 51 | def _decode_multistr(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 52 | numstr = ord(data[0]) << 8 | ord(data[1]) |
| 53 | rv = [] |
| 54 | ptr = 2 |
| 55 | for i in range(numstr): |
| 56 | strlen = ord(data[ptr]) |
| 57 | str = data[ptr+1:ptr+strlen+1] |
| 58 | rv.append(str) |
| 59 | ptr = ptr + strlen + 1 |
| 60 | return rv |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 61 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 62 | def _decode_fontrecord(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 63 | size = ord(data[0]) << 8 | ord(data[1]) |
| 64 | face = ord(data[2]) |
| 65 | namelen = ord(data[4]) |
| 66 | return size, face, data[5:5+namelen] |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 67 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 68 | def _decode_boolean(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 69 | return ord(data[0]) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 70 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 71 | def _decode_text(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 72 | return data |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 73 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 74 | def _decode_charset(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 75 | return data[:256], data[256:] |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 76 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 77 | def _decode_appspec(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 78 | namelen = ord(data[4]) |
| 79 | return data[0:4], data[5:5+namelen] |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 80 | |
| 81 | def _code_default(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 82 | return chr(len(data)) + data |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 83 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 84 | def _code_multistr(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 85 | numstr = len(data) |
| 86 | rv = chr((numstr>>8) & 0xff) + chr(numstr & 0xff) |
| 87 | for i in data: |
| 88 | rv = rv + _code_default(i) |
| 89 | return rv |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 90 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 91 | def _code_fontrecord(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 92 | size, face, name = data |
| 93 | return chr((size>>8) & 0xff) + chr(size & 0xff) + chr(face & 0xff) + \ |
| 94 | chr(0) + _code_default(name) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 95 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 96 | def _code_boolean(data, key): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 97 | print 'XXXX boolean:', repr(data) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 98 | return chr(data) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 99 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 100 | def _code_text(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 101 | return data |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 102 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 103 | def _code_charset(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 104 | return data[0] + data[1] |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 105 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 106 | def _code_appspec(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 107 | return data[0] + _code_default(data[1]) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 108 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 109 | _decoder_table = { |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 110 | "ArchieAll" : (_decode_multistr , _code_multistr), |
| 111 | "UMichAll" : (_decode_multistr , _code_multistr), |
| 112 | "InfoMacAll" : (_decode_multistr , _code_multistr), |
| 113 | "ListFont" : (_decode_fontrecord , _code_fontrecord), |
| 114 | "ScreenFont" : (_decode_fontrecord , _code_fontrecord), |
| 115 | "PrinterFont" : (_decode_fontrecord , _code_fontrecord), |
| 116 | # "DownloadFolder" : (_decode_filespec , _code_filespec), |
| 117 | "Signature": (_decode_text , _code_text), |
| 118 | "Plan" : (_decode_text , _code_text), |
| 119 | "MailHeaders" : (_decode_text , _code_text), |
| 120 | "NewsHeaders" : (_decode_text , _code_text), |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 121 | # "Mapping" |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 122 | "CharacterSet" : (_decode_charset , _code_charset), |
| 123 | "Helper\245" : (_decode_appspec , _code_appspec), |
| 124 | # "Services" : (_decode_services, ????), |
| 125 | "NewMailFlashIcon" : (_decode_boolean , _code_boolean), |
| 126 | "NewMailDialog" : (_decode_boolean , _code_boolean), |
| 127 | "NewMailPlaySound" : (_decode_boolean , _code_boolean), |
| 128 | # "WebBackgroundColor" : _decode_color, |
| 129 | "NoProxyDomains" : (_decode_multistr , _code_multistr), |
| 130 | "UseHTTPProxy" : (_decode_boolean , _code_boolean), |
| 131 | "UseGopherProxy": (_decode_boolean , _code_boolean), |
| 132 | "UseFTPProxy" : (_decode_boolean , _code_boolean), |
| 133 | "UsePassiveFTP" : (_decode_boolean , _code_boolean), |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | def _decode(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 137 | if '\245' in key: |
| 138 | key2 = key[:string.index(key, '\245')+1] |
| 139 | else: |
| 140 | key2 = key |
| 141 | if _decoder_table.has_key(key2): |
| 142 | decoder = _decoder_table[key2][0] |
| 143 | else: |
| 144 | decoder = _decode_default |
| 145 | return decoder(data, key) |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 146 | |
| 147 | def _code(data, key): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 148 | if type(data) == _ICOpaqueDataType: |
| 149 | return data.data |
| 150 | if '\245' in key: |
| 151 | key2 = key[:string.index(key, '\245')+1] |
| 152 | else: |
| 153 | key2 = key |
| 154 | if _decoder_table.has_key(key2): |
| 155 | coder = _decoder_table[key2][1] |
| 156 | else: |
| 157 | coder = _code_default |
| 158 | return coder(data, key) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 159 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 160 | class IC: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 161 | def __init__(self, signature='Pyth', ic=None): |
| 162 | if ic: |
| 163 | self.ic = ic |
| 164 | else: |
| 165 | self.ic = icglue.ICStart(signature) |
| 166 | if hasattr(self.ic, 'ICFindConfigFile'): |
| 167 | self.ic.ICFindConfigFile() |
| 168 | self.h = Res.Resource('') |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 169 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 170 | def keys(self): |
| 171 | rv = [] |
| 172 | self.ic.ICBegin(icReadOnlyPerm) |
| 173 | num = self.ic.ICCountPref() |
| 174 | for i in range(num): |
| 175 | rv.append(self.ic.ICGetIndPref(i+1)) |
| 176 | self.ic.ICEnd() |
| 177 | return rv |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 178 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 179 | def has_key(self, key): |
| 180 | return self.__contains__(key) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 181 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 182 | def __contains__(self, key): |
| 183 | try: |
| 184 | dummy = self.ic.ICFindPrefHandle(key, self.h) |
| 185 | except icglue.error: |
| 186 | return 0 |
| 187 | return 1 |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 188 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 189 | def __getitem__(self, key): |
| 190 | attr = self.ic.ICFindPrefHandle(key, self.h) |
| 191 | return _decode(self.h.data, key) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 192 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 193 | def __setitem__(self, key, value): |
| 194 | value = _code(value, key) |
| 195 | self.ic.ICSetPref(key, ICattr_no_change, value) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 196 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 197 | def launchurl(self, url, hint=""): |
| 198 | # Work around a bug in ICLaunchURL: file:/foo does |
| 199 | # not work but file:///foo does. |
| 200 | if url[:6] == 'file:/' and url[6] != '/': |
| 201 | url = 'file:///' + url[6:] |
| 202 | self.ic.ICLaunchURL(hint, url, 0, len(url)) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 203 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 204 | def parseurl(self, data, start=None, end=None, hint=""): |
| 205 | if start == None: |
| 206 | selStart = 0 |
| 207 | selEnd = len(data) |
| 208 | else: |
| 209 | selStart = selEnd = start |
| 210 | if end != None: |
| 211 | selEnd = end |
| 212 | selStart, selEnd = self.ic.ICParseURL(hint, data, selStart, selEnd, self.h) |
| 213 | return self.h.data, selStart, selEnd |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 214 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 215 | def mapfile(self, file): |
| 216 | if type(file) != type(''): |
| 217 | file = file.as_tuple()[2] |
| 218 | return self.ic.ICMapFilename(file) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 219 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 220 | def maptypecreator(self, type, creator, filename=""): |
| 221 | return self.ic.ICMapTypeCreator(type, creator, filename) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 222 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 223 | def settypecreator(self, file): |
| 224 | file = Carbon.File.pathname(file) |
| 225 | record = self.mapfile(os.path.split(file)[1]) |
| 226 | MacOS.SetCreatorAndType(file, record[2], record[1]) |
| 227 | macostools.touched(fss) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 228 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 229 | # Convenience routines |
| 230 | _dft_ic = None |
| 231 | |
| 232 | def launchurl(url, hint=""): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 233 | global _dft_ic |
| 234 | if _dft_ic == None: _dft_ic = IC() |
| 235 | return _dft_ic.launchurl(url, hint) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 236 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 237 | def parseurl(data, start=None, end=None, hint=""): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 238 | global _dft_ic |
| 239 | if _dft_ic == None: _dft_ic = IC() |
| 240 | return _dft_ic.parseurl(data, start, end, hint) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 241 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 242 | def mapfile(filename): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 243 | global _dft_ic |
| 244 | if _dft_ic == None: _dft_ic = IC() |
| 245 | return _dft_ic.mapfile(filename) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 246 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 247 | def maptypecreator(type, creator, filename=""): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 248 | global _dft_ic |
| 249 | if _dft_ic == None: _dft_ic = IC() |
| 250 | return _dft_ic.maptypecreator(type, creator, filename) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 251 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 252 | def settypecreator(file): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 253 | global _dft_ic |
| 254 | if _dft_ic == None: _dft_ic = IC() |
| 255 | return _dft_ic.settypecreator(file) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 256 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 257 | def _test(): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 258 | ic = IC() |
| 259 | for k in ic.keys(): |
| 260 | try: |
| 261 | v = ic[k] |
| 262 | except error: |
| 263 | v = '????' |
| 264 | print k, '\t', v |
| 265 | sys.exit(1) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 266 | |
Jack Jansen | 4b76ba3 | 1997-01-09 16:26:23 +0000 | [diff] [blame] | 267 | if __name__ == '__main__': |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 268 | _test() |