Documented instance root

This commit is contained in:
Armin Ronacher 2011-08-10 13:55:57 +02:00
parent 153ecbc920
commit 187cb80dcc
4 changed files with 78 additions and 3 deletions

View file

@ -554,6 +554,17 @@ class Flask(_PackageBoundObject):
basedir = os.path.normpath(os.path.abspath(instance_path))
return os.path.join(basedir, 'instance')
def open_instance_resource(self, resource, mode='rb'):
"""Opens a resource from the application's instance folder
(:attr:`instance_path`). Otherwise works like
:meth:`open_resource`. Instance resources can also be opened for
writing.
:param resource: the name of the resource. To access resources within
subfolders use forward slashes as separator.
"""
return open(os.path.join(self.instance_path, resource), mode)
def create_jinja_environment(self):
"""Creates the Jinja2 environment based on :attr:`jinja_options`
and :meth:`select_jinja_autoescape`. Since 0.7 this also adds

View file

@ -565,7 +565,7 @@ class _PackageBoundObject(object):
raise RuntimeError('No static folder for this object')
return send_from_directory(self.static_folder, filename)
def open_resource(self, resource):
def open_resource(self, resource, mode='rb'):
"""Opens a resource from the application's resource folder. To see
how this works, consider the following folder structure::
@ -587,4 +587,6 @@ class _PackageBoundObject(object):
:param resource: the name of the resource. To access resources within
subfolders use forward slashes as separator.
"""
return open(os.path.join(self.root_path, resource), 'rb')
if mode not in ('r', 'rb'):
raise ValueError('Resources can only be opened for reading')
return open(os.path.join(self.root_path, resource), mode)