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' )
35 if isinstance( value, NameError ):
36 res = _NAMEREX.search(
str(value) )
38 cmd = res.group( _NAME )
41 if cmd
in _unacceptable:
44 elif isinstance( value, SyntaxError ):
53 if exe ==
'cd' and len(args) == 2:
55 log.info(
'new directory: %s', os.getcwd() )
58 if exe ==
'help' and len(args) == 2:
60 exec (
'help( %s )' % args[1]
in __main__.__dict__, __main__.__dict__)
64 if exe
not in _shellCommands:
65 log.debug(
'accepting executable "%s"', exe )
66 if unixtools.which( exe ):
67 _shellCommands.append( exe )
72 log.debug(
'executing shell command "%s"', cmd )
81 """Configure interactive prompt. The optional completionDict is used to
82 configure the readline completer."""
89 fhistory = os.path.expanduser(
'~/.athena.history' )
91 if completionDict
is not None:
92 readline.set_completer(rlcompleter.Completer(completionDict).complete)
94 readline.parse_and_bind(
'tab: complete' )
95 readline.parse_and_bind(
'set show-all-if-ambiguous On' )
97 if os.path.exists( fhistory ):
98 readline.read_history_file( fhistory )
100 readline.set_history_length( 1024 )
103 atexit.register( readline.write_history_file, fhistory )