codekingpro/portable-devtools
115k
1import importlib2import inspect3import os.path4 5 6class Data:7 def __init__(self, name):8 self.name = name9 m = importlib.import_module(name)10 f = inspect.getsourcefile(m)11 assert f is not None12 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 = dirname22 return ret23 24 def path(self, path):25 """26 Returns a path to the package data housed at 'path' under this27 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 fullpath35 36 37pkg_data = Data(__name__).push("..")38 