def __init__ (
self, baseModules, destinationDirectory = ".",
recursion = 1, exclusions = (),
recursionStops = (),
formatter = None
):
self.destinationDirectory = os.path.abspath( destinationDirectory)
self.exclusions = {}
self.warnings = []
self.baseSpecifiers = {}
self.completed = {}
self.recursionStops = {}
self.recursion = recursion
for stop in recursionStops:
self.recursionStops[ stop ] = 1
self.pending = []
for exclusion in exclusions:
try:
self.exclusions[ exclusion ]= pydoc.locate ( exclusion)
except pydoc.ErrorDuringImport, value:
self.warn( """Unable to import the module %s which was specified as an exclusion module"""% (repr(exclusion)))
self.formatter = formatter or DefaultFormatter()
for base in baseModules:
self.addBase( base )
python类ErrorDuringImport()的实例源码
def generatedocs(module, filename):
try:
sys.path.insert(0, os.getcwd() + '/..')
# Attempt import
mod = pydoc.safeimport(module)
if mod is None:
print("Module not found")
# Module imported correctly, let's create the docs
with open(filename, 'w') as f:
f.write(getmarkdown(mod))
except pydoc.ErrorDuringImport as e:
print("Error while trying to import " + module)
# if __name__ == '__main__':
def _writedoc(doc, thing, forceload=0):
"""Write HTML documentation to a file in the current directory.
"""
try:
obj, name = pydoc.resolve(thing, forceload)
page = pydoc.html.page(pydoc.describe(obj), pydoc.html.document(obj, name))
fname = os.path.join(doc, name + '.html')
fd = open(fname, 'w')
fd.write(page)
fd.close()
except (ImportError, pydoc.ErrorDuringImport):
traceback.print_exc(sys.stderr)
else:
return name + '.html'
def _writeclientdoc(doc, thing, forceload=0):
"""Write HTML documentation to a file in the current directory.
"""
docmodule = pydoc.HTMLDoc.docmodule
def strongarm(self, obj, name=None, mod=None, *ignored):
result = docmodule(self, obj, name, mod, *ignored)
# Grab all the aliases to pyclasses and create links.
nonmembers = []
push = nonmembers.append
for k,v in inspect.getmembers(obj, inspect.isclass):
if inspect.getmodule(v) is not obj and getattr(v,'typecode',None) is not None:
push('<a href="%s.html">%s</a>: pyclass alias<br/>' %(v.__name__,k))
result += self.bigsection('Aliases', '#ffffff', '#eeaa77', ''.join(nonmembers))
return result
pydoc.HTMLDoc.docmodule = strongarm
try:
obj, name = pydoc.resolve(thing, forceload)
page = pydoc.html.page(pydoc.describe(obj), pydoc.html.document(obj, name))
name = os.path.join(doc, name + '.html')
fd = open(name, 'w')
fd.write(page)
fd.close()
except (ImportError, pydoc.ErrorDuringImport), value:
log.debug(str(value))
pydoc.HTMLDoc.docmodule = docmodule
def _writebrokedoc(doc, ex, name, forceload=0):
try:
fname = os.path.join(doc, name + '.html')
page = pydoc.html.page(pydoc.describe(ex), pydoc.html.document(str(ex), fname))
fd = open(fname, 'w')
fd.write(page)
fd.close()
except (ImportError, pydoc.ErrorDuringImport), value:
log.debug(str(value))
return name + '.html'
def generatedocs(module):
try:
sys.path.append(os.getcwd())
# Attempt import
mod = pydoc.safeimport(module)
if mod is None:
print("Module not found")
# Module imported correctly, let's create the docs
return getmarkdown(mod)
except pydoc.ErrorDuringImport as e:
print("Error while trying to import " + module)
def generatedocs(module):
try:
sys.path.append(os.getcwd())
# Attempt import
mod = pydoc.safeimport(module)
if mod is None:
print("Module not found")
# Module imported correctly, let's create the docs
return getmarkdown(mod)
except pydoc.ErrorDuringImport as e:
print("Error while trying to import " + module)
def addBase(self, specifier):
"""Set the base of the documentation set, only children of these modules will be documented"""
try:
self.baseSpecifiers [specifier] = pydoc.locate ( specifier)
self.pending.append (specifier)
except pydoc.ErrorDuringImport, value:
self.warn( """Unable to import the module %s which was specified as a base module"""% (repr(specifier)))
def process( self ):
"""Having added all of the base and/or interesting modules,
proceed to generate the appropriate documentation for each
module in the appropriate directory, doing the recursion
as we go."""
try:
while self.pending:
try:
if self.completed.has_key( self.pending[0] ):
raise AlreadyDone( self.pending[0] )
self.info( """Start %s"""% (repr(self.pending[0])))
object = pydoc.locate ( self.pending[0] )
self.info( """ ... found %s"""% (repr(object.__name__)))
except AlreadyDone:
pass
except pydoc.ErrorDuringImport, value:
self.info( """ ... FAILED %s"""% (repr( value)))
self.warn( """Unable to import the module %s"""% (repr(self.pending[0])))
except (SystemError, SystemExit), value:
self.info( """ ... FAILED %s"""% (repr( value)))
self.warn( """Unable to import the module %s"""% (repr(self.pending[0])))
except Exception, value:
self.info( """ ... FAILED %s"""% (repr( value)))
self.warn( """Unable to import the module %s"""% (repr(self.pending[0])))
else:
page = self.formatter.page(
pydoc.describe(object),
self.formatter.docmodule(
object,
object.__name__,
packageContext = self,
)
)
file = open (
os.path.join(
self.destinationDirectory,
self.pending[0] + ".html",
),
'w',
)
file.write(page)
file.close()
self.completed[ self.pending[0]] = object
del self.pending[0]
finally:
for item in self.warnings:
print item