"""Command-line user interface of igraph

The command-line interface launches a Python shell with the igraph
module automatically imported into the main namespace. This is mostly a
convenience module and it is used only by the C{igraph} command line
script which executes a suitable Python shell and automatically imports
C{igraph}'s classes and functions in the top-level namespace.

Supported Python shells are:

  - IPython shell (class L{IPythonShell})
  - Classic Python shell (class L{ClassicPythonShell})
"""
from igraph import *
from igraph import __version__
import sys

class Shell(object):
	"""Superclass of the embeddable shells supported by igraph"""

	def __init__(self):
		raise ValueError, "abstract class"
	def __call__(self):
		raise ValueError, "abstract class"

class IPythonShell(Shell):
	"""IPython embedded shell interface.

	This class allows igraph to be embedded in IPython's interactive shell."""

	def __init__(self):
		"""Constructor.

		Imports IPython's embedded shell with separator lines removed."""
		from IPython.Shell import IPShellEmbed
		self._shell = IPShellEmbed(['-nosep'])

	def __call__(self):
		"""Starts the embedded shell."""
		print "igraph %s running inside" % __version__,
		print self._shell.IP.BANNER,
		self._shell()

class ClassicPythonShell(Shell):
	"""Classic Python shell interface.

	This class allows igraph to be embedded in Python's shell."""
	def __init__(self):
		from code import InteractiveConsole
		self._shell = InteractiveConsole()

	def __call__(self):
		print >>sys.stderr, "igraph %s running inside " % __version__,
		self._shell.runsource("from igraph import *")
		self._shell.interact()

shell_classes = [IPythonShell, ClassicPythonShell]

def main():
	for shell_class in shell_classes:
		try:
			shell = shell_class()
			break
		except:
			# Try the next one
			pass

	if isinstance(shell, Shell):
		shell()
	else:
		print "No suitable Python shell was found"

if __name__ == '__main__': main()



syntax highlighted by Code2HTML, v. 0.9.1