Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 1 | """macfs - Pure Python module designed to be backward compatible with |
| 2 | macfs and MACFS. |
| 3 | """ |
| 4 | import sys |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 5 | import struct |
| 6 | import Carbon.Res |
| 7 | import Carbon.File |
Jack Jansen | 374c435 | 2003-01-21 22:58:39 +0000 | [diff] [blame] | 8 | import warnings |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 9 | |
Jack Jansen | abeb7d5 | 2003-02-27 23:17:59 +0000 | [diff] [blame] | 10 | warnings.warn("macfs is deprecated, use Carbon.File, Carbon.Folder or EasyDialogs", |
| 11 | DeprecationWarning, stacklevel=2) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 12 | |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 13 | # First step: ensure we also emulate the MACFS module, which contained |
| 14 | # all the constants |
| 15 | |
| 16 | sys.modules['MACFS'] = sys.modules[__name__] |
| 17 | |
| 18 | # Import all those constants |
Jack Jansen | 4235e71 | 2002-12-19 23:26:07 +0000 | [diff] [blame] | 19 | from Carbon.Files import * |
| 20 | from Carbon.Folders import * |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 21 | |
| 22 | # For some obscure historical reason these are here too: |
| 23 | READ = 1 |
| 24 | WRITE = 2 |
| 25 | smAllScripts = -3 |
| 26 | |
Jack Jansen | 2d0909b | 2003-01-15 22:36:16 +0000 | [diff] [blame] | 27 | # |
| 28 | # Find the epoch conversion for file dates in a way that works on OS9 and OSX |
| 29 | import time |
| 30 | if time.gmtime(0)[0] == 1970: |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 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): |
| 36 | t = int(t) - _EPOCHCONVERT |
| 37 | if t < -0x7fffffff: |
| 38 | t = t + 0x10000000L |
| 39 | return (0, int(t), 0) |
Jack Jansen | 2d0909b | 2003-01-15 22:36:16 +0000 | [diff] [blame] | 40 | else: |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 41 | def _utc2time(utc): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 42 | t = utc[1] |
| 43 | if t < 0: |
| 44 | t = t + 0x100000000L |
| 45 | return t |
| 46 | def _time2utc(t): |
| 47 | if t > 0x7fffffff: |
| 48 | t = t - 0x100000000L |
| 49 | return (0, int(t), 0) |
Jack Jansen | 2d0909b | 2003-01-15 22:36:16 +0000 | [diff] [blame] | 50 | |
Jack Jansen | 4235e71 | 2002-12-19 23:26:07 +0000 | [diff] [blame] | 51 | # The old name of the error object: |
| 52 | error = Carbon.File.Error |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 53 | |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 54 | # |
| 55 | # The various objects macfs used to export. We override them here, because some |
| 56 | # of the method names are subtly different. |
| 57 | # |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 58 | class FSSpec(Carbon.File.FSSpec): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 59 | def as_fsref(self): |
| 60 | return FSRef(self) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 61 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 62 | def NewAlias(self, src=None): |
| 63 | return Alias(Carbon.File.NewAlias(src, self)) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 64 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 65 | def GetCreatorType(self): |
| 66 | finfo = self.FSpGetFInfo() |
| 67 | return finfo.Creator, finfo.Type |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 68 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 69 | def SetCreatorType(self, ctor, tp): |
| 70 | finfo = self.FSpGetFInfo() |
| 71 | finfo.Creator = ctor |
| 72 | finfo.Type = tp |
| 73 | self.FSpSetFInfo(finfo) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 74 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 75 | def GetFInfo(self): |
| 76 | return self.FSpGetFInfo() |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 77 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 78 | def SetFInfo(self, info): |
| 79 | return self.FSpSetFInfo(info) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 80 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 81 | def GetDates(self): |
| 82 | 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) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 88 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 89 | 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) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 96 | |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 97 | class FSRef(Carbon.File.FSRef): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 98 | def as_fsspec(self): |
| 99 | return FSSpec(self) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 100 | |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 101 | class Alias(Carbon.File.Alias): |
| 102 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 103 | def GetInfo(self, index): |
| 104 | return self.GetAliasInfo(index) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 105 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 106 | def Update(self, *args): |
| 107 | pass # print "Alias.Update not yet implemented" |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 108 | |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 109 | def Resolve(self, src=None): |
| 110 | fss, changed = self.ResolveAlias(src) |
| 111 | return FSSpec(fss), changed |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 112 | |
Jack Jansen | 4235e71 | 2002-12-19 23:26:07 +0000 | [diff] [blame] | 113 | from Carbon.File import FInfo |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 114 | |
| 115 | # Backward-compatible type names: |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 116 | FSSpecType = FSSpec |
| 117 | FSRefType = FSRef |
| 118 | AliasType = Alias |
| 119 | FInfoType = FInfo |
| 120 | |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 121 | # Global functions: |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 122 | def ResolveAliasFile(fss, chain=1): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 123 | fss, isdir, isalias = Carbon.File.ResolveAliasFile(fss, chain) |
| 124 | return FSSpec(fss), isdir, isalias |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 125 | |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 126 | def RawFSSpec(data): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 127 | return FSSpec(rawdata=data) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 128 | |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 129 | def RawAlias(data): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 130 | return Alias(rawdata=data) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 131 | |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 132 | def FindApplication(*args): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 133 | raise NotImplementedError, "FindApplication no longer implemented" |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 134 | |
Jack Jansen | 58fc91f | 2002-12-17 23:28:24 +0000 | [diff] [blame] | 135 | def NewAliasMinimalFromFullPath(path): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 136 | return Alias(Carbon.File.NewAliasMinimalFromFullPath(path, '', '')) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 137 | |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 138 | # Another global function: |
| 139 | from Carbon.Folder import FindFolder |
| 140 | |
| 141 | # |
| 142 | # Finally the old Standard File routine emulators. |
| 143 | # |
| 144 | |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 145 | _curfolder = None |
| 146 | |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 147 | def StandardGetFile(*typelist): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 148 | """Ask for an input file, optionally specifying 4-char file types that are |
| 149 | allowable""" |
| 150 | return PromptGetFile('', *typelist) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 151 | |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 152 | def PromptGetFile(prompt, *typelist): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 153 | """Ask for an input file giving the user a prompt message. Optionally you can |
| 154 | specifying 4-char file types that are allowable""" |
| 155 | import EasyDialogs |
| 156 | warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen", |
Jack Jansen | 374c435 | 2003-01-21 22:58:39 +0000 | [diff] [blame] | 157 | DeprecationWarning, stacklevel=2) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 158 | if not typelist: |
| 159 | typelist = None |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 160 | fss = EasyDialogs.AskFileForOpen(message=prompt, wanted=FSSpec, |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 161 | typeList=typelist, defaultLocation=_handleSetFolder()) |
| 162 | return fss, not fss is None |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 163 | |
| 164 | def StandardPutFile(prompt, default=None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 165 | """Ask the user for an output file, with a prompt. Optionally you cn supply a |
| 166 | default output filename""" |
| 167 | import EasyDialogs |
| 168 | warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen", |
Jack Jansen | 374c435 | 2003-01-21 22:58:39 +0000 | [diff] [blame] | 169 | DeprecationWarning, stacklevel=2) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 170 | fss = EasyDialogs.AskFileForSave(wanted=FSSpec, message=prompt, |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 171 | savedFileName=default, defaultLocation=_handleSetFolder()) |
| 172 | return fss, not fss is None |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 173 | |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 174 | def SetFolder(folder): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 175 | global _curfolder |
| 176 | warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen", |
Jack Jansen | 374c435 | 2003-01-21 22:58:39 +0000 | [diff] [blame] | 177 | DeprecationWarning, stacklevel=2) |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 178 | if _curfolder: |
| 179 | rv = FSSpec(_curfolder) |
| 180 | else: |
| 181 | rv = None |
| 182 | _curfolder = folder |
| 183 | return rv |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 184 | |
Jack Jansen | 01524d0 | 2003-01-21 15:31:16 +0000 | [diff] [blame] | 185 | def _handleSetFolder(): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 186 | global _curfolder |
| 187 | rv = _curfolder |
| 188 | _curfolder = None |
| 189 | return rv |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 190 | |
Jack Jansen | 6dd561b | 2002-12-26 21:09:39 +0000 | [diff] [blame] | 191 | def GetDirectory(prompt=None): |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 192 | """Ask the user to select a folder. Optionally you can give a prompt.""" |
| 193 | import EasyDialogs |
| 194 | warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen", |
Jack Jansen | 374c435 | 2003-01-21 22:58:39 +0000 | [diff] [blame] | 195 | DeprecationWarning, stacklevel=2) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 196 | fss = EasyDialogs.AskFolder(message=prompt, wanted=FSSpec, |
Jack Jansen | 0ae3220 | 2003-04-09 13:25:43 +0000 | [diff] [blame] | 197 | defaultLocation=_handleSetFolder()) |
| 198 | return fss, not fss is None |