blob: 8c26dd5fbf8fd01e2a86f7580566d05bd9a3a5b3 [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
Jack Jansenabeb7d52003-02-27 23:17:59 +000010warnings.warn("macfs is deprecated, use Carbon.File, Carbon.Folder or EasyDialogs",
11 DeprecationWarning, stacklevel=2)
12
Jack Jansen58fc91f2002-12-17 23:28:24 +000013# First step: ensure we also emulate the MACFS module, which contained
14# all the constants
15
16sys.modules['MACFS'] = sys.modules[__name__]
17
18# Import all those constants
Jack Jansen4235e712002-12-19 23:26:07 +000019from Carbon.Files import *
20from Carbon.Folders import *
Jack Jansen58fc91f2002-12-17 23:28:24 +000021
22# For some obscure historical reason these are here too:
23READ = 1
24WRITE = 2
25smAllScripts = -3
26
Jack Jansen2d0909b2003-01-15 22:36:16 +000027#
28# Find the epoch conversion for file dates in a way that works on OS9 and OSX
29import time
30if time.gmtime(0)[0] == 1970:
31 _EPOCHCONVERT = -((1970-1904)*365 + 17) * (24*60*60) + 0x100000000L
32 def _utc2time(utc):
33 t = utc[1] + _EPOCHCONVERT
34 return int(t)
35 def _time2utc(t):
Jack Jansen32417e72003-01-22 14:04:18 +000036 t = int(t) - _EPOCHCONVERT
Jack Jansen2d0909b2003-01-15 22:36:16 +000037 if t < -0x7fffffff:
38 t = t + 0x10000000L
39 return (0, int(t), 0)
40else:
Jack Jansencbdffce2003-01-29 09:56:56 +000041 def _utc2time(utc):
42 t = utc[1]
43 if t < 0:
44 t = t + 0x100000000L
45 return t
Jack Jansen32417e72003-01-22 14:04:18 +000046 def _time2utc(t):
47 if t > 0x7fffffff:
48 t = t - 0x100000000L
49 return (0, int(t), 0)
Jack Jansen2d0909b2003-01-15 22:36:16 +000050
Jack Jansen4235e712002-12-19 23:26:07 +000051# The old name of the error object:
52error = Carbon.File.Error
Jack Jansen58fc91f2002-12-17 23:28:24 +000053
Jack Jansen6dd561b2002-12-26 21:09:39 +000054#
55# The various objects macfs used to export. We override them here, because some
56# of the method names are subtly different.
57#
Jack Jansen58fc91f2002-12-17 23:28:24 +000058class FSSpec(Carbon.File.FSSpec):
Jack Jansen4235e712002-12-19 23:26:07 +000059 def as_fsref(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000060 return FSRef(self)
61
62 def NewAlias(self, src=None):
Jack Jansen4235e712002-12-19 23:26:07 +000063 return Alias(Carbon.File.NewAlias(src, self))
Jack Jansen58fc91f2002-12-17 23:28:24 +000064
65 def GetCreatorType(self):
66 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000067 return finfo.Creator, finfo.Type
Jack Jansen58fc91f2002-12-17 23:28:24 +000068
69 def SetCreatorType(self, ctor, tp):
70 finfo = self.FSpGetFInfo()
Jack Jansen4235e712002-12-19 23:26:07 +000071 finfo.Creator = ctor
72 finfo.Type = tp
Jack Jansen58fc91f2002-12-17 23:28:24 +000073 self.FSpSetFInfo(finfo)
74
75 def GetFInfo(self):
76 return self.FSpGetFInfo()
77
78 def SetFInfo(self, info):
79 return self.FSpSetFInfo(info)
80
81 def GetDates(self):
Jack Jansen2d0909b2003-01-15 22:36:16 +000082 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
83 catinfo, d1, d2, d3 = FSRef(self).FSGetCatalogInfo(catInfoFlags)
84 cdate = catinfo.createDate
85 mdate = catinfo.contentModDate
86 bdate = catinfo.backupDate
87 return _utc2time(cdate), _utc2time(mdate), _utc2time(bdate)
Jack Jansen58fc91f2002-12-17 23:28:24 +000088
Jack Jansen2d0909b2003-01-15 22:36:16 +000089 def SetDates(self, cdate, mdate, bdate):
90 catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
91 catinfo = Carbon.File.FSCatalogInfo(
92 createDate = _time2utc(cdate),
93 contentModDate = _time2utc(mdate),
94 backupDate = _time2utc(bdate))
95 FSRef(self).FSSetCatalogInfo(catInfoFlags, catinfo)
Jack Jansen58fc91f2002-12-17 23:28:24 +000096
97class FSRef(Carbon.File.FSRef):
Jack Jansen4235e712002-12-19 23:26:07 +000098 def as_fsspec(self):
Jack Jansen58fc91f2002-12-17 23:28:24 +000099 return FSSpec(self)
100
101class Alias(Carbon.File.Alias):
102
Jack Jansen58fc91f2002-12-17 23:28:24 +0000103 def GetInfo(self, index):
104 return self.GetAliasInfo(index)
105
106 def Update(self, *args):
Jack Jansen27d19c42003-01-08 16:32:29 +0000107 pass # print "Alias.Update not yet implemented"
Jack Jansen4235e712002-12-19 23:26:07 +0000108
109 def Resolve(self, src=None):
Jack Jansen315e9be2002-12-26 20:46:54 +0000110 fss, changed = self.ResolveAlias(src)
111 return FSSpec(fss), changed
Jack Jansen4235e712002-12-19 23:26:07 +0000112
113from Carbon.File import FInfo
Jack Jansen6dd561b2002-12-26 21:09:39 +0000114
115# Backward-compatible type names:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000116FSSpecType = FSSpec
117FSRefType = FSRef
118AliasType = Alias
119FInfoType = FInfo
120
Jack Jansen6dd561b2002-12-26 21:09:39 +0000121# Global functions:
Jack Jansen58fc91f2002-12-17 23:28:24 +0000122def ResolveAliasFile(fss, chain=1):
Jack Jansen315e9be2002-12-26 20:46:54 +0000123 fss, isdir, isalias = Carbon.File.ResolveAliasFile(fss, chain)
124 return FSSpec(fss), isdir, isalias
Jack Jansen58fc91f2002-12-17 23:28:24 +0000125
126def RawFSSpec(data):
127 return FSSpec(rawdata=data)
128
129def RawAlias(data):
130 return Alias(rawdata=data)
131
132def FindApplication(*args):
133 raise NotImplementedError, "FindApplication no longer implemented"
134
135def NewAliasMinimalFromFullPath(path):
Jack Jansen315e9be2002-12-26 20:46:54 +0000136 return Alias(Carbon.File.NewAliasMinimalFromFullPath(path, '', ''))
Jack Jansen4235e712002-12-19 23:26:07 +0000137
Jack Jansen6dd561b2002-12-26 21:09:39 +0000138# Another global function:
139from Carbon.Folder import FindFolder
140
141#
142# Finally the old Standard File routine emulators.
143#
144
Jack Jansen6dd561b2002-12-26 21:09:39 +0000145_curfolder = None
146
Jack Jansen6dd561b2002-12-26 21:09:39 +0000147def StandardGetFile(*typelist):
148 """Ask for an input file, optionally specifying 4-char file types that are
149 allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000150 return PromptGetFile('', *typelist)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000151
152def PromptGetFile(prompt, *typelist):
153 """Ask for an input file giving the user a prompt message. Optionally you can
154 specifying 4-char file types that are allowable"""
Jack Jansen01524d02003-01-21 15:31:16 +0000155 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000156 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
157 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000158 if not typelist:
159 typelist = None
160 fss = EasyDialogs.AskFileForOpen(message=prompt, wanted=FSSpec,
161 typeList=typelist, defaultLocation=_handleSetFolder())
162 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000163
164def StandardPutFile(prompt, default=None):
165 """Ask the user for an output file, with a prompt. Optionally you cn supply a
166 default output filename"""
Jack Jansen01524d02003-01-21 15:31:16 +0000167 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000168 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
169 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000170 fss = EasyDialogs.AskFileForSave(wanted=FSSpec, message=prompt,
171 savedFileName=default, defaultLocation=_handleSetFolder())
172 return fss, not fss is None
Jack Jansen6dd561b2002-12-26 21:09:39 +0000173
174def SetFolder(folder):
175 global _curfolder
Jack Jansen374c4352003-01-21 22:58:39 +0000176 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
177 DeprecationWarning, stacklevel=2)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000178 if _curfolder:
Jack Jansen01524d02003-01-21 15:31:16 +0000179 rv = FSSpec(_curfolder)
Jack Jansen6dd561b2002-12-26 21:09:39 +0000180 else:
181 rv = None
Jack Jansen01524d02003-01-21 15:31:16 +0000182 _curfolder = folder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000183 return rv
184
Jack Jansen01524d02003-01-21 15:31:16 +0000185def _handleSetFolder():
Jack Jansen6dd561b2002-12-26 21:09:39 +0000186 global _curfolder
Jack Jansen01524d02003-01-21 15:31:16 +0000187 rv = _curfolder
Jack Jansen6dd561b2002-12-26 21:09:39 +0000188 _curfolder = None
Jack Jansen01524d02003-01-21 15:31:16 +0000189 return rv
Jack Jansen6dd561b2002-12-26 21:09:39 +0000190
191def GetDirectory(prompt=None):
192 """Ask the user to select a folder. Optionally you can give a prompt."""
Jack Jansen01524d02003-01-21 15:31:16 +0000193 import EasyDialogs
Jack Jansen374c4352003-01-21 22:58:39 +0000194 warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
195 DeprecationWarning, stacklevel=2)
Jack Jansen01524d02003-01-21 15:31:16 +0000196 fss = EasyDialogs.AskFolder(message=prompt, wanted=FSSpec,
197 defaultLocation=_handleSetFolder())
198 return fss, not fss is None