[PYTHONMAC-SIG] import/source dialog

Steven D. Majewski sdm7g@Virginia.EDU
3 Oct 97 12:04:14 -0400


Here's a couple quick but useful utilities for interactive Python
development. I've been wanting something like this on the mac for
a while and I finally got impatient enough to go and write it. 
Windows folks may also find it useful -- the GetFileDialog is the
only Mac specific part -- maybe you can replace it with a windows
or Tkinter dialog. 
  When working in a GUI desktop environment, I find myself spreading
my files out a lot more than I did in Unix, and even worse, moving
them around -- especially putting current or new work on the desktop,
putting it away in the proper folder only when done. Also, since I
don't have to type pathnames very often, my working pathnames tend to get
longer than they  typically did on unix, with embedded spaces and other 
chars that make it awkward to get right. 
  ( If you've masteded emaces or alpha mode for interactive execution,
then you also probably don't have much use for these. ) 

 Maybe this can be done more cleanly using the 'imp' module, but this
was a quick hack and I didn't want to dig out the library docs. ( I was
at home where they weren't a web-click away. ) 



# Here's the mac specific function -- which really just removes the 
# macisms from StandardGetFile, so the rest of the code can be more
# portable. 

from macfs import StandardGetFile
def GetFileDialog( ):
	result = StandardGetFile()
	if result[1]:
		return result[0].as_pathname()


# This does import via the file dialog, doing a reload if necessary -
# which is frequent when testing and debugging modules. 

from os import path
import sys

def Import( filename=None ):
	if not filename:
		filename = GetFileDialog()
	if filename:
		dir,name = path.split( filename )
		modname,ext = path.splitext( name )
		sys.path.insert(0,dir)  # temporarily insert this path at head of list
		if modname in sys.modules.keys(): 
			PREVIOUSLY_LOADED = 1
		else:
			PREVIOUSLY_LOADED = None
		try:
			module = __import__(modname)
			setattr( __main__, modname, module )	# assuming interactive use
			if PREVIOUSLY_LOADED:  	# if it's already loaded
				reload( module )	# we need to do a reload
		finally: 
			del sys.path[0]		# undo temporary path insertion 
		


# And sometimes there are "if name == '__main__' :" bits of code to
# be tested. On unix, without the GUI startup time in a process, it's
# trivial to quit and run it from the shell, but it's a pain on the
# Mac to quit and restart Python for some trivial tests. 
# Also, sometimes I want to write code in the editor and run it from
# main. 

import __main__

def Source( filename=None, into=__main__ ):
	if not filename:
		filename = GetFileDialog()
	if filename: 
		exec open(filename).read() in into.__dict__

# This is just here for testing. Souce/Import myself.
print __name__


  Further ideas: 

   Maybe this ought to be part of the MacPython shell's implementation --
i.e. "Import" and "Source" ought to be added to the standard menu. 
( Counter argument: Then there's one more thing to take out of applications
  where it doesn't belong. )

   Using the 'imp' module, it could be added to the default import. 
 Unfortunately "import" with no module name is a syntax error, so 
 we can't use a null import to trigger the dialog. But we could reserve
 a module name, so that "import xxx" triggers the dialog. 


- Steve Majewski





_______________
PYTHONMAC-SIG  - SIG on Python for the Apple Macintosh

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