blob: 1a8a4f8a77d6ceaf8dbcea4dc95787f28dbd69c8 [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):
Jack Jansen32417e72003-01-22 14:04:18 +000034 t = int(t) - _EPOCHCONVERT
Jack Jansen2d0909b2003-01-15 22:36:16 +000035 if t < -0x7fffffff:
36 t = t + 0x10000000L
37 return (0, int(t), 0)
38else:
39 def _utc2time(utc): return utc[1]
Jack Jansen32417e72003-01-22 14:04:18 +000040 def _time2utc(t):
41 if t > 0x7fffffff:
42 t = t - 0x100000000L
43 return (0, int(t), 0)
Jack Jansen2d0909b2003-01-15 22:36:16 +000044
Jack Jansen4235e712002-12-19 23:26:07 +000045# The old name of the error object:
46error = Carbon.File.Error
Jack Jansen58fc91f2002-12-17 23:28:24 +000047
Jack Jansen6dd561b2002-12-26 21:09:39 +000048#
49# The various objects macfs used to export. We override them here, because some
50# of the method names are subtly different.
51#
Jack Jansen58fc91f2002-12-17 23:28:24 +000052class FSSpec(Carbon.File.FSSpec):
Jack Jansen4235e712002-12-19 23:26:07 +000053 def as_fsref(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000054 return FSRef(self)
55
56 def NewAlias(self, src=None):
Jack Jansen4235e712002-12-19 23:26:07 +000057 return Alias(Carbon.File.NewAlias(src, self))
Jack Jansen58fc91f2002-12-17 23:28:24 +000058
59 def GetCreatorType(self):
60 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000061 return finfo.Creator, finfo.Type
Jack Jansen58fc91f2002-12-17 23:28:24 +000062
63 def SetCreatorType(self, ctor, tp):
64 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000065 finfo.Creator = ctor
66 finfo.Type = tp
Jack Jansen58fc91f2002-12-17 23:28:24 +000067 self.FSpSetFInfo(finfo)
68
69 def GetFInfo(self):
70 return self.FSpGetFInfo()
71
72 def SetFInfo(self, info):
73 return self.FSpSetFInfo(info)
74
75 def GetDates(self):
Jack Jansen2d0909b2003-01-15 22:36:16 +000076 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
77 catinfo, d1, d2, d3 = FSRef(self).FSGetCatalogInfo(catInfoFlags)
78 cdate = catinfo.createDate
79 mdate = catinfo.contentModDate
80 bdate = catinfo.backupDate
81 return _utc2time(cdate), _utc2time(mdate), _utc2time(bdate)
Jack Jansen58fc91f2002-12-17 23:28:24 +000082
Jack Jansen2d0909b2003-01-15 22:36:16 +000083 def SetDates(self, cdate, mdate, bdate):
84 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
85 catinfo = Carbon.File.FSCatalogInfo(
86 createDate = _time2utc(cdate),
87 contentModDate = _time2utc(mdate),
88 backupDate = _time2utc(bdate))
89 FSRef(self).FSSetCatalogInfo(catInfoFlags, catinfo)
Jack Jansen58fc91f2002-12-17 23:28:24 +000090
91class FSRef(Carbon.File.FSRef):
Jack Jansen4235e712002-12-19 23:26:07 +000092 def as_fsspec(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000093 return FSSpec(self)
94
95class Alias(Carbon.File.Alias):
96
Jack Jansen58fc91f2002-12-17 23:28:24 +000097 def GetInfo(self, index):
98 return self.GetAliasInfo(index)
99
100 def Update(self, *args):
Jack Jansen27d19c42003-01-08 16:32:29 +0000101 pass # print "Alias.Update not yet implemented"
Jack Jansen4235e712002-12-19 23:26:07 +0000102
103 def Resolve(self, src=None):
Jack Jansen315e9be2002-12-26 20:46:54 +0000104 fss, changed = self.ResolveAlias(src)
105 return FSSpec(fss), changed
Jack Jansen4235e712002-12-19 23:26:07 +0000106
107from Carbon.File import FInfo
Jack Jansen6dd561b2002-12-26 21:09:39 +0000108
109# Backward-compatible type names:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000110FSSpecType = FSSpec
111FSRefType = FSRef
112AliasType = Alias
113FInfoType = FInfo
114
Jack Jansen6dd561b2002-12-26 21:09:39 +0000115# Global functions:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000116def ResolveAliasFile(fss, chain=1):
Jack Jansen315e9be2002-12-26 20:46:54 +0000117 fss, isdir, isalias = Carbon.File.ResolveAliasFile(fss, chain)
118 return FSSpec(fss), isdir, isalias
Jack Jansen58fc91f2002-12-17 23:28:24 +0000119
120def RawFSSpec(data):
121 return FSSpec(rawdata=data)
122
123def RawAlias(data):
124 return Alias(rawdata=data)
125
126def FindApplication(*args):
127 raise NotImplementedError, "FindApplication no longer implemented"
128
129def NewAliasMinimalFromFullPath(path):
Jack Jansen315e9be2002-12-26 20:46:54 +0000130 return Alias(Carbon.File.NewAliasMinimalFromFullPath(path, '', ''))
Jack Jansen4235e712002-12-19 23:26:07 +0000131
Jack Jansen6dd561b2002-12-26 21:09:39 +0000132# Another global function:
133from Carbon.Folder import FindFolder
134
135#
136# Finally the old Standard File routine emulators.
137#
138
Jack Jansen6dd561b2002-12-26 21:09:39 +0000139_curfolder = None
140
Jack Jansen6dd561b2002-12-26 21:09:39 +0000141def StandardGetFile(*typelist):
142 """Ask for an input file, optionally specifying 4-char file types that are
143 allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000144 return PromptGetFile('', *typelist)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000145
146def PromptGetFile(prompt, *typelist):
147 """Ask for an input file giving the user a prompt message. Optionally you can
148 specifying 4-char file types that are allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000149 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000150 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
151 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000152 if not typelist:
153 typelist = None
154 fss = EasyDialogs.AskFileForOpen(message=prompt, wanted=FSSpec,
155 typeList=typelist, defaultLocation=_handleSetFolder())
156 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000157
158def StandardPutFile(prompt, default=None):
159 """Ask the user for an output file, with a prompt. Optionally you cn supply a
160 default output filename"""
Jack Jansen01524d02003-01-21 15:31:16 +0000161 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000162 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
163 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000164 fss = EasyDialogs.AskFileForSave(wanted=FSSpec, message=prompt,
165 savedFileName=default, defaultLocation=_handleSetFolder())
166 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000167
168def SetFolder(folder):
169 global _curfolder
Jack Jansen374c4352003-01-21 22:58:39 +0000170 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
171 DeprecationWarning, stacklevel=2)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000172 if _curfolder:
Jack Jansen01524d02003-01-21 15:31:16 +0000173 rv = FSSpec(_curfolder)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000174 else:
175 rv = None
Jack Jansen01524d02003-01-21 15:31:16 +0000176 _curfolder = folder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000177 return rv
178
Jack Jansen01524d02003-01-21 15:31:16 +0000179def _handleSetFolder():
Jack Jansen6dd561b2002-12-26 21:09:39 +0000180 global _curfolder
Jack Jansen01524d02003-01-21 15:31:16 +0000181 rv = _curfolder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000182 _curfolder = None
Jack Jansen01524d02003-01-21 15:31:16 +0000183 return rv
Jack Jansen6dd561b2002-12-26 21:09:39 +0000184
185def GetDirectory(prompt=None):
186 """Ask the user to select a folder. Optionally you can give a prompt."""
Jack Jansen01524d02003-01-21 15:31:16 +0000187 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000188 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
189 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000190 fss = EasyDialogs.AskFolder(message=prompt, wanted=FSSpec,
191 defaultLocation=_handleSetFolder())
192 return fss, not fss is None