1from __future__ import print_function, division, absolute_import 2from fontTools.misc.py23 import * 3import sys 4try: 5 import xattr 6except ImportError: 7 xattr = None 8try: 9 import MacOS 10except ImportError: 11 MacOS = None 12 13 14def _reverseString(s): 15 s = list(s) 16 s.reverse() 17 return strjoin(s) 18 19 20def getMacCreatorAndType(path): 21 if xattr is not None: 22 try: 23 finderInfo = xattr.getxattr(path, 'com.apple.FinderInfo') 24 except (KeyError, IOError): 25 pass 26 else: 27 fileType = Tag(finderInfo[:4]) 28 fileCreator = Tag(finderInfo[4:8]) 29 return fileCreator, fileType 30 if MacOS is not None: 31 fileCreator, fileType = MacOS.GetCreatorAndType(path) 32 if sys.version_info[:2] < (2, 7) and sys.byteorder == "little": 33 # work around bug in MacOS.GetCreatorAndType() on intel: 34 # http://bugs.python.org/issue1594 35 # (fixed with Python 2.7) 36 fileCreator = _reverseString(fileCreator) 37 fileType = _reverseString(fileType) 38 return fileCreator, fileType 39 else: 40 return None, None 41 42 43def setMacCreatorAndType(path, fileCreator, fileType): 44 if xattr is not None: 45 from fontTools.misc.textTools import pad 46 if not all(len(s) == 4 for s in (fileCreator, fileType)): 47 raise TypeError('arg must be string of 4 chars') 48 finderInfo = pad(bytesjoin([fileType, fileCreator]), 32) 49 xattr.setxattr(path, 'com.apple.FinderInfo', finderInfo) 50 if MacOS is not None: 51 MacOS.SetCreatorAndType(path, fileCreator, fileType) 52