• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.misc.textTools import Tag, bytesjoin, strjoin
2try:
3	import xattr
4except ImportError:
5	xattr = None
6
7
8def _reverseString(s):
9	s = list(s)
10	s.reverse()
11	return strjoin(s)
12
13
14def getMacCreatorAndType(path):
15	"""Returns file creator and file type codes for a path.
16
17	Args:
18		path (str): A file path.
19
20	Returns:
21		A tuple of two :py:class:`fontTools.textTools.Tag` objects, the first
22		representing the file creator and the second representing the
23		file type.
24	"""
25	if xattr is not None:
26		try:
27			finderInfo = xattr.getxattr(path, 'com.apple.FinderInfo')
28		except (KeyError, IOError):
29			pass
30		else:
31			fileType = Tag(finderInfo[:4])
32			fileCreator = Tag(finderInfo[4:8])
33			return fileCreator, fileType
34	return None, None
35
36
37def setMacCreatorAndType(path, fileCreator, fileType):
38	"""Set file creator and file type codes for a path.
39
40	Note that if the ``xattr`` module is not installed, no action is
41	taken but no error is raised.
42
43	Args:
44		path (str): A file path.
45		fileCreator: A four-character file creator tag.
46		fileType: A four-character file type tag.
47
48	"""
49	if xattr is not None:
50		from fontTools.misc.textTools import pad
51		if not all(len(s) == 4 for s in (fileCreator, fileType)):
52			raise TypeError('arg must be string of 4 chars')
53		finderInfo = pad(bytesjoin([fileType, fileCreator]), 32)
54		xattr.setxattr(path, 'com.apple.FinderInfo', finderInfo)
55