blob: 68abc2c09bb72681f8a845958d7668ecf70c7a6b [file] [log] [blame]
Jack Jansen58fc91f2002-12-17 23:28:24 +00001"""macfs - Pure Python module designed to be backward compatible with
2macfs and MACFS.
3"""
4import sys
Jack Jansen6dd561b2002-12-26 21:09:39 +00005import struct
6import Carbon.Res
7import Carbon.File
Jack Jansen374c4352003-01-21 22:58:39 +00008import warnings
Jack Jansen58fc91f2002-12-17 23:28:24 +00009
10# First step: ensure we also emulate the MACFS module, which contained
11# all the constants
12
13sys.modules['MACFS'] = sys.modules[__name__]
14
15# Import all those constants
Jack Jansen4235e712002-12-19 23:26:07 +000016from Carbon.Files import *
17from Carbon.Folders import *
Jack Jansen58fc91f2002-12-17 23:28:24 +000018
19# For some obscure historical reason these are here too:
20READ = 1
21WRITE = 2
22smAllScripts = -3
23
Jack Jansen2d0909b2003-01-15 22:36:16 +000024#
25# Find the epoch conversion for file dates in a way that works on OS9 and OSX
26import time
27if time.gmtime(0)[0] == 1970:
28 _EPOCHCONVERT = -((1970-1904)*365 + 17) * (24*60*60) + 0x100000000L
29 def _utc2time(utc):
30 t = utc[1] + _EPOCHCONVERT
31 return int(t)
32 def _time2utc(t):
Jack Jansen32417e72003-01-22 14:04:18 +000033 t = int(t) - _EPOCHCONVERT
Jack Jansen2d0909b2003-01-15 22:36:16 +000034 if t < -0x7fffffff:
35 t = t + 0x10000000L
36 return (0, int(t), 0)
37else:
Jack Jansencbdffce2003-01-29 09:56:56 +000038 def _utc2time(utc):
39 t = utc[1]
40 if t < 0:
41 t = t + 0x100000000L
42 return t
Jack Jansen32417e72003-01-22 14:04:18 +000043 def _time2utc(t):
44 if t > 0x7fffffff:
45 t = t - 0x100000000L
46 return (0, int(t), 0)
Jack Jansen2d0909b2003-01-15 22:36:16 +000047
Jack Jansen4235e712002-12-19 23:26:07 +000048# The old name of the error object:
49error = Carbon.File.Error
Jack Jansen58fc91f2002-12-17 23:28:24 +000050
Jack Jansen6dd561b2002-12-26 21:09:39 +000051#
52# The various objects macfs used to export. We override them here, because some
53# of the method names are subtly different.
54#
Jack Jansen58fc91f2002-12-17 23:28:24 +000055class FSSpec(Carbon.File.FSSpec):
Jack Jansen4235e712002-12-19 23:26:07 +000056 def as_fsref(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000057 return FSRef(self)
58
59 def NewAlias(self, src=None):
Jack Jansen4235e712002-12-19 23:26:07 +000060 return Alias(Carbon.File.NewAlias(src, self))
Jack Jansen58fc91f2002-12-17 23:28:24 +000061
62 def GetCreatorType(self):
63 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000064 return finfo.Creator, finfo.Type
Jack Jansen58fc91f2002-12-17 23:28:24 +000065
66 def SetCreatorType(self, ctor, tp):
67 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000068 finfo.Creator = ctor
69 finfo.Type = tp
Jack Jansen58fc91f2002-12-17 23:28:24 +000070 self.FSpSetFInfo(finfo)
71
72 def GetFInfo(self):
73 return self.FSpGetFInfo()
74
75 def SetFInfo(self, info):
76 return self.FSpSetFInfo(info)
77
78 def GetDates(self):
Jack Jansen2d0909b2003-01-15 22:36:16 +000079 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
80 catinfo, d1, d2, d3 = FSRef(self).FSGetCatalogInfo(catInfoFlags)
81 cdate = catinfo.createDate
82 mdate = catinfo.contentModDate
83 bdate = catinfo.backupDate
84 return _utc2time(cdate), _utc2time(mdate), _utc2time(bdate)
Jack Jansen58fc91f2002-12-17 23:28:24 +000085
Jack Jansen2d0909b2003-01-15 22:36:16 +000086 def SetDates(self, cdate, mdate, bdate):
87 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
88 catinfo = Carbon.File.FSCatalogInfo(
89 createDate = _time2utc(cdate),
90 contentModDate = _time2utc(mdate),
91 backupDate = _time2utc(bdate))
92 FSRef(self).FSSetCatalogInfo(catInfoFlags, catinfo)
Jack Jansen58fc91f2002-12-17 23:28:24 +000093
94class FSRef(Carbon.File.FSRef):
Jack Jansen4235e712002-12-19 23:26:07 +000095 def as_fsspec(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000096 return FSSpec(self)
97
98class Alias(Carbon.File.Alias):
99
Jack Jansen58fc91f2002-12-17 23:28:24 +0000100 def GetInfo(self, index):
101 return self.GetAliasInfo(index)
102
103 def Update(self, *args):
Jack Jansen27d19c42003-01-08 16:32:29 +0000104 pass # print "Alias.Update not yet implemented"
Jack Jansen4235e712002-12-19 23:26:07 +0000105
106 def Resolve(self, src=None):
Jack Jansen315e9be2002-12-26 20:46:54 +0000107 fss, changed = self.ResolveAlias(src)
108 return FSSpec(fss), changed
Jack Jansen4235e712002-12-19 23:26:07 +0000109
110from Carbon.File import FInfo
Jack Jansen6dd561b2002-12-26 21:09:39 +0000111
112# Backward-compatible type names:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000113FSSpecType = FSSpec
114FSRefType = FSRef
115AliasType = Alias
116FInfoType = FInfo
117
Jack Jansen6dd561b2002-12-26 21:09:39 +0000118# Global functions:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000119def ResolveAliasFile(fss, chain=1):
Jack Jansen315e9be2002-12-26 20:46:54 +0000120 fss, isdir, isalias = Carbon.File.ResolveAliasFile(fss, chain)
121 return FSSpec(fss), isdir, isalias
Jack Jansen58fc91f2002-12-17 23:28:24 +0000122
123def RawFSSpec(data):
124 return FSSpec(rawdata=data)
125
126def RawAlias(data):
127 return Alias(rawdata=data)
128
129def FindApplication(*args):
130 raise NotImplementedError, "FindApplication no longer implemented"
131
132def NewAliasMinimalFromFullPath(path):
Jack Jansen315e9be2002-12-26 20:46:54 +0000133 return Alias(Carbon.File.NewAliasMinimalFromFullPath(path, '', ''))
Jack Jansen4235e712002-12-19 23:26:07 +0000134
Jack Jansen6dd561b2002-12-26 21:09:39 +0000135# Another global function:
136from Carbon.Folder import FindFolder
137
138#
139# Finally the old Standard File routine emulators.
140#
141
Jack Jansen6dd561b2002-12-26 21:09:39 +0000142_curfolder = None
143
Jack Jansen6dd561b2002-12-26 21:09:39 +0000144def StandardGetFile(*typelist):
145 """Ask for an input file, optionally specifying 4-char file types that are
146 allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000147 return PromptGetFile('', *typelist)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000148
149def PromptGetFile(prompt, *typelist):
150 """Ask for an input file giving the user a prompt message. Optionally you can
151 specifying 4-char file types that are allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000152 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000153 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
154 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000155 if not typelist:
156 typelist = None
157 fss = EasyDialogs.AskFileForOpen(message=prompt, wanted=FSSpec,
158 typeList=typelist, defaultLocation=_handleSetFolder())
159 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000160
161def StandardPutFile(prompt, default=None):
162 """Ask the user for an output file, with a prompt. Optionally you cn supply a
163 default output filename"""
Jack Jansen01524d02003-01-21 15:31:16 +0000164 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000165 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
166 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000167 fss = EasyDialogs.AskFileForSave(wanted=FSSpec, message=prompt,
168 savedFileName=default, defaultLocation=_handleSetFolder())
169 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000170
171def SetFolder(folder):
172 global _curfolder
Jack Jansen374c4352003-01-21 22:58:39 +0000173 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
174 DeprecationWarning, stacklevel=2)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000175 if _curfolder:
Jack Jansen01524d02003-01-21 15:31:16 +0000176 rv = FSSpec(_curfolder)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000177 else:
178 rv = None
Jack Jansen01524d02003-01-21 15:31:16 +0000179 _curfolder = folder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000180 return rv
181
Jack Jansen01524d02003-01-21 15:31:16 +0000182def _handleSetFolder():
Jack Jansen6dd561b2002-12-26 21:09:39 +0000183 global _curfolder
Jack Jansen01524d02003-01-21 15:31:16 +0000184 rv = _curfolder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000185 _curfolder = None
Jack Jansen01524d02003-01-21 15:31:16 +0000186 return rv
Jack Jansen6dd561b2002-12-26 21:09:39 +0000187
188def GetDirectory(prompt=None):
189 """Ask the user to select a folder. Optionally you can give a prompt."""
Jack Jansen01524d02003-01-21 15:31:16 +0000190 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000191 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
192 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000193 fss = EasyDialogs.AskFolder(message=prompt, wanted=FSSpec,
194 defaultLocation=_handleSetFolder())
195 return fss, not fss is None