blob: 58667323e67ed6e9c0beee63b9815c2363bb01a7 [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
8import Nav
Jack Jansen374c4352003-01-21 22:58:39 +00009import warnings
Jack Jansen58fc91f2002-12-17 23:28:24 +000010
11# First step: ensure we also emulate the MACFS module, which contained
12# all the constants
13
14sys.modules['MACFS'] = sys.modules[__name__]
15
16# Import all those constants
Jack Jansen4235e712002-12-19 23:26:07 +000017from Carbon.Files import *
18from Carbon.Folders import *
Jack Jansen58fc91f2002-12-17 23:28:24 +000019
20# For some obscure historical reason these are here too:
21READ = 1
22WRITE = 2
23smAllScripts = -3
24
Jack Jansen2d0909b2003-01-15 22:36:16 +000025#
26# Find the epoch conversion for file dates in a way that works on OS9 and OSX
27import time
28if time.gmtime(0)[0] == 1970:
29 _EPOCHCONVERT = -((1970-1904)*365 + 17) * (24*60*60) + 0x100000000L
30 def _utc2time(utc):
31 t = utc[1] + _EPOCHCONVERT
32 return int(t)
33 def _time2utc(t):
34 t = t - _EPOCHCONVERT
35 if t < -0x7fffffff:
36 t = t + 0x10000000L
37 return (0, int(t), 0)
38else:
39 def _utc2time(utc): return utc[1]
40 def _time2utc(t): return (0, t, 0)
41
Jack Jansen4235e712002-12-19 23:26:07 +000042# The old name of the error object:
43error = Carbon.File.Error
Jack Jansen58fc91f2002-12-17 23:28:24 +000044
Jack Jansen6dd561b2002-12-26 21:09:39 +000045#
46# The various objects macfs used to export. We override them here, because some
47# of the method names are subtly different.
48#
Jack Jansen58fc91f2002-12-17 23:28:24 +000049class FSSpec(Carbon.File.FSSpec):
Jack Jansen4235e712002-12-19 23:26:07 +000050 def as_fsref(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000051 return FSRef(self)
52
53 def NewAlias(self, src=None):
Jack Jansen4235e712002-12-19 23:26:07 +000054 return Alias(Carbon.File.NewAlias(src, self))
Jack Jansen58fc91f2002-12-17 23:28:24 +000055
56 def GetCreatorType(self):
57 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000058 return finfo.Creator, finfo.Type
Jack Jansen58fc91f2002-12-17 23:28:24 +000059
60 def SetCreatorType(self, ctor, tp):
61 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000062 finfo.Creator = ctor
63 finfo.Type = tp
Jack Jansen58fc91f2002-12-17 23:28:24 +000064 self.FSpSetFInfo(finfo)
65
66 def GetFInfo(self):
67 return self.FSpGetFInfo()
68
69 def SetFInfo(self, info):
70 return self.FSpSetFInfo(info)
71
72 def GetDates(self):
Jack Jansen2d0909b2003-01-15 22:36:16 +000073 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
74 catinfo, d1, d2, d3 = FSRef(self).FSGetCatalogInfo(catInfoFlags)
75 cdate = catinfo.createDate
76 mdate = catinfo.contentModDate
77 bdate = catinfo.backupDate
78 return _utc2time(cdate), _utc2time(mdate), _utc2time(bdate)
Jack Jansen58fc91f2002-12-17 23:28:24 +000079
Jack Jansen2d0909b2003-01-15 22:36:16 +000080 def SetDates(self, cdate, mdate, bdate):
81 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
82 catinfo = Carbon.File.FSCatalogInfo(
83 createDate = _time2utc(cdate),
84 contentModDate = _time2utc(mdate),
85 backupDate = _time2utc(bdate))
86 FSRef(self).FSSetCatalogInfo(catInfoFlags, catinfo)
Jack Jansen58fc91f2002-12-17 23:28:24 +000087
88class FSRef(Carbon.File.FSRef):
Jack Jansen4235e712002-12-19 23:26:07 +000089 def as_fsspec(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000090 return FSSpec(self)
91
92class Alias(Carbon.File.Alias):
93
Jack Jansen58fc91f2002-12-17 23:28:24 +000094 def GetInfo(self, index):
95 return self.GetAliasInfo(index)
96
97 def Update(self, *args):
Jack Jansen27d19c42003-01-08 16:32:29 +000098 pass # print "Alias.Update not yet implemented"
Jack Jansen4235e712002-12-19 23:26:07 +000099
100 def Resolve(self, src=None):
Jack Jansen315e9be2002-12-26 20:46:54 +0000101 fss, changed = self.ResolveAlias(src)
102 return FSSpec(fss), changed
Jack Jansen4235e712002-12-19 23:26:07 +0000103
104from Carbon.File import FInfo
Jack Jansen6dd561b2002-12-26 21:09:39 +0000105
106# Backward-compatible type names:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000107FSSpecType = FSSpec
108FSRefType = FSRef
109AliasType = Alias
110FInfoType = FInfo
111
Jack Jansen6dd561b2002-12-26 21:09:39 +0000112# Global functions:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000113def ResolveAliasFile(fss, chain=1):
Jack Jansen315e9be2002-12-26 20:46:54 +0000114 fss, isdir, isalias = Carbon.File.ResolveAliasFile(fss, chain)
115 return FSSpec(fss), isdir, isalias
Jack Jansen58fc91f2002-12-17 23:28:24 +0000116
117def RawFSSpec(data):
118 return FSSpec(rawdata=data)
119
120def RawAlias(data):
121 return Alias(rawdata=data)
122
123def FindApplication(*args):
124 raise NotImplementedError, "FindApplication no longer implemented"
125
126def NewAliasMinimalFromFullPath(path):
Jack Jansen315e9be2002-12-26 20:46:54 +0000127 return Alias(Carbon.File.NewAliasMinimalFromFullPath(path, '', ''))
Jack Jansen4235e712002-12-19 23:26:07 +0000128
Jack Jansen6dd561b2002-12-26 21:09:39 +0000129# Another global function:
130from Carbon.Folder import FindFolder
131
132#
133# Finally the old Standard File routine emulators.
134#
135
Jack Jansen6dd561b2002-12-26 21:09:39 +0000136_curfolder = None
137
Jack Jansen6dd561b2002-12-26 21:09:39 +0000138def StandardGetFile(*typelist):
139 """Ask for an input file, optionally specifying 4-char file types that are
140 allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000141 return PromptGetFile('', *typelist)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000142
143def PromptGetFile(prompt, *typelist):
144 """Ask for an input file giving the user a prompt message. Optionally you can
145 specifying 4-char file types that are allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000146 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000147 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
148 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000149 if not typelist:
150 typelist = None
151 fss = EasyDialogs.AskFileForOpen(message=prompt, wanted=FSSpec,
152 typeList=typelist, defaultLocation=_handleSetFolder())
153 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000154
155def StandardPutFile(prompt, default=None):
156 """Ask the user for an output file, with a prompt. Optionally you cn supply a
157 default output filename"""
Jack Jansen01524d02003-01-21 15:31:16 +0000158 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000159 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
160 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000161 fss = EasyDialogs.AskFileForSave(wanted=FSSpec, message=prompt,
162 savedFileName=default, defaultLocation=_handleSetFolder())
163 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000164
165def SetFolder(folder):
166 global _curfolder
Jack Jansen374c4352003-01-21 22:58:39 +0000167 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
168 DeprecationWarning, stacklevel=2)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000169 if _curfolder:
Jack Jansen01524d02003-01-21 15:31:16 +0000170 rv = FSSpec(_curfolder)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000171 else:
172 rv = None
Jack Jansen01524d02003-01-21 15:31:16 +0000173 _curfolder = folder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000174 return rv
175
Jack Jansen01524d02003-01-21 15:31:16 +0000176def _handleSetFolder():
Jack Jansen6dd561b2002-12-26 21:09:39 +0000177 global _curfolder
Jack Jansen01524d02003-01-21 15:31:16 +0000178 rv = _curfolder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000179 _curfolder = None
Jack Jansen01524d02003-01-21 15:31:16 +0000180 return rv
Jack Jansen6dd561b2002-12-26 21:09:39 +0000181
182def GetDirectory(prompt=None):
183 """Ask the user to select a folder. Optionally you can give a prompt."""
Jack Jansen01524d02003-01-21 15:31:16 +0000184 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000185 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
186 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000187 fss = EasyDialogs.AskFolder(message=prompt, wanted=FSSpec,
188 defaultLocation=_handleSetFolder())
189 return fss, not fss is None