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
Provenance
- Commit
4f8ee8f- Tree
033aa7c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
dd59d72
11 files changed+242 added−151 removed
modified:flask/__init__.py+1 added
14 unmodified lines15 from werkzeug import abort, redirect16 from jinja2 import Markup, escape17 18 from flask.app import Flask19+from flask.config import Config20 from 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, json23 from flask.globals import current_app, g, request, session, _request_ctx_stack1 unmodified linemodified:flask/app.py+12 added-1 removed
1+# -*- coding: utf-8 -*-2+"""3+ flask.app4+ ~~~~~~~~~5+6+ This module implements the central WSGI application object.7+8+ :copyright: (c) 2010 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12 from threading import Lock13 from datetime import timedelta, datetime14 from itertools import chain15 4 unmodified lines20 21 from flask.helpers import _PackageBoundObject, url_for, get_flashed_messages, \22 _tojson_filter, get_pkg_resources23 from flask.wrappers import Request, Response13-from flask.conf import ConfigAttribute, Config24+from flask.config import ConfigAttribute, Config25 from flask.ctx import _default_template_ctx_processor, _RequestContext26 from flask.globals import _request_ctx_stack, request27 from flask.session import Session, _NullSession28 from flask.module import _ModuleSetupState807 unmodified linesdeleted:flask/conf.py-139 removed
1-import os2-import sys3-4-from werkzeug import import_string5-6-7-class ConfigAttribute(object):8- """Makes an attribute forward to the config"""9-10- def __init__(self, name):11- self.__name__ = name12-13- def __get__(self, obj, type=None):14- if obj is None:15- return self16- return obj.config[self.__name__]17-18- def __set__(self, obj, value):19- obj.config[self.__name__] = value20-21-22-class 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.26-27- Either you can fill the config from a config file::28-29- app.config.from_pyfile('yourconfig.cfg')30-31- 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::36-37- DEBUG = True38- SECRET_KEY = 'development key'39- app.config.from_object(__name__)40-41- 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.46-47- Probably the most interesting way to load configurations is from an48- environment variable pointing to a file::49-50- app.config.from_envvar('YOURAPPLICATION_SETTINGS')51-52- 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::55-56- export YOURAPPLICATION_SETTINGS='/path/to/config/file'57-58- On windows use `set` instead.59-60- :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- """65-66- def __init__(self, root_path, defaults=None):67- dict.__init__(self, defaults or {})68- self.root_path = root_path69-70- 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::74-75- app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])76-77- :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 True93-94- 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.98-99- :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)108-109- def from_object(self, obj):110- """Updates the values from the given object. An object can be of one111- of the following two types:112-113- - a string: in this case the object with that name will be imported114- - an actual object reference: that object is used directly115-116- Objects are usually either modules or classes.117-118- Just the uppercase variables in that object are stored in the config119- after lowercasing. Example usage::120-121- app.config.from_object('yourapplication.default_config')122- from yourapplication import default_config123- app.config.from_object(default_config)124-125- 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.129-130- :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)137-138- 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+ ~~~~~~~~~~~~5+6+ Implements the configuration related objects.7+8+ :copyright: (c) 2010 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12+import os13+import sys14+15+from werkzeug import import_string16+17+18+class ConfigAttribute(object):19+ """Makes an attribute forward to the config"""20+21+ def __init__(self, name):22+ self.__name__ = name23+24+ def __get__(self, obj, type=None):25+ if obj is None:26+ return self27+ return obj.config[self.__name__]28+29+ def __set__(self, obj, value):30+ obj.config[self.__name__] = value31+32+33+class 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.37+38+ Either you can fill the config from a config file::39+40+ app.config.from_pyfile('yourconfig.cfg')41+42+ 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::47+48+ DEBUG = True49+ SECRET_KEY = 'development key'50+ app.config.from_object(__name__)51+52+ 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.57+58+ Probably the most interesting way to load configurations is from an59+ environment variable pointing to a file::60+61+ app.config.from_envvar('YOURAPPLICATION_SETTINGS')62+63+ 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::66+67+ export YOURAPPLICATION_SETTINGS='/path/to/config/file'68+69+ On windows use `set` instead.70+71+ :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+ """76+77+ def __init__(self, root_path, defaults=None):78+ dict.__init__(self, defaults or {})79+ self.root_path = root_path80+81+ 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::85+86+ app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])87+88+ :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 True104+105+ 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.109+110+ :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)119+120+ def from_object(self, obj):121+ """Updates the values from the given object. An object can be of one122+ of the following two types:123+124+ - a string: in this case the object with that name will be imported125+ - an actual object reference: that object is used directly126+127+ Objects are usually either modules or classes.128+129+ Just the uppercase variables in that object are stored in the config130+ after lowercasing. Example usage::131+132+ app.config.from_object('yourapplication.default_config')133+ from yourapplication import default_config134+ app.config.from_object(default_config)135+136+ 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.140+141+ :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)148+149+ 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+ ~~~~~~~~~5+6+ Implements the objects required to keep the context.7+8+ :copyright: (c) 2010 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12 from werkzeug.exceptions import HTTPException13 3-from flask.wrappers import _RequestGlobals14 from flask.globals import _request_ctx_stack15 from flask.session import _NullSession16 17 18+class _RequestGlobals(object):19+ pass20+21+22 class _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 the51 unmodified linesmodified:flask/globals.py+13 added-1 removed
1+# -*- coding: utf-8 -*-2+"""3+ flask.globals4+ ~~~~~~~~~~~~~5+6+ Defines all the global objects that are proxies to the current7+ active context.8+9+ :copyright: (c) 2010 by Armin Ronacher.10+ :license: BSD, see LICENSE for more details.11+"""12+13 from werkzeug import LocalStack, LocalProxy14 15 # context locals16 _request_ctx_stack = LocalStack()17 current_app = LocalProxy(lambda: _request_ctx_stack.top.app)18 request = LocalProxy(lambda: _request_ctx_stack.top.request)19 session = LocalProxy(lambda: _request_ctx_stack.top.session)8-g = LocalProxy(lambda: _request_ctx_stack.top.g)20+g = LocalProxy(lambda: _request_ctx_stack.top.g)modified:flask/helpers.py+16 added-3 removed
1+# -*- coding: utf-8 -*-2+"""3+ flask.helpers4+ ~~~~~~~~~~~~~5+6+ Implements various helpers.7+8+ :copyright: (c) 2010 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12 import os13 import sys14 import mimetypes15 6 unmodified lines22 try:23 import json24 except ImportError:25 json_available = False15- 26+27 from werkzeug import Headers, wrap_file28 29 from flask.globals import session, _request_ctx_stack, current_app, request30 from flask.wrappers import Response31 21- 32+33 def _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 lines45 return json.dumps(*args, **kwargs).replace('/', '\\/')46 else:47 _tojson_filter = json.dumps48 49+50 def 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 lines77 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- 81+82+83 def 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 linesmodified:flask/module.py+11 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.module4+ ~~~~~~~~~~~~5+6+ Implements a class that represents module blueprints.7+8+ :copyright: (c) 2010 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12 from flask.helpers import _PackageBoundObject13 14 15 class _ModuleSetupState(object):148 unmodified linesmodified:flask/session.py+12 added
1+# -*- coding: utf-8 -*-2+"""3+ flask.session4+ ~~~~~~~~~~~~~5+6+ Implements cookie based sessions based on Werkzeug's secure cookie7+ system.8+9+ :copyright: (c) 2010 by Armin Ronacher.10+ :license: BSD, see LICENSE for more details.11+"""12+13 from werkzeug.contrib.securecookie import SecureCookie14 15 16 class Session(SecureCookie):27 unmodified linesmodified:flask/wrappers.py+11 added-5 removed
1+# -*- coding: utf-8 -*-2+"""3+ flask.wrappers4+ ~~~~~~~~~~~~~~5+6+ Implements the WSGI wrappers (request and response).7+8+ :copyright: (c) 2010 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12 from werkzeug import Request as RequestBase, Response as ResponseBase, \13 cached_property14 15 from helpers import json51 unmodified lines67 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'60-61-62-class _RequestGlobals(object):63- pass64-modified:setup.py+1 added-1 removed
56 unmodified lines57 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