Skip to content

Commit

Permalink
Refactoring: extract method ModuleGraph.warn()
Browse files Browse the repository at this point in the history
Strictly speaking this also changes observable behaviour: duplicate "bad
file name extension" warnings will not be repeated.  I'd say that's a
bugfix :)
  • Loading branch information
mgedmin committed Apr 13, 2015
1 parent 3efbeda commit f5d9736
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
23 changes: 13 additions & 10 deletions findimports.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
findimports.py foo.importcache -d -N -c -p -l 2 > graph2.dot
Copyright (c) 2003--2014 Marius Gedminas <marius@pov.lt>
Copyright (c) 2003--2015 Marius Gedminas <marius@pov.lt>
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Expand Down Expand Up @@ -384,6 +384,15 @@ def __init__(self):
self.path = sys.path
self._module_cache = {}
self._warned_about = set()
self._stderr = sys.stderr

def warn(self, about, message, *args):
if about in self._warned_about:
return
if args:
message = message % args
print >> self._stderr, message
self._warned_about.add(about)

def parsePathname(self, pathname):
"""Parse one or more source files.
Expand Down Expand Up @@ -440,7 +449,7 @@ def filenameToModname(self, filename):
filename = filename[:-len(ext)]
break
else:
print >> sys.stderr, "%s: unknown file name extension" % filename
self.warn(filename, '%s: unknown file name extension', filename)
filename = os.path.abspath(filename)
elements = filename.split(os.path.sep)
modname = []
Expand Down Expand Up @@ -480,10 +489,7 @@ def findModuleOfName(self, dotted_name, level, filename, extrapath=None):
if candidate:
return candidate
name = name[:name.rfind('.')]
if dotted_name not in self._warned_about:
print >> sys.stderr, ("%s: could not find %s"
% (filename, dotted_name))
self._warned_about.add(dotted_name)
self.warn(dotted_name, '%s: could not find %s', filename, dotted_name)
return dotted_name

def isModule(self, dotted_name, extrapath=None):
Expand Down Expand Up @@ -515,10 +521,7 @@ def isModule(self, dotted_name, extrapath=None):
try:
zf = zipfile.ZipFile(dir)
except zipfile.BadZipfile:
if dir not in self._warned_about:
print >> sys.stderr, ("%s: not a directory or zip file"
% dir)
self._warned_about.add(dir)
self.warn(dir, "%s: not a directory or zip file", dir)
continue
names = zf.namelist()
for ext in ('.py', '.so', '.dll'):
Expand Down
22 changes: 22 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import unittest
from cStringIO import StringIO

import findimports


class TestModuleGraph(unittest.TestCase):

def setUp(self):
self.warnings = []

def warn(self, about, message, *args):
if args:
message = message % args
self.warnings.append(message)

def test_warn(self):
mg = findimports.ModuleGraph()
mg._stderr = StringIO()
mg.warn('foo', 'no module %s', 'foo')
self.assertEqual(mg._stderr.getvalue(), 'no module foo\n')

def test_warn_suppresses_duplicates(self):
mg = findimports.ModuleGraph()
mg._stderr = StringIO()
mg.warn('foo', 'no module foo')
mg.warn('foo', 'no module foo (again)')
self.assertEqual(mg._stderr.getvalue(), 'no module foo\n')

def test_isModule(self):
mg = findimports.ModuleGraph()
self.assertTrue(mg.isModule('os'))
Expand Down

0 comments on commit f5d9736

Please sign in to comment.