11
11
12
12
class gtts (object ):
13
13
def __init__ (self , app = None , temporary = True , tempdir = 'flask_gtts' , route = False ,
14
- route_path = '/gtts' , route_decorator = None , failsafe = False ):
14
+ route_path = '/gtts' , route_decorator = None , failsafe = False , logging = True ):
15
15
'''Extension to help in generating Google Text-To-Speech files.
16
16
17
17
Parameters
@@ -30,6 +30,8 @@ def __init__(self, app=None, temporary=True, tempdir='flask_gtts', route=False,
30
30
Decorator to wrap route endpoint, by default None
31
31
failsafe : bool, optional
32
32
Failsafe or throw exceptions, by default False
33
+ logging : bool, optional
34
+ Enable or disable logging, by default True
33
35
'''
34
36
self .temporary = temporary
35
37
self .tempdir = 'flask_gtts' if not tempdir or tempdir .startswith ('/' ) else tempdir
@@ -38,6 +40,7 @@ def __init__(self, app=None, temporary=True, tempdir='flask_gtts', route=False,
38
40
self .route_decorator = route_decorator
39
41
self .failsafe = failsafe
40
42
self .files = {}
43
+ self .logging = logging
41
44
42
45
app and self .init_app (app )
43
46
@@ -62,11 +65,23 @@ def init_app(self, app):
62
65
self .route and self .set_route ()
63
66
self .temporary and at_exit .register (self .teardown )
64
67
68
+ def _handle_exception (self , exception ):
69
+ if not self .failsafe :
70
+ raise exception
71
+
72
+ if self .logging :
73
+ self .app .logger .exception (exception )
74
+
75
+ return ''
76
+
65
77
def teardown (self ):
66
78
'''Remove the cache directory and its content.'''
67
79
self .files = {}
68
80
69
- os .path .isdir (self .tempdir ) and rmtree (self .tempdir )
81
+ try :
82
+ os .path .isdir (self .tempdir ) and rmtree (self .tempdir )
83
+ except Exception as e :
84
+ self ._handle_exception (e )
70
85
71
86
def inject (self ):
72
87
'''Inject say and read into templates.'''
@@ -89,13 +104,6 @@ def say(self, lang='en-us', text='Flask says Hi!'):
89
104
str
90
105
Relative url of the generated TTS audio file.
91
106
'''
92
- def _handle_exception (exception ):
93
- if not self .failsafe :
94
- raise exception
95
-
96
- self .app .logger .exception (exception )
97
- return ''
98
-
99
107
if (text , lang ) not in self .files :
100
108
generator = gTTS (text = text ) if lang == 'skip' else gTTS (lang = lang , text = text )
101
109
file_name = None
@@ -113,7 +121,7 @@ def _handle_exception(exception):
113
121
try :
114
122
generator .save (file_path )
115
123
except Exception as e :
116
- return _handle_exception (e )
124
+ return self . _handle_exception (e )
117
125
118
126
file_name = os .path .basename (self .files .get ((text , lang )))
119
127
relative_dir = os .path .basename (self .tempdir )
@@ -123,7 +131,7 @@ def _handle_exception(exception):
123
131
return url_for ('static' ,
124
132
filename = os .path .join (relative_dir , file_name ))
125
133
except Exception as e :
126
- return _handle_exception (e )
134
+ return self . _handle_exception (e )
127
135
128
136
def read (self , id = '.toRead' , mouseover = False ):
129
137
'''Read an HTML element inner text.
0 commit comments