|
1
|
1 |
|
import os |
|
2
|
1 |
|
import logging |
|
3
|
1 |
|
from urllib.parse import urlencode, unquote |
|
4
|
|
|
|
|
5
|
1 |
|
from flask import request, current_app |
|
|
|
|
|
|
6
|
1 |
|
from flask_api import FlaskAPI |
|
|
|
|
|
|
7
|
1 |
|
from flask_api.exceptions import APIException, NotFound |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
1 |
|
from . import services |
|
10
|
1 |
|
from . import stores |
|
11
|
1 |
|
from . import routes |
|
12
|
1 |
|
from . import extensions |
|
13
|
|
|
|
|
14
|
1 |
|
log = logging.getLogger('api') |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
1 |
|
class TemplateNotFound(NotFound): |
|
|
|
|
|
|
18
|
1 |
|
detail = "Template not found." |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
1 |
|
class InvalidMaskedCode(NotFound): |
|
|
|
|
|
|
22
|
1 |
|
detail = "Masked URL does not match any image." |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
1 |
|
class FilenameTooLong(APIException): |
|
|
|
|
|
|
26
|
1 |
|
status_code = 414 |
|
27
|
1 |
|
detail = "Filename too long." |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
1 |
|
class InvalidImageLink(APIException): |
|
|
|
|
|
|
31
|
1 |
|
status_code = 415 |
|
32
|
1 |
|
detail = "Unsupported image type." |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
1 |
|
def create_app(config): |
|
36
|
1 |
|
app = FlaskAPI(__name__) |
|
37
|
1 |
|
app.config.from_object(config) |
|
38
|
|
|
|
|
39
|
1 |
|
configure_logging(app) |
|
40
|
|
|
|
|
41
|
1 |
|
register_extensions(app) |
|
42
|
|
|
|
|
43
|
1 |
|
register_services(app) |
|
44
|
1 |
|
register_blueprints(app) |
|
45
|
|
|
|
|
46
|
1 |
|
return app |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
1 |
|
def configure_logging(app): |
|
50
|
1 |
|
if app.config['DEBUG']: |
|
51
|
1 |
|
level = logging.DEBUG |
|
52
|
|
|
else: |
|
53
|
1 |
|
level = logging.INFO |
|
54
|
1 |
|
logging.basicConfig(level=level, format="%(levelname)s: %(message)s") |
|
55
|
1 |
|
logging.getLogger('yorm').setLevel(logging.WARNING) |
|
56
|
1 |
|
logging.getLogger('requests').setLevel(logging.WARNING) |
|
57
|
1 |
|
logging.getLogger('PIL').setLevel(logging.INFO) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
1 |
|
def register_extensions(app): |
|
61
|
1 |
|
extensions.db.init_app(app) |
|
62
|
1 |
|
extensions.migrate.init_app(app, extensions.db) |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
1 |
|
def register_services(app): |
|
66
|
1 |
|
exceptions = services.Exceptions( |
|
67
|
|
|
TemplateNotFound, |
|
68
|
|
|
InvalidMaskedCode, |
|
69
|
|
|
FilenameTooLong, |
|
70
|
|
|
InvalidImageLink, |
|
71
|
|
|
) |
|
72
|
|
|
|
|
73
|
1 |
|
templates_root = os.path.join(app.config['ROOT'], 'data', 'templates') |
|
74
|
1 |
|
template_store = stores.template.TemplateStore(templates_root) |
|
75
|
1 |
|
images_root = os.path.join(app.config['ROOT'], 'data', 'images') |
|
76
|
1 |
|
image_store = stores.image.ImageStore(images_root, app.config) |
|
77
|
|
|
|
|
78
|
1 |
|
app.link_service = services.link.LinkService( |
|
79
|
|
|
exceptions=exceptions, |
|
80
|
|
|
template_store=template_store, |
|
81
|
|
|
) |
|
82
|
1 |
|
app.template_service = services.template.TemplateService( |
|
83
|
|
|
exceptions=exceptions, |
|
84
|
|
|
template_store=template_store, |
|
85
|
|
|
) |
|
86
|
1 |
|
app.image_service = services.image.ImageService( |
|
87
|
|
|
exceptions=exceptions, |
|
88
|
|
|
template_store=template_store, |
|
89
|
|
|
image_store=image_store, |
|
90
|
|
|
) |
|
91
|
|
|
|
|
92
|
1 |
|
def log_request(response): |
|
93
|
1 |
|
if current_app.debug: |
|
94
|
1 |
|
path = request.path |
|
95
|
1 |
|
if request.args: |
|
96
|
1 |
|
path += "?%s" % unquote(urlencode(request.args)) |
|
97
|
1 |
|
log.info("%s: %s - %i", request.method, path, |
|
98
|
|
|
response.status_code) |
|
99
|
1 |
|
return response |
|
100
|
1 |
|
app.after_request(log_request) |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
1 |
|
def register_blueprints(app): |
|
104
|
1 |
|
app.register_blueprint(routes.aliases.blueprint) |
|
105
|
1 |
|
app.register_blueprint(routes.image.blueprint) |
|
106
|
1 |
|
app.register_blueprint(routes.index.blueprint) |
|
107
|
1 |
|
app.register_blueprint(routes.latest.blueprint) |
|
108
|
1 |
|
app.register_blueprint(routes.links.blueprint) |
|
109
|
1 |
|
app.register_blueprint(routes.magic.blueprint) |
|
110
|
1 |
|
app.register_blueprint(routes.root.blueprint) |
|
111
|
1 |
|
app.register_blueprint(routes.static.blueprint) |
|
112
|
|
|
app.register_blueprint(routes.templates.blueprint) |
|
113
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.