This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss_flask.py
135 lines (106 loc) · 2.99 KB
/
rss_flask.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
@file
@brief Uses `Flask <http://flask.pocoo.org/>`_ to build pages.
"""
import os
from flask import Flask, Response, render_template
try:
from .rss_flask_helper import get_text_file, get_binary_file
except ImportError:
from rss_flask_helper import get_text_file, get_binary_file
# -- HELP BEGIN EXCLUDE --
app = Flask(__name__)
app.config.from_object(__name__)
# -- HELP END EXCLUDE --
# -- HELP BEGIN EXCLUDE --
@app.route("/")
# -- HELP END EXCLUDE --
def main_page():
"""
Serves the main page.
"""
return get_resource("rss_reader.html")
# -- HELP BEGIN EXCLUDE --
@app.errorhandler(500)
# -- HELP END EXCLUDE --
def internal_error(error):
"""
Intercepts an error.
"""
return render_template("errors.html",
error=str(error),
message="Internal Error"), 500
# -- HELP BEGIN EXCLUDE --
@app.errorhandler(404)
# -- HELP END EXCLUDE --
def not_found(error):
"""
Intercepts an error.
"""
return render_template("errors.html",
error=str(error),
message="Not Found"), 404
# -- HELP BEGIN EXCLUDE --
@app.route('/js/', defaults={'path': ''})
@app.route('/js/<path:path>')
# -- HELP END EXCLUDE --
def get_js(path): # pragma: no cover
"""
Serves static files.
@param path relative path
@return content
"""
try:
mimetypes = {
".js": ("application/javascript", get_text_file),
}
ext = os.path.splitext(path)[1]
cp = mimetypes[ext]
mimetype = cp[0]
content = cp[1](os.path.join("..", "javascript", path))
r = Response(content, mimetype=mimetype)
return r
except Exception as e:
print(e)
raise e
# -- HELP BEGIN EXCLUDE --
@app.route('/logs/', defaults={'path': ''})
@app.route('/logs/<path:path>')
# -- HELP END EXCLUDE --
def url_logging(path): # pragma: no cover
"""
Serves static files.
@param path relative path
@return content
"""
print("logging", path)
# -- HELP BEGIN EXCLUDE --
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
# -- HELP END EXCLUDE --
def get_resource(path): # pragma: no cover
"""
Serves static files.
@param path relative path
@return content
"""
try:
mimetypes = {
".css": ("text/css", get_text_file),
".html": ("text/html", get_text_file),
".js": ("application/javascript", get_text_file),
".png": ("image/png", get_binary_file),
}
ext = os.path.splitext(path)[1]
cp = mimetypes.get(ext, ("text/plain", get_binary_file))
mimetype = cp[0]
content = cp[1](path)
r = Response(content, mimetype=mimetype)
return r
except Exception as e:
print(e)
raise e
# -- HELP BEGIN EXCLUDE --
if __name__ == "__main__":
app.run()
# -- HELP END EXCLUDE --