blob: 56033f319a938c15740449318265887c6fd6b923 [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:
38 def _utc2time(utc): return utc[1]
Jack Jansen32417e72003-01-22 14:04:18 +000039 def _time2utc(t):
40 if t > 0x7fffffff:
41 t = t - 0x100000000L
42 return (0, int(t), 0)
Jack Jansen2d0909b2003-01-15 22:36:16 +000043
Jack Jansen4235e712002-12-19 23:26:07 +000044# The old name of the error object:
45error = Carbon.File.Error
Jack Jansen58fc91f2002-12-17 23:28:24 +000046
Jack Jansen6dd561b2002-12-26 21:09:39 +000047#
48# The various objects macfs used to export. We override them here, because some
49# of the method names are subtly different.
50#
Jack Jansen58fc91f2002-12-17 23:28:24 +000051class FSSpec(Carbon.File.FSSpec):
Jack Jansen4235e712002-12-19 23:26:07 +000052 def as_fsref(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000053 return FSRef(self)
54
55 def NewAlias(self, src=None):
Jack Jansen4235e712002-12-19 23:26:07 +000056 return Alias(Carbon.File.NewAlias(src, self))
Jack Jansen58fc91f2002-12-17 23:28:24 +000057
58 def GetCreatorType(self):
59 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000060 return finfo.Creator, finfo.Type
Jack Jansen58fc91f2002-12-17 23:28:24 +000061
62 def SetCreatorType(self, ctor, tp):
63 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000064 finfo.Creator = ctor
65 finfo.Type = tp
Jack Jansen58fc91f2002-12-17 23:28:24 +000066 self.FSpSetFInfo(finfo)
67
68 def GetFInfo(self):
69 return self.FSpGetFInfo()
70
71 def SetFInfo(self, info):
72 return self.FSpSetFInfo(info)
73
74 def GetDates(self):
Jack Jansen2d0909b2003-01-15 22:36:16 +000075 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
76 catinfo, d1, d2, d3 = FSRef(self).FSGetCatalogInfo(catInfoFlags)
77 cdate = catinfo.createDate
78 mdate = catinfo.contentModDate
79 bdate = catinfo.backupDate
80 return _utc2time(cdate), _utc2time(mdate), _utc2time(bdate)
Jack Jansen58fc91f2002-12-17 23:28:24 +000081
Jack Jansen2d0909b2003-01-15 22:36:16 +000082 def SetDates(self, cdate, mdate, bdate):
83 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
84 catinfo = Carbon.File.FSCatalogInfo(
85 createDate = _time2utc(cdate),
86 contentModDate = _time2utc(mdate),
87 backupDate = _time2utc(bdate))
88 FSRef(self).FSSetCatalogInfo(catInfoFlags, catinfo)
Jack Jansen58fc91f2002-12-17 23:28:24 +000089
90class FSRef(Carbon.File.FSRef):
Jack Jansen4235e712002-12-19 23:26:07 +000091 def as_fsspec(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000092 return FSSpec(self)
93
94class Alias(Carbon.File.Alias):
95
Jack Jansen58fc91f2002-12-17 23:28:24 +000096 def GetInfo(self, index):
97 return self.GetAliasInfo(index)
98
99 def Update(self, *args):
Jack Jansen27d19c42003-01-08 16:32:29 +0000100 pass # print "Alias.Update not yet implemented"
Jack Jansen4235e712002-12-19 23:26:07 +0000101
102 def Resolve(self, src=None):
Jack Jansen315e9be2002-12-26 20:46:54 +0000103 fss, changed = self.ResolveAlias(src)
104 return FSSpec(fss), changed
Jack Jansen4235e712002-12-19 23:26:07 +0000105
106from Carbon.File import FInfo
Jack Jansen6dd561b2002-12-26 21:09:39 +0000107
108# Backward-compatible type names:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000109FSSpecType = FSSpec
110FSRefType = FSRef
111AliasType = Alias
112FInfoType = FInfo
113
Jack Jansen6dd561b2002-12-26 21:09:39 +0000114# Global functions:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000115def ResolveAliasFile(fss, chain=1):
Jack Jansen315e9be2002-12-26 20:46:54 +0000116 fss, isdir, isalias = Carbon.File.ResolveAliasFile(fss, chain)
117 return FSSpec(fss), isdir, isalias
Jack Jansen58fc91f2002-12-17 23:28:24 +0000118
119def RawFSSpec(data):
120 return FSSpec(rawdata=data)
121
122def RawAlias(data):
123 return Alias(rawdata=data)
124
125def FindApplication(*args):
126 raise NotImplementedError, "FindApplication no longer implemented"
127
128def NewAliasMinimalFromFullPath(path):
Jack Jansen315e9be2002-12-26 20:46:54 +0000129 return Alias(Carbon.File.NewAliasMinimalFromFullPath(path, '', ''))
Jack Jansen4235e712002-12-19 23:26:07 +0000130
Jack Jansen6dd561b2002-12-26 21:09:39 +0000131# Another global function:
132from Carbon.Folder import FindFolder
133
134#
135# Finally the old Standard File routine emulators.
136#
137
Jack Jansen6dd561b2002-12-26 21:09:39 +0000138_curfolder = None
139
Jack Jansen6dd561b2002-12-26 21:09:39 +0000140def StandardGetFile(*typelist):
141 """Ask for an input file, optionally specifying 4-char file types that are
142 allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000143 return PromptGetFile('', *typelist)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000144
145def PromptGetFile(prompt, *typelist):
146 """Ask for an input file giving the user a prompt message. Optionally you can
147 specifying 4-char file types that are allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000148 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000149 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
150 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000151 if not typelist:
152 typelist = None
153 fss = EasyDialogs.AskFileForOpen(message=prompt, wanted=FSSpec,
154 typeList=typelist, defaultLocation=_handleSetFolder())
155 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000156
157def StandardPutFile(prompt, default=None):
158 """Ask the user for an output file, with a prompt. Optionally you cn supply a
159 default output filename"""
Jack Jansen01524d02003-01-21 15:31:16 +0000160 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000161 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
162 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000163 fss = EasyDialogs.AskFileForSave(wanted=FSSpec, message=prompt,
164 savedFileName=default, defaultLocation=_handleSetFolder())
165 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000166
167def SetFolder(folder):
168 global _curfolder
Jack Jansen374c4352003-01-21 22:58:39 +0000169 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
170 DeprecationWarning, stacklevel=2)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000171 if _curfolder:
Jack Jansen01524d02003-01-21 15:31:16 +0000172 rv = FSSpec(_curfolder)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000173 else:
174 rv = None
Jack Jansen01524d02003-01-21 15:31:16 +0000175 _curfolder = folder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000176 return rv
177
Jack Jansen01524d02003-01-21 15:31:16 +0000178def _handleSetFolder():
Jack Jansen6dd561b2002-12-26 21:09:39 +0000179 global _curfolder
Jack Jansen01524d02003-01-21 15:31:16 +0000180 rv = _curfolder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000181 _curfolder = None
Jack Jansen01524d02003-01-21 15:31:16 +0000182 return rv
Jack Jansen6dd561b2002-12-26 21:09:39 +0000183
184def GetDirectory(prompt=None):
185 """Ask the user to select a folder. Optionally you can give a prompt."""
Jack Jansen01524d02003-01-21 15:31:16 +0000186 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000187 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
188 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000189 fss = EasyDialogs.AskFolder(message=prompt, wanted=FSSpec,
190 defaultLocation=_handleSetFolder())
191 return fss, not fss is None