diff --git a/scripts/process-text-files.py b/scripts/process-text-files.py index 8209843..6cf4b20 100644 --- a/scripts/process-text-files.py +++ b/scripts/process-text-files.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/bin/python3 # -*- coding: utf-8 -*- from __future__ import print_function @@ -93,9 +93,9 @@ class Cmd(jwutils.Cmd): @staticmethod def _replace_cpp_symbol(data, src, target, context=None): - stopc = "^a-zA-Z0-9_" - stopa = "(^|[" + stopc + "])" - stope = "([" + stopc + "]|$)" + stopc = "^a-zA-Z0-9_" + stopa = "(^|[" + stopc + "])" + stope = "([" + stopc + "]|$)" f = stopa + src + stope t = "\\1" + target + "\\2" done = False @@ -125,7 +125,7 @@ class Cmd(jwutils.Cmd): with open(path) as infile, open(tmp, 'w') as outfile: data = infile.read() #slog(NOTICE, "-- opened", path) - for src, target in replacements.iteritems(): + for src, target in replacements.items(): #slog(NOTICE, "replacing", src, "to", target) odata = data #data = data.replace(src, target) @@ -148,7 +148,7 @@ class Cmd(jwutils.Cmd): if func is None: func = self._replace_pattern for line in iter(string.splitlines()): - for src, target in replacements.iteritems(): + for src, target in replacements.items(): line = func(line, src, target) r = r + line return r @@ -223,12 +223,12 @@ class Cmd(jwutils.Cmd): def _init(self, args): if args.replace_patterns_from is not None: - self.replacements = dict() + self.replacements = dict() with open(args.replace_patterns_from) as infile: for line in infile: s = re.split('->', line) self.replacements[s[0]] = s[1].rstrip('\n') - #slog(NOTICE, "replacements =", self.replacements) + #slog(NOTICE, "replacements =", self.replacements) # overriding def run(self, args): @@ -286,7 +286,7 @@ class CmdReplaceCppSymbols(Cmd): def _init(self, args): r = super(CmdReplaceCppSymbols, self)._init(args) self.file_truncs = set() - if self.replacements is not None: + if self.replacements is not None: for patt in self.replacements: self.file_truncs.add(patt.lower()) return r @@ -303,7 +303,7 @@ class CmdReplaceCppSymbols(Cmd): continue if not trunc.lower() in self.file_truncs: continue - for patt, repl in self.replacements.iteritems(): + for patt, repl in self.replacements.items(): if patt == trunc: path = dir + '/' + name new_path = dir + '/' + repl + ext @@ -393,7 +393,7 @@ class CmdAddCppNamespace(Cmd): def _init(self, args): r = super(CmdAddCppNamespace, self)._init(args) self.file_truncs = set() - if self.replacements is not None: + if self.replacements is not None: for patt in self.replacements: self.file_truncs.add(patt.lower()) return r diff --git a/scripts/trim-src.py b/scripts/trim-src.py index 7ac2317..8b7f234 100644 --- a/scripts/trim-src.py +++ b/scripts/trim-src.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2 +#!/usr/bin/python3 # -*- coding: utf-8 -*- from abc import abstractmethod diff --git a/test/trim-src/Makefile b/test/trim-src/Makefile index f725694..6fae84f 100644 --- a/test/trim-src/Makefile +++ b/test/trim-src/Makefile @@ -18,5 +18,5 @@ clean.test: clean: clean.test help: - python2 $(EXE) -h - python2 $(EXE) beautify -h + python3 $(EXE) -h + python3 $(EXE) beautify -h diff --git a/tools/python/jwutils/Cmd.py b/tools/python/jwutils/Cmd.py index 936e764..ae88910 100644 --- a/tools/python/jwutils/Cmd.py +++ b/tools/python/jwutils/Cmd.py @@ -1,26 +1,25 @@ -from abc import ABCMeta, abstractmethod - +import abc 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): pass def __init__(self, name, help): - self.name = name + self.name = name self.help = help def _run(self, args): pass def add_parser(self, parsers): - r = parsers.add_parser(self.name, help=self.help, + r = parsers.add_parser(self.name, help=self.help, formatter_class=argparse.ArgumentDefaultsHelpFormatter) - r.set_defaults(func=self.run) + r.set_defaults(func=self.run) return r diff --git a/tools/python/jwutils/Cmds.py b/tools/python/jwutils/Cmds.py index 7ff5078..eb54a71 100644 --- a/tools/python/jwutils/Cmds.py +++ b/tools/python/jwutils/Cmds.py @@ -1,13 +1,13 @@ import os import sys import argparse -import Object -import jwutils.log +import jwutils import importlib import inspect import re +import pickle -class Cmds(Object.Object): # export +class Cmds: # export def __init__(self, description = '', filter = '^Cmd.*', modules=None): self.__description = description diff --git a/tools/python/jwutils/Object.py b/tools/python/jwutils/Object.py index aade56b..78e3b3c 100644 --- a/tools/python/jwutils/Object.py +++ b/tools/python/jwutils/Object.py @@ -1,3 +1,4 @@ +from __future__ import print_function import jwutils.log class Object(object): # export @@ -14,7 +15,7 @@ class Object(object): # export for count, thing in enumerate(args): msg += ' ' + str(*thing) if len(msg): - print msg[1:] + print(msg[1:]) def debug(self, *args): jwutils.log.slog(jwutils.log.DEBUG, args) diff --git a/tools/python/jwutils/algo/ShuntingYard.py b/tools/python/jwutils/algo/ShuntingYard.py index d30ce90..07800ee 100644 --- a/tools/python/jwutils/algo/ShuntingYard.py +++ b/tools/python/jwutils/algo/ShuntingYard.py @@ -2,6 +2,12 @@ from collections import namedtuple import re import shlex +# --- python 2 / 3 compatibility stuff +try: + basestring +except NameError: + basestring = str + L, R = 'Left Right'.split() ARG, KEYW, QUOTED, LPAREN, RPAREN = 'arg kw quoted ( )'.split() @@ -49,7 +55,7 @@ class ShuntingYard(object): # export for count, thing in enumerate(args): msg += ' ' + str(thing) if len(msg): - print msg[1:] + print(msg[1:]) def token_string(self): r = "" @@ -65,7 +71,7 @@ class ShuntingYard(object): # export if len(r): return r[2:] - return r + return r def tokenize(self, spec): @@ -162,9 +168,9 @@ class ShuntingYard(object): # export if self.do_debug: maxcolwidths = [len(max(x, key=len)) for x in zip(*table)] 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:]: - 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] def infix_to_postfix_orig(self, infix): diff --git a/tools/python/jwutils/log.py b/tools/python/jwutils/log.py index 9d5e967..fecd299 100644 --- a/tools/python/jwutils/log.py +++ b/tools/python/jwutils/log.py @@ -3,7 +3,13 @@ import syslog import sys import inspect 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 ALERT = syslog.LOG_ALERT