From 2234c998d4eb7672065fb02c4974c0535f409dca Mon Sep 17 00:00:00 2001 From: Malthe Borch Date: Thu, 27 Aug 2009 10:26:12 +0000 Subject: Rebase module-relative static views on the nearest package path. --- repoze/bfg/static.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'repoze/bfg/static.py') 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, -- cgit v1.2.3