Coverage for src/pyrsslocal/rss/rss_flask.py: 0%

25 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2023-02-02 02:59 +0100

1""" 

2@file 

3@brief Uses `Flask <http://flask.pocoo.org/>`_ to build pages. 

4""" 

5import os 

6from flask import Flask, Response, render_template 

7 

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 

12 

13# -- HELP BEGIN EXCLUDE -- 

14 

15app = Flask(__name__) 

16app.config.from_object(__name__) 

17 

18# -- HELP END EXCLUDE -- 

19 

20 

21# -- HELP BEGIN EXCLUDE -- 

22 

23@app.route("/") 

24# -- HELP END EXCLUDE -- 

25def main_page(): 

26 """ 

27 Serves the main page. 

28 """ 

29 return get_resource("rss_reader.html") 

30 

31# -- HELP BEGIN EXCLUDE -- 

32 

33 

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 

43 

44# -- HELP BEGIN EXCLUDE -- 

45 

46 

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 

56 

57# -- HELP BEGIN EXCLUDE -- 

58 

59 

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. 

66 

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 

83 

84# -- HELP BEGIN EXCLUDE -- 

85 

86 

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. 

93 

94 @param path relative path 

95 @return content 

96 """ 

97 print("logging", path) 

98 

99# -- HELP BEGIN EXCLUDE -- 

100 

101 

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. 

108 

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 

128 

129 

130# -- HELP BEGIN EXCLUDE -- 

131 

132if __name__ == "__main__": 

133 app.run() 

134 

135# -- HELP END EXCLUDE --