| 4 | |
| 5 | |
| 6 | class Data: |
| 7 | def __init__(self, name): |
| 8 | self.name = name |
| 9 | m = importlib.import_module(name) |
| 10 | f = inspect.getsourcefile(m) |
| 11 | assert f is not None |
| 12 | dirname = os.path.dirname(f) |
| 13 | self.dirname = os.path.abspath(dirname) |
| 14 | |
| 15 | def push(self, subpath): |
| 16 | """ |
| 17 | Change the data object to a path relative to the module. |
| 18 | """ |
| 19 | dirname = os.path.normpath(os.path.join(self.dirname, subpath)) |
| 20 | ret = Data(self.name) |
| 21 | ret.dirname = dirname |
| 22 | return ret |
| 23 | |
| 24 | def path(self, path): |
| 25 | """ |
| 26 | Returns a path to the package data housed at 'path' under this |
| 27 | module.Path can be a path to a file, or to a directory. |
| 28 | |
| 29 | This function will raise ValueError if the path does not exist. |
| 30 | """ |
| 31 | fullpath = os.path.normpath(os.path.join(self.dirname, path)) |
| 32 | if not os.path.exists(fullpath): |
| 33 | raise ValueError("dataPath: %s does not exist." % fullpath) |
| 34 | return fullpath |
| 35 | |
| 36 | |
| 37 | pkg_data = Data(__name__).push("..") |