From 061a45aba1f91ec167eb9ac283a4c0a0afb52798 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 23 Jul 2016 02:55:24 +0300 Subject: [PATCH] Update code for compatibility with Python 3 It's still not tested under Python 3. --- m_librarian/config.py | 5 +-- m_librarian/db.py | 7 +++-- m_librarian/download.py | 3 +- m_librarian/glst.py | 3 +- scripts/ml-initdb.py | 5 +-- scripts/ml-search.py | 68 ++++++++++++++++++++++------------------- tests/__init__.py | 2 +- tests/run_all.py | 3 +- 8 files changed, 53 insertions(+), 43 deletions(-) diff --git a/m_librarian/config.py b/m_librarian/config.py index fed06ae..2540ac4 100755 --- a/m_librarian/config.py +++ b/m_librarian/config.py @@ -1,5 +1,6 @@ #! /usr/bin/env python +from __future__ import print_function import os from ConfigParser import RawConfigParser @@ -50,8 +51,8 @@ def get_config(config_path=None): def test(): config_dirs = find_config_dirs() - print "Config dirs:", config_dirs - print "Config file:", find_config_file(config_dirs) + print("Config dirs:", config_dirs) + print("Config file:", find_config_file(config_dirs)) if __name__ == '__main__': test() diff --git a/m_librarian/db.py b/m_librarian/db.py index e7eea99..ecabfdb 100755 --- a/m_librarian/db.py +++ b/m_librarian/db.py @@ -1,5 +1,6 @@ #! /usr/bin/env python +from __future__ import print_function import os from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \ ForeignKey, DateCol, DatabaseIndex, RelatedJoin, \ @@ -130,7 +131,7 @@ def find_sqlite_dburi(db_dirs=None): else: # octal; -rw-------; # make the database file/directory readable/writeable only by the user - os.umask(0066) + os.umask(0o66) db_dir = db_dirs[0] try: os.makedirs(db_dir) @@ -220,8 +221,8 @@ def update_counters(): def test(): db_dirs = find_sqlite_db_dirs() - print "DB dirs:", db_dirs - print "DB URI:", find_sqlite_dburi() + print("DB dirs:", db_dirs) + print("DB URI:", find_sqlite_dburi()) if __name__ == '__main__': test() diff --git a/m_librarian/download.py b/m_librarian/download.py index 8e309cb..6f4daf4 100755 --- a/m_librarian/download.py +++ b/m_librarian/download.py @@ -1,5 +1,6 @@ #! /usr/bin/env python +from __future__ import print_function import os from time import mktime from shutil import copyfileobj @@ -108,7 +109,7 @@ def download(book, path=None, a_format=None): def test(): _compile_format() - print compiled_format + print(compiled_format) if __name__ == '__main__': test() diff --git a/m_librarian/glst.py b/m_librarian/glst.py index a4ac27d..f639c1b 100755 --- a/m_librarian/glst.py +++ b/m_librarian/glst.py @@ -1,5 +1,6 @@ #! /usr/bin/env python +from __future__ import print_function import codecs import os from sqlobject import sqlhub, SQLObjectNotFound @@ -58,7 +59,7 @@ def import_glst(): def test(): ml_dir = os.path.dirname(__file__) - print parse_glst_file(os.path.join(ml_dir, 'glst', 'genres_fb2.glst')) + print(parse_glst_file(os.path.join(ml_dir, 'glst', 'genres_fb2.glst'))) if __name__ == '__main__': test() diff --git a/scripts/ml-initdb.py b/scripts/ml-initdb.py index 0d4e73b..6535d45 100755 --- a/scripts/ml-initdb.py +++ b/scripts/ml-initdb.py @@ -1,5 +1,6 @@ #! /usr/bin/env python +from __future__ import print_function import argparse from m_librarian.config import get_config from m_librarian.db import open_db, init_db @@ -18,6 +19,6 @@ if __name__ == '__main__': init_db() old, new = import_glst() if old: - print "Imported %d genres (ignored %d existing)" % (new, old) + print("Imported %d genres (ignored %d existing)" % (new, old)) else: - print "Imported %d genres" % new + print("Imported %d genres" % new) diff --git a/scripts/ml-search.py b/scripts/ml-search.py index bd5df8c..1aad934 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -1,5 +1,6 @@ #! /usr/bin/env python +from __future__ import print_function import argparse import sys from sqlobject.sqlbuilder import CONCAT @@ -35,7 +36,7 @@ def _guess_case_sensitivity(values): def print_count(count): - print _("Found").encode(default_encoding), ":", count + print(_("Found").encode(default_encoding), ":", count) def _search_authors(case_sensitive, search_type, args): @@ -63,11 +64,11 @@ def _search_authors(case_sensitive, search_type, args): return count = 0 for author in authors: - print author.fullname.encode(default_encoding), \ - (u"(%s: %d)" % (_('books'), author.count))\ - .encode(default_encoding), + print(author.fullname.encode(default_encoding), + (u"(%s: %d)" % (_('books'), author.count)) + .encode(default_encoding), end='') if args.verbose >= 1: - print "(id=%d)" % author.id, + print("(id=%d)" % author.id, end='') print count += 1 print_count(count) @@ -157,38 +158,38 @@ def _search_books(case_sensitive, search_type, args): sys.exit(1) count = 0 for book in books: - print book.title.encode(default_encoding), + print(book.title.encode(default_encoding), end='') if args.verbose >= 1: - print "(id=%d)" % book.id, + print("(id=%d)" % book.id, end='') print if args.verbose >= 1: - print " ", _("Author(s)").encode(default_encoding), ":", + print(" ", _("Author(s)").encode(default_encoding), ":", end='') for author in book.authors: - print author.fullname.encode(default_encoding), + print(author.fullname.encode(default_encoding), end='') print - print " ", _("Genre(s)").encode(default_encoding), ":", + print(" ", _("Genre(s)").encode(default_encoding), ":", end='') for genre in book.genres: - print (genre.title or genre.name).encode(default_encoding), + print((genre.title or genre.name).encode(default_encoding), end='') print if book.series: - print " ", _("Series").encode(default_encoding), ":", - print book.series.encode(default_encoding), \ - "(%d)" % book.ser_no + print(" ", _("Series").encode(default_encoding), ":", end='') + print(book.series.encode(default_encoding), + "(%d)" % book.ser_no) if args.verbose >= 2: - print " ", _("Date").encode(default_encoding), ":", book.date - print " ", _("Language").encode(default_encoding), ":", \ - book.language.name.encode(default_encoding) + print(" ", _("Date").encode(default_encoding), ":", book.date) + print(" ", _("Language").encode(default_encoding), ":", + book.language.name.encode(default_encoding)) if args.verbose >= 3: - print " ", _("Archive").encode(default_encoding), ":", book.archive - print " ", _("File").encode(default_encoding), ":", book.file - print " ", _("Extension").encode(default_encoding), ":", \ - book.extension.name.encode(default_encoding) - print " ", _("Size").encode(default_encoding), ":", \ - book.size, _("bytes").encode(default_encoding) - print " ", _("Deleted").encode(default_encoding), ":", \ - _(str(book.deleted)).encode(default_encoding) + print(" ", _("Archive").encode(default_encoding), ":", book.archive) + print(" ", _("File").encode(default_encoding), ":", book.file) + print(" ", _("Extension").encode(default_encoding), ":", + book.extension.name.encode(default_encoding)) + print(" ", _("Size").encode(default_encoding), ":", + book.size, _("bytes").encode(default_encoding)) + print(" ", _("Deleted").encode(default_encoding), ":", + _(str(book.deleted)).encode(default_encoding)) if args.get or args.get_many: download(book, args.path, args.format) count += 1 @@ -208,10 +209,11 @@ def _search_extensions(case_sensitive, search_type, args): return count = 0 for ext in extensions: - print ext.name.encode(default_encoding), \ + print(ext.name.encode(default_encoding), (u"(%s: %d)" % (_('books'), ext.count)).encode(default_encoding), + end='') if args.verbose >= 1: - print "(id=%d)" % ext.id, + print("(id=%d)" % ext.id, end='') print count += 1 print_count(count) @@ -229,10 +231,11 @@ def _search_genres(case_sensitive, search_type, args): for genre in genres: names = filter(None, (genre.name, genre.title)) fullname = u' '.join(names) - print fullname.encode(default_encoding), \ + print(fullname.encode(default_encoding), (u"(%s: %d)" % (_('books'), genre.count)).encode(default_encoding), + end='') if args.verbose >= 1: - print "(id=%d)" % genre.id, + print("(id=%d)" % genre.id, end='') print count += 1 print_count(count) @@ -251,10 +254,11 @@ def _search_languages(case_sensitive, search_type, args): return count = 0 for lang in languages: - print lang.name.encode(default_encoding), \ + print(lang.name.encode(default_encoding), (u"(%s: %d)" % (_('books'), lang.count)).encode(default_encoding), + end='') if args.verbose >= 1: - print "(id=%d)" % lang.id, + print("(id=%d)" % lang.id, end='') print count += 1 print_count(count) @@ -311,7 +315,7 @@ if __name__ == '__main__': parser.add_argument('--lid', type=int, help='search by language\'s id') parser.add_argument('-P', '--path', help='path to the library archives') parser.add_argument('-F', '--format', - help='download format, default is %f') + help='download format, default is %%f') parser.add_argument('--get', action='store_true', help='download exactly one book') parser.add_argument('--get-many', type=int, diff --git a/tests/__init__.py b/tests/__init__.py index e40219a..e55661f 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -29,7 +29,7 @@ class TestCase(unittest.TestCase): def main(): try: unittest.main(testRunner=unittest.TextTestRunner()) - except SystemExit, msg: + except SystemExit as msg: result = msg.args[0] else: result = 0 diff --git a/tests/run_all.py b/tests/run_all.py index 2e217bf..c04d9ed 100755 --- a/tests/run_all.py +++ b/tests/run_all.py @@ -1,6 +1,7 @@ #! /usr/bin/env python +from __future__ import print_function import os import sys import subprocess @@ -31,7 +32,7 @@ def main(): os.environ["PYTHONPATH"] = os.curdir for test in sorted(tests): - print test + print(test) subprocess.call((sys.executable, '-m', test)) if __name__ == '__main__': -- 2.39.5