Skip to content

Commit

Permalink
Simplify 'str' and 'repr' methods for python3
Browse files Browse the repository at this point in the history
Remove 'py23_repr(x)', because it returns 'x' for python3.
Rename method 'py23_str' to 'py3_str' and remove support for python2.

Refs #422
  • Loading branch information
rcrdnalor committed Dec 20, 2021
1 parent 4c50f59 commit d24ae4a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 43 deletions.
16 changes: 8 additions & 8 deletions mythtv/bindings/python/MythTV/database.py
Expand Up @@ -8,7 +8,7 @@
from MythTV.altdict import OrdDict, DictData
from MythTV.logging import MythLog
from MythTV.msearch import MSearch
from MythTV.utility import datetime, dt, _donothing, QuickProperty, py23_repr
from MythTV.utility import datetime, dt, _donothing, QuickProperty
from MythTV.exceptions import MythError, MythDBError, MythTZError
from MythTV.connections import DBConnection, LoggedCursor, XMLConnection

Expand Down Expand Up @@ -220,7 +220,7 @@ def __str__(self):
hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def __getstate__(self):
data = {'data':DictData.__getstate__(self)}
Expand Down Expand Up @@ -463,7 +463,7 @@ def __init__(self, data):
self._changed = True
self.__hash__()
def __str__(self): return str(self.items())
def __repr__(self): return py23_repr(str(self))
def __repr__(self): return str(self)
def __hash__(self):
if self._changed:
self._hash = hash(sum(map(hash,self.values())))
Expand Down Expand Up @@ -1115,7 +1115,7 @@ class _TableFields( OrdDict ):
"""Provides a dictionary-like list of table fieldnames"""
class _FieldData( OrdDict ):
def __str__(self): return str(list(self))
def __repr__(self): return py23_repr(str(self))
def __repr__(self): return str(self)
def __iter__(self): return self.iterkeys()
def __init__(self, result):
# remove comments in 'type' like "datetime /* mariadb-5.3 */"
Expand All @@ -1135,7 +1135,7 @@ def __getitem__(self,key):
raise KeyError(str(key))
_localvars = ['_field_order','_db','_log']
def __str__(self): return str(list(self))
def __repr__(self): return py23_repr(str(self))
def __repr__(self): return str(self)
def __iter__(self): return self.iterkeys()
def __init__(self, db, log):
OrdDict.__init__(self)
Expand All @@ -1160,7 +1160,7 @@ class _Settings( OrdDict ):
class _HostSettings( OrdDict ):
_localvars = ['_field_order','_log','_db','_host','_insert','_where']
def __str__(self): return str(list(self))
def __repr__(self): return py23_repr(str(self))
def __repr__(self): return str(self)
def __iter__(self): return self.iterkeys()
def __init__(self, db, log, host):
OrdDict.__init__(self)
Expand Down Expand Up @@ -1234,7 +1234,7 @@ def getall(self):

_localvars = ['_field_order','_log','_db']
def __str__(self): return str(list(self))
def __repr__(self): return py23_repr(str(self))
def __repr__(self): return str(self)
def __iter__(self): return self.iterkeys()
def __init__(self, db, log):
OrdDict.__init__(self)
Expand Down Expand Up @@ -1432,7 +1432,7 @@ def __str__(self):
(self.groupname, self.hostname,
self.dirname, hex(id(self)))

def __repr__(self): return py23_repr(str(self))
def __repr__(self): return str(self)

def _evalwheredat(self, wheredat=None):
DBData._evalwheredat(self, wheredat)
Expand Down
23 changes: 11 additions & 12 deletions mythtv/bindings/python/MythTV/dataheap.py
Expand Up @@ -10,8 +10,7 @@
from MythTV.database import *
from MythTV.system import Grabber, InternetMetadata, VideoMetadata
from MythTV.mythproto import ftopen, FileOps, Program
from MythTV.utility import CMPRecord, CMPVideo, MARKUPLIST, datetime, ParseSet,\
py23_repr
from MythTV.utility import CMPRecord, CMPVideo, MARKUPLIST, datetime, ParseSet

