2002-12-17 19:28:24 -04:00
|
|
|
"""macfs - Pure Python module designed to be backward compatible with
|
|
|
|
macfs and MACFS.
|
|
|
|
"""
|
|
|
|
import sys
|
2002-12-26 17:09:39 -04:00
|
|
|
import struct
|
|
|
|
import Carbon.Res
|
|
|
|
import Carbon.File
|
|
|
|
import Nav
|
2003-01-21 18:58:39 -04:00
|
|
|
import warnings
|
2002-12-17 19:28:24 -04:00
|
|
|
|
|
|
|
# First step: ensure we also emulate the MACFS module, which contained
|
|
|
|
# all the constants
|
|
|
|
|
|
|
|
sys.modules['MACFS'] = sys.modules[__name__]
|
|
|
|
|
|
|
|
# Import all those constants
|
2002-12-19 19:26:07 -04:00
|
|
|
from Carbon.Files import *
|
|
|
|
from Carbon.Folders import *
|
2002-12-17 19:28:24 -04:00
|
|
|
|
|
|
|
# For some obscure historical reason these are here too:
|
|
|
|
READ = 1
|
|
|
|
WRITE = 2
|
|
|
|
smAllScripts = -3
|
|
|
|
|
2003-01-15 18:36:16 -04:00
|
|
|
#
|
|
|
|
# Find the epoch conversion for file dates in a way that works on OS9 and OSX
|
|
|
|
import time
|
|
|
|
if time.gmtime(0)[0] == 1970:
|
|
|
|
_EPOCHCONVERT = -((1970-1904)*365 + 17) * (24*60*60) + 0x100000000L
|
|
|
|
def _utc2time(utc):
|
|
|
|
t = utc[1] + _EPOCHCONVERT
|
|
|
|
return int(t)
|
|
|
|
def _time2utc(t):
|
|
|
|
t = t - _EPOCHCONVERT
|
|
|
|
if t < -0x7fffffff:
|
|
|
|
t = t + 0x10000000L
|
|
|
|
return (0, int(t), 0)
|
|
|
|
else:
|
|
|
|
def _utc2time(utc): return utc[1]
|
|
|
|
def _time2utc(t): return (0, t, 0)
|
|
|
|
|
2002-12-19 19:26:07 -04:00
|
|
|
# The old name of the error object:
|
|
|
|
error = Carbon.File.Error
|
2002-12-17 19:28:24 -04:00
|
|
|
|
2002-12-26 17:09:39 -04:00
|
|
|
#
|
|
|
|
# The various objects macfs used to export. We override them here, because some
|
|
|
|
# of the method names are subtly different.
|
|
|
|
#
|
2002-12-17 19:28:24 -04:00
|
|
|
class FSSpec(Carbon.File.FSSpec):
|
2002-12-19 19:26:07 -04:00
|
|
|
def as_fsref(self):
|
2002-12-17 19:28:24 -04:00
|
|
|
return FSRef(self)
|
|
|
|
|
|
|
|
def NewAlias(self, src=None):
|
2002-12-19 19:26:07 -04:00
|
|
|
return Alias(Carbon.File.NewAlias(src, self))
|
2002-12-17 19:28:24 -04:00
|
|
|
|
|
|
|
def GetCreatorType(self):
|
|
|
|
finfo = self.FSpGetFInfo()
|
2002-12-19 19:26:07 -04:00
|
|
|
return finfo.Creator, finfo.Type
|
2002-12-17 19:28:24 -04:00
|
|
|
|
|
|
|
def SetCreatorType(self, ctor, tp):
|
|
|
|
finfo = self.FSpGetFInfo()
|
2002-12-19 19:26:07 -04:00
|
|
|
finfo.Creator = ctor
|
|
|
|
finfo.Type = tp
|
2002-12-17 19:28:24 -04:00
|
|
|
self.FSpSetFInfo(finfo)
|
|
|
|
|
|
|
|
def GetFInfo(self):
|
|
|
|
return self.FSpGetFInfo()
|
|
|
|
|
|
|
|
def SetFInfo(self, info):
|
|
|
|
return self.FSpSetFInfo(info)
|
|
|
|
|
|
|
|
def GetDates(self):
|
2003-01-15 18:36:16 -04:00
|
|
|
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)
|
2002-12-17 19:28:24 -04:00
|
|
|
|
2003-01-15 18:36:16 -04:00
|
|
|
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)
|
2002-12-17 19:28:24 -04:00
|
|
|
|
|
|
|
class FSRef(Carbon.File.FSRef):
|
2002-12-19 19:26:07 -04:00
|
|
|
def as_fsspec(self):
|
2002-12-17 19:28:24 -04:00
|
|
|
return FSSpec(self)
|
|
|
|
|
|
|
|
class Alias(Carbon.File.Alias):
|
|
|
|
|
|
|
|
def GetInfo(self, index):
|
|
|
|
return self.GetAliasInfo(index)
|
|
|
|
|
|
|
|
def Update(self, *args):
|
2003-01-08 12:32:29 -04:00
|
|
|
pass # print "Alias.Update not yet implemented"
|
2002-12-19 19:26:07 -04:00
|
|
|
|
|
|
|
def Resolve(self, src=None):
|
2002-12-26 16:46:54 -04:00
|
|
|
fss, changed = self.ResolveAlias(src)
|
|
|
|
return FSSpec(fss), changed
|
2002-12-19 19:26:07 -04:00
|
|
|
|
|
|
|
from Carbon.File import FInfo
|
2002-12-26 17:09:39 -04:00
|
|
|
|
|
|
|
# Backward-compatible type names:
|
2002-12-17 19:28:24 -04:00
|
|
|
FSSpecType = FSSpec
|
|
|
|
FSRefType = FSRef
|
|
|
|
AliasType = Alias
|
|
|
|
FInfoType = FInfo
|
|
|
|
|
2002-12-26 17:09:39 -04:00
|
|
|
# Global functions:
|
2002-12-17 19:28:24 -04:00
|
|
|
def ResolveAliasFile(fss, chain=1):
|
2002-12-26 16:46:54 -04:00
|
|
|
fss, isdir, isalias = Carbon.File.ResolveAliasFile(fss, chain)
|
|
|
|
return FSSpec(fss), isdir, isalias
|
2002-12-17 19:28:24 -04:00
|
|
|
|
|
|
|
def RawFSSpec(data):
|
|
|
|
return FSSpec(rawdata=data)
|
|
|
|
|
|
|
|
def RawAlias(data):
|
|
|
|
return Alias(rawdata=data)
|
|
|
|
|
|
|
|
def FindApplication(*args):
|
|
|
|
raise NotImplementedError, "FindApplication no longer implemented"
|
|
|
|
|
|
|
|
def NewAliasMinimalFromFullPath(path):
|
2002-12-26 16:46:54 -04:00
|
|
|
return Alias(Carbon.File.NewAliasMinimalFromFullPath(path, '', ''))
|
2002-12-19 19:26:07 -04:00
|
|
|
|
2002-12-26 17:09:39 -04:00
|
|
|
# Another global function:
|
|
|
|
from Carbon.Folder import FindFolder
|
|
|
|
|
|
|
|
#
|
|
|
|
# Finally the old Standard File routine emulators.
|
|
|
|
#
|
|
|
|
|
|
|
|
_curfolder = None
|
|
|
|
|
|
|
|
def StandardGetFile(*typelist):
|
|
|
|
"""Ask for an input file, optionally specifying 4-char file types that are
|
|
|
|
allowable"""
|
2003-01-21 11:31:16 -04:00
|
|
|
return PromptGetFile('', *typelist)
|
2002-12-26 17:09:39 -04:00
|
|
|
|
|
|
|
def PromptGetFile(prompt, *typelist):
|
|
|
|
"""Ask for an input file giving the user a prompt message. Optionally you can
|
|
|
|
specifying 4-char file types that are allowable"""
|
2003-01-21 11:31:16 -04:00
|
|
|
import EasyDialogs
|
2003-01-21 18:58:39 -04:00
|
|
|
warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
2003-01-21 11:31:16 -04:00
|
|
|
if not typelist:
|
|
|
|
typelist = None
|
|
|
|
fss = EasyDialogs.AskFileForOpen(message=prompt, wanted=FSSpec,
|
|
|
|
typeList=typelist, defaultLocation=_handleSetFolder())
|
|
|
|
return fss, not fss is None
|
2002-12-26 17:09:39 -04:00
|
|
|
|
|
|
|
def StandardPutFile(prompt, default=None):
|
|
|
|
"""Ask the user for an output file, with a prompt. Optionally you cn supply a
|
|
|
|
default output filename"""
|
2003-01-21 11:31:16 -04:00
|
|
|
import EasyDialogs
|
2003-01-21 18:58:39 -04:00
|
|
|
warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
2003-01-21 11:31:16 -04:00
|
|
|
fss = EasyDialogs.AskFileForSave(wanted=FSSpec, message=prompt,
|
|
|
|
savedFileName=default, defaultLocation=_handleSetFolder())
|
|
|
|
return fss, not fss is None
|
2002-12-26 17:09:39 -04:00
|
|
|
|
|
|
|
def SetFolder(folder):
|
|
|
|
global _curfolder
|
2003-01-21 18:58:39 -04:00
|
|
|
warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
2002-12-26 17:09:39 -04:00
|
|
|
if _curfolder:
|
2003-01-21 11:31:16 -04:00
|
|
|
rv = FSSpec(_curfolder)
|
2002-12-26 17:09:39 -04:00
|
|
|
else:
|
|
|
|
rv = None
|
2003-01-21 11:31:16 -04:00
|
|
|
_curfolder = folder
|
2002-12-26 17:09:39 -04:00
|
|
|
return rv
|
|
|
|
|
2003-01-21 11:31:16 -04:00
|
|
|
def _handleSetFolder():
|
2002-12-26 17:09:39 -04:00
|
|
|
global _curfolder
|
2003-01-21 11:31:16 -04:00
|
|
|
rv = _curfolder
|
2002-12-26 17:09:39 -04:00
|
|
|
_curfolder = None
|
2003-01-21 11:31:16 -04:00
|
|
|
return rv
|
2002-12-26 17:09:39 -04:00
|
|
|
|
|
|
|
def GetDirectory(prompt=None):
|
|
|
|
"""Ask the user to select a folder. Optionally you can give a prompt."""
|
2003-01-21 11:31:16 -04:00
|
|
|
import EasyDialogs
|
2003-01-21 18:58:39 -04:00
|
|
|
warnings.warn("macfs.StandardGetFile and friends are deprecated, use EasyDialogs.AskFileForOpen",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
2003-01-21 11:31:16 -04:00
|
|
|
fss = EasyDialogs.AskFolder(message=prompt, wanted=FSSpec,
|
|
|
|
defaultLocation=_handleSetFolder())
|
|
|
|
return fss, not fss is None
|