Skip to content

Commit 70c572b

Browse files
authored
Merge pull request #10 from mrf345/testing
Add logging option, and failsafe teardown.
2 parents bf41ef2 + 050f106 commit 70c572b

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

flask_gtts/about.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '0.17'
1+
__version__ = '0.18'
22
__doc__ = 'A Flask extension to help in generating Google Text-To-Speech files.'
33
__license__ = 'MIT'
44
__author__ = 'Mohamed Feddad'

flask_gtts/main.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class gtts(object):
1313
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):
1515
'''Extension to help in generating Google Text-To-Speech files.
1616
1717
Parameters
@@ -30,6 +30,8 @@ def __init__(self, app=None, temporary=True, tempdir='flask_gtts', route=False,
3030
Decorator to wrap route endpoint, by default None
3131
failsafe : bool, optional
3232
Failsafe or throw exceptions, by default False
33+
logging : bool, optional
34+
Enable or disable logging, by default True
3335
'''
3436
self.temporary = temporary
3537
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,
3840
self.route_decorator = route_decorator
3941
self.failsafe = failsafe
4042
self.files = {}
43+
self.logging = logging
4144

4245
app and self.init_app(app)
4346

@@ -62,11 +65,23 @@ def init_app(self, app):
6265
self.route and self.set_route()
6366
self.temporary and at_exit.register(self.teardown)
6467

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+
6577
def teardown(self):
6678
'''Remove the cache directory and its content.'''
6779
self.files = {}
6880

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)
7085

7186
def inject(self):
7287
'''Inject say and read into templates.'''
@@ -89,13 +104,6 @@ def say(self, lang='en-us', text='Flask says Hi!'):
89104
str
90105
Relative url of the generated TTS audio file.
91106
'''
92-
def _handle_exception(exception):
93-
if not self.failsafe:
94-
raise exception
95-
96-
self.app.logger.exception(exception)
97-
return ''
98-
99107
if (text, lang) not in self.files:
100108
generator = gTTS(text=text) if lang == 'skip' else gTTS(lang=lang, text=text)
101109
file_name = None
@@ -113,7 +121,7 @@ def _handle_exception(exception):
113121
try:
114122
generator.save(file_path)
115123
except Exception as e:
116-
return _handle_exception(e)
124+
return self._handle_exception(e)
117125

118126
file_name = os.path.basename(self.files.get((text, lang)))
119127
relative_dir = os.path.basename(self.tempdir)
@@ -123,7 +131,7 @@ def _handle_exception(exception):
123131
return url_for('static',
124132
filename=os.path.join(relative_dir, file_name))
125133
except Exception as e:
126-
return _handle_exception(e)
134+
return self._handle_exception(e)
127135

128136
def read(self, id='.toRead', mouseover=False):
129137
'''Read an HTML element inner text.

0 commit comments

Comments
 (0)