import re
import locale
Expand Down Expand Up @@ -155,7 +154,7 @@ def __str__(self):
% (self.title, self.type, hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def __init__(self, data=None, db=None, template=None):
DBDataWrite.__init__(self, data, db)
Expand Down Expand Up @@ -323,7 +322,7 @@ def __str__(self):
self.starttime.isoformat(' '), hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def __init__(self, data=None, db=None):
if data is not None:
Expand Down Expand Up @@ -564,7 +563,7 @@ def __str__(self):
self.recordedid, hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def __init__(self, data=None, db=None):
DBDataWrite.__init__(self, data, db)
Expand Down Expand Up @@ -599,7 +598,7 @@ def __str__(self):
self.starttime.isoformat(' '), hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def __init__(self, data=None, db=None):
if data is not None:
Expand Down Expand Up @@ -637,7 +636,7 @@ def __str__(self):
self.starttime.isoformat(' '), hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def __init__(self, data=None, db=None):
if data is not None:
Expand Down Expand Up @@ -681,7 +680,7 @@ def __str__(self):
(self.inetref, self.season, hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

coverart = Artwork('coverart')
fanart = Artwork('fanart')
Expand All @@ -703,7 +702,7 @@ def __str__(self):
return u"<Job '%s' at %s>" % (self.id, hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def setComment(self,comment):
"""Job.setComment(comment) -> None, updates comment"""
Expand Down Expand Up @@ -774,7 +773,7 @@ def __str__(self):
(self.chanid, self.name, hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

class Guide( CMPRecord, DBData ):
"""
Expand All @@ -791,7 +790,7 @@ def __str__(self):
self.starttime.isoformat(' '), hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def getRecStatus(self):
be = FileOps(db=self._db)
Expand Down Expand Up @@ -936,7 +935,7 @@ def __repr__(self):
res += u' - %dx%02d' % (self.season, self.episode)
if self.subtitle:
res += u' - '+self.subtitle
return py23_repr(u"<Video '%s' at %s>" % (res, hex(id(self))))
return (u"<Video '%s' at %s>" % (res, hex(id(self))))

def _postinit(self):
self._fill_cm()
Expand Down
6 changes: 3 additions & 3 deletions mythtv/bindings/python/MythTV/mythproto.py
Expand Up @@ -12,7 +12,7 @@
from MythTV.connections import BEConnection, BEEventConnection
from MythTV.database import DBCache
from MythTV.utility import CMPRecord, datetime, ParseEnum, \
CopyData, CopyData2, check_ipv6, py23_repr, resolve_ip
CopyData, CopyData2, check_ipv6, resolve_ip

from datetime import date
from time import sleep
Expand Down Expand Up @@ -823,7 +823,7 @@ def __str__(self):
% (self.path, self.host, hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def __init__(self, raw):
DictData.__init__(self, raw)
Expand Down Expand Up @@ -874,7 +874,7 @@ def __str__(self):
self.starttime.isoformat(' '), hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def __init__(self, raw, db=None):
DictData.__init__(self, raw)
Expand Down
4 changes: 2 additions & 2 deletions mythtv/bindings/python/MythTV/system.py
Expand Up @@ -7,7 +7,7 @@
from MythTV.exceptions import MythError, MythDBError, MythFileError
from MythTV.logging import MythLog
from MythTV.altdict import DictData, OrdDict
from MythTV.utility import levenshtein, DequeBuffer, py23_repr
from MythTV.utility import levenshtein, DequeBuffer
from MythTV.database import DBCache

from subprocess import Popen
Expand Down Expand Up @@ -153,7 +153,7 @@ def __str__(self):
self.path, hex(id(self)))

def __repr__(self):
return py23_repr(str(self))
return str(self)

def append(self, *args):
"""
Expand Down
3 changes: 1 addition & 2 deletions mythtv/bindings/python/MythTV/utility/__init__.py
Expand Up @@ -7,6 +7,5 @@

from .other import _donothing, SchemaUpdate, databaseSearch, deadlinesocket, \
MARKUPLIST, levenshtein, ParseEnum, ParseSet, CopyData, \
CopyData2, check_ipv6, QuickProperty, py23_str, py23_repr, \
resolve_ip
CopyData2, check_ipv6, QuickProperty, py3_str, resolve_ip

23 changes: 7 additions & 16 deletions mythtv/bindings/python/MythTV/utility/other.py
Expand Up @@ -382,14 +382,14 @@ def recvheader(self, flags=0, deadline=None):
data = self.dlrecv(size, flags, deadline)
# data is utf-8 encoded, convert to unicode for logging and ignore errors
self.log(MythLog.SOCKET|MythLog.NETWORK, MythLog.DEBUG, \
'read <-- %d' % size, py23_str(data, True))
'read <-- %d' % size, py3_str(data, True))
return data

def sendheader(self, data, flags=0):
"""Send data, prepending the length in the first 8 bytes."""
try:
self.log(MythLog.SOCKET|MythLog.NETWORK, MythLog.DEBUG, \
'write --> %d' % len(data), py23_str(data, True))
'write --> %d' % len(data), py3_str(data, True))
# build the byte array:
length = b'%-8d' % len(data)
data = b"".join([length, data])
Expand Down Expand Up @@ -581,22 +581,13 @@ def resolve_ip(host, port):
except:
return (None, None)

def py23_str(value, ignore_errors=False):
def py3_str(value, ignore_errors=False):
error_methods = ('strict', 'ignore')
error_method = error_methods[ignore_errors]
try: # Python 2
return unicode(value, errors=error_method, encoding='utf-8')
except NameError: # Python 3
try:
return str(value, errors=error_method, encoding='utf-8')
except TypeError: # Wasn't a bytes object, no need to decode
return str(value)

def py23_repr(x):
if sys.version_info[0] == 2:
return(x.encode('utf-8'))
else:
return(x)
try:
return str(value, errors=error_method, encoding='utf-8')
except TypeError: # Wasn't a bytes object, no need to decode
return str(value)

class QuickProperty( object ):
def __init__(self, maskedvar, default=None, handler=None):
Expand Down

0 comments on commit d24ae4a

Please sign in to comment.