Remove PY2 checks

This commit is contained in:
Labrys of Knossos 2022-12-03 00:49:53 -05:00
commit 683411bfe1
3 changed files with 31 additions and 108 deletions

View file

@ -37,9 +37,6 @@ CONFIG_TV_FILE = os.path.join(APP_ROOT, 'autoProcessTv.cfg')
TEST_FILE = os.path.join(APP_ROOT, 'tests', 'test.mp4') TEST_FILE = os.path.join(APP_ROOT, 'tests', 'test.mp4')
MYAPP = None MYAPP = None
import six
from six.moves import reload_module
from core import logger, main_db, version_check, databases, transcoder from core import logger, main_db, version_check, databases, transcoder
from core.configuration import config from core.configuration import config
from core.plugins.downloaders.configuration import ( from core.plugins.downloaders.configuration import (
@ -305,23 +302,6 @@ def configure_locale():
if not SYS_ENCODING or SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'): if not SYS_ENCODING or SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
SYS_ENCODING = 'UTF-8' SYS_ENCODING = 'UTF-8'
if six.PY2:
if not hasattr(sys, 'setdefaultencoding'):
reload_module(sys)
try:
# pylint: disable=E1101
# On non-unicode builds this will raise an AttributeError, if encoding type is not valid it throws a LookupError
sys.setdefaultencoding(SYS_ENCODING)
except Exception:
print('Sorry, you MUST add the nzbToMedia folder to the PYTHONPATH environment variable'
'\nor find another way to force Python to use {codec} for string encoding.'.format
(codec=SYS_ENCODING))
if 'NZBOP_SCRIPTDIR' in os.environ:
sys.exit(NZBGET_POSTPROCESS_ERROR)
else:
sys.exit(1)
def configure_migration(): def configure_migration():
global CONFIG_FILE global CONFIG_FILE

View file

@ -2,36 +2,11 @@ import re
import sqlite3 import sqlite3
import time import time
from six import text_type, PY2 from six import text_type
import core import core
from core import logger from core import logger
if PY2:
class Row(sqlite3.Row, object):
"""
Row factory that uses Byte Strings for keys.
The sqlite3.Row in Python 2 does not support unicode keys.
This overrides __getitem__ to attempt to encode the key to bytes first.
"""
def __getitem__(self, item):
"""
Get an item from the row by index or key.
:param item: Index or Key of item to return.
:return: An item from the sqlite3.Row.
"""
try:
# sqlite3.Row column names should be Bytes in Python 2
item = item.encode()
except AttributeError:
pass # assume item is a numeric index
return super(Row, self).__getitem__(item)
else:
from sqlite3 import Row
def db_filename(filename='nzbtomedia.db', suffix=None): def db_filename(filename='nzbtomedia.db', suffix=None):
""" """
@ -53,7 +28,7 @@ class DBConnection:
self.filename = filename self.filename = filename
self.connection = sqlite3.connect(db_filename(filename), 20) self.connection = sqlite3.connect(db_filename(filename), 20)
self.connection.row_factory = Row self.connection.row_factory = sqlite3.Row
def check_db_version(self): def check_db_version(self):
result = None result = None

View file

@ -1,14 +1,11 @@
import os import os
from builtins import bytes
from six import text_type from six import text_type
from six import PY2
import core import core
from core import logger from core import logger
if not PY2:
from builtins import bytes
def char_replace(name_in): def char_replace(name_in):
# Special character hex range: # Special character hex range:
@ -21,66 +18,37 @@ def char_replace(name_in):
encoding = None encoding = None
if isinstance(name_in, text_type): if isinstance(name_in, text_type):
return encoded, name_in return encoded, name_in
if PY2: name = bytes(name_in)
name = name_in for Idx in range(len(name)):
for Idx in range(len(name)): # print('Trying to intuit the encoding')
# print('Trying to intuit the encoding') # /!\ detection is done 2char by 2char for UTF-8 special character
# /!\ detection is done 2char by 2char for UTF-8 special character if (len(name) != 1) & (Idx < (len(name) - 1)):
if (len(name) != 1) & (Idx < (len(name) - 1)): # Detect UTF-8
# Detect UTF-8 if ((name[Idx] == 0xC2) | (name[Idx] == 0xC3)) & (
if ((name[Idx] == '\xC2') | (name[Idx] == '\xC3')) & ( (name[Idx + 1] >= 0xA0) & (name[Idx + 1] <= 0xFF)):
(name[Idx + 1] >= '\xA0') & (name[Idx + 1] <= '\xFF')): encoding = 'utf-8'
encoding = 'utf-8' break
break # Detect CP850
# Detect CP850 elif (name[Idx] >= 0x80) & (name[Idx] <= 0xA5):
elif (name[Idx] >= '\x80') & (name[Idx] <= '\xA5'): encoding = 'cp850'
encoding = 'cp850' break
break # Detect ISO-8859-15
# Detect ISO-8859-15 elif (name[Idx] >= 0xA6) & (name[Idx] <= 0xFF):
elif (name[Idx] >= '\xA6') & (name[Idx] <= '\xFF'): encoding = 'iso-8859-15'
encoding = 'iso-8859-15' break
break else:
else: # Detect CP850
# Detect CP850 if (name[Idx] >= 0x80) & (name[Idx] <= 0xA5):
if (name[Idx] >= '\x80') & (name[Idx] <= '\xA5'): encoding = 'cp850'
encoding = 'cp850' break
break # Detect ISO-8859-15
# Detect ISO-8859-15 elif (name[Idx] >= 0xA6) & (name[Idx] <= 0xFF):
elif (name[Idx] >= '\xA6') & (name[Idx] <= '\xFF'): encoding = 'iso-8859-15'
encoding = 'iso-8859-15' break
break
else:
name = bytes(name_in)
for Idx in range(len(name)):
# print('Trying to intuit the encoding')
# /!\ detection is done 2char by 2char for UTF-8 special character
if (len(name) != 1) & (Idx < (len(name) - 1)):
# Detect UTF-8
if ((name[Idx] == 0xC2) | (name[Idx] == 0xC3)) & (
(name[Idx + 1] >= 0xA0) & (name[Idx + 1] <= 0xFF)):
encoding = 'utf-8'
break
# Detect CP850
elif (name[Idx] >= 0x80) & (name[Idx] <= 0xA5):
encoding = 'cp850'
break
# Detect ISO-8859-15
elif (name[Idx] >= 0xA6) & (name[Idx] <= 0xFF):
encoding = 'iso-8859-15'
break
else:
# Detect CP850
if (name[Idx] >= 0x80) & (name[Idx] <= 0xA5):
encoding = 'cp850'
break
# Detect ISO-8859-15
elif (name[Idx] >= 0xA6) & (name[Idx] <= 0xFF):
encoding = 'iso-8859-15'
break
if encoding: if encoding:
encoded = True encoded = True
name = name.decode(encoding) name = name.decode(encoding)
elif not PY2: else:
name = name.decode() name = name.decode()
return encoded, name return encoded, name