Return path of the module given a frame object from the call stack. Returned path is relative to parent_path when given, otherwise it is absolute path.
(frame, parent_path=None)
| 146 | return path |
| 147 | |
| 148 | def get_path_from_frame(frame, parent_path=None): |
| 149 | """Return path of the module given a frame object from the call stack. |
| 150 | |
| 151 | Returned path is relative to parent_path when given, |
| 152 | otherwise it is absolute path. |
| 153 | """ |
| 154 | |
| 155 | # First, try to find if the file name is in the frame. |
| 156 | try: |
| 157 | caller_file = eval('__file__', frame.f_globals, frame.f_locals) |
| 158 | d = os.path.dirname(os.path.abspath(caller_file)) |
| 159 | except NameError: |
| 160 | # __file__ is not defined, so let's try __name__. We try this second |
| 161 | # because setuptools spoofs __name__ to be '__main__' even though |
| 162 | # sys.modules['__main__'] might be something else, like easy_install(1). |
| 163 | caller_name = eval('__name__', frame.f_globals, frame.f_locals) |
| 164 | __import__(caller_name) |
| 165 | mod = sys.modules[caller_name] |
| 166 | if hasattr(mod, '__file__'): |
| 167 | d = os.path.dirname(os.path.abspath(mod.__file__)) |
| 168 | else: |
| 169 | # we're probably running setup.py as execfile("setup.py") |
| 170 | # (likely we're building an egg) |
| 171 | d = os.path.abspath('.') |
| 172 | |
| 173 | if parent_path is not None: |
| 174 | d = rel_path(d, parent_path) |
| 175 | |
| 176 | return d or '.' |
| 177 | |
| 178 | def njoin(*path): |
| 179 | """Join two or more pathname components + |