Fix multiple Python 3 compatibility issues

Changes in Python 3 that made the code choke:

  o basestring is merged into str
  o print() needs parentesis
  o Class inheritance syntax changed
  o Abstract baseclass (ABCMeta) syntax changed
  o map.iteritems() is replaced by map.items()
  o Inconsistent use of tabs and spaces are no longer tolerated

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2019-03-10 16:38:59 +01:00
commit 6dd594d47b
8 changed files with 44 additions and 32 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/python2 #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import print_function from __future__ import print_function
@ -125,7 +125,7 @@ class Cmd(jwutils.Cmd):
with open(path) as infile, open(tmp, 'w') as outfile: with open(path) as infile, open(tmp, 'w') as outfile:
data = infile.read() data = infile.read()
#slog(NOTICE, "-- opened", path) #slog(NOTICE, "-- opened", path)
for src, target in replacements.iteritems(): for src, target in replacements.items():
#slog(NOTICE, "replacing", src, "to", target) #slog(NOTICE, "replacing", src, "to", target)
odata = data odata = data
#data = data.replace(src, target) #data = data.replace(src, target)
@ -148,7 +148,7 @@ class Cmd(jwutils.Cmd):
if func is None: if func is None:
func = self._replace_pattern func = self._replace_pattern
for line in iter(string.splitlines()): for line in iter(string.splitlines()):
for src, target in replacements.iteritems(): for src, target in replacements.items():
line = func(line, src, target) line = func(line, src, target)
r = r + line r = r + line
return r return r
@ -303,7 +303,7 @@ class CmdReplaceCppSymbols(Cmd):
continue continue
if not trunc.lower() in self.file_truncs: if not trunc.lower() in self.file_truncs:
continue continue
for patt, repl in self.replacements.iteritems(): for patt, repl in self.replacements.items():
if patt == trunc: if patt == trunc:
path = dir + '/' + name path = dir + '/' + name
new_path = dir + '/' + repl + ext new_path = dir + '/' + repl + ext

View file

@ -1,4 +1,4 @@
#!/usr/bin/python2 #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from abc import abstractmethod from abc import abstractmethod

View file

@ -18,5 +18,5 @@ clean.test:
clean: clean.test clean: clean.test
help: help:
python2 $(EXE) -h python3 $(EXE) -h
python2 $(EXE) beautify -h python3 $(EXE) beautify -h

View file

@ -1,13 +1,12 @@
from abc import ABCMeta, abstractmethod import abc
import argparse import argparse
import Object
class Cmd(Object.Object): # export # compatible with Python 2 *and* 3
ABC = abc.ABCMeta('ABC', (object,), {'__slots__': ()})
__metaclass__=ABCMeta class Cmd(ABC): # export
@abstractmethod @abc.abstractmethod
def run(self, args): def run(self, args):
pass pass

View file

@ -1,13 +1,13 @@
import os import os
import sys import sys
import argparse import argparse
import Object import jwutils
import jwutils.log
import importlib import importlib
import inspect import inspect
import re import re
import pickle
class Cmds(Object.Object): # export class Cmds: # export
def __init__(self, description = '', filter = '^Cmd.*', modules=None): def __init__(self, description = '', filter = '^Cmd.*', modules=None):
self.__description = description self.__description = description

View file

@ -1,3 +1,4 @@
from __future__ import print_function
import jwutils.log import jwutils.log
class Object(object): # export class Object(object): # export
@ -14,7 +15,7 @@ class Object(object): # export
for count, thing in enumerate(args): for count, thing in enumerate(args):
msg += ' ' + str(*thing) msg += ' ' + str(*thing)
if len(msg): if len(msg):
print msg[1:] print(msg[1:])
def debug(self, *args): def debug(self, *args):
jwutils.log.slog(jwutils.log.DEBUG, args) jwutils.log.slog(jwutils.log.DEBUG, args)

View file

@ -2,6 +2,12 @@ from collections import namedtuple
import re import re
import shlex import shlex
# --- python 2 / 3 compatibility stuff
try:
basestring
except NameError:
basestring = str
L, R = 'Left Right'.split() L, R = 'Left Right'.split()
ARG, KEYW, QUOTED, LPAREN, RPAREN = 'arg kw quoted ( )'.split() ARG, KEYW, QUOTED, LPAREN, RPAREN = 'arg kw quoted ( )'.split()
@ -49,7 +55,7 @@ class ShuntingYard(object): # export
for count, thing in enumerate(args): for count, thing in enumerate(args):
msg += ' ' + str(thing) msg += ' ' + str(thing)
if len(msg): if len(msg):
print msg[1:] print(msg[1:])
def token_string(self): def token_string(self):
r = "" r = ""
@ -162,9 +168,9 @@ class ShuntingYard(object): # export
if self.do_debug: if self.do_debug:
maxcolwidths = [len(max(x, key=len)) for x in zip(*table)] maxcolwidths = [len(max(x, key=len)) for x in zip(*table)]
row = table[0] row = table[0]
print( ' '.join('{cell:^{width}}'.format(width=width, cell=cell) for (width, cell) in zip(maxcolwidths, row))) print(' '.join('{cell:^{width}}'.format(width=width, cell=cell) for (width, cell) in zip(maxcolwidths, row)))
for row in table[1:]: for row in table[1:]:
print( ' '.join('{cell:<{width}}'.format(width=width, cell=cell) for (width, cell) in zip(maxcolwidths, row))) print(' '.join('{cell:<{width}}'.format(width=width, cell=cell) for (width, cell) in zip(maxcolwidths, row)))
return table[-1][2] return table[-1][2]
def infix_to_postfix_orig(self, infix): def infix_to_postfix_orig(self, infix):

View file

@ -3,7 +3,13 @@ import syslog
import sys import sys
import inspect import inspect
from os.path import basename from os.path import basename
import misc from . import misc
# --- python 2 / 3 compatibility stuff
try:
basestring
except NameError:
basestring = str
EMERG = syslog.LOG_EMERG EMERG = syslog.LOG_EMERG
ALERT = syslog.LOG_ALERT ALERT = syslog.LOG_ALERT