macfs.py 5.97 KB
Newer Older
1 2 3 4
"""macfs - Pure Python module designed to be backward compatible with
macfs and MACFS.
"""
import sys
5 6 7
import struct
import Carbon.Res
import Carbon.File
8
import warnings
9

10 11 12
warnings.warn("macfs is deprecated, use Carbon.File, Carbon.Folder or EasyDialogs",
              DeprecationWarning, stacklevel=2)
              
13 14 15 16 17 18
# First step: ensure we also emulate the MACFS module, which contained
# all the constants

sys.modules['MACFS'] = sys.modules[__name__]

# Import all those constants
19 20
from Carbon.Files import *
from Carbon.Folders import *
21 22 23 24 25 26

# For some obscure historical reason these are here too:
READ = 1
WRITE = 2
smAllScripts = -3

27 28 29 30
#
# Find the epoch conversion for file dates in a way that works on OS9 and OSX
import time
if time.gmtime(0)[0] == 1970:
Jack Jansen's avatar
Jack Jansen committed
31 32 33 34 35 36 37 38 39
    _EPOCHCONVERT = -((1970-1904)*365 + 17) * (24*60*60) + 0x100000000L
    def _utc2time(utc):
        t = utc[1] + _EPOCHCONVERT
        return int(t)
    def _time2utc(t):
        t = int(t) - _EPOCHCONVERT
        if t < -0x7fffffff:
            t = t + 0x10000000L
        return (0, int(t), 0)
40
else:
Jack Jansen's avatar
Jack Jansen committed
41 42 43 44 45 46 47 48 49
    def _utc2time(utc): 
        t = utc[1]
        if t < 0:
            t = t + 0x100000000L
        return t
    def _time2utc(t):
        if t > 0x7fffffff:
            t = t - 0x100000000L
        return (0, int(t), 0)
50

51 52
# The old name of the error object:
error = Carbon.File.Error
53

54 55 56 57
#
# The various objects macfs used to export. We override them here, because some
# of the method names are subtly different.
#
58
class FSSpec(Carbon.File.FSSpec):
Jack Jansen's avatar
Jack Jansen committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    def as_fsref(self):
        return FSRef(self)
        
    def NewAlias(self, src=None):
        return Alias(Carbon.File.NewAlias(src, self))
        
    def GetCreatorType(self):
        finfo = self.FSpGetFInfo()
        return finfo.Creator, finfo.Type
        
    def SetCreatorType(self, ctor, tp):
        finfo = self.FSpGetFInfo()
        finfo.Creator = ctor
        finfo.Type = tp
        self.FSpSetFInfo(finfo)
        
    def GetFInfo(self):
        return self.FSpGetFInfo()
        
    def SetFInfo(self, info):
        return self.FSpSetFInfo(info)
        
    def GetDates(self):
        catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
        catinfo, d1, d2, d3 = FSRef(self).FSGetCatalogInfo(catInfoFlags)
        cdate = catinfo.createDate
        mdate = catinfo.contentModDate
        bdate = catinfo.backupDate
        return _utc2time(cdate), _utc2time(mdate), _utc2time(bdate)
    
    def SetDates(self, cdate, mdate, bdate):
        catInfoFlags = kFSCatInfoCreateDate|kFSCatInfoContentMod|kFSCatInfoBackupDate
        catinfo = Carbon.File.FSCatalogInfo(
            createDate = _time2utc(cdate),
            contentModDate = _time2utc(mdate),
            backupDate = _time2utc(bdate))
        FSRef(self).FSSetCatalogInfo(catInfoFlags, catinfo)
    
97
class FSRef(Carbon.File.FSRef):
Jack Jansen's avatar
Jack Jansen committed
98 99 100
    def as_fsspec(self):
        return FSSpec(self)
    
101 102
class Alias(Carbon.File.Alias):

Jack Jansen's avatar
Jack Jansen committed
103 104 105 106 107 108 109 110 111 112
    def GetInfo(self, index):
        return self.GetAliasInfo(index)
        
    def Update(self, *args):
        pass # print "Alias.Update not yet implemented"
        
    def Resolve(self, src=None):
        fss, changed = self.ResolveAlias(src)
        return FSSpec(fss), changed
        
