Mercurial > hg > config
comparison python/resource_filename.py @ 269:cbd7651dbd52
add sample example code
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Sat, 23 Mar 2013 21:37:56 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 268:06fcec56e8fe | 269:cbd7651dbd52 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 # example code | |
| 4 | |
| 5 import os | |
| 6 | |
| 7 def resource_path(path): | |
| 8 """ | |
| 9 getting a resource filename (absolute path) | |
| 10 - path: relative path | |
| 11 """ | |
| 12 | |
| 13 try: | |
| 14 # use pkg_resources if available | |
| 15 # http://pythonhosted.org/distribute/setuptools.html#non-package-data-files | |
| 16 from pkg_resources import Requirement, resource_filename | |
| 17 return resource_filename(Requirement.parse("MyProject"),path) | |
| 18 except ImportError: | |
| 19 # assume file lives relative to this file | |
| 20 here = os.path.dirname(os.path.abspath(__file__)) | |
| 21 return os.path.join(here, path) |
