Coverage for src/pyrsslocal/rss/rss_flask.py: 0%
25 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-09-19 08:41 +0200
« prev ^ index » next coverage.py v7.1.0, created at 2023-09-19 08:41 +0200
1"""
2@file
3@brief Uses `Flask <http://flask.pocoo.org/>`_ to build pages.
4"""
5import os
6from flask import Flask, Response, render_template
8try:
9 from .rss_flask_helper import get_text_file, get_binary_file
10except ImportError:
11 from rss_flask_helper import get_text_file, get_binary_file
13# -- HELP BEGIN EXCLUDE --
15app = Flask(__name__)
16app.config.from_object(__name__)
18# -- HELP END EXCLUDE --
21# -- HELP BEGIN EXCLUDE --
23@app.route("/")
24# -- HELP END EXCLUDE --
25def main_page():
26 """
27 Serves the main page.
28 """
29 return get_resource("rss_reader.html")
31# -- HELP BEGIN EXCLUDE --
34@app.errorhandler(500)
35# -- HELP END EXCLUDE --
36def internal_error(error):
37 """
38 Intercepts an error.
39 """
40 return render_template("errors.html",
41 error=str(error),
42 message="Internal Error"), 500
44# -- HELP BEGIN EXCLUDE --
47@app.errorhandler(404)
48# -- HELP END EXCLUDE --
49def not_found(error):
50 """
51 Intercepts an error.
52 """
53 return render_template("errors.html",
54 error=str(error),
55 message="Not Found"), 404
57# -- HELP BEGIN EXCLUDE --
60@app.route('/js/', defaults={'path': ''})
61@app.route('/js/<path:path>')
62# -- HELP END EXCLUDE --
63def get_js(path): # pragma: no cover
64 """
65 Serves static files.
67 @param path relative path
68 @return content
69 """
70 try:
71 mimetypes = {
72 ".js": ("application/javascript", get_text_file),
73 }
74 ext = os.path.splitext(path)[1]
75 cp = mimetypes[ext]
76 mimetype = cp[0]
77 content = cp[1](os.path.join("..", "javascript", path))
78 r = Response(content, mimetype=mimetype)
79 return r
80 except Exception as e:
81 print(e)
82 raise e
84# -- HELP BEGIN EXCLUDE --
87@app.route('/logs/', defaults={'path': ''})
88@app.route('/logs/<path:path>')
89# -- HELP END EXCLUDE --
90def url_logging(path): # pragma: no cover
91 """
92 Serves static files.
94 @param path relative path
95 @return content
96 """
97 print("logging", path)
99# -- HELP BEGIN EXCLUDE --
102@app.route('/', defaults={'path': ''})
103@app.route('/<path:path>')
104# -- HELP END EXCLUDE --
105def get_resource(path): # pragma: no cover
106 """
107 Serves static files.
109 @param path relative path
110 @return content
111 """
112 try:
113 mimetypes = {
114 ".css": ("text/css", get_text_file),
115 ".html": ("text/html", get_text_file),
116 ".js": ("application/javascript", get_text_file),
117 ".png": ("image/png", get_binary_file),
118 }
119 ext = os.path.splitext(path)[1]
120 cp = mimetypes.get(ext, ("text/plain", get_binary_file))
121 mimetype = cp[0]
122 content = cp[1](path)
123 r = Response(content, mimetype=mimetype)
124 return r
125 except Exception as e:
126 print(e)
127 raise e
130# -- HELP BEGIN EXCLUDE --
132if __name__ == "__main__":
133 app.run()
135# -- HELP END EXCLUDE --