From 2e1d35a6e8b13a1a57e11a4bfcc6218f718f7305 Mon Sep 17 00:00:00 2001 From: Gilles DAVID Date: Thu, 30 Mar 2017 19:48:18 +0200 Subject: [PATCH] [PY3] 2to3 -v -w -n --no-diffs -f all -f idioms -f buffer -f set_literal * --- autotools/m4/check_swig.m4 | 2 +- autotools/m4/pyembed.m4 | 10 ++++----- autotools/m4/python.m4 | 4 ++-- config/scfg | 31 ++++++++++++++-------------- config/scfgcmp | 36 ++++++++++++++++----------------- config/sconfig/salome_config.py | 34 +++++++++++++++---------------- 6 files changed, 58 insertions(+), 59 deletions(-) diff --git a/autotools/m4/check_swig.m4 b/autotools/m4/check_swig.m4 index 8136f11..4b61ca0 100644 --- a/autotools/m4/check_swig.m4 +++ b/autotools/m4/check_swig.m4 @@ -58,7 +58,7 @@ EOF AC_MSG_RESULT($swig_ok) fi -numpydir=`$PYTHON -c "import numpy;print numpy.get_include()" 2>/dev/null` +numpydir=`$PYTHON -c "import numpy;print(numpy.get_include())" 2>/dev/null` if test -d "$numpydir"; then numpy_ok=yes PYTHON_INCLUDES="-I$numpydir $PYTHON_INCLUDES" diff --git a/autotools/m4/pyembed.m4 b/autotools/m4/pyembed.m4 index 9f15314..f40fd02 100644 --- a/autotools/m4/pyembed.m4 +++ b/autotools/m4/pyembed.m4 @@ -32,7 +32,7 @@ AC_MSG_CHECKING(for flags used to embed python interpreter) changequote(,)dnl py_makefile="`$PYTHON -c ' import sys -print \"%s/lib/python%s/config/Makefile\"%(sys.exec_prefix, sys.version[:3])'`" +print(\"%s/lib/python%s/config/Makefile\"%(sys.exec_prefix, sys.version[:3]))'`" changequote([,])dnl if test ! -f "$py_makefile"; then AC_MSG_ERROR([*** Couldn't find the python config makefile. Maybe you are @@ -44,11 +44,11 @@ py_lib="`$PYTHON -c ' import sys ver = sys.version[:3] pre = sys.exec_prefix -print \"-L%s/lib/python%s/config\" % (pre, ver), +print(\"-L%s/lib/python%s/config\" % (pre, ver)), if ver == \"1.4\": - print \"-lPython -lObjects -lParser\" + print(\"-lPython -lObjects -lParser\") else: - print \"-lpython\" + ver + print(\"-lpython\" + ver) changequote([,])dnl @@ -83,7 +83,7 @@ AC_MSG_CHECKING(for config.c.in) changequote(,)dnl py_config_in="`$PYTHON -c ' import sys -print \"%s/lib/python%s/config/config.c.in\"%(sys.exec_prefix, sys.version[:3])'`" +print(\"%s/lib/python%s/config/config.c.in\"%(sys.exec_prefix, sys.version[:3]))'`" changequote([,])dnl if test ! -f "$py_config_in"; then AC_MSG_ERROR([*** Couldn't find the config.c.in file. Maybe you are diff --git a/autotools/m4/python.m4 b/autotools/m4/python.m4 index 9153ded..fc21aa3 100644 --- a/autotools/m4/python.m4 +++ b/autotools/m4/python.m4 @@ -59,7 +59,7 @@ AC_DEFUN([CHECK_PYTHON], AC_SUBST(PYTHONHOME) changequote(<<, >>)dnl - PYTHON_VERSION=`$PYTHON -c "import sys; print sys.version[:3]"` + PYTHON_VERSION=`$PYTHON -c "import sys; print(sys.version[:3])"` changequote([, ])dnl AC_SUBST(PYTHON_VERSION) @@ -98,7 +98,7 @@ AC_DEFUN([CHECK_PYTHON], dnl to know which OS Python thinks this is. AC_SUBST(PYTHON_PLATFORM) - PYTHON_PLATFORM=`$PYTHON -c "import sys; print sys.platform"` + PYTHON_PLATFORM=`$PYTHON -c "import sys; print(sys.platform)"` AC_SUBST(PYTHON_SITE) AC_ARG_WITH(python-site, diff --git a/config/scfg b/config/scfg index 870e045..5272c8d 100755 --- a/config/scfg +++ b/config/scfg @@ -68,9 +68,9 @@ def verbose(): def debug(*args): if verbose(): - print "[DEBUG]", - for a in args: print a, - print + print("[DEBUG]", end=' ') + for a in args: print(a, end=' ') + print() pass ## @@ -83,7 +83,7 @@ def debug(*args): ## def error_exit(msg): - print "%s: %s" % (get_tool_name(), msg) + print("%s: %s" % (get_tool_name(), msg)) sys.exit(1) pass @@ -215,9 +215,8 @@ def format_string(s, width): def format_commands(): commands = get_commands() # filter out tool itself - commands = filter(lambda a: a != "", commands) + commands = sorted([a for a in commands if a != ""]) # sort commands alphabetically - commands.sort() # get max command's length max_length = max([ len(i) for i in commands]) # generate formatting string @@ -248,7 +247,7 @@ def format_commands(): def format_list_options(cmd): opts = get_opts(cmd) - return "".join([" [%s]" % i for i in opts.keys()]) + return "".join([" [%s]" % i for i in opts]) ## # function: format_options @@ -263,7 +262,7 @@ def format_list_options(cmd): def format_options(cmd): opts = get_opts(cmd) - opts_names = opts.keys() + opts_names = list(opts.keys()) if not opts: return "" # get max command's length max_length = max([ len(i) for i in opts_names]) @@ -302,7 +301,7 @@ def usage(cmd=None): fmt[ "opt_list" ] = format_options(cmd) help_string = get_help(cmd) help_string = help_string.replace("\t", " ") - print help_string.format(**fmt) + print(help_string.format(**fmt)) sys.exit(0) pass @@ -326,7 +325,7 @@ def parse_cmd_line(args): # empty option error_exit("empty option is not allowed") if a.startswith("-"): - allowed_opts = get_opts(cmd).keys() + allowed_opts = list(get_opts(cmd).keys()) allowed_opts = dict([(i[1], len(i)>2) for i in allowed_opts]) processed_opts = tool_opts if cmd is None else cmd_opts cmd_descr = "" if cmd is None else " for command '%s'" % cmd @@ -337,7 +336,7 @@ def parse_cmd_line(args): if o == "-": # "--" format is not supported error_exit("invalid format of option") - elif o in allowed_opts.keys(): + elif o in allowed_opts: if allowed_opts[o]: # supported option; requires parameter if opts: @@ -474,7 +473,7 @@ def main(): try: # check if custom source file is specified - src_file = opts["s"] if opts.has_key("s") else None + src_file = opts["s"] if "s" in opts else None debug("source file:", src_file) # create config tool @@ -496,9 +495,9 @@ def main(): error_exit("%s: can't read more than one parameter at once!" % cmd) def _toString(v): if v is None: return "" - elif type(v) is list: return " ".join(v) + elif isinstance(v, list): return " ".join(v) else: return v - print _toString(cfg_tool.get(tgt, params[0])) + print(_toString(cfg_tool.get(tgt, params[0]))) pass elif cmd == "set": if not tgt: @@ -525,7 +524,7 @@ def main(): errors = [] if cfg_tool.verify(tgt, errors): msg = " (no products found)" if not cfg_tool.get("cfg", "products") else "" - print "Configuration is valid%s." % msg + print("Configuration is valid%s." % msg) else: raise Exception("Configuration is invalid:\n"+ "\n".join(errors)) pass @@ -542,7 +541,7 @@ def main(): error_exit("unknown command: %s" % cmd) pass pass - except Exception, e: + except Exception as e: error_exit(e) pass diff --git a/config/scfgcmp b/config/scfgcmp index 43f97a7..dd5c10e 100755 --- a/config/scfgcmp +++ b/config/scfgcmp @@ -15,25 +15,25 @@ def get_tool_name(): return os.path.basename(sys.argv[0]) def error_exit(msg): - print "%s: %s" % (get_tool_name(), msg) - print "Try '%s --help' for more information." % get_tool_name() + print("%s: %s" % (get_tool_name(), msg)) + print("Try '%s --help' for more information." % get_tool_name()) sys.exit(2) pass def usage(): - print "Command line tool to compare two SALOME configurations." - print - print "Usage: %s [options] CFGFILE1 [CFGFILE2]" % get_tool_name() - print - print " CFGFILE1 First configuration file to compare." - print " CFGFILE2 Second configuration file to compare; if not specified," - print " default one is used: %s." % defaultConfFile() - print - print "Options:" - print " --help (-h) Prints this help information." - print " --force (-f) Perform full comparation, including optional parameters." - print " --verbose (-v) Print results of comparison to the screen." - print + print("Command line tool to compare two SALOME configurations.") + print() + print("Usage: %s [options] CFGFILE1 [CFGFILE2]" % get_tool_name()) + print() + print(" CFGFILE1 First configuration file to compare.") + print(" CFGFILE2 Second configuration file to compare; if not specified,") + print(" default one is used: %s." % defaultConfFile()) + print() + print("Options:") + print(" --help (-h) Prints this help information.") + print(" --force (-f) Perform full comparation, including optional parameters.") + print(" --verbose (-v) Print results of comparison to the screen.") + print() def main(): # parse command line @@ -76,7 +76,7 @@ def main(): try: tool1 = CfgTool(cfgfile1) tool2 = CfgTool(cfgfile2) - except Exception, e: + except Exception as e: error_exit(str(e)) pass @@ -145,8 +145,8 @@ def main(): if errors: if isverbose: - print "Files %s and %s differ:" % (tool1.cfgFile, tool2.cfgFile) - print "\n".join([" " + i for i in errors]) + print("Files %s and %s differ:" % (tool1.cfgFile, tool2.cfgFile)) + print("\n".join([" " + i for i in errors])) pass return 1 diff --git a/config/sconfig/salome_config.py b/config/sconfig/salome_config.py index 6105406..b20b5bd 100644 --- a/config/sconfig/salome_config.py +++ b/config/sconfig/salome_config.py @@ -247,16 +247,16 @@ class CfgTool(object): self.tree = ET.parse(self.cfgFile).getroot() self._checkConfig() pass - except IOError, e: + except IOError as e: self.tree = self._new() pass - except exceptionClass, e: + except exceptionClass as e: if e.code == 3: # no element found, it's OK self.tree = self._new() else: raise Exception("bad XML file %s: %s" % (self.cfgFile, str(e))) pass - except Exception, e: + except Exception as e: raise Exception("unkwnown error: %s" % str(e)) pass @@ -323,7 +323,7 @@ class CfgTool(object): tag = path[-1][0] params = {} # process keyword arguments - for param, value in kwargs.items(): + for param, value in list(kwargs.items()): if param not in tagAttributes(tag): raise Exception("unsupported parameter %s for target %s" % (param, target)) params[param] = value @@ -344,7 +344,7 @@ class CfgTool(object): pass # create / modify target elem = self._findPath(path, True) - for param, value in params.items(): + for param, value in list(params.items()): elem.set(param, value) pass self._write() @@ -528,7 +528,7 @@ class CfgTool(object): elem = self.tree for tag, name in path[1:]: if name: - children = filter(lambda i: i.tag == tag and i.get(nameAttr()) == name, elem.getchildren()) + children = [i for i in elem.getchildren() if i.tag == tag and i.get(nameAttr()) == name] if len(children) > 1: raise Exception("error parsing target path: more than one child element found") elif len(children) == 1: @@ -542,7 +542,7 @@ class CfgTool(object): return None pass else: - children = filter(lambda i: i.tag == tag, elem.getchildren()) + children = [i for i in elem.getchildren() if i.tag == tag] if len(children) > 1: raise Exception("error parsing target path: more than one child element found") elif len(children) == 1: @@ -571,7 +571,7 @@ class CfgTool(object): _name = _obj.tag attrs = tagAttributes(_obj.tag, True) if nameAttr() in attrs and attrs[nameAttr()]: - if nameAttr() not in _obj.keys(): _name += " [unnamed]" + if nameAttr() not in list(_obj.keys()): _name += " [unnamed]" else: _name += " [%s]" % _obj.get(nameAttr()) pass return _name @@ -596,7 +596,7 @@ class CfgTool(object): """ result = [] result += [i.get(nameAttr()) for i in \ - filter(lambda i: i.tag == param and i.get(nameAttr()), elem.getchildren())] + [i for i in elem.getchildren() if i.tag == param and i.get(nameAttr())]] for c in elem.getchildren(): result += self._children(c, param) pass @@ -619,7 +619,7 @@ class CfgTool(object): et.write(f, self.encoding()) pass pass - except IOError, e: + except IOError as e: raise Exception("can't write to %s: %s" % (self.cfgFile, e.strerror)) pass @@ -661,15 +661,15 @@ class CfgTool(object): return indent = " " # dump element - print "%s%s" % (indent * level, elem.tag) + print("%s%s" % (indent * level, elem.tag)) attrs = tagAttributes(elem.tag, True) format = "%" + "-%ds" % max([len(i) for i in supportedAttributes()]) + " : %s" for a in attrs: - if a in elem.attrib.keys(): - print indent*(level+1) + format % (a, elem.get(a)) + if a in list(elem.attrib.keys()): + print(indent*(level+1) + format % (a, elem.get(a))) pass pass - print + print() # dump all childrens recursively for c in elem.getchildren(): self._dump(c, level+1) @@ -702,7 +702,7 @@ class CfgTool(object): errors.append("bad XML element: %s" % elem.tag) else: # check attributes - attrs = elem.keys() + attrs = list(elem.keys()) for attr in attrs: if attr not in tagAttributes(elem.tag, True): errors.append("unsupported attribute '%s' for XML element '%s'" % (attr, elem.tag)) @@ -729,14 +729,14 @@ class CfgTool(object): attrs = tagAttributes(elem.tag, True) # check mandatory attributes for attr in attrs: - if attrs[attr] and (attr not in elem.keys() or not elem.get(attr).strip()): + if attrs[attr] and (attr not in list(elem.keys()) or not elem.get(attr).strip()): errors.append("mandatory parameter '%s' of object '%s' is not set" % (attr, self._path(elem))) pass pass # specific check for particular XML element try: self._checkObject(elem) - except Exception, e: + except Exception as e: errors.append("%s : %s" % (self._path(elem), str(e))) # check all childrens recursively for c in elem.getchildren(): -- 2.30.2