| 1 | """IC wrapper module, based on Internet Config 1.3"""
|
|---|
| 2 |
|
|---|
| 3 | import icglue
|
|---|
| 4 | import string
|
|---|
| 5 | import sys
|
|---|
| 6 | import os
|
|---|
| 7 | from Carbon import Res
|
|---|
| 8 | import Carbon.File
|
|---|
| 9 | import macostools
|
|---|
| 10 |
|
|---|
| 11 | error=icglue.error
|
|---|
| 12 |
|
|---|
| 13 | # From ictypes.h:
|
|---|
| 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
|
|---|
| 24 |
|
|---|
| 25 | ICattr_no_change = -1
|
|---|
| 26 |
|
|---|
| 27 | icNoPerm = 0
|
|---|
| 28 | icReadOnlyPerm = 1
|
|---|
| 29 | icReadWritePerm = 2
|
|---|
| 30 | # End of ictypes.h
|
|---|
| 31 |
|
|---|
| 32 | class ICOpaqueData:
|
|---|
| 33 | """An unparseable IC entry"""
|
|---|
| 34 | def __init__(self, data):
|
|---|
| 35 | self.data = data
|
|---|
| 36 |
|
|---|
| 37 | def __repr__(self):
|
|---|
| 38 | return "ICOpaqueData(%r)"%(self.data,)
|
|---|
| 39 |
|
|---|
| 40 | _ICOpaqueDataType=type(ICOpaqueData(''))
|
|---|
| 41 |
|
|---|
| 42 | def _decode_default(data, key):
|
|---|
| 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)
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | def _decode_multistr(data, key):
|
|---|
| 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
|
|---|
| 61 |
|
|---|
| 62 | def _decode_fontrecord(data, key):
|
|---|
| 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]
|
|---|
| 67 |
|
|---|
| 68 | def _decode_boolean(data, key):
|
|---|
| 69 | return ord(data[0])
|
|---|
| 70 |
|
|---|
| 71 | def _decode_text(data, key):
|
|---|
| 72 | return data
|
|---|
| 73 |
|
|---|
| 74 | def _decode_charset(data, key):
|
|---|
| 75 | return data[:256], data[256:]
|
|---|
| 76 |
|
|---|
| 77 | def _decode_appspec(data, key):
|
|---|
| 78 | namelen = ord(data[4])
|
|---|
| 79 | return data[0:4], data[5:5+namelen]
|
|---|
| 80 |
|
|---|
| 81 | def _code_default(data, key):
|
|---|
| 82 | return chr(len(data)) + data
|
|---|
| 83 |
|
|---|
| 84 | def _code_multistr(data, key):
|
|---|
| 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
|
|---|
| 90 |
|
|---|
| 91 | def _code_fontrecord(data, key):
|
|---|
| 92 | size, face, name = data
|
|---|
| 93 | return chr((size>>8) & 0xff) + chr(size & 0xff) + chr(face & 0xff) + \
|
|---|
| 94 | chr(0) + _code_default(name)
|
|---|
| 95 |
|
|---|
| 96 | def _code_boolean(data, key):
|
|---|
| 97 | print 'XXXX boolean:', repr(data)
|
|---|
| 98 | return chr(data)
|
|---|
| 99 |
|
|---|
| 100 | def _code_text(data, key):
|
|---|
| 101 | return data
|
|---|
| 102 |
|
|---|
| 103 | def _code_charset(data, key):
|
|---|
| 104 | return data[0] + data[1]
|
|---|
| 105 |
|
|---|
| 106 | def _code_appspec(data, key):
|
|---|
| 107 | return data[0] + _code_default(data[1])
|
|---|
| 108 |
|
|---|
| 109 | _decoder_table = {
|
|---|
| 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),
|
|---|
| 121 | # "Mapping"
|
|---|
| 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),
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | def _decode(data, key):
|
|---|
| 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)
|
|---|
| 146 |
|
|---|
| 147 | def _code(data, key):
|
|---|
| 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)
|
|---|
| 159 |
|
|---|
| 160 | class IC:
|
|---|
| 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('')
|
|---|
| 169 |
|
|---|
| 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
|
|---|
| 178 |
|
|---|
| 179 | def has_key(self, key):
|
|---|
| 180 | return self.__contains__(key)
|
|---|
| 181 |
|
|---|
| 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
|
|---|
| 188 |
|
|---|
| 189 | def __getitem__(self, key):
|
|---|
| 190 | attr = self.ic.ICFindPrefHandle(key, self.h)
|
|---|
| 191 | return _decode(self.h.data, key)
|
|---|
| 192 |
|
|---|
| 193 | def __setitem__(self, key, value):
|
|---|
| 194 | value = _code(value, key)
|
|---|
| 195 | self.ic.ICSetPref(key, ICattr_no_change, value)
|
|---|
| 196 |
|
|---|
| 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))
|
|---|
| 203 |
|
|---|
| 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
|
|---|
| 214 |
|
|---|
| 215 | def mapfile(self, file):
|
|---|
| 216 | if type(file) != type(''):
|
|---|
| 217 | file = file.as_tuple()[2]
|
|---|
| 218 | return self.ic.ICMapFilename(file)
|
|---|
| 219 |
|
|---|
| 220 | def maptypecreator(self, type, creator, filename=""):
|
|---|
| 221 | return self.ic.ICMapTypeCreator(type, creator, filename)
|
|---|
| 222 |
|
|---|
| 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)
|
|---|
| 228 |
|
|---|
| 229 | # Convenience routines
|
|---|
| 230 | _dft_ic = None
|
|---|
| 231 |
|
|---|
| 232 | def launchurl(url, hint=""):
|
|---|
| 233 | global _dft_ic
|
|---|
| 234 | if _dft_ic == None: _dft_ic = IC()
|
|---|
| 235 | return _dft_ic.launchurl(url, hint)
|
|---|
| 236 |
|
|---|
| 237 | def parseurl(data, start=None, end=None, hint=""):
|
|---|
| 238 | global _dft_ic
|
|---|
| 239 | if _dft_ic == None: _dft_ic = IC()
|
|---|
| 240 | return _dft_ic.parseurl(data, start, end, hint)
|
|---|
| 241 |
|
|---|
| 242 | def mapfile(filename):
|
|---|
| 243 | global _dft_ic
|
|---|
| 244 | if _dft_ic == None: _dft_ic = IC()
|
|---|
| 245 | return _dft_ic.mapfile(filename)
|
|---|
| 246 |
|
|---|
| 247 | def maptypecreator(type, creator, filename=""):
|
|---|
| 248 | global _dft_ic
|
|---|
| 249 | if _dft_ic == None: _dft_ic = IC()
|
|---|
| 250 | return _dft_ic.maptypecreator(type, creator, filename)
|
|---|
| 251 |
|
|---|
| 252 | def settypecreator(file):
|
|---|
| 253 | global _dft_ic
|
|---|
| 254 | if _dft_ic == None: _dft_ic = IC()
|
|---|
| 255 | return _dft_ic.settypecreator(file)
|
|---|
| 256 |
|
|---|
| 257 | def _test():
|
|---|
| 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)
|
|---|
| 266 |
|
|---|
| 267 | if __name__ == '__main__':
|
|---|
| 268 | _test()
|
|---|