-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
104 lines (83 loc) · 3.81 KB
/
app.py
File metadata and controls
104 lines (83 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os
from flask import Flask, request
from werkzeug import import_string
from feedbundle.corelib.logging import make_file_handler, make_smtp_handler
class FeedBundle(Flask):
"""The web application."""
CONFIG_ENV = "FEEDBUNDLE_CONFIG"
BUILTIN_CONFIG = "app.cfg"
def __init__(self, import_name=__package__, *args, **kwargs):
super(FeedBundle, self).__init__(import_name, *args, **kwargs)
#: load built-in and local configuration
self.config.from_pyfile(self.get_absolute_path(self.BUILTIN_CONFIG))
self.config.from_envvar(self.CONFIG_ENV)
self.config.setdefault("BUILTIN_BLUEPRINTS", [])
self.config.setdefault("BUILTIN_EXTENSIONS", [])
self.config.setdefault("BLUEPRINTS", [])
self.config.setdefault("EXTENSIONS", [])
self.config.setdefault("LOGGING_FILE", None)
self.config.setdefault("LOGGING_EMAILS", [])
#: initialize the application
self.init_extensions()
self.init_logger()
self.init_blueprints()
def init_extensions(self):
"""Initialize the extensions of the application."""
#: make a set to contain names of the extensions
extensions = set(self.config['BUILTIN_EXTENSIONS'])
extensions.update(self.config['EXTENSIONS'])
#: import and install the extensions
for extension_name in extensions:
extension = import_string(extension_name)
extension.init_app(self)
def init_logger(self):
"""Append more handlers to the application logger."""
#: create a file handler and install it
filepath = self.config['LOGGING_FILE']
if filepath:
file_handler = make_file_handler(filepath)
self.logger.addHandler(file_handler)
#: create a email handler and install it
emails = self.config['LOGGING_EMAILS']
smtp_handler_installed = [False]
@self.before_first_request
def register_smtp_handler():
#: defer initialize because of the 'request.host'
if emails and not smtp_handler_installed[0]:
fromaddr = "applog@%s" % request.host
subject = "FeedBundle Application Log"
smtp_handler = make_smtp_handler(fromaddr, emails, subject)
self.logger.addHandler(smtp_handler)
smtp_handler_installed[0] = True
def init_blueprints(self):
"""Register all blueprints to the application."""
#: make a set to contain names of the blueprints
blueprints = set(self.config['BUILTIN_BLUEPRINTS'])
blueprints.update(self.config['BLUEPRINTS'])
#: import and install all blueprints
for blueprint_name in blueprints:
package_name = blueprint_name.replace(":", ".").rsplit(".", 1)[0]
self.register_blueprint_by_name(blueprint_name, package_name)
def register_blueprint_by_name(self, name, package_name):
"""Register the blueprint by its name."""
#: import the blueprint object and its package
blueprint = import_string(name)
package = import_string(package_name)
#: load the all submodules
for module_name in getattr(package, "__all__", []):
import_string("%s.%s" % (package_name, module_name), silent=True)
#: register the blueprint
self.register_blueprint(blueprint)
self.logger.info("Loaded %r" % name)
def get_absolute_path(self, relative_path):
"""Create a absolute path from a relative path.
Example:
>>> app.root_path
'/home/tonyseek/projects/myapp/myapp'
>>> app.get_full_path("../somefile")
'/home/tonyseek/projects/myapp/somefile'
"""
path = os.path.join(self.root_path, relative_path)
return os.path.abspath(path)