Change exception from legacy code.

Change all except: clause to except Exception as e:  for further revise.
This commit is contained in:
林檎 2017-03-06 11:53:17 +08:00
parent 0bc4f52783
commit 6e3d4877ed
4 changed files with 24 additions and 25 deletions

View File

@ -299,7 +299,7 @@ def setup_db():
try: try:
conn = engine.connect() conn = engine.connect()
except: except Exception as e:
content = ub.session.query(ub.Settings).first() content = ub.session.query(ub.Settings).first()
content.config_calibre_dir = None content.config_calibre_dir = None
content.db_configured = False content.db_configured = False

View File

@ -38,7 +38,7 @@ from tornado.ioloop import IOLoop
try: try:
import unidecode import unidecode
use_unidecode=True use_unidecode=True
except: except Exception as e:
use_unidecode=False use_unidecode=False
# Global variables # Global variables
@ -249,7 +249,6 @@ def get_valid_filename(value, replace_whitespace=True):
value = str(re_slugify.sub('', value).strip()) value = str(re_slugify.sub('', value).strip())
except UnicodeEncodeError as e: #will exception on Python2.7 except UnicodeEncodeError as e: #will exception on Python2.7
value = unicode(re_slugify.sub('', value).strip()) value = unicode(re_slugify.sub('', value).strip())
raise
if replace_whitespace: if replace_whitespace:
#*+:\"/<>? werden durch _ ersetzt #*+:\"/<>? werden durch _ ersetzt
value = re.sub('[\*\+:\\\"/<>\?]+', u'_', value, flags=re.U) value = re.sub('[\*\+:\\\"/<>\?]+', u'_', value, flags=re.U)
@ -389,7 +388,7 @@ class Updater(threading.Thread):
try: try:
os.chown(dst_file, permission.st_uid, permission.st_uid) os.chown(dst_file, permission.st_uid, permission.st_uid)
# print('Permissions: User '+str(new_permissions.st_uid)+' Group '+str(new_permissions.st_uid)) # print('Permissions: User '+str(new_permissions.st_uid)+' Group '+str(new_permissions.st_uid))
except: except Exception as e:
e = sys.exc_info() e = sys.exc_info()
logging.getLogger('cps.web').debug('Fail '+str(dst_file)+' error: '+str(e)) logging.getLogger('cps.web').debug('Fail '+str(dst_file)+' error: '+str(e))
return return
@ -431,7 +430,7 @@ class Updater(threading.Thread):
logging.getLogger('cps.web').debug("Delete file " + item_path) logging.getLogger('cps.web').debug("Delete file " + item_path)
log_from_thread("Delete file " + item_path) log_from_thread("Delete file " + item_path)
os.remove(item_path) os.remove(item_path)
except: except Exception as e:
logging.getLogger('cps.web').debug("Could not remove:" + item_path) logging.getLogger('cps.web').debug("Could not remove:" + item_path)
shutil.rmtree(source, ignore_errors=True) shutil.rmtree(source, ignore_errors=True)

View File

@ -439,7 +439,7 @@ def create_anonymous_user():
session.add(user) session.add(user)
try: try:
session.commit() session.commit()
except: except Exception as e:
session.rollback() session.rollback()
pass pass
@ -457,7 +457,7 @@ def create_admin_user():
session.add(user) session.add(user)
try: try:
session.commit() session.commit()
except: except Exception as e:
session.rollback() session.rollback()
pass pass

View File

