Implemented instance paths
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 6387661 · Browse files · 3 files·+94 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+94 added−8 removed
- tests:+18 added−0 removed
- other:+76 added−8 removed
Provenance
- Commit
153ecbc- Tree
27a33fd- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
6387661
3 files changed+94 added−8 removed
modified:flask/app.py+71 added-8 removed
9 unmodified lines10 """11 12 from __future__ import with_statement13 14+import os15 import sys16 from threading import Lock17 from datetime import timedelta18 from itertools import chain84 unmodified lines103 up, that debugging information is lost. (For example it would only104 pick up SQL queries in `yourapplication.app` and not105 `yourapplication.views.frontend`)106 106- .. versionadded:: 0.5107- The `static_path` parameter was added.107+ .. versionadded:: 0.7108+ The `static_url_path`, `static_folder`, and `template_folder`109+ parameters were added.110 111+ .. versionadded:: 0.8112+ The `instance_path` and `instance_relative_config` parameters were113+ added.114+115 :param import_name: the name of the application package110- :param static_path: can be used to specify a different path for the111- static files on the web. Defaults to ``/static``.112- This does not affect the folder the files are served113- *from*.116+ :param static_url_path: can be used to specify a different path for the117+ static files on the web. Defaults to the name118+ of the `static_folder` folder.119+ :param static_folder: the folder with static files that should be served120+ at `static_url_path`. Defaults to the ``'static'``121+ folder in the root path of the application.122+ :param template_folder: the folder that contains the templates that should123+ be used by the application. Defaults to124+ ``'templates'`` folder in the root path of the125+ application.126+ :param instance_path: An alternative instance path for the application.127+ By default the folder ``'instance'`` next to the128+ package or module is assumed to be the instance129+ path.130+ :param instance_relative_config: if set to `True` relative filenames131+ for loading the config are assumed to132+ be relative to the instance path instead133+ of the application root.134 """135 136 #: The class that is used for request objects. See :class:`~flask.Request`137 #: for more information.119 unmodified lines257 #: .. versionadded:: 0.7258 session_interface = SecureCookieSessionInterface()259 260 def __init__(self, import_name, static_path=None, static_url_path=None,241- static_folder='static', template_folder='templates'):261+ static_folder='static', template_folder='templates',262+ instance_path=None, instance_relative_config=False):263 _PackageBoundObject.__init__(self, import_name,264 template_folder=template_folder)265 if static_path is not None:266 from warnings import warn4 unmodified lines271 if static_url_path is not None:272 self.static_url_path = static_url_path273 if static_folder is not None:274 self.static_folder = static_folder275+ if instance_path is None:276+ instance_path = self.auto_find_instance_path()277+ elif not os.path.isabs(instance_path):278+ raise ValueError('If an instance path is provided it must be '279+ 'absolute. A relative path was given instead.')280 281+ #: Holds the path to the instance folder.282+ #:283+ #: .. versionadded:: 0.8284+ self.instance_path = instance_path285+286 #: The configuration dictionary as :class:`Config`. This behaves287 #: exactly like a regular dictionary but supports additional methods288 #: to load a config from files.258- self.config = Config(self.root_path, self.default_config)289+ self.config = self.make_config(instance_relative_config)290 291 # Prepare the deferred setup of the logger.292 self._logger = None293 self.logger_name = self.import_name227 unmodified lines521 .. versionadded:: 0.8522 """523 return self._got_first_request524 525+ def make_config(self, instance_relative=False):526+ """Used to create the config attribute by the Flask constructor.527+ The `instance_relative` parameter is passed in from the constructor528+ of Flask (there named `instance_relative_config`) and indicates if529+ the config should be relative to the instance path or the root path530+ of the application.531+532+ .. versionadded:: 0.8533+ """534+ root_path = self.root_path535+ if instance_relative:536+ root_path = self.instance_path537+ return Config(root_path, self.default_config)538+539+ def auto_find_instance_path(self):540+ """Tries to locate the instance path if it was not provided to the541+ constructor of the application class. It will basically calculate542+ the path to a folder named ``instance`` next to your main file or543+ the package.544+545+ .. versionadded:: 0.8546+ """547+ root_mod = sys.modules[self.import_name.split('.')[0]]548+ instance_path = None549+ if hasattr(root_mod, '__path__'):550+ package_dir = os.path.dirname(root_mod.__file__)551+ instance_path = os.path.join(package_dir, os.path.pardir)552+ else:553+ instance_path = os.path.dirname(root_mod.__file__)554+ basedir = os.path.normpath(os.path.abspath(instance_path))555+ return os.path.join(basedir, 'instance')556+557 def create_jinja_environment(self):558 """Creates the Jinja2 environment based on :attr:`jinja_options`559 and :meth:`select_jinja_autoescape`. Since 0.7 this also adds560 the Jinja2 globals and filters after initialization. Override952 unmodified linesmodified:tests/flask_tests.py+18 added
1003 unmodified lines1004 with app.test_client() as c:1005 rv = c.post('/foo', data={}, follow_redirects=True)1006 self.assertEqual(rv.data, 'success')1007 1008+ def test_basic_instance_paths(self):1009+ here = os.path.abspath(os.path.dirname(__file__))1010+ app = flask.Flask(__name__)1011+ self.assertEqual(app.instance_path, os.path.join(here, 'instance'))1012 1013+ app = flask.Flask(__name__, instance_path=here)1014+ self.assertEqual(app.instance_path, here)1015+1016+ try:1017+ flask.Flask(__name__, instance_path='instance')1018+ except ValueError, e:1019+ self.assert_('must be absolute' in str(e))1020+ else:1021+ self.fail('Expected value error')1022+1023+ from blueprintapp import app1024+ self.assertEqual(app.instance_path, os.path.join(here, 'instance'))1025+1026+1027 class JSONTestCase(unittest.TestCase):1028 1029 def test_json_bad_requests(self):1030 app = flask.Flask(__name__)1097 unmodified linesmodified:CHANGES+5 added
24 unmodified lines25 the beginning of the first request. (:meth:`Flask.before_first_request`)26 - Malformed JSON data will now trigger a bad request HTTP exception instead27 of a value error which usually would result in a 500 internal server28 error if not handled. This is a backwards incompatible change.29+- Applications now not only have a root path where the resources and modules30+ are located but also an instane path which is the designated place to31+ drop files that are modified at runtime (uploads etc.). Also this is32+ conceptionally only instance depending and outside version control so it's33+ the perfect place to put configuration files etc.34 35 Version 0.7.336 -------------37 238 unmodified lines