annotate lib/pkgdata.py @ 3172:d1b367fc08bc tests-returncode

Use a return code of 1 when the tests fail.
author Thomas Kluyver <takowl@gmail.com>
date Wed, 09 Jan 2013 10:13:57 +0000
parents ae2160cc5b0e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
505
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
1 """
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
2 pkgdata is a simple, extensible way for a package to acquire data file
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
3 resources.
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
4
581
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
5 The getResource function is equivalent to the standard idioms, such as
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
6 the following minimal implementation:
505
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
7
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
8 import sys, os
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
9
581
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
10 def getResource(identifier, pkgname=__name__):
505
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
11 pkgpath = os.path.dirname(sys.modules[pkgname].__file__)
565
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
12 path = os.path.join(pkgpath, identifier)
581
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
13 return file(os.path.normpath(path), mode='rb')
505
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
14
565
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
15 When a __loader__ is present on the module given by __name__, it will defer
581
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
16 getResource to its get_data implementation and return it as a file-like
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
17 object (such as StringIO).
505
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
18 """
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
19
581
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
20 __all__ = ['getResource']
565
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
21 import sys
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
22 import os
1553
ebd545641c8b merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents: 748
diff changeset
23 from pygame.compat import get_BytesIO
ebd545641c8b merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents: 748
diff changeset
24 BytesIO = get_BytesIO()
505
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
25
748
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
26 try:
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
27 from pkg_resources import resource_stream, resource_exists
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
28 except ImportError:
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
29 def resource_exists(package_or_requirement, resource_name):
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
30 return False
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
31 def resource_stream(package_of_requirement, resource_name):
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
32 raise NotImplementedError
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
33
505
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
34 def getResource(identifier, pkgname=__name__):
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
35 """
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
36 Acquire a readable object for a given package name and identifier.
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
37 An IOError will be raised if the resource can not be found.
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
38
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
39 For example:
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
40 mydata = getResource('mypkgdata.jpg').read()
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
41
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
42 Note that the package name must be fully qualified, if given, such
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
43 that it would be found in sys.modules.
581
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
44
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
45 In some cases, getResource will return a real file object. In that
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
46 case, it may be useful to use its name attribute to get the path
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
47 rather than use it as a file-like object. For example, you may
b891b294ae2f remove some unused code
bob
parents: 565
diff changeset
48 be handing data off to a C API.
505
951a91f0ab40 Added an extensible package data acquisition system (pkgdata)
bob
parents:
diff changeset
49 """
748
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
50 if resource_exists(pkgname, identifier):
052feee00091 setuptools/pkg_resources support for pkgdata
bob
parents: 581
diff changeset
51 return resource_stream(pkgname, identifier)
565
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
52
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
53 mod = sys.modules[pkgname]
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
54 fn = getattr(mod, '__file__', None)
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
55 if fn is None:
1553
ebd545641c8b merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents: 748
diff changeset
56 raise IOError("%s has no __file__!" % repr(mod))
565
9ee5c22d5c61 rewrite pkgdata, fix some whitespace issues
bob
parents: 560
diff changeset
57 path = os.path.join(os.path.dirname(fn), identifier)
3150
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
58 if sys.version_info < (3, 3):
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
59 loader = getattr(mod, '__loader__', None)
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
60 if loader is not None:
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
61 try:
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
62 data = loader.get_data(path)
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
63 except IOError:
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
64 pass
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
65 else:
ae2160cc5b0e Patch Python 3.3 pygame.pkgdata bug for issue #146
Lenard Lindstrom <len-l@telus.net>
parents: 1553
diff changeset
66 return BytesIO(data)
1553
ebd545641c8b merging with python3 branch r 2048
Lenard Lindstrom <len-l@telus.net>
parents: 748
diff changeset
67 return open(os.path.normpath(path), 'rb')