"""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 = {}
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
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})