Mercurial > pygame
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 |
| 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 | 5 The getResource function is equivalent to the standard idioms, such as |
| 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 | 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 | 12 path = os.path.join(pkgpath, identifier) |
| 581 | 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 | 15 When a __loader__ is present on the module given by __name__, it will defer |
| 581 | 16 getResource to its get_data implementation and return it as a file-like |
| 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 | 20 __all__ = ['getResource'] |
| 565 | 21 import sys |
| 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 | 26 try: |
| 27 from pkg_resources import resource_stream, resource_exists | |
| 28 except ImportError: | |
| 29 def resource_exists(package_or_requirement, resource_name): | |
| 30 return False | |
| 31 def resource_stream(package_of_requirement, resource_name): | |
| 32 raise NotImplementedError | |
| 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 | 44 |
| 45 In some cases, getResource will return a real file object. In that | |
| 46 case, it may be useful to use its name attribute to get the path | |
| 47 rather than use it as a file-like object. For example, you may | |
| 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 | 50 if resource_exists(pkgname, identifier): |
| 51 return resource_stream(pkgname, identifier) | |
| 565 | 52 |
| 53 mod = sys.modules[pkgname] | |
| 54 fn = getattr(mod, '__file__', None) | |
| 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 | 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') |
