[PYTHON DOC-SIG] ni and gendoc

Mark Hammond MHammond@skippinet.com.au
Tue, 28 Jan 1997 10:11:18 +1000


> Yes, please do. I haven't had the time I would like to spend on gendoc
> recently.
> Documenting packages was one of the things I really wanted to do, so I
> would
> be very happy to get your "hacks".

OK - my hacks are below.

There is a problem with them, which I can't sort out in a clean way

The key change is that instead of unconditionally stripping the 
extension of a filename, we check if it ".py" or ".pyc", and if not, 
retain the entire name to be imported.

Then a small bit of trickery to get the actual module associated with 
the full "a.b.c" name (which could be reduced to a 1-liner by going 
through sy.modules, where it will end up anyway?).

I have a problem with HTML generation.  In htmlgenformatter.py, there 
is render_external_link().
This code checks for an extension on the file, and if empty, adds 
".html".  On "a.b.c", this fails, and no ".html" is appended.
My hack here (not included) was to unconditionally add ".html" (ie, 
just comment out the if)

This is not the correct answer, and there are no "reasonable" 
extensions to check for - the link could be to a custom 
extenion/format (.pdf/.ps/.anything!).  I thought of changing it to generate 
"a_b_c" from "a.b.c", but this seemed to ingrained to change (ie, 
object name==filename, but this is not what we want - object 
name="a.b.c", file name = "a_b_c")

Any thoughts on this last little nasty?

Mark.

*** \temp\doc_collect.py	Thu Sep 05 01:35:20 1996
--- doc_collect.py	Mon Jan 27 09:56:01 1997
***************
*** 120,132 ****
      # attribute to determine the module name
      funcs = filter(lambda member: type(member) == FunctionType, 
cls.__dict__.values())
      if funcs:
! 	filename = funcs[0].func_code.co_filename
! 	import os
! 	module = os.path.splitext(os.path.basename(filename))[0]
! 	d = {}
! 	exec 'import %s' % module in d
! 	_classmap[cls] = d[module]
! 	return d[module]
      #
      import sys
      clsname = cls.__name__
--- 120,141 ----
      # attribute to determine the module name
      funcs = filter(lambda member: type(member) == FunctionType, 
cls.__dict__.values())
      if funcs:
! 	try:
! 	    # May be ni
! 	    # Cheat, and assume that the globals for the special
! 	    # attribute '__' is the module.
! 	    mod = funcs[0].func_globals['__']
! 	    _classmap[cls] = mod
! 	    return mod
! 	except:
! 	    # Old style.
! 	    filename = funcs[0].func_code.co_filename
! 	    import os
! 	    module = os.path.splitext(os.path.basename(filename))[0]
! 	    d = {}
! 	    exec 'import %s' % module in d
! 	    _classmap[cls] = d[module]
! 	    return d[module]
      #
      import sys
      clsname = cls.__name__

*** \temp\gendoc.py	Fri Sep 06 21:06:28 1996
--- gendoc.py	Mon Jan 27 14:22:26 1997
***************
*** 239,249 ****
      
      modules=[]
      for file in files:
! 	dir = os.path.dirname(file)
! 	if dir and dir not in sys.path:
! 	    sys.path.insert(0, dir)
! 	    
! 	module = splitfields(os.path.basename(file), '.')[0]
  	dict = {}
  
  	# Skip certain modules that I know causes problems
--- 239,260 ----
      
      modules=[]
      for file in files:
! 	base, ext = os.path.splitext(os.path.basename(file))
! 	# If the file has a recognised extension, then assume it
! 	# is a path to a .py file.  If no extension, assume it is
! 	# just a module name which can be directly imported.
! 	# Note this also supports ni names - eg, the module can contain 
"."
! 	if string.lower(ext) in [".py",".pyc"]:
! 		module = base
! 		# ensure on path
! 		dir = os.path.dirname(file)
! 		if dir and dir not in sys.path:
! 		    sys.path.insert(0, dir)
! 
! 	else:
! 		# a ni module - assume already on path.
! 		import ni
! 		module = file
  	dict = {}
  
  	# Skip certain modules that I know causes problems
***************
*** 257,263 ****
  
  	try:
  	    exec 'import '+module in dict
! 	    modules.append(dict[module])
  	except SyntaxError:
  	    sys.stderr.write('*** Syntax errors in ' + file + '\n');
  	except:
--- 268,281 ----
  
  	try:
  	    exec 'import '+module in dict
! 	    # We now have done the import, but must walk down the
! 	    # "." sep'd sub-attributes in the module name
! 	    portions = splitfields(module,".")
! 	    look = dict[portions[0]]
! 	    for subAttr in portions[1:]:
! 	        look = getattr(look, subAttr)
! 
! 	    modules.append(look)
  	except SyntaxError:
  	    sys.stderr.write('*** Syntax errors in ' + file + '\n');
  	except:
----------------------------------------------------------------------
Mark Hammond - MHammond@skippinet.com.au 
Check out Python - _the_ language for the Web/CGI/Windows/MFC/Unix/etc
<http://www.python.org> & <http://www.python.org/ftp/python/pythonwin>

_______________
DOC-SIG  - SIG for the Python Documentation Project

send messages to: doc-sig@python.org
administrivia to: doc-sig-request@python.org
_______________