113
from Carbon.File import FInfo
114 115

# Backward-compatible type names:
116 117 118 119 120
FSSpecType = FSSpec
FSRefType = FSRef
AliasType = Alias
FInfoType = FInfo

121
# Global functions:
122
def ResolveAliasFile(fss, chain=1):
Jack Jansen's avatar
Jack Jansen committed
123 124 125
    fss, isdir, isalias = Carbon.File.ResolveAliasFile(fss, chain)
    return FSSpec(fss), isdir, isalias
    
126
def RawFSSpec(data):
Jack Jansen's avatar
Jack Jansen committed
127 128
    return FSSpec(rawdata=data)
    
129
def RawAlias(data):
Jack Jansen's avatar
Jack Jansen committed
130 131
    return Alias(rawdata=data)
    
132
def FindApplication(*args):
Jack Jansen's avatar
Jack Jansen committed
133 134
    raise NotImplementedError, "FindApplication no longer implemented"
    
135
def NewAliasMinimalFromFullPath(path):
Jack Jansen's avatar
Jack Jansen committed
136 137
    return Alias(Carbon.File.NewAliasMinimalFromFullPath(path, '', ''))
    
138 139 140 141 142 143 144 145 146 147
# Another global function:
from Carbon.Folder import FindFolder

#
# Finally the old Standard File routine emulators.
#

_curfolder = None

def StandardGetFile(*typelist):
Jack Jansen's avatar
Jack Jansen committed
148 149 150 151
    """Ask for an input file, optionally specifying 4-char file types that are
    allowable"""
    return PromptGetFile('', *typelist)
    
152
def PromptGetFile(prompt, *typelist):
Jack Jansen's avatar
Jack Jansen committed
153 154 155 156
    """Ask for an input file giving the user a prompt message. Optionally you can
    specifying 4-char file types that are allowable"""
    import EasyDialogs
    warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
157
              DeprecationWarning, stacklevel=2)
Jack Jansen's avatar
Jack Jansen committed
158 159 160 161 162
    if not typelist:
        typelist = None
    fss = EasyDialogs.AskFileForOpen(message=prompt, wanted=FSSpec, 
        typeList=typelist, defaultLocation=_handleSetFolder())
    return fss, not fss is None
163 164

def StandardPutFile(prompt, default=None):
Jack Jansen's avatar
Jack Jansen committed
165 166 167 168
    """Ask the user for an output file, with a prompt. Optionally you cn supply a
    default output filename"""
    import EasyDialogs
    warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
169
              DeprecationWarning, stacklevel=2)
Jack Jansen's avatar
Jack Jansen committed
170 171 172 173
    fss = EasyDialogs.AskFileForSave(wanted=FSSpec, message=prompt, 
    savedFileName=default, defaultLocation=_handleSetFolder())
    return fss, not fss is None
    
174
def SetFolder(folder):
Jack Jansen's avatar
Jack Jansen committed
175 176
    global _curfolder
    warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
177
              DeprecationWarning, stacklevel=2)
Jack Jansen's avatar
Jack Jansen committed
178 179 180 181 182 183 184
    if _curfolder:
        rv = FSSpec(_curfolder)
    else:
        rv = None
    _curfolder = folder
    return rv
    
185
def _handleSetFolder():
Jack Jansen's avatar
Jack Jansen committed
186 187 188 189 190
    global _curfolder
    rv = _curfolder
    _curfolder = None
    return rv
    
191
def GetDirectory(prompt=None):
Jack Jansen's avatar
Jack Jansen committed
192 193 194
    """Ask the user to select a folder. Optionally you can give a prompt."""
    import EasyDialogs
    warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
195
              DeprecationWarning, stacklevel=2)
Jack Jansen's avatar
Jack Jansen committed
196 197 198
    fss = EasyDialogs.AskFolder(message=prompt, wanted=FSSpec, 
        defaultLocation=_handleSetFolder())
    return fss, not fss is None