Documented instance root
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 153ecbc · Browse files · 4 files·+78 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
4 files+78 added−3 removed
- other:+78 added−3 removed
Provenance
- Commit
187cb80- Tree
8e01e49- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
153ecbc
4 files changed+78 added−3 removed
modified:docs/config.rst+61 added
265 unmodified lines266 details about how to do that, head over to the267 :ref:`fabric-deployment` pattern.268 269 .. _fabric: http://fabfile.org/270+271+272+.. _instance-folders:273+274+Instance Folders275+----------------276+277+.. versionadded:: 0.8278+279+Flask 0.8 introduces instance folders. Flask for a long time made it280+possible to refer to paths relative to the application's folder directly281+(via :attr:`Flask.root_path`). This was also how many developers loaded282+configurations stored next to the application. Unfortunately however this283+only works well if applications are not packages in which case the root284+path refers to the contents of the package.285+286+With Flask 0.8 a new attribute was introduced:287+:attr:`Flask.instance_path`. It refers to a new concept called the288+“instance folder”. The instance folder is designed to not be under289+version control and be deployment specific. It's the perfect place to290+drop things that either change at runtime or configuration files.291+292+To make it easier to put this folder into an ignore list for your version293+control system it's called ``instance`` and placed directly next to your294+package or module by default. This path can be overridden by specifying295+the `instance_path` parameter to your application::296+297+ app = Flask(__name__, instance_path='/path/to/instance')298+299+Please keep in mind that this path *must* be absolute when provided.300+301+Since the config object provided loading of configuration files from302+relative filenames we made it possible to change the loading via filenames303+to be relative to the instance path if wanted. The behavior of relative304+paths in config files can be flipped between “relative to the application305+root” (the default) to “relative to instance folder” via the306+`instance_relative_config` switch to the application constructor::307+308+ app = Flask(__name__, instance_relative_config=True)309+310+Here is a full example of how to configure Flask to preload the config311+from a module and then override the config from a file in the config312+folder if it exists::313+314+ app = Flask(__name__, instance_relative_config=True)315+ app.config.from_object('yourapplication.default_settings')316+ app.config.from_pyfile('application.cfg', silent=True)317+318+The path to the instance folder can be found via the319+:attr:`Flask.instance_path`. Flask also provides a shortcut to open a320+file from the instnace folder with :meth:`Flask.open_instance_resource`.321+322+Example usage for both::323+324+ filename = os.path.join(app.instance_root, 'application.cfg')325+ with open(filename) as f:326+ config = f.read()327+328+ # or via open_instance_resource:329+ with app.open_instance_resource('application.cfg') as f:330+ config = f.read()modified:flask/app.py+11 added
552 unmodified lines553 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 open_instance_resource(self, resource, mode='rb'):558+ """Opens a resource from the application's instance folder559+ (:attr:`instance_path`). Otherwise works like560+ :meth:`open_resource`. Instance resources can also be opened for561+ writing.562+563+ :param resource: the name of the resource. To access resources within564+ subfolders use forward slashes as separator.565+ """566+ return open(os.path.join(self.instance_path, resource), mode)567+568 def create_jinja_environment(self):569 """Creates the Jinja2 environment based on :attr:`jinja_options`570 and :meth:`select_jinja_autoescape`. Since 0.7 this also adds571 the Jinja2 globals and filters after initialization. Override952 unmodified linesmodified:flask/helpers.py+4 added-2 removed
563 unmodified lines564 if not self.has_static_folder:565 raise RuntimeError('No static folder for this object')566 return send_from_directory(self.static_folder, filename)567 568- def open_resource(self, resource):568+ def open_resource(self, resource, mode='rb'):569 """Opens a resource from the application's resource folder. To see570 how this works, consider the following folder structure::571 572 /myapplication.py13 unmodified lines586 587 :param resource: the name of the resource. To access resources within588 subfolders use forward slashes as separator.589 """590+ if mode not in ('r', 'rb'):591+ raise ValueError('Resources can only be opened for reading')590- return open(os.path.join(self.root_path, resource), 'rb')592+ return open(os.path.join(self.root_path, resource), mode)modified:CHANGES+2 added-1 removed
28 unmodified lines29 - 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.33+ the perfect place to put configuration files etc. For more information34+ see :ref:`instance-folders`.35 36 Version 0.7.337 -------------38 238 unmodified lines