Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame^] | 1 | """Data file path abstraction. |
| 2 | |
| 3 | Functions in this module use sysconfig to find the paths to the resource |
| 4 | files registered in project's setup.cfg file. See the documentation for |
| 5 | more information. |
| 6 | """ |
| 7 | # TODO write that documentation |
| 8 | |
| 9 | from packaging.database import get_distribution |
| 10 | |
| 11 | __all__ = ['get_file_path', 'get_file'] |
| 12 | |
| 13 | |
| 14 | def get_file_path(distribution_name, relative_path): |
| 15 | """Return the path to a resource file.""" |
| 16 | dist = get_distribution(distribution_name) |
| 17 | if dist != None: |
| 18 | return dist.get_resource_path(relative_path) |
| 19 | raise LookupError('no distribution named %r found' % distribution_name) |
| 20 | |
| 21 | |
| 22 | def get_file(distribution_name, relative_path, *args, **kwargs): |
| 23 | """Open and return a resource file.""" |
| 24 | return open(get_file_path(distribution_name, relative_path), |
| 25 | *args, **kwargs) |