From b70191ca2a350827a0d50855c2b62f8415bed42d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=AA=8E?= Date: Sun, 5 Mar 2017 17:40:39 +0800 Subject: [PATCH] Least change to adapt with python3 change some syntax - except clause - unicode -> bulitins.str - sqllite uri - fix import local path - 01 to 1 (0 is meaningless) add module - future - builtins (from future) - imp (python3 ) - past (from future) - sqlalchemy (update one) refer to http://python-future.org/compatible_idioms.html --- cps.py | 4 +++- cps/book_formats.py | 10 +++++----- cps/db.py | 3 ++- cps/fb2.py | 6 +++--- cps/helper.py | 18 ++++++++++++------ cps/ub.py | 3 ++- cps/web.py | 14 ++++++++++---- requirements.txt | 2 ++ 8 files changed, 39 insertions(+), 21 deletions(-) create mode 100644 requirements.txt diff --git a/cps.py b/cps.py index fdc5bce7..ed939c37 100755 --- a/cps.py +++ b/cps.py @@ -6,7 +6,9 @@ import sys base_path = os.path.dirname(os.path.abspath(__file__)) # Insert local directories into path -sys.path.insert(0, os.path.join(base_path, 'vendor')) +sys.path.append(base_path) +sys.path.append(os.path.join(base_path, 'cps')) +sys.path.append(os.path.join(base_path, 'vendor')) from cps import web from tornado.wsgi import WSGIContainer diff --git a/cps/book_formats.py b/cps/book_formats.py index 934638fc..3d13d778 100644 --- a/cps/book_formats.py +++ b/cps/book_formats.py @@ -14,28 +14,28 @@ try: from wand.image import Image from wand import version as ImageVersion use_generic_pdf_cover = False -except ImportError, e: +except ImportError as e: logger.warning('cannot import Image, generating pdf covers for pdf uploads will not work: %s', e) use_generic_pdf_cover = True try: from PyPDF2 import PdfFileReader from PyPDF2 import __version__ as PyPdfVersion use_pdf_meta = True -except ImportError, e: +except ImportError as e: logger.warning('cannot import PyPDF2, extracting pdf metadata will not work: %s', e) use_pdf_meta = False try: import epub use_epub_meta = True -except ImportError, e: +except ImportError as e: logger.warning('cannot import epub, extracting epub metadata will not work: %s', e) use_epub_meta = False try: import fb2 use_fb2_meta = True -except ImportError, e: +except ImportError as e: logger.warning('cannot import fb2, extracting fb2 metadata will not work: %s', e) use_fb2_meta = False @@ -48,7 +48,7 @@ def process(tmp_file_path, original_file_name, original_file_extension): return epub.get_epub_info(tmp_file_path, original_file_name, original_file_extension) if ".FB2" == original_file_extension.upper() and use_fb2_meta is True: return fb2.get_fb2_info(tmp_file_path, original_file_extension) - except Exception, e: + except Exception as e: logger.warning('cannot parse metadata, using default: %s', e) return default_meta(tmp_file_path, original_file_name, original_file_extension) diff --git a/cps/db.py b/cps/db.py index 9c48cfbb..9d8e5522 100755 --- a/cps/db.py +++ b/cps/db.py @@ -294,7 +294,8 @@ def setup_db(): return False dbpath = os.path.join(config.config_calibre_dir, "metadata.db") - engine = create_engine('sqlite:///{0}'.format(dbpath.encode('utf-8')), echo=False, isolation_level="SERIALIZABLE") + #engine = create_engine('sqlite:///{0}'.format(dbpath.encode('utf-8')), echo=False, isolation_level="SERIALIZABLE") + engine = create_engine('sqlite:///'+ dbpath, echo=False, isolation_level="SERIALIZABLE") try: conn = engine.connect() diff --git a/cps/fb2.py b/cps/fb2.py index 7f0bf2af..c68fc380 100644 --- a/cps/fb2.py +++ b/cps/fb2.py @@ -37,16 +37,16 @@ def get_fb2_info(tmp_file_path, original_file_extension): first_name = u'' return first_name + ' ' + middle_name + ' ' + last_name - author = unicode(", ".join(map(get_author, authors))) + author = str(", ".join(map(get_author, authors))) title = tree.xpath('/fb:FictionBook/fb:description/fb:title-info/fb:book-title/text()', namespaces=ns) if len(title): - title = unicode(title[0]) + title = str(title[0]) else: title = u'' description = tree.xpath('/fb:FictionBook/fb:description/fb:publish-info/fb:book-name/text()', namespaces=ns) if len(description): - description = unicode(description[0]) + description = str(description[0]) else: description = u'' diff --git a/cps/helper.py b/cps/helper.py index 54fa1946..1a6df917 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -13,11 +13,17 @@ import os import traceback import re import unicodedata -from StringIO import StringIO +try: + from StringIO import StringIO + from email.MIMEBase import MIMEBase + from email.MIMEMultipart import MIMEMultipart + from email.MIMEText import MIMEText +except ImportError: + from io import StringIO + from email.mime.base import MIMEBase + from email.mime.multipart import MIMEMultipart + from email.mime.text import MIMEText from email import encoders -from email.MIMEBase import MIMEBase -from email.MIMEMultipart import MIMEMultipart -from email.MIMEText import MIMEText from email.generator import Generator from email.utils import formatdate from email.utils import make_msgid @@ -147,7 +153,7 @@ def send_raw_email(kindle_mail, msg): smtplib.stderr = org_stderr - except (socket.error, smtplib.SMTPRecipientsRefused, smtplib.SMTPException), e: + except (socket.error, smtplib.SMTPRecipientsRefused, smtplib.SMTPException) as e: app.logger.error(traceback.print_exc()) return _("Failed to send mail: %s" % str(e)) @@ -239,7 +245,7 @@ def get_valid_filename(value, replace_whitespace=True): value=value.replace(u'ß',u'ss') value = unicodedata.normalize('NFKD', value) re_slugify = re.compile('[\W\s-]', re.UNICODE) - value = unicode(re_slugify.sub('', value).strip()) + value = str(re_slugify.sub('', value).strip()) if replace_whitespace: #*+:\"/<>? werden durch _ ersetzt value = re.sub('[\*\+:\\\"/<>\?]+', u'_', value, flags=re.U) diff --git a/cps/ub.py b/cps/ub.py index 653454ce..659106cb 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -10,6 +10,7 @@ import os import logging from werkzeug.security import generate_password_hash from flask_babel import gettext as _ +from builtins import str dbpath = os.path.join(os.path.normpath(os.path.dirname(os.path.realpath(__file__)) + os.sep + ".." + os.sep), "app.db") engine = create_engine('sqlite:///{0}'.format(dbpath), echo=False) @@ -90,7 +91,7 @@ class UserBase: return False def get_id(self): - return unicode(self.id) + return str(self.id) def filter_language(self): return self.default_language diff --git a/cps/web.py b/cps/web.py index 5687d604..19f23593 100755 --- a/cps/web.py +++ b/cps/web.py @@ -48,11 +48,17 @@ from shutil import move, copyfile from tornado.ioloop import IOLoop from tornado import version as tornadoVersion +try: + from imp import reload + from past.builtins import xrange +except: + pass + try: from wand.image import Image use_generic_pdf_cover = False -except ImportError, e: +except ImportError as e: use_generic_pdf_cover = True from cgi import escape @@ -2186,12 +2192,12 @@ def upload(): return redirect(url_for('index')) try: copyfile(meta.file_path, saved_filename) - except OSError, e: + except OSError as e: flash(_(u"Failed to store file %s (Permission denied)." % saved_filename), category="error") return redirect(url_for('index')) try: os.unlink(meta.file_path) - except OSError, e: + except OSError as e: flash(_(u"Failed to delete file %s (Permission denied)." % meta.file_path), category="warning") file_size = os.path.getsize(saved_filename) @@ -2223,7 +2229,7 @@ def upload(): db.session.add(db_language) # combine path and normalize path from windows systems path = os.path.join(author_dir, title_dir).replace('\\','/') - db_book = db.Books(title, "", db_author.sort, datetime.datetime.now(), datetime.datetime(101, 01, 01), 1, + db_book = db.Books(title, "", db_author.sort, datetime.datetime.now(), datetime.datetime(101, 1, 1), 1, datetime.datetime.now(), path, has_cover, db_author, [], db_language) db_book.authors.append(db_author) if db_language is not None: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..b6d2560e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +future +sqlalchemy