working import layout for module
This commit is contained in:
parent
d0dc89ea80
commit
c4f64c1c47
9 changed files with 78 additions and 47 deletions
|
|
@ -9,50 +9,15 @@
|
||||||
:copyright: (c) 2010 by Armin Ronacher.
|
:copyright: (c) 2010 by Armin Ronacher.
|
||||||
:license: BSD, see LICENSE for more details.
|
:license: BSD, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
from __future__ import with_statement
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import mimetypes
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
|
|
||||||
# this is a workaround for appengine. Do not remove this import
|
|
||||||
import werkzeug
|
|
||||||
|
|
||||||
from itertools import chain
|
|
||||||
from threading import Lock
|
|
||||||
from jinja2 import Environment, PackageLoader, FileSystemLoader
|
|
||||||
from werkzeug import Request as RequestBase, Response as ResponseBase, \
|
|
||||||
LocalStack, LocalProxy, create_environ, SharedDataMiddleware, \
|
|
||||||
ImmutableDict, cached_property, wrap_file, Headers, \
|
|
||||||
import_string
|
|
||||||
from werkzeug.routing import Map, Rule
|
|
||||||
from werkzeug.exceptions import HTTPException, InternalServerError
|
|
||||||
from werkzeug.contrib.securecookie import SecureCookie
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# utilities we import from Werkzeug and Jinja2 that are unused
|
# utilities we import from Werkzeug and Jinja2 that are unused
|
||||||
# in the module but are exported as public interface.
|
# in the module but are exported as public interface.
|
||||||
from werkzeug import abort, redirect
|
from werkzeug import abort, redirect
|
||||||
from jinja2 import Markup, escape
|
from jinja2 import Markup, escape
|
||||||
|
|
||||||
# use pkg_resource if that works, otherwise fall back to cwd. The
|
from flask.app import Flask
|
||||||
# current working directory is generally not reliable with the notable
|
from flask.helpers import url_for, jsonify, json_available, flash, send_file, \
|
||||||
# exception of google appengine.
|
get_flashed_messages, render_template, render_template, render_template_string, \
|
||||||
try:
|
get_template_attribute
|
||||||
import pkg_resources
|
from flask.globals import current_app, g, request, session, _request_ctx_stack
|
||||||
pkg_resources.resource_stream
|
from flask.module import Module
|
||||||
except (ImportError, AttributeError):
|
|
||||||
pkg_resources = None
|
|
||||||
|
|
||||||
# a lock used for logger initialization
|
|
||||||
_logger_lock = Lock()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# context locals
|
|
||||||
_request_ctx_stack = LocalStack()
|
|
||||||
current_app = LocalProxy(lambda: _request_ctx_stack.top.app)
|
|
||||||
request = LocalProxy(lambda: _request_ctx_stack.top.request)
|
|
||||||
session = LocalProxy(lambda: _request_ctx_stack.top.session)
|
|
||||||
g = LocalProxy(lambda: _request_ctx_stack.top.g)
|
|
||||||
|
|
|
||||||
24
flask/app.py
24
flask/app.py
|
|
@ -1,3 +1,23 @@
|
||||||
|
from threading import Lock
|
||||||
|
from datetime import timedelta, datetime
|
||||||
|
from itertools import chain
|
||||||
|
|
||||||
|
from jinja2 import Environment, PackageLoader, FileSystemLoader
|
||||||
|
from werkzeug import ImmutableDict, SharedDataMiddleware, create_environ
|
||||||
|
from werkzeug.routing import Map, Rule
|
||||||
|
from werkzeug.exceptions import HTTPException, InternalServerError
|
||||||
|
|
||||||
|
from flask.helpers import _PackageBoundObject, url_for, get_flashed_messages, \
|
||||||
|
_tojson_filter, get_pkg_resources
|
||||||
|
from flask.wrappers import Request, Response
|
||||||
|
from flask.conf import ConfigAttribute, Config
|
||||||
|
from flask.ctx import _default_template_ctx_processor, _RequestContext
|
||||||
|
from flask.globals import _request_ctx_stack, request
|
||||||
|
from flask.session import Session, _NullSession
|
||||||
|
from flask.module import _ModuleSetupState
|
||||||
|
|
||||||
|
# a lock used for logger initialization
|
||||||
|
_logger_lock = Lock()
|
||||||
|
|
||||||
|
|
||||||
class Flask(_PackageBoundObject):
|
class Flask(_PackageBoundObject):
|
||||||
|
|
@ -219,7 +239,7 @@ class Flask(_PackageBoundObject):
|
||||||
if self.static_path is not None:
|
if self.static_path is not None:
|
||||||
self.add_url_rule(self.static_path + '/<filename>',
|
self.add_url_rule(self.static_path + '/<filename>',
|
||||||
build_only=True, endpoint='static')
|
build_only=True, endpoint='static')
|
||||||
if pkg_resources is not None:
|
if get_pkg_resources() is not None:
|
||||||
target = (self.import_name, 'static')
|
target = (self.import_name, 'static')
|
||||||
else:
|
else:
|
||||||
target = os.path.join(self.root_path, 'static')
|
target = os.path.join(self.root_path, 'static')
|
||||||
|
|
@ -279,7 +299,7 @@ class Flask(_PackageBoundObject):
|
||||||
`templates` folder. To add other loaders it's possible to
|
`templates` folder. To add other loaders it's possible to
|
||||||
override this method.
|
override this method.
|
||||||
"""
|
"""
|
||||||
if pkg_resources is None:
|
if get_pkg_resources() is None:
|
||||||
return FileSystemLoader(os.path.join(self.root_path, 'templates'))
|
return FileSystemLoader(os.path.join(self.root_path, 'templates'))
|
||||||
return PackageLoader(self.import_name)
|
return PackageLoader(self.import_name)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from werkzeug import import_string
|
||||||
|
|
||||||
|
|
||||||
class ConfigAttribute(object):
|
class ConfigAttribute(object):
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
|
from werkzeug.exceptions import HTTPException
|
||||||
|
|
||||||
|
from flask.wrappers import _RequestGlobals
|
||||||
|
from flask.globals import _request_ctx_stack
|
||||||
|
from flask.session import _NullSession
|
||||||
|
|
||||||
|
|
||||||
class _RequestContext(object):
|
class _RequestContext(object):
|
||||||
|
|
|
||||||
8
flask/globals.py
Normal file
8
flask/globals.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
from werkzeug import LocalStack, LocalProxy
|
||||||
|
|
||||||
|
# context locals
|
||||||
|
_request_ctx_stack = LocalStack()
|
||||||
|
current_app = LocalProxy(lambda: _request_ctx_stack.top.app)
|
||||||
|
request = LocalProxy(lambda: _request_ctx_stack.top.request)
|
||||||
|
session = LocalProxy(lambda: _request_ctx_stack.top.session)
|
||||||
|
g = LocalProxy(lambda: _request_ctx_stack.top.g)
|
||||||
|
|
@ -1,3 +1,7 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
# try to load the best simplejson implementation available. If JSON
|
# try to load the best simplejson implementation available. If JSON
|
||||||
# is not installed, we add a failing class.
|
# is not installed, we add a failing class.
|
||||||
json_available = True
|
json_available = True
|
||||||
|
|
@ -9,6 +13,12 @@ except ImportError:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
json_available = False
|
json_available = False
|
||||||
|
|
||||||
|
from werkzeug import Headers, wrap_file
|
||||||
|
|
||||||
|
from flask.globals import session, _request_ctx_stack, current_app, request
|
||||||
|
from flask.wrappers import Response
|
||||||
|
|
||||||
|
|
||||||
def _assert_have_json():
|
def _assert_have_json():
|
||||||
"""Helper function that fails if JSON is unavailable."""
|
"""Helper function that fails if JSON is unavailable."""
|
||||||
if not json_available:
|
if not json_available:
|
||||||
|
|
@ -57,6 +67,17 @@ def jsonify(*args, **kwargs):
|
||||||
return current_app.response_class(json.dumps(dict(*args, **kwargs),
|
return current_app.response_class(json.dumps(dict(*args, **kwargs),
|
||||||
indent=None if request.is_xhr else 2), mimetype='application/json')
|
indent=None if request.is_xhr else 2), mimetype='application/json')
|
||||||
|
|
||||||
|
def get_pkg_resources():
|
||||||
|
"""Use pkg_resource if that works, otherwise fall back to cwd. The
|
||||||
|
current working directory is generally not reliable with the notable
|
||||||
|
exception of google appengine.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
import pkg_resources
|
||||||
|
pkg_resources.resource_stream
|
||||||
|
except (ImportError, AttributeError):
|
||||||
|
return
|
||||||
|
return pkg_resources
|
||||||
|
|
||||||
|
|
||||||
def url_for(endpoint, **values):
|
def url_for(endpoint, **values):
|
||||||
|
|
@ -164,7 +185,6 @@ def get_flashed_messages(with_categories=False):
|
||||||
return flashes
|
return flashes
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def send_file(filename_or_fp, mimetype=None, as_attachment=False,
|
def send_file(filename_or_fp, mimetype=None, as_attachment=False,
|
||||||
attachment_filename=None):
|
attachment_filename=None):
|
||||||
"""Sends the contents of a file to the client. This will use the
|
"""Sends the contents of a file to the client. This will use the
|
||||||
|
|
@ -262,7 +282,6 @@ def render_template_string(source, **context):
|
||||||
return current_app.jinja_env.from_string(source).render(context)
|
return current_app.jinja_env.from_string(source).render(context)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _get_package_path(name):
|
def _get_package_path(name):
|
||||||
"""Returns the path to a package or cwd if that cannot be found."""
|
"""Returns the path to a package or cwd if that cannot be found."""
|
||||||
try:
|
try:
|
||||||
|
|
@ -271,7 +290,6 @@ def _get_package_path(name):
|
||||||
return os.getcwd()
|
return os.getcwd()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class _PackageBoundObject(object):
|
class _PackageBoundObject(object):
|
||||||
|
|
||||||
def __init__(self, import_name):
|
def __init__(self, import_name):
|
||||||
|
|
@ -304,7 +322,7 @@ class _PackageBoundObject(object):
|
||||||
:param resource: the name of the resource. To access resources within
|
:param resource: the name of the resource. To access resources within
|
||||||
subfolders use forward slashes as separator.
|
subfolders use forward slashes as separator.
|
||||||
"""
|
"""
|
||||||
|
pkg_resources = get_pkg_resources()
|
||||||
if pkg_resources is None:
|
if pkg_resources is None:
|
||||||
return open(os.path.join(self.root_path, resource), 'rb')
|
return open(os.path.join(self.root_path, resource), 'rb')
|
||||||
return pkg_resources.resource_stream(self.import_name, resource)
|
return pkg_resources.resource_stream(self.import_name, resource)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
from flask.helpers import _PackageBoundObject
|
||||||
|
|
||||||
|
|
||||||
class _ModuleSetupState(object):
|
class _ModuleSetupState(object):
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
from werkzeug.contrib.securecookie import SecureCookie
|
||||||
|
|
||||||
|
|
||||||
class Session(SecureCookie):
|
class Session(SecureCookie):
|
||||||
"""Expands the session with support for switching between permanent
|
"""Expands the session with support for switching between permanent
|
||||||
and non-permanent sessions.
|
and non-permanent sessions.
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,9 @@
|
||||||
|
from werkzeug import Request as RequestBase, Response as ResponseBase, \
|
||||||
|
cached_property
|
||||||
|
|
||||||
|
from helpers import json
|
||||||
|
|
||||||
|
|
||||||
class Request(RequestBase):
|
class Request(RequestBase):
|
||||||
"""The request object used by default in flask. Remembers the
|
"""The request object used by default in flask. Remembers the
|
||||||
matched endpoint and view arguments.
|
matched endpoint and view arguments.
|
||||||
|
|
@ -35,6 +41,7 @@ class Request(RequestBase):
|
||||||
parsed JSON data.
|
parsed JSON data.
|
||||||
"""
|
"""
|
||||||
if __debug__:
|
if __debug__:
|
||||||
|
from flask.helpers import _assert_have_json
|
||||||
_assert_have_json()
|
_assert_have_json()
|
||||||
if self.mimetype == 'application/json':
|
if self.mimetype == 'application/json':
|
||||||
return json.loads(self.data)
|
return json.loads(self.data)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue