File: resources.py

package info (click to toggle)
pysdl2 0.9.9%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,276 kB
  • sloc: python: 18,592; makefile: 148; sh: 40
file content (318 lines) | stat: -rw-r--r-- 11,084 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""
Resource management methods.
"""
import sys
import os
import re
import zipfile
import tarfile
import io

__all__ = ["open_zipfile", "open_tarfile", "open_url", "Resources"]

# Python 3.x workarounds for the changed urllib modules.
if sys.version_info[0] >= 3:
    import urllib.parse as urlparse
    import urllib.request as urllib2
else:
    import urlparse
    import urllib2


def open_zipfile(archive, filename, directory=None):
    """Opens and reads a certain file from a ZIP archive.

    Opens and reads a certain file from a ZIP archive. The result is
    returned as StringIO stream. filename can be a relative or absolute
    path within the ZIP archive. The optional directory argument can be
    used to supply a relative directory path, under which filename will
    be searched.

    If the filename could not be found, a KeyError will be raised.
    Raises a TypeError, if archive is not a valid ZIP archive.
    """
    data = None
    opened = False

    if not isinstance(archive, zipfile.ZipFile):
        if not zipfile.is_zipfile(archive):
            raise TypeError("passed file does not seem to be a ZIP archive")
        else:
            archive = zipfile.ZipFile(archive, 'r')
            opened = True

    apath = filename
    if directory:
        apath = "%s/%s" % (directory, filename)

    try:
        dmpdata = archive.open(apath)
        data = io.BytesIO(dmpdata.read())
    finally:
        if opened:
            archive.close()
    return data


def open_tarfile(archive, filename, directory=None, ftype=None):
    """Opens and reads a certain file from a TAR archive.

    Opens and reads a certain file from a TAR archive. The result is
    returned as StringIO stream. filename can be a relative or absolute
    path within the TAR archive. The optional directory argument can be
    used to supply a relative directory path, under which filename will
    be searched.

    ftype is used to supply additional compression information, in case
    the system cannot determine the compression type itself, and can be
    either 'gz' for gzip compression or 'bz2' for bzip2 compression.

    Note:

      If ftype is supplied, the compression mode will be enforced for
      opening and reading.

    If the filename could not be found or an error occured on reading it,
    None will be returned.

    Raises a TypeError, if archive is not a valid TAR archive or if type
    is not a valid value of ('gz', 'bz2').
    """
    data = None
    opened = False

    mode = 'r'
    if ftype:
        if ftype not in ('gz', 'bz2'):
            raise TypeError("invalid TAR compression type")
        mode = "r:%s" % ftype

    if not isinstance(archive, tarfile.TarFile):
        if not tarfile.is_tarfile(archive):
            raise TypeError("passed file does not seem to be a TAR archive")
        else:
            archive = tarfile.open(archive, mode)
            opened = True

    apath = filename
    if directory:
        apath = "%s/%s" % (directory, filename)

    try:
        dmpdata = archive.extractfile(apath)
        data = io.BytesIO(dmpdata.read())
    finally:
        if opened:
            archive.close()
    return data


def open_url(filename, basepath=None):
    """Opens and reads a certain file from a web or remote location.

    Opens and reads a certain file from a web or remote location. This
    function utilizes the urllib2 module, which means that it is
    restricted to the types of remote locations supported by urllib2.

    basepath can be used to supply an additional location prefix.
    """
    url = filename
    if basepath:
        url = urlparse.urljoin(basepath, filename)
    return urllib2.urlopen(url)


class Resources(object):
    """The Resources class manages a set of file resources and eases
    accessing them by using relative paths, scanning archives
    automatically and so on.
    """
    def __init__(self, path=None, subdir=None, excludepattern=None):
        """Creates a new resource container instance.

        If path is provided, the resource container will scan the path
        and add all found files to itself by invoking
        scan(path, subdir, excludepattern).
        """
        self.files = {}
        if path:
            self.scan(path, subdir, excludepattern)

    def _scanzip(self, filename):
        """Scans the passed ZIP archive and indexes all the files
        contained by it.
        """
        if not zipfile.is_zipfile(filename):
            raise TypeError("file '%s' is not a valid ZIP archive" % filename)
        archname = os.path.abspath(filename)
        zipf = zipfile.ZipFile(filename, 'r')
        for path in zipf.namelist():
            fname = os.path.split(path)[1]
            if fname:
                self.files[fname] = (archname, 'zip', path)
        zipf.close()

    def _scantar(self, filename, ftype=None):
        """Scans the passed TAR archive and indexes all the files
        contained by it.
        """
        if not tarfile.is_tarfile(filename):
            raise TypeError("file '%s' is not a valid TAR archive" % filename)
        mode = 'r'
        if ftype:
            if ftype not in ('gz', 'bz2'):
                raise TypeError("invalid TAR compression type")
            mode = "r:%s" % ftype
        archname = os.path.abspath(filename)
        archtype = 'tar'
        if ftype:
            archtype = 'tar%s' % ftype
        tar = tarfile.open(filename, mode)
        for path in tar.getnames():
            fname = os.path.split(path)[1]
            self.files[fname] = (archname, archtype, path)
        tar.close()

    def add(self, filename):
        """Adds a file to the Resources container.

        Depending on the file type (determined by the file suffix or name),
        the file will be automatically scanned (if it is an archive) or
        checked for availability (if it is a stream/network resource).
        """
        if not os.path.exists(filename):
            raise ValueError("invalid file path")
        if zipfile.is_zipfile(filename):
            self.add_archive(filename)
        elif tarfile.is_tarfile(filename):
            self.add_archive(filename, 'tar')
        else:
            self.add_file(filename)

    def add_file(self, filename):
        """Adds a file to the Resources container.

        This will only add the passed file and do not scan an archive or
        check a stream for availability.
        """
        if not os.path.exists(filename):
            raise ValueError("invalid file path")
        abspath = os.path.abspath(filename)
        fname = os.path.split(abspath)[1]
        if not fname:
            raise ValueError("invalid file path")
        self.files[fname] = (None, None, abspath)

    def add_archive(self, filename, typehint='zip'):
        """Adds an archive file to the Resources container.

        This will scan the passed archive and add its contents to the
        list of available resources.
        """
        if not os.path.exists(filename):
            raise ValueError("invalid file path")
        if typehint == 'zip':
            self._scanzip(filename)
        elif typehint == 'tar':
            self._scantar(filename)
        elif typehint == 'tarbz2':
            self._scantar(filename, 'bz2')
        elif typehint == 'targz':
            self._scantar(filename, 'gz')
        else:
            raise ValueError("unsupported archive type")

    def get(self, filename):
        """Gets a specific file from the Resources.

        Raises a KeyError, if filename could not be found.
        """
        archive, ftype, pathname = self.files[filename]
        if archive:
            if ftype == 'zip':
                return open_zipfile(archive, pathname)
            elif ftype == 'tar':
                return open_tarfile(archive, pathname)
            elif ftype == 'tarbz2':
                return open_tarfile(archive, pathname, ftype='bz2')
            elif ftype == 'targz':
                return open_tarfile(archive, pathname, ftype='gz')
            else:
                raise ValueError("unsupported archive type")
        dmpdata = open(pathname, 'rb')
        data = io.BytesIO(dmpdata.read())
        dmpdata.close()
        return data

    def get_filelike(self, filename):
        """Like get(), but tries to return the original file handle, if
        possible.

        If the passed filename is only available within an archive, a
        StringIO instance will be returned.

        Raises a KeyError, if filename could not be found.
        """
        archive, ftype, pathname = self.files[filename]
        if archive:
            if ftype == 'zip':
                return open_zipfile(archive, pathname)
            elif ftype == 'tar':
                return open_tarfile(archive, pathname)
            elif ftype == 'tarbz2':
                return open_tarfile(archive, pathname, ftype='bz2')
            elif ftype == 'targz':
                return open_tarfile(archive, pathname, ftype='gz')
            else:
                raise ValueError("unsupported archive type")
        return open(pathname, 'rb')

    def get_path(self, filename):
        """Gets the path of the passed filename.

        If filename is only available within an archive, a string in
        the form 'filename@archivename' will be returned.

        Raises a KeyError, if filename could not be found.
        """
        archive, ftype, pathname = self.files[filename]
        if archive:
            return '%s@%s' % (pathname, archive)
        return pathname

    def scan(self, path, subdir=None, excludepattern=None):
        """Scans a path and adds all found files to the Resources
        container.

        Scans a path and adds all found files to the Resources
        container. If a file is a supported (ZIP or TAR) archive, its
        contents will be indexed and added automatically.

        The method will consider the directory part (os.path.dirname) of
        the provided path as path to scan, if the path is not a
        directory. If subdir is provided, it will be appended to the
        path and used as starting point for adding files to the
        Resources container.

        excludepattern can be a regular expression to skip directories, which
        match the pattern.
        """
        match = None
        if excludepattern:
            match = re.compile(excludepattern).match
        join = os.path.join
        add = self.add
        abspath = os.path.abspath(path)
        if not os.path.exists(abspath):
            raise ValueError("invalid path '%s'" % abspath)
        if not os.path.isdir(abspath):
            abspath = os.path.dirname(abspath)
        if subdir is not None:
            abspath = os.path.join(abspath, subdir)
        if not os.path.exists(abspath):
            raise ValueError("invalid path '%s'" % abspath)
        for (pdir, dirnames, filenames) in os.walk(abspath):
            if match and match(pdir) is not None:
                continue
            for fname in filenames:
                add(join(pdir, fname))