def __init__(self, import_name, static_url_path=None, static_folder='static', static_host=None, host_matching=False, subdomain_matching=False, template_folder='templates', instance_path=None, instance_relative_config=False, root_path=None): self.version_dict = {} _PackageBoundObject.__init__(self, import_name, template_folder=template_folder, root_path=root_path) if static_url_path is not None: self.static_url_path = static_url_path if static_folder is not None: self.static_folder = static_folder if instance_path is None: instance_path = self.auto_find_instance_path() elif not os.path.isabs(instance_path): raise ValueError('If an instance path is provided it must be ' 'absolute. A relative path was given instead.') # 保存实例文件夹的路径。 versionadded:: 0.8 self.instance_path = instance_path # 这种行为就像普通字典,但支持其他方法从文件加载配置文件。 self.config = self.make_config(instance_relative_config) # 准备记录日志的设置 self._logger = None self.logger_name = self.import_name # 注册所有视图函数的字典。钥匙会是:用于生成URL的函数名值是函数对象本身。 # 注册一个视图函数,使用:route装饰器 self.view_functions = {} # 支持现在不推荐的Error处理程序属性。现在将使用 #: A dictionary of all registered error handlers. The key is ``None`` #: for error handlers active on the application, otherwise the key is #: the name of the blueprint. Each key points to another dictionary #: where the key is the status code of the http exception. The #: special key ``None`` points to a list of tuples where the first item #: is the class for the instance check and the second the error handler #: function. #: #: To register a error handler, use the :meth:`errorhandler` #: decorator. self.error_handler_spec = {} #: A list of functions that are called when :meth:`url_for` raises a #: :exc:`~werkzeug.routing.BuildError`. Each function registered here #: is called with `error`, `endpoint` and `values`. If a function #: returns ``None`` or raises a :exc:`BuildError` the next function is #: tried. #: #: .. versionadded:: 0.9 self.url_build_error_handlers = [] self.before_request_funcs = {} self.before_first_request_funcs = [] self.after_request_funcs = {} self.teardown_request_funcs = {} self.teardown_appcontext_funcs = [] self.url_value_preprocessors = {} self.url_default_functions = {} self.template_context_processors = { None: [_default_template_ctx_processor] } self.shell_context_processor = [] self.blueprints = {} self._blueprint_order = [] self.extensions = {} # 使用这个可以在创建类之后改变路由转换器的但是在任何线路连接之前 # from werkzeug.routing import BaseConverter #: class ListConverter(BaseConverter): #: def to_python(self, value): #: return value.split(',') #: def to_url(self, values): #: return ','.join(BaseConverter.to_url(value) #: for value in values) #: #: view = Flask(__name__) #: view.url_map.converters['list'] = ListConverter # : @list.route("/job.json") >>> /list/job.json self.url_map = MFMap() self.url_map.host_matching = host_matching self.subdomain_matching = subdomain_matching # 如果应用程序已经处理至少一个,则在内部跟踪 self._got_first_request = False self._before_request_lock = Lock() # 使用提供的静态URL路径、静态主机和静态文件夹添加静态路由 if self.has_static_folder: assert bool( static_host ) == host_matching, 'Invalid static_host/host_matching combination' self.add_url_rule(self.static_url_path + '/<path:filename>', endpoint='static', host=static_host, view_func=self.send_static_file) self.cli = cli.AppGroup(self.name)
def __init__(self, import_name): _PackageBoundObject.__init__(self, import_name) #: The configuration dictionary as :class:`Config`. This behaves #: exactly like a regular dictionary but supports additional methods #: to load a config from files. self.config = Config(self.root_path, self.default_config) #: Prepare the deferred setup of the logger. self._logger = None self.logger_name = self.import_name #: A dictionary of all view functions registered. The keys will #: be function names which are also used to generate URLs and #: the values are the function objects themselves. #: to register a view function, use the :meth:`route` decorator. self.view_functions = {} #: A dictionary of all registered error handlers. The key is #: be the error code as integer, the value the function that #: should handle that error. #: To register a error handler, use the :meth:`errorhandler` #: decorator. self.error_handlers = {} #: A dictionary with lists of functions that should be called at the #: beginning of the request. The key of the dictionary is the name of #: the module this function is active for, `None` for all requests. #: This can for example be used to open database connections or #: getting hold of the currently logged in user. To register a #: function here, use the :meth:`before_request` decorator. self.before_request_funcs = {} #: A dictionary with lists of functions that should be called after #: each request. The key of the dictionary is the name of the module #: this function is active for, `None` for all requests. This can for #: example be used to open database connections or getting hold of the #: currently logged in user. To register a function here, use the #: :meth:`before_request` decorator. self.after_request_funcs = {} #: A dictionary with list of functions that are called without argument #: to populate the template context. They key of the dictionary is the #: name of the module this function is active for, `None` for all #: requests. Each returns a dictionary that the template context is #: updated with. To register a function here, use the #: :meth:`context_processor` decorator. self.template_context_processors = { None: [_default_template_ctx_processor] } #: The :class:`~werkzeug.routing.Map` for this instance. You can use #: this to change the routing converters after the class was created #: but before any routes are connected. Example:: #: #: from werkzeug import BaseConverter #: #: class ListConverter(BaseConverter): #: def to_python(self, value): #: return value.split(',') #: def to_url(self, values): #: return ','.join(BaseConverter.to_url(value) #: for value in values) self.jinja_env = self.create_jinja_environment() self.init_jinja_globals() #: #: app = Flask(__name__) #: app.url_map.converters['list'] = ListConverter self.url_map = Map() if self.static_path is not None: self.add_url_rule(self.static_path + '/<filename>', build_only=True, endpoint='static') if get_pkg_resources() is not None: target = (self.import_name, 'static') else: target = os.path.join(self.root_path, 'static') self.wsgi_app = SharedDataMiddleware(self.wsgi_app, { self.static_path: target })