From: W. Trevor King Date: Sun, 9 Jan 2011 05:08:30 +0000 (-0500) Subject: Don't setdefault in mymf.import_module if import_hook hasn't been run. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;p=depgraph.git Don't setdefault in mymf.import_module if import_hook hasn't been run. It raised exceptions trying to get _last_caller.__name__ when _last_caller was None. Also: * Add logger to py2depgraph * Disable (for now) ModuleFinder's internal debugging. --- diff --git a/py2depgraph.py b/py2depgraph.py index a733fc4..fa1b930 100755 --- a/py2depgraph.py +++ b/py2depgraph.py @@ -25,8 +25,18 @@ """Extract a tree of module imports from a Python script. """ +import logging import modulefinder +LOG = logging.getLogger('py2depgraph') +LOG.setLevel(logging.DEBUG) +_STREAM_HANDLER = logging.StreamHandler() +_STREAM_HANDLER.setLevel(logging.CRITICAL) +_STREAM_HANDLER.setFormatter( + logging.Formatter('%(levelname)s - %(message)s')) +LOG.addHandler(_STREAM_HANDLER) + + class mymf(modulefinder.ModuleFinder): def __init__(self,*args,**kwargs): self._depgraph = {} @@ -42,10 +52,12 @@ class mymf(modulefinder.ModuleFinder): return modulefinder.ModuleFinder.import_hook(self,name,caller,fromlist, level) finally: self._last_caller = old_last_caller + LOG.debug('after import_hook(%s, %s, %s, %s), last_caller is %s' + % (name, caller, fromlist, level, self._last_caller)) def import_module(self,partnam,fqname,parent): r = modulefinder.ModuleFinder.import_module(self,partnam,fqname,parent) - if r is not None: + if None not in [r, self._last_caller]: self._depgraph.setdefault(self._last_caller.__name__,{})[r.__name__] = 1 return r @@ -69,8 +81,13 @@ if __name__=='__main__': options,args = p.parse_args() + log_level = logging.CRITICAL - 10*options.verbose + _STREAM_HANDLER.setLevel(log_level) + script = args[0] - mf = mymf(path=sys.path[:], debug=options.verbose, excludes=[]) + debug = options.verbose + debug = 0 + mf = mymf(path=sys.path[:], debug=debug, excludes=[]) mf.run_script(script) pprint({'depgraph':mf._depgraph,'types':mf._types,'paths':mf._paths})