3 """Utilities for the interactive athena prompt."""
8 import AthenaCommon.Utils.unixtools
as unixtools
9 from AthenaCommon.Logging
import log
11 _shellCommands = [
'echo',
'emacs',
'env',
'ls',
'less',
'more',
'pico',
'vi' ]
12 _unacceptable = [
'false',
'true',
'sys' ]
14 _NAMEREX = re.compile(
r"named? '?(?P<%s>[\w\d]+)'?" % _NAME )
19 """Provide shell escapes from the prompt by catching name and syntax errors."""
23 log.debug(
'shell short-cuts enabled' )
27 log.debug(
'shell short-cuts disabled' )
33 if isinstance( value, NameError ):
34 res = _NAMEREX.search(
str(value) )
36 cmd = res.group( _NAME )
39 if cmd
in _unacceptable:
42 elif isinstance( value, SyntaxError ):
51 if exe ==
'cd' and len(args) == 2:
53 log.info(
'new directory: %s', os.getcwd() )
56 if exe ==
'help' and len(args) == 2:
58 exec (
'help( %s )' % args[1]
in __main__.__dict__, __main__.__dict__)
62 if exe
not in _shellCommands:
63 log.debug(
'accepting executable "%s"', exe )
64 if unixtools.which( exe ):
65 _shellCommands.append( exe )
70 log.debug(
'executing shell command "%s"', cmd )
79 """Configure interactive prompt. The optional completionDict is used to
80 configure the readline completer."""
87 fhistory = os.path.expanduser(
'~/.athena.history' )
89 if completionDict
is not None:
90 readline.set_completer(rlcompleter.Completer(completionDict).complete)
92 readline.parse_and_bind(
'tab: complete' )
93 readline.parse_and_bind(
'set show-all-if-ambiguous On' )
95 if os.path.exists( fhistory ):
96 readline.read_history_file( fhistory )
98 readline.set_history_length( 1024 )
101 atexit.register( readline.write_history_file, fhistory )