Flask now runs without the presence of pkg_resources as well.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent ca520fb · Browse files · 1 file·+24 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+24 added−3 removed
- other:+24 added−3 removed
Provenance
- Commit
2d9bb69- Tree
adb9370- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ca520fb
1 file changed+24 added−3 removed
modified:flask.py+24 added-3 removed
9 unmodified lines10 :license: BSD, see LICENSE for more details.11 """12 import os13 import sys14-import pkg_resources14+15 from threading import local16 from contextlib import contextmanager17-from jinja2 import Environment, PackageLoader17+from jinja2 import Environment, PackageLoader, FileSystemLoader18 from werkzeug import Request as RequestBase, Response as ResponseBase, \19 LocalStack, LocalProxy, create_environ, cached_property, \20 SharedDataMiddleware21 from werkzeug.routing import Map, Rule4 unmodified lines26 # in the module but are exported as public interface.27 from werkzeug import abort, redirect28 from jinja2 import Markup, escape29 30+# use pkg_resource if that works, otherwise fall back to cwd. The31+# current working directory is generally not reliable with the notable32+# exception of google appengine.33+try:34+ import pkg_resources35+ pkg_resources.resource_stream36+except (ImportError, AttributeError):37+ pkg_resources = None38 39+40 class Request(RequestBase):41 """The request object used by default in flask. Remembers the42 matched endpoint and view arguments.43 166 unmodified lines210 #: the name of the package or module. Do not change this once211 #: it was set by the constructor.212 self.package_name = package_name213 214+ #: where is the app root located?215+ self.root_path = os.path.abspath(os.path.dirname(216+ sys.modules[self.package_name].__file__))217+218 #: a dictionary of all view functions registered. The keys will219 #: be function names which are also used to generate URLs and220 #: the values are the function objects themselves.221 #: to register a view function, use the :meth:`route` decorator.32 unmodified lines254 255 if self.static_path is not None:256 self.url_map.add(Rule(self.static_path + '/<filename>',257 build_only=True, endpoint='static'))258+ if pkg_resources is not None:259+ target = (self.package_name, 'static')260+ else:261+ target = os.path.join(self.root_path, 'static')262 self.wsgi_app = SharedDataMiddleware(self.wsgi_app, {246- self.static_path: (self.package_name, 'static')263+ self.static_path: target264 })265 266 #: the Jinja2 environment. It is created from the267 #: :attr:`jinja_options` and the loader that is returned10 unmodified lines278 the configured package is returned that looks up templates in the279 `templates` folder. To add other loaders it's possible to280 override this method.281 """282+ if pkg_resources is None:283+ return FileSystemLoader(os.path.join(self.root_path, 'templates'))284 return PackageLoader(self.package_name)285 286 def update_template_context(self, context):287 """Update the template context with some commonly used variables.53 unmodified lines341 342 :param resource: the name of the resource. To access resources within343 subfolders use forward slashes as separator.344 """345+ if pkg_resources is None:346+ return open(os.path.join(self.root_path, resource), 'rb')347 return pkg_resources.resource_stream(self.package_name, resource)348 349 def open_session(self, request):350 """Creates or opens a new session. Default implementation stores all300 unmodified lines