1 """IC wrapper module, based on Internet Config 1.3"""
13 icPrefNotFoundErr
= -666 # preference not found (duh!)
14 icPermErr
= -667 # cannot set preference
15 icPrefDataErr
= -668 # problem with preference data
16 icInternalErr
= -669 # hmm, this is not good
17 icTruncatedErr
= -670 # more data was present than was returned
18 icNoMoreWritersErr
= -671 # you cannot begin a write session because someone else is already doing it */
19 icNothingToOverrideErr
= -672 # no component for the override component to capture
20 icNoURLErr
= -673 # no URL found
21 icConfigNotFoundErr
= -674 # no configuration was found
22 icConfigInappropriateErr
= -675 # incorrect manufacturer code
32 """An unparseable IC entry"""
33 def __init__(self
, data
):
37 return "ICOpaqueData(%s)"%`self
.data`
39 _ICOpaqueDataType
=type(ICOpaqueData(''))
41 def _decode_default(data
, key
):
44 if ord(data
[0]) == len(data
)-1:
47 return ICOpaqueData(data
)
50 def _decode_multistr(data
, key
):
51 numstr
= ord(data
[0]) << 8 |
ord(data
[1])
54 for i
in range(numstr
):
55 strlen
= ord(data
[ptr
])
56 str = data
[ptr
+1:ptr
+strlen
+1]
58 ptr
= ptr
+ strlen
+ 1
61 def _decode_fontrecord(data
, key
):
62 size
= ord(data
[0]) << 8 |
ord(data
[1])
64 namelen
= ord(data
[4])
65 return size
, face
, data
[5:5+namelen
]
67 def _decode_boolean(data
, key
):
70 def _decode_text(data
, key
):
73 def _decode_charset(data
, key
):
74 return data
[:256], data
[256:]
76 def _decode_appspec(data
, key
):
77 namelen
= ord(data
[4])
78 return data
[0:4], data
[5:5+namelen
]
80 def _code_default(data
, key
):
81 return chr(len(data
)) + data
83 def _code_multistr(data
, key
):
85 rv
= chr((numstr
>>8) & 0xff) + chr(numstr
& 0xff)
87 rv
= rv
+ _code_default(i
)
90 def _code_fontrecord(data
, key
):
91 size
, face
, name
= data
92 return chr((size
>>8) & 0xff) + chr(size
& 0xff) + chr(face
& 0xff) + \
93 chr(0) + _code_default(name
)
95 def _code_boolean(data
, key
):
96 print 'XXXX boolean:', `data`
99 def _code_text(data
, key
):
102 def _code_charset(data
, key
):
103 return data
[0] + data
[1]
105 def _code_appspec(data
, key
):
106 return data
[0] + _code_default(data
[1])
109 "ArchieAll" : (_decode_multistr
, _code_multistr
),
110 "UMichAll" : (_decode_multistr
, _code_multistr
),
111 "InfoMacAll" : (_decode_multistr
, _code_multistr
),
112 "ListFont" : (_decode_fontrecord
, _code_fontrecord
),
113 "ScreenFont" : (_decode_fontrecord
, _code_fontrecord
),
114 "PrinterFont" : (_decode_fontrecord
, _code_fontrecord
),
115 # "DownloadFolder" : (_decode_filespec , _code_filespec),
116 "Signature": (_decode_text
, _code_text
),
117 "Plan" : (_decode_text
, _code_text
),
118 "MailHeaders" : (_decode_text
, _code_text
),
119 "NewsHeaders" : (_decode_text
, _code_text
),
121 "CharacterSet" : (_decode_charset
, _code_charset
),
122 "Helper\245" : (_decode_appspec
, _code_appspec
),
123 # "Services" : (_decode_services, ????),
124 "NewMailFlashIcon" : (_decode_boolean
, _code_boolean
),
125 "NewMailDialog" : (_decode_boolean
, _code_boolean
),
126 "NewMailPlaySound" : (_decode_boolean
, _code_boolean
),
127 # "WebBackgroundColor" : _decode_color,
128 "NoProxyDomains" : (_decode_multistr
, _code_multistr
),
129 "UseHTTPProxy" : (_decode_boolean
, _code_boolean
),
130 "UseGopherProxy": (_decode_boolean
, _code_boolean
),
131 "UseFTPProxy" : (_decode_boolean
, _code_boolean
),
132 "UsePassiveFTP" : (_decode_boolean
, _code_boolean
),
135 def _decode(data
, key
):
137 key2
= key
[:string
.index(key
, '\245')+1]
140 if _decoder_table
.has_key(key2
):
141 decoder
= _decoder_table
[key2
][0]
143 decoder
= _decode_default
144 return decoder(data
, key
)
146 def _code(data
, key
):
147 if type(data
) == _ICOpaqueDataType
:
150 key2
= key
[:string
.index(key
, '\245')+1]
153 if _decoder_table
.has_key(key2
):
154 coder
= _decoder_table
[key2
][1]
156 coder
= _code_default
157 return coder(data
, key
)
160 def __init__(self
, signature
='Pyth', ic
=None):
164 self
.ic
= icglue
.ICStart(signature
)
165 self
.ic
.ICFindConfigFile()
166 self
.h
= Res
.Resource('')
170 self
.ic
.ICBegin(icReadOnlyPerm
)
171 num
= self
.ic
.ICCountPref()
173 rv
.append(self
.ic
.ICGetIndPref(i
+1))
177 def has_key(self
, key
):
179 dummy
= self
.ic
.ICFindPrefHandle(key
, self
.h
)
184 def __getitem__(self
, key
):
185 attr
= self
.ic
.ICFindPrefHandle(key
, self
.h
)
186 return _decode(self
.h
.data
, key
)
188 def __setitem__(self
, key
, value
):
189 value
= _code(value
, key
)
190 self
.ic
.ICSetPref(key
, ICattr_no_change
, value
)
192 def launchurl(self
, url
, hint
=""):
193 self
.ic
.ICLaunchURL(hint
, url
, 0, len(url
))
195 def parseurl(self
, data
, start
=None, end
=None, hint
=""):
200 selStart
= selEnd
= start
203 selStart
, selEnd
= self
.ic
.ICParseURL(hint
, data
, selStart
, selEnd
, self
.h
)
204 return self
.h
.data
, selStart
, selEnd
206 def mapfile(self
, file):
207 if type(file) != type(''):
208 file = file.as_tuple()[2]
209 return self
.ic
.ICMapFilename(file)
211 def maptypecreator(self
, type, creator
, filename
=""):
212 return self
.ic
.ICMapTypeCreator(type, creator
, filename
)
214 def settypecreator(self
, file):
215 if type(file) == type(''):
216 fss
= macfs
.FSSpec(file)
219 name
= fss
.as_tuple()[2]
220 record
= self
.mapfile(name
)
221 fss
.SetCreatorType(record
[2], record
[1])
222 macostools
.touched(fss
)
224 # Convenience routines
227 def launchurl(url
, hint
=""):
229 if _dft_ic
== None: _dft_ic
= IC()
230 return _dft_ic
.launchurl(url
, hint
)
232 def parseurl(data
, start
=None, end
=None, hint
=""):
234 if _dft_ic
== None: _dft_ic
= IC()
235 return _dft_ic
.parseurl(data
, start
, end
, hint
)
237 def mapfile(filename
):
239 if _dft_ic
== None: _dft_ic
= IC()
240 return _dft_ic
.mapfile(filename
)
242 def maptypecreator(type, creator
, filename
=""):
244 if _dft_ic
== None: _dft_ic
= IC()
245 return _dft_ic
.maptypecreator(type, creator
, filename
)
247 def settypecreator(file):
249 if _dft_ic
== None: _dft_ic
= IC()
250 return _dft_ic
.settypecreator(file)
262 if __name__
== '__main__':