From: Oleg Broytman Date: Mon, 25 Jul 2016 12:50:49 +0000 (+0300) Subject: Python 3 compatibility X-Git-Tag: 0.0.14~5 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=51aa2c71d5bebc4be0c98f00c97d2f47fcde4d3a;p=m_librarian.git Python 3 compatibility ml-search.py still doesn't work with Python 3 - it requires m_lib.defenc and m_lib hasn't been ported to Python 3 yet. --- diff --git a/m_librarian/compat.py b/m_librarian/compat.py new file mode 100644 index 0000000..a3f342f --- /dev/null +++ b/m_librarian/compat.py @@ -0,0 +1,11 @@ +import sys + +# Compatability definitions +PY2 = sys.version_info[0] < 3 +if PY2: + # disable flake8 checks on python 3 + string_type = basestring # noqa + unicode_type = unicode # noqa +else: + string_type = str + unicode_type = str diff --git a/m_librarian/config.py b/m_librarian/config.py index 2540ac4..e1ee7d9 100755 --- a/m_librarian/config.py +++ b/m_librarian/config.py @@ -2,7 +2,10 @@ from __future__ import print_function import os -from ConfigParser import RawConfigParser +try: + from ConfigParser import RawConfigParser +except ImportError: # py3 + from configparser import RawConfigParser __all__ = ['get_config'] diff --git a/m_librarian/db.py b/m_librarian/db.py index ecabfdb..42735af 100755 --- a/m_librarian/db.py +++ b/m_librarian/db.py @@ -5,6 +5,7 @@ import os from sqlobject import SQLObject, StringCol, UnicodeCol, IntCol, BoolCol, \ ForeignKey, DateCol, DatabaseIndex, RelatedJoin, \ connectionForURI, sqlhub, SQLObjectNotFound, dberrors +from .compat import string_type from .config import get_config __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language', @@ -156,7 +157,7 @@ def open_db(db_uri=None): if connection.dbName == 'sqlite': def lower(s): - if isinstance(s, basestring): + if isinstance(s, string_type): return s.lower() return s diff --git a/m_librarian/inp.py b/m_librarian/inp.py index a030ebc..e16df6c 100644 --- a/m_librarian/inp.py +++ b/m_librarian/inp.py @@ -72,6 +72,7 @@ def import_inp(archive, inp): Select(Book.q.file, Book.q.archive == archive))): files.add(file) for line in inp: + line = line.decode('utf-8') parts = split_line(line) file = parts[5] if file not in files: diff --git a/scripts/ml-search.py b/scripts/ml-search.py index 1aad934..bae79cb 100755 --- a/scripts/ml-search.py +++ b/scripts/ml-search.py @@ -6,6 +6,7 @@ import sys from sqlobject.sqlbuilder import CONCAT from m_lib.defenc import default_encoding +from m_librarian.compat import string_type from m_librarian.config import get_config from m_librarian.db import Author, Book, Extension, Genre, Language, open_db from m_librarian.download import download @@ -22,7 +23,7 @@ def _get_values(args, *columns): for column in columns: value = getattr(args, column) if value: - if isinstance(value, basestring): + if isinstance(value, string_type): value = unicode(value, default_encoding) values[column] = value return values @@ -30,7 +31,7 @@ def _get_values(args, *columns): def _guess_case_sensitivity(values): for value in values.values(): - if isinstance(value, basestring) and not value.islower(): + if isinstance(value, string_type) and not value.islower(): return True return False