@ -287,7 +287,7 @@ def shortentitle_filter(s):
def mimetype_filter(val): def mimetype_filter(val):
try: try:
s = mimetypes.types_map['.' + val] s = mimetypes.types_map['.' + val]
except: except Exception as e:
s = 'application/octet-stream' s = 'application/octet-stream'
return s return s
@ -762,7 +762,7 @@ def get_updater_status():
elif request.method == "GET": elif request.method == "GET":
try: try:
status['status']=helper.updater_thread.get_update_status() status['status']=helper.updater_thread.get_update_status()
except: except Exception as e:
status['status'] = 7 status['status'] = 7
return json.dumps(status) return json.dumps(status)
@ -777,7 +777,7 @@ def get_languages_json():
try: try:
cur_l = LC.parse(lang.lang_code) cur_l = LC.parse(lang.lang_code)
lang.name = cur_l.get_language_name(get_locale()) lang.name = cur_l.get_language_name(get_locale())
except: except Exception as e:
lang.name = _(isoLanguages.get(part3=lang.lang_code).name) lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
entries = [s for s in languages if query in s.name.lower()] entries = [s for s in languages if query in s.name.lower()]
json_dumps = json.dumps([dict(name=r.name) for r in entries]) json_dumps = json.dumps([dict(name=r.name) for r in entries])
@ -937,13 +937,13 @@ def language_overview():
try: try:
cur_l = LC.parse(lang.lang_code) cur_l = LC.parse(lang.lang_code)
lang.name = cur_l.get_language_name(get_locale()) lang.name = cur_l.get_language_name(get_locale())
except: except Exception as e:
lang.name = _(isoLanguages.get(part3=lang.lang_code).name) lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
else: else:
try: try:
langfound = 1 langfound = 1
cur_l = LC.parse(current_user.filter_language()) cur_l = LC.parse(current_user.filter_language())
except: except Exception as e:
langfound = 0 langfound = 0
languages = db.session.query(db.Languages).filter( languages = db.session.query(db.Languages).filter(
db.Languages.lang_code == current_user.filter_language()).all() db.Languages.lang_code == current_user.filter_language()).all()
@ -967,7 +967,7 @@ def language(name, page):
try: try:
cur_l = LC.parse(name) cur_l = LC.parse(name)
name = cur_l.get_language_name(get_locale()) name = cur_l.get_language_name(get_locale())
except: except Exception as e:
name = _(isoLanguages.get(part3=name).name) name = _(isoLanguages.get(part3=name).name)
return render_title_template('index.html', random=random, entries=entries, pagination=pagination, return render_title_template('index.html', random=random, entries=entries, pagination=pagination,
title=_(u"Language: %(name)s", name=name)) title=_(u"Language: %(name)s", name=name))
@ -1011,7 +1011,7 @@ def show_book(id):
try: try:
entries.languages[index].language_name = LC.parse(entries.languages[index].lang_code).get_language_name( entries.languages[index].language_name = LC.parse(entries.languages[index].lang_code).get_language_name(
get_locale()) get_locale())
except: except Exception as e:
entries.languages[index].language_name = _( entries.languages[index].language_name = _(
isoLanguages.get(part3=entries.languages[index].lang_code).name) isoLanguages.get(part3=entries.languages[index].lang_code).name)
cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all()
@ -1155,7 +1155,7 @@ def advanced_search():
try: try:
cur_l = LC.parse(lang.lang_code) cur_l = LC.parse(lang.lang_code)
lang.name = cur_l.get_language_name(get_locale()) lang.name = cur_l.get_language_name(get_locale())
except: except Exception as e:
lang.name = _(isoLanguages.get(part3=lang.lang_code).name) lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
searchterm.extend(language.name for language in language_names) searchterm.extend(language.name for language in language_names)
searchterm = " + ".join(filter(None, searchterm)) searchterm = " + ".join(filter(None, searchterm))
@ -1187,7 +1187,7 @@ def advanced_search():
try: try:
cur_l = LC.parse(lang.lang_code) cur_l = LC.parse(lang.lang_code)
lang.name = cur_l.get_language_name(get_locale()) lang.name = cur_l.get_language_name(get_locale())
except: except Exception as e:
lang.name = _(isoLanguages.get(part3=lang.lang_code).name) lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
else: else:
languages = None languages = None
@ -1289,7 +1289,7 @@ def get_download_link(book_id, format):
send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + format)) send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + format))
try: try:
response.headers["Content-Type"] = mimetypes.types_map['.' + format] response.headers["Content-Type"] = mimetypes.types_map['.' + format]
except: except Exception as e:
pass pass
response.headers["Content-Disposition"] = "attachment; filename=\"%s.%s\"" % (quote(file_name.encode('utf-8')), format) response.headers["Content-Disposition"] = "attachment; filename=\"%s.%s\"" % (quote(file_name.encode('utf-8')), format)
return response return response
@ -1321,7 +1321,7 @@ def register():
try: try:
ub.session.add(content) ub.session.add(content)
ub.session.commit() ub.session.commit()
except: except Exception as e:
ub.session.rollback() ub.session.rollback()
flash(_(u"An unknown error occured. Please try again later."), category="error") flash(_(u"An unknown error occured. Please try again later."), category="error")
return render_title_template('register.html', title=_(u"register")) return render_title_template('register.html', title=_(u"register"))
@ -1446,7 +1446,7 @@ def create_shelf():
ub.session.add(shelf) ub.session.add(shelf)
ub.session.commit() ub.session.commit()
flash(_(u"Shelf %(title)s created", title=to_save["title"]), category="success") flash(_(u"Shelf %(title)s created", title=to_save["title"]), category="success")
except: except Exception as e:
flash(_(u"There was an error"), category="error") flash(_(u"There was an error"), category="error")
return render_title_template('shelf_edit.html', shelf=shelf, title=_(u"create a shelf")) return render_title_template('shelf_edit.html', shelf=shelf, title=_(u"create a shelf"))
else: else:
@ -1474,7 +1474,7 @@ def edit_shelf(shelf_id):
try: try:
ub.session.commit() ub.session.commit()
flash(_(u"Shelf %(title)s changed", title=to_save["title"]), category="success") flash(_(u"Shelf %(title)s changed", title=to_save["title"]), category="success")
except: except Exception as e:
flash(_(u"There was an error"), category="error") flash(_(u"There was an error"), category="error")
return render_title_template('shelf_edit.html', shelf=shelf, title=_(u"Edit a shelf")) return render_title_template('shelf_edit.html', shelf=shelf, title=_(u"Edit a shelf"))
else: else:
@ -1562,7 +1562,7 @@ def profile():
try: try:
cur_l = LC.parse(lang.lang_code) cur_l = LC.parse(lang.lang_code)
lang.name = cur_l.get_language_name(get_locale()) lang.name = cur_l.get_language_name(get_locale())
except: except Exception as e:
lang.name = _(isoLanguages.get(part3=lang.lang_code).name) lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
translations = babel.list_translations() + [LC('en')] translations = babel.list_translations() + [LC('en')]
for book in content.downloads: for book in content.downloads:
@ -1738,7 +1738,7 @@ def new_user():
try: try:
cur_l = LC.parse(lang.lang_code) cur_l = LC.parse(lang.lang_code)
lang.name = cur_l.get_language_name(get_locale()) lang.name = cur_l.get_language_name(get_locale())
except: except Exception as e:
lang.name = _(isoLanguages.get(part3=lang.lang_code).name) lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
translations = [LC('en')] + babel.list_translations() translations = [LC('en')] + babel.list_translations()
if request.method == "POST": if request.method == "POST":
@ -1836,7 +1836,7 @@ def edit_user(user_id):
try: try:
cur_l = LC.parse(lang.lang_code) cur_l = LC.parse(lang.lang_code)
lang.name = cur_l.get_language_name(get_locale()) lang.name = cur_l.get_language_name(get_locale())
except: except Exception as e:
lang.name = _(isoLanguages.get(part3=lang.lang_code).name) lang.name = _(isoLanguages.get(part3=lang.lang_code).name)
translations = babel.list_translations() + [LC('en')] translations = babel.list_translations() + [LC('en')]
for book in content.downloads: for book in content.downloads:
@ -1958,7 +1958,7 @@ def edit_book(book_id):
try: try:
book.languages[index].language_name = LC.parse(book.languages[index].lang_code).get_language_name( book.languages[index].language_name = LC.parse(book.languages[index].lang_code).get_language_name(
get_locale()) get_locale())
except: except Exception as e:
book.languages[index].language_name = _(isoLanguages.get(part3=book.languages[index].lang_code).name) book.languages[index].language_name = _(isoLanguages.get(part3=book.languages[index].lang_code).name)
for author in book.authors: for author in book.authors:
author_names.append(author.name) author_names.append(author.name)
@ -2008,7 +2008,7 @@ def edit_book(book_id):
for lang in languages: for lang in languages:
try: try:
lang.name = LC.parse(lang.lang_code).get_language_name(get_locale()).lower() lang.name = LC.parse(lang.lang_code).get_language_name(get_locale()).lower()
except: except Exception as e:
lang.name = _(isoLanguages.get(part3=lang.lang_code).name).lower() lang.name = _(isoLanguages.get(part3=lang.lang_code).name).lower()
for inp_lang in input_languages: for inp_lang in input_languages:
if inp_lang == lang.name: if inp_lang == lang.name: