diff options
| author | Malthe Borch <mborch@gmail.com> | 2009-08-27 10:26:12 +0000 |
|---|---|---|
| committer | Malthe Borch <mborch@gmail.com> | 2009-08-27 10:26:12 +0000 |
| commit | 2234c998d4eb7672065fb02c4974c0535f409dca (patch) | |
| tree | 3755d4bd9f8722b728bee92ec4741fc942e48134 /repoze/bfg/static.py | |
| parent | 3d1abbd5be1d1bc0f27e488059ccf7aaca225f31 (diff) | |
| download | pyramid-2234c998d4eb7672065fb02c4974c0535f409dca.tar.gz pyramid-2234c998d4eb7672065fb02c4974c0535f409dca.tar.bz2 pyramid-2234c998d4eb7672065fb02c4974c0535f409dca.zip | |
Rebase module-relative static views on the nearest package path.
Diffstat (limited to 'repoze/bfg/static.py')
| -rw-r--r-- | repoze/bfg/static.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/repoze/bfg/static.py b/repoze/bfg/static.py index 01ce98a30..be33ebee1 100644 --- a/repoze/bfg/static.py +++ b/repoze/bfg/static.py @@ -1,4 +1,5 @@ import os +import sys import pkg_resources from paste.httpheaders import ETAG @@ -6,6 +7,26 @@ from paste.urlparser import StaticURLParser from paste import httpexceptions from paste import request +def find_package(path): + """ + Determine the Python-package where path is located. If the path + is not located within the Python sys-path, return ``None``. + """ + + syspaths = list(sys.path) + syspaths.sort(key=lambda syspath: path.startswith('path') and 'path', reverse=True) + syspath = syspaths[0] + + path = os.path.normpath(path) + if not path.startswith(syspath): + return + + path = path[len(syspath):] + if path.startswith(os.path.sep): + path = path[1:] + + return path + class PackageURLParser(StaticURLParser): """ This probably won't work with zipimported resources """ def __init__(self, package_name, resource_name, root_resource=None, |
