diff options
Diffstat (limited to 'Lib/fontTools/misc/macRes.py')
-rw-r--r-- | Lib/fontTools/misc/macRes.py | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/Lib/fontTools/misc/macRes.py b/Lib/fontTools/misc/macRes.py index db832ec5..2c15b347 100644 --- a/Lib/fontTools/misc/macRes.py +++ b/Lib/fontTools/misc/macRes.py @@ -1,13 +1,9 @@ -""" Tools for reading Mac resource forks. """ -from __future__ import print_function, division, absolute_import -from fontTools.misc.py23 import * +from fontTools.misc.py23 import bytesjoin, tostr +from io import BytesIO import struct from fontTools.misc import sstruct from collections import OrderedDict -try: - from collections.abc import MutableMapping -except ImportError: - from UserDict import DictMixin as MutableMapping +from collections.abc import MutableMapping class ResourceError(Exception): @@ -15,8 +11,25 @@ class ResourceError(Exception): class ResourceReader(MutableMapping): + """Reader for Mac OS resource forks. + Parses a resource fork and returns resources according to their type. + If run on OS X, this will open the resource fork in the filesystem. + Otherwise, it will open the file itself and attempt to read it as + though it were a resource fork. + + The returned object can be indexed by type and iterated over, + returning in each case a list of py:class:`Resource` objects + representing all the resources of a certain type. + + """ def __init__(self, fileOrPath): + """Open a file + + Args: + fileOrPath: Either an object supporting a ``read`` method, an + ``os.PathLike`` object, or a string. + """ self._resources = OrderedDict() if hasattr(fileOrPath, 'read'): self.file = fileOrPath @@ -125,6 +138,7 @@ class ResourceReader(MutableMapping): @property def types(self): + """A list of the types of resources in the resource fork.""" return list(self._resources.keys()) def countResources(self, resType): @@ -135,6 +149,7 @@ class ResourceReader(MutableMapping): return 0 def getIndices(self, resType): + """Returns a list of indices of resources of a given type.""" numRes = self.countResources(resType) if numRes: return list(range(1, numRes+1)) @@ -171,6 +186,15 @@ class ResourceReader(MutableMapping): class Resource(object): + """Represents a resource stored within a resource fork. + + Attributes: + type: resource type. + data: resource data. + id: ID. + name: resource name. + attr: attributes. + """ def __init__(self, resType=None, resData=None, resID=None, resName=None, resAttr=None): |