##// END OF EJS Templates
Add runner factory to irunner
fperez -
Show More
@@ -337,6 +337,52 b' class SAGERunner(InteractiveRunner):'
337 prompts = ['sage: ',r'\s*\.\.\. ']
337 prompts = ['sage: ',r'\s*\.\.\. ']
338 InteractiveRunner.__init__(self,program,prompts,args,out,echo)
338 InteractiveRunner.__init__(self,program,prompts,args,out,echo)
339
339
340
341 class RunnerFactory(object):
342 """Code runner factory.
343
344 This class provides an IPython code runner, but enforces that only one
345 runner is ever instantiated. The runner is created based on the extension
346 of the first file to run, and it raises an exception if a runner is later
347 requested for a different extension type.
348
349 This ensures that we don't generate example files for doctest with a mix of
350 python and ipython syntax.
351 """
352
353 def __init__(self,out=sys.stdout):
354 """Instantiate a code runner."""
355
356 self.out = out
357 self.runner = None
358 self.runnerClass = None
359
360 def _makeRunner(self,runnerClass):
361 self.runnerClass = runnerClass
362 self.runner = runnerClass(out=self.out)
363 return self.runner
364
365 def __call__(self,fname):
366 """Return a runner for the given filename."""
367
368 if fname.endswith('.py'):
369 runnerClass = PythonRunner
370 elif fname.endswith('.ipy'):
371 runnerClass = IPythonRunner
372 else:
373 raise ValueError('Unknown file type for Runner: %r' % fname)
374
375 if self.runner is None:
376 return self._makeRunner(runnerClass)
377 else:
378 if runnerClass==self.runnerClass:
379 return self.runner
380 else:
381 e='A runner of type %r can not run file %r' % \
382 (self.runnerClass,fname)
383 raise ValueError(e)
384
385
340 # Global usage string, to avoid indentation issues if typed in a function def.
386 # Global usage string, to avoid indentation issues if typed in a function def.
341 MAIN_USAGE = """
387 MAIN_USAGE = """
342 %prog [options] file_to_run
388 %prog [options] file_to_run
@@ -1,5 +1,8 b''
1 2007-08-04 Fernando Perez <Fernando.Perez@colorado.edu>
1 2007-08-04 Fernando Perez <Fernando.Perez@colorado.edu>
2
2
3 * IPython/irunner.py (RunnerFactory): Add new factory class for
4 creating reusable runners based on filenames.
5
3 * IPython/Extensions/ipy_profile_doctest.py: New profile for
6 * IPython/Extensions/ipy_profile_doctest.py: New profile for
4 doctest support. It sets prompts/exceptions as similar to
7 doctest support. It sets prompts/exceptions as similar to
5 standard Python as possible, so that ipython sessions in this
8 standard Python as possible, so that ipython sessions in this
General Comments 0
You need to be logged in to leave comments. Login now