Skip to content
Myr

Added missing comments, fixed setup.py and made tests pass

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 dd59d72 · Browse files · 11 files+242 added−151 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

11 files+242 added−151 removed

  • other:+242 added−151 removed

11 files+242 added−151 removed

modified:flask/__init__.py+1 added
14 unmodified lines
15from werkzeug import abort, redirect16from jinja2 import Markup, escape1718from flask.app import Flask19from flask.config import Config20from flask.helpers import url_for, jsonify, json_available, flash, send_file, \21 get_flashed_messages, render_template, render_template, render_template_string, \22 get_template_attribute, json23from flask.globals import current_app, g, request, session, _request_ctx_stack
1 unmodified line
modified:flask/app.py+12 added-1 removed
1# -*- coding: utf-8 -*-2"""3 flask.app4 ~~~~~~~~~56 This module implements the central WSGI application object.78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112from threading import Lock13from datetime import timedelta, datetime14from itertools import chain15
4 unmodified lines
2021from flask.helpers import _PackageBoundObject, url_for, get_flashed_messages, \22 _tojson_filter, get_pkg_resources23from flask.wrappers import Request, Response13from flask.conf import ConfigAttribute, Config24from flask.config import ConfigAttribute, Config25from flask.ctx import _default_template_ctx_processor, _RequestContext26from flask.globals import _request_ctx_stack, request27from flask.session import Session, _NullSession28from flask.module import _ModuleSetupState
807 unmodified lines
deleted:flask/conf.py-139 removed
1import os2import sys34from werkzeug import import_string567class ConfigAttribute(object):8 """Makes an attribute forward to the config"""910 def __init__(self, name):11 self.__name__ = name1213 def __get__(self, obj, type=None):14 if obj is None:15 return self16 return obj.config[self.__name__]1718 def __set__(self, obj, value):19 obj.config[self.__name__] = value202122class Config(dict):23 """Works exactly like a dict but provides ways to fill it from files24 or special dictionaries. There are two common patterns to populate the25 config.2627 Either you can fill the config from a config file::2829 app.config.from_pyfile('yourconfig.cfg')3031 Or alternatively you can define the configuration options in the32 module that calls :meth:`from_object` or provide an import path to33 a module that should be loaded. It is also possible to tell it to34 use the same module and with that provide the configuration values35 just before the call::3637 DEBUG = True38 SECRET_KEY = 'development key'39 app.config.from_object(__name__)4041 In both cases (loading from any Python file or loading from modules),42 only uppercase keys are added to the config. This makes it possible to use43 lowercase values in the config file for temporary values that are not added44 to the config or to define the config keys in the same file that implements45 the application.4647 Probably the most interesting way to load configurations is from an48 environment variable pointing to a file::4950 app.config.from_envvar('YOURAPPLICATION_SETTINGS')5152 In this case before launching the application you have to set this53 environment variable to the file you want to use. On Linux and OS X54 use the export statement::5556 export YOURAPPLICATION_SETTINGS='/path/to/config/file'5758 On windows use `set` instead.5960 :param root_path: path to which files are read relative from. When the61 config object is created by the application, this is62 the application's :attr:`~flask.Flask.root_path`.63 :param defaults: an optional dictionary of default values64 """6566 def __init__(self, root_path, defaults=None):67 dict.__init__(self, defaults or {})68 self.root_path = root_path6970 def from_envvar(self, variable_name, silent=False):71 """Loads a configuration from an environment variable pointing to72 a configuration file. This basically is just a shortcut with nicer73 error messages for this line of code::7475 app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])7677 :param variable_name: name of the environment variable78 :param silent: set to `True` if you want silent failing for missing79 files.80 :return: bool. `True` if able to load config, `False` otherwise.81 """82 rv = os.environ.get(variable_name)83 if not rv:84 if silent:85 return False86 raise RuntimeError('The environment variable %r is not set '87 'and as such configuration could not be '88 'loaded. Set this variable and make it '89 'point to a configuration file' %90 variable_name)91 self.from_pyfile(rv)92 return True9394 def from_pyfile(self, filename):95 """Updates the values in the config from a Python file. This function96 behaves as if the file was imported as module with the97 :meth:`from_object` function.9899 :param filename: the filename of the config. This can either be an100 absolute filename or a filename relative to the101 root path.102 """103 filename = os.path.join(self.root_path, filename)104 d = type(sys)('config')105 d.__file__ = filename106 execfile(filename, d.__dict__)107 self.from_object(d)108109 def from_object(self, obj):110 """Updates the values from the given object. An object can be of one111 of the following two types:112113 - a string: in this case the object with that name will be imported114 - an actual object reference: that object is used directly115116 Objects are usually either modules or classes.117118 Just the uppercase variables in that object are stored in the config119 after lowercasing. Example usage::120121 app.config.from_object('yourapplication.default_config')122 from yourapplication import default_config123 app.config.from_object(default_config)124125 You should not use this function to load the actual configuration but126 rather configuration defaults. The actual config should be loaded127 with :meth:`from_pyfile` and ideally from a location not within the128 package because the package might be installed system wide.129130 :param obj: an import name or object131 """132 if isinstance(obj, basestring):133 obj = import_string(obj)134 for key in dir(obj):135 if key.isupper():136 self[key] = getattr(obj, key)137138 def __repr__(self):139 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))
added:flask/config.py+150 added
1# -*- coding: utf-8 -*-2"""3 flask.config4 ~~~~~~~~~~~~56 Implements the configuration related objects.78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112import os13import sys1415from werkzeug import import_string161718class ConfigAttribute(object):19 """Makes an attribute forward to the config"""2021 def __init__(self, name):22 self.__name__ = name2324 def __get__(self, obj, type=None):25 if obj is None:26 return self27 return obj.config[self.__name__]2829 def __set__(self, obj, value):30 obj.config[self.__name__] = value313233class Config(dict):34 """Works exactly like a dict but provides ways to fill it from files35 or special dictionaries. There are two common patterns to populate the36 config.3738 Either you can fill the config from a config file::3940 app.config.from_pyfile('yourconfig.cfg')4142 Or alternatively you can define the configuration options in the43 module that calls :meth:`from_object` or provide an import path to44 a module that should be loaded. It is also possible to tell it to45 use the same module and with that provide the configuration values46 just before the call::4748 DEBUG = True49 SECRET_KEY = 'development key'50 app.config.from_object(__name__)5152 In both cases (loading from any Python file or loading from modules),53 only uppercase keys are added to the config. This makes it possible to use54 lowercase values in the config file for temporary values that are not added55 to the config or to define the config keys in the same file that implements56 the application.5758 Probably the most interesting way to load configurations is from an59 environment variable pointing to a file::6061 app.config.from_envvar('YOURAPPLICATION_SETTINGS')6263 In this case before launching the application you have to set this64 environment variable to the file you want to use. On Linux and OS X65 use the export statement::6667 export YOURAPPLICATION_SETTINGS='/path/to/config/file'6869 On windows use `set` instead.7071 :param root_path: path to which files are read relative from. When the72 config object is created by the application, this is73 the application's :attr:`~flask.Flask.root_path`.74 :param defaults: an optional dictionary of default values75 """7677 def __init__(self, root_path, defaults=None):78 dict.__init__(self, defaults or {})79 self.root_path = root_path8081 def from_envvar(self, variable_name, silent=False):82 """Loads a configuration from an environment variable pointing to83 a configuration file. This basically is just a shortcut with nicer84 error messages for this line of code::8586 app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])8788 :param variable_name: name of the environment variable89 :param silent: set to `True` if you want silent failing for missing90 files.91 :return: bool. `True` if able to load config, `False` otherwise.92 """93 rv = os.environ.get(variable_name)94 if not rv:95 if silent:96 return False97 raise RuntimeError('The environment variable %r is not set '98 'and as such configuration could not be '99 'loaded. Set this variable and make it '100 'point to a configuration file' %101 variable_name)102 self.from_pyfile(rv)103 return True104105 def from_pyfile(self, filename):106 """Updates the values in the config from a Python file. This function107 behaves as if the file was imported as module with the108 :meth:`from_object` function.109110 :param filename: the filename of the config. This can either be an111 absolute filename or a filename relative to the112 root path.113 """114 filename = os.path.join(self.root_path, filename)115 d = type(sys)('config')116 d.__file__ = filename117 execfile(filename, d.__dict__)118 self.from_object(d)119120 def from_object(self, obj):121 """Updates the values from the given object. An object can be of one122 of the following two types:123124 - a string: in this case the object with that name will be imported125 - an actual object reference: that object is used directly126127 Objects are usually either modules or classes.128129 Just the uppercase variables in that object are stored in the config130 after lowercasing. Example usage::131132 app.config.from_object('yourapplication.default_config')133 from yourapplication import default_config134 app.config.from_object(default_config)135136 You should not use this function to load the actual configuration but137 rather configuration defaults. The actual config should be loaded138 with :meth:`from_pyfile` and ideally from a location not within the139 package because the package might be installed system wide.140141 :param obj: an import name or object142 """143 if isinstance(obj, basestring):144 obj = import_string(obj)145 for key in dir(obj):146 if key.isupper():147 self[key] = getattr(obj, key)148149 def __repr__(self):150 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))
modified:flask/ctx.py+15 added-1 removed
1# -*- coding: utf-8 -*-2"""3 flask.ctx4 ~~~~~~~~~56 Implements the objects required to keep the context.78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112from werkzeug.exceptions import HTTPException133from flask.wrappers import _RequestGlobals14from flask.globals import _request_ctx_stack15from flask.session import _NullSession161718class _RequestGlobals(object):19 pass202122class _RequestContext(object):23 """The request context contains all request relevant information. It is24 created at the beginning of the request and pushed to the25 `_request_ctx_stack` and removed at the end of it. It will create the
51 unmodified lines
modified:flask/globals.py+13 added-1 removed
1# -*- coding: utf-8 -*-2"""3 flask.globals4 ~~~~~~~~~~~~~56 Defines all the global objects that are proxies to the current7 active context.89 :copyright: (c) 2010 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11"""1213from werkzeug import LocalStack, LocalProxy1415# context locals16_request_ctx_stack = LocalStack()17current_app = LocalProxy(lambda: _request_ctx_stack.top.app)18request = LocalProxy(lambda: _request_ctx_stack.top.request)19session = LocalProxy(lambda: _request_ctx_stack.top.session)8g = LocalProxy(lambda: _request_ctx_stack.top.g)20g = LocalProxy(lambda: _request_ctx_stack.top.g)
modified:flask/helpers.py+16 added-3 removed
1# -*- coding: utf-8 -*-2"""3 flask.helpers4 ~~~~~~~~~~~~~56 Implements various helpers.78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112import os13import sys14import mimetypes15
6 unmodified lines
22 try:23 import json24 except ImportError:25 json_available = False15 2627from werkzeug import Headers, wrap_file2829from flask.globals import session, _request_ctx_stack, current_app, request30from flask.wrappers import Response3121 3233def _assert_have_json():34 """Helper function that fails if JSON is unavailable."""35 if not json_available:36 raise RuntimeError('simplejson not installed')
8 unmodified lines
45 return json.dumps(*args, **kwargs).replace('/', '\\/')46else:47 _tojson_filter = json.dumps484950def jsonify(*args, **kwargs):51 """Creates a :class:`~flask.Response` with the JSON representation of52 the given arguments with an `application/json` mimetype. The arguments53 to this function are the same as to the :class:`dict` constructor.
23 unmodified lines
77 if __debug__:78 _assert_have_json()79 return current_app.response_class(json.dumps(dict(*args, **kwargs),80 indent=None if request.is_xhr else 2), mimetype='application/json')69 818283def get_pkg_resources():84 """Use pkg_resource if that works, otherwise fall back to cwd. The85 current working directory is generally not reliable with the notable86 exception of google appengine.
255 unmodified lines
modified:flask/module.py+11 added
1# -*- coding: utf-8 -*-2"""3 flask.module4 ~~~~~~~~~~~~56 Implements a class that represents module blueprints.78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112from flask.helpers import _PackageBoundObject131415class _ModuleSetupState(object):
148 unmodified lines
modified:flask/session.py+12 added
1# -*- coding: utf-8 -*-2"""3 flask.session4 ~~~~~~~~~~~~~56 Implements cookie based sessions based on Werkzeug's secure cookie7 system.89 :copyright: (c) 2010 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11"""1213from werkzeug.contrib.securecookie import SecureCookie141516class Session(SecureCookie):
27 unmodified lines
modified:flask/wrappers.py+11 added-5 removed
1# -*- coding: utf-8 -*-2"""3 flask.wrappers4 ~~~~~~~~~~~~~~56 Implements the WSGI wrappers (request and response).78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112from werkzeug import Request as RequestBase, Response as ResponseBase, \13 cached_property1415from helpers import json
51 unmodified lines
67 If you want to replace the response object used you can subclass this and68 set :attr:`~flask.Flask.response_class` to your subclass.69 """70 default_mimetype = 'text/html'606162class _RequestGlobals(object):63 pass64
modified:setup.py+1 added-1 removed
56 unmodified lines
57 author_email='armin.ronacher@active-4.com',58 description='A microframework based on Werkzeug, Jinja2 '59 'and good intentions',60 long_description=__doc__,61 py_modules=['flask'],61 packages=['flask'],62 zip_safe=False,63 platforms='any',64 install_requires=[65 'Werkzeug>=0.6.1',
14 unmodified lines