wmlscope: used print function

This replaces usages of print >> sys.stderr adn sys.stderr.write
This commit is contained in:
Elvish_Hunter 2015-07-30 10:51:49 +02:00
parent 2dee033335
commit d8e69a4c40

View File

@ -92,6 +92,8 @@
# #
# sets the warning level. # sets the warning level.
from __future__ import print_function
import sys, os, time, re, getopt, hashlib, glob import sys, os, time, re, getopt, hashlib, glob
from wesnoth.wmltools import * from wesnoth.wmltools import *
@ -140,9 +142,9 @@ class CrossRefLister(CrossRef):
type_ = "global" type_ = "global"
nrefs = len(defn.references) nrefs = len(defn.references)
if nrefs == 0: if nrefs == 0:
print "%s: %s macro %s is unused" % (defn, type_, name) print("%s: %s macro %s is unused" % (defn, type_, name))
else: else:
print "%s: %s macro %s is used in %d files:" % (defn, type_, name, nrefs) print("%s: %s macro %s is used in %d files:" % (defn, type_, name, nrefs))
defn.dump_references() defn.dump_references()
for name in sorted(self.fileref.keys()): for name in sorted(self.fileref.keys()):
defloc = self.fileref[name] defloc = self.fileref[name]
@ -150,20 +152,20 @@ class CrossRefLister(CrossRef):
continue continue
nrefs = len(defloc.references) nrefs = len(defloc.references)
if nrefs == 0: if nrefs == 0:
print "Resource %s is unused" % defloc print("Resource %s is unused" % defloc)
else: else:
print "Resource %s is used in %d files:" % (defloc, nrefs) print("Resource %s is used in %d files:" % (defloc, nrefs))
defloc.dump_references() defloc.dump_references()
def unresdump(self): def unresdump(self):
"Report unresolved references, arity mismatches, duplicate unit IDs." "Report unresolved references, arity mismatches, duplicate unit IDs."
# First the unresolved references # First the unresolved references
if len(self.unresolved) == 0 and len(self.missing) == 0: if len(self.unresolved) == 0 and len(self.missing) == 0:
print "# No unresolved references" print("# No unresolved references")
else: else:
#print self.fileref.keys() #print(self.fileref.keys())
for (name, reference) in self.unresolved + self.missing: for (name, reference) in self.unresolved + self.missing:
print "%s: Unresolved reference -> %s" % (reference, name) print("%s: Unresolved reference -> %s" % (reference, name))
mismatched = [] mismatched = []
for name in sorted(self.xref.keys()): for name in sorted(self.xref.keys()):
for defn in self.xref[name]: for defn in self.xref[name]:
@ -172,12 +174,12 @@ class CrossRefLister(CrossRef):
mismatched.append((name, m)) mismatched.append((name, m))
# Then the type mismatches # Then the type mismatches
if mismatched: if mismatched:
print "# Mismatched references:" print("# Mismatched references:")
for (n, m) in mismatched: for (n, m) in mismatched:
print "%s: macro %s(%s) has mismatches:" % (m, n, ", ".join(["{}={}".format(x, formaltype(x)) for x in m.args])) print("%s: macro %s(%s) has mismatches:" % (m, n, ", ".join(["{}={}".format(x, formaltype(x)) for x in m.args])))
for (file, refs) in m.references.items(): for (file, refs) in m.references.items():
for (ln, args) in refs: for (ln, args) in refs:
print '"%s", line %d: %s(%s) with signature (%s)' % (file, ln, n, ", ".join(args), ", ".join(["{}={}".format(f, actualtype(a)) for f,a in zip(m.args, args)])) print('"%s", line %d: %s(%s) with signature (%s)' % (file, ln, n, ", ".join(args), ", ".join(["{}={}".format(f, actualtype(a)) for f,a in zip(m.args, args)])))
def undersized(self): def undersized(self):
"Report undersized images that cannot be safely overlaid on a hex." "Report undersized images that cannot be safely overlaid on a hex."
@ -189,11 +191,11 @@ class CrossRefLister(CrossRef):
with Image.open(filename) as im: with Image.open(filename) as im:
(x, y) = im.size (x, y) = im.size
if x <= 60 or y <= 60: if x <= 60 or y <= 60:
print "%s: %d by %d" % (filename, x, y) print("%s: %d by %d" % (filename, x, y))
except IOError: except IOError:
sys.stderr.write("%s: PIL internal error\n" % filename) print("%s: PIL internal error" % filename, file=sys.stderr)
except ImportError: except ImportError:
sys.stderr.write("Install Python Imaging Library to enable size check.\n") print("Install Python Imaging Library to enable size check.", file=sys.stderr)
def duplicates(self, exportonly): def duplicates(self, exportonly):
"Dump duplicate unit IDs." "Dump duplicate unit IDs."
duplicate_latch = False duplicate_latch = False
@ -202,11 +204,11 @@ class CrossRefLister(CrossRef):
if exportonly and not [x for x in value if self.exports(x.namespace)]: if exportonly and not [x for x in value if self.exports(x.namespace)]:
continue continue
if not duplicate_latch: if not duplicate_latch:
print "# Duplicate IDs" print("# Duplicate IDs")
duplicate_latch = True duplicate_latch = True
print "%s: occurs %d times as unit ID" % (key, len(value)) print("%s: occurs %d times as unit ID" % (key, len(value)))
for ref in value: for ref in value:
print "%s: exported=%s" % (ref, self.exports(ref.namespace)) print("%s: exported=%s" % (ref, self.exports(ref.namespace)))
def typelist(self, branch): def typelist(self, branch):
"Dump actual and formal arguments for macros in specified file" "Dump actual and formal arguments for macros in specified file"
@ -217,21 +219,21 @@ class CrossRefLister(CrossRef):
if filename.endswith(branch): if filename.endswith(branch):
if name not in already_seen: if name not in already_seen:
already_seen.append(name) already_seen.append(name)
print "%s: macro %s(%s):" % (defn, name, ", ".join(["{}={}".format(x, formaltype(x)) for x in defn.args])) print("%s: macro %s(%s):" % (defn, name, ", ".join(["{}={}".format(x, formaltype(x)) for x in defn.args])))
for (ln, args) in refs: for (ln, args) in refs:
print '"%s", line %d: %s(%s) with signature (%s)' % (filename, ln, name, ", ".join(args), ", ".join(["{}={}".format(f, actualtype(a)) for f,a in zip(defn.args, args)])) print('"%s", line %d: %s(%s) with signature (%s)' % (filename, ln, name, ", ".join(args), ", ".join(["{}={}".format(f, actualtype(a)) for f,a in zip(defn.args, args)])))
def deflist(self, pred=None): def deflist(self, pred=None):
"List all resource definitions." "List all resource definitions."
for name in sorted(self.xref.keys()): for name in sorted(self.xref.keys()):
for defn in self.xref[name]: for defn in self.xref[name]:
if not pred or pred(name, defn): if not pred or pred(name, defn):
print "macro", name, " ".join(["{}={}".format(x, formaltype(x)) for x in defn.args]) print("macro", name, " ".join(["{}={}".format(x, formaltype(x)) for x in defn.args]))
for name in sorted(self.fileref.keys()): for name in sorted(self.fileref.keys()):
defloc = self.fileref[name] defloc = self.fileref[name]
if not pred or pred(name, defloc): if not pred or pred(name, defloc):
print "resource", name print("resource", name)
for uid in sorted(self.unit_ids.keys()): for uid in sorted(self.unit_ids.keys()):
print "unit", uid print("unit", uid)
def unchecked(self, fp): def unchecked(self, fp):
"List all macro definitions with untyped formals." "List all macro definitions with untyped formals."
@ -250,16 +252,16 @@ class CrossRefLister(CrossRef):
unchecked.append((name, defn)) unchecked.append((name, defn))
unresolvedcount += len(defn.references) unresolvedcount += len(defn.references)
if unchecked: if unchecked:
print "# %d of %d (%d%%) macro definitions and %d of %d calls (%d%%) have untyped formals:" \ print("# %d of %d (%d%%) macro definitions and %d of %d calls (%d%%) have untyped formals:" \
% (len(unchecked), % (len(unchecked),
defcount, defcount,
int((100 * len(unchecked)) / defcount), int((100 * len(unchecked)) / defcount),
unresolvedcount, unresolvedcount,
callcount, callcount,
int((100 * unresolvedcount) / callcount)) int((100 * unresolvedcount) / callcount)))
unchecked.sort(lambda a, b: cmp(a[1], b[1])) unchecked.sort(lambda a, b: cmp(a[1], b[1]))
for (name, defn) in unchecked: for (name, defn) in unchecked:
print "%s: %s(%s)" % (defn, name, ", ".join(defn.args)) print("%s: %s(%s)" % (defn, name, ", ".join(defn.args)))
def extracthelp(self, pref, fp): def extracthelp(self, pref, fp):
"Deliver all macro help comments in HTML form." "Deliver all macro help comments in HTML form."
@ -325,7 +327,7 @@ class CrossRefLister(CrossRef):
if __name__ == "__main__": if __name__ == "__main__":
def help(): def help():
sys.stderr.write("""\ print("""\
Usage: wmlscope [options] dirpath Usage: wmlscope [options] dirpath
Options may be any of these: Options may be any of these:
-h, --help Emit this help message and quit -h, --help Emit this help message and quit
@ -344,7 +346,7 @@ Usage: wmlscope [options] dirpath
--unchecked Report all macros with untyped formals. --unchecked Report all macros with untyped formals.
Options may be followed by any number of directiories to check. If no Options may be followed by any number of directiories to check. If no
directories are given, all files under the current directory are checked. directories are given, all files under the current directory are checked.
""") """, file=sys.stderr)
try: try:
# Process options # Process options
@ -426,20 +428,20 @@ Usage: wmlscope [options] dirpath
else: else:
dirpath = ['.'] dirpath = ['.']
if not extracthelp: if not extracthelp:
print "# Wmlscope reporting on %s" % time.ctime() print("# Wmlscope reporting on %s" % time.ctime())
print "# Invocation: %s" % " ".join(sys.argv) print("# Invocation: %s" % " ".join(sys.argv))
print "# Working directory: %s" % os.getcwd() print("# Working directory: %s" % os.getcwd())
starttime = time.time() starttime = time.time()
xref = CrossRefLister(dirpath, "|".join(exclude), warnlevel, progress) xref = CrossRefLister(dirpath, "|".join(exclude), warnlevel, progress)
if not extracthelp: if not extracthelp:
print "#Cross-reference time: %d seconds" % (time.time()-starttime) print("#Cross-reference time: %d seconds" % (time.time()-starttime))
if extracthelp: if extracthelp:
xref.extracthelp(dirpath[0], sys.stdout) xref.extracthelp(dirpath[0], sys.stdout)
elif unchecked: elif unchecked:
xref.unchecked(sys.stdout) xref.unchecked(sys.stdout)
elif listfiles: elif listfiles:
for (namespace, filename) in xref.filelist.generator(): for (namespace, filename) in xref.filelist.generator():
print filename print(filename)
if collisions: if collisions:
collisions = [] collisions = []
for (namespace, filename) in xref.filelist.generator(): for (namespace, filename) in xref.filelist.generator():
@ -454,9 +456,9 @@ Usage: wmlscope [options] dirpath
lasthash = None lasthash = None
for (n, h) in collisions: for (n, h) in collisions:
if h != lasthash: if h != lasthash:
print "%%" print("%%")
lasthash = h lasthash = h
print n print(n)
xref.duplicates(exportonly=False) xref.duplicates(exportonly=False)
elif typelist: elif typelist:
xref.typelist(typelist) xref.typelist(typelist)
@ -471,7 +473,7 @@ Usage: wmlscope [options] dirpath
return True return True
if crossreference: if crossreference:
if xref.noxref: if xref.noxref:
print >>sys.stderr, "wmlscope: can't make cross-reference, input included a definitions file." print("wmlscope: can't make cross-reference, input included a definitions file.", file=sys.stderr)
else: else:
xref.xrefdump(predicate) xref.xrefdump(predicate)
if definitions: if definitions:
@ -481,6 +483,6 @@ Usage: wmlscope [options] dirpath
xref.unresdump() xref.unresdump()
xref.duplicates(exportonly=True) xref.duplicates(exportonly=True)
except KeyboardInterrupt: except KeyboardInterrupt:
print >>sys.stderr, "wmlscope: aborted." print("wmlscope: aborted.", file=sys.stderr)
# wmlscope ends here # wmlscope ends here