<s> import os <EOL> import re <EOL> try : <EOL> import readline <EOL> except ImportError : <EOL> import warnings <EOL> warnings . warn ( '<STR_LIT>' ) <EOL> readline = None <EOL> import sys <EOL> from nagios_cli import command , nagios <EOL> from nagios_cli . context import Context <EOL> from nagios_cli . ui import UI <EOL> from nagios_cli . objects import Objects <EOL> RE_SPACING = re . compile ( r'<STR_LIT>' ) <EOL> RE_RUN_GIVEN = re . compile ( r'<STR_LIT>' ) <EOL> class CLI ( object ) : <EOL> def __init__ ( self , config ) : <EOL> self . config = config <EOL> self . ui = UI ( config ) <EOL> self . commands = { } <EOL> self . context = Context ( ) <EOL> self . matches = [ ] <EOL> self . objects = Objects ( self , config ) <EOL> self . command = nagios . Command ( config ) <EOL> self . setup_prompt ( ) <EOL> if readline : <EOL> self . setup_readline ( ) <EOL> self . setup_commands ( ) <EOL> def setup_prompt ( self ) : <EOL> prompt = self . config . get ( '<STR_LIT>' , '<STR_LIT::>' ) . join ( <EOL> map ( str , self . context . stack ) <EOL> ) <EOL> self . prompt = self . ui . color ( '<STR_LIT>' , <EOL> self . config . get ( '<STR_LIT>' ) % ( <EOL> self . ui . color ( '<STR_LIT>' , <EOL> self . config . get ( '<STR_LIT>' , '<STR_LIT::>' ) . join ( <EOL> map ( str , self . context . stack ) <EOL> ) <EOL> ) <EOL> ) <EOL> ) + '<STR_LIT:U+0020>' <EOL> def setup_readline ( self ) : <EOL> history = self . config . get ( '<STR_LIT>' ) <EOL> if history : <EOL> history = os . path . expanduser ( history ) <EOL> try : <EOL> readline . read_history_file ( history ) <EOL> except IOError : <EOL> pass <EOL> import atexit <EOL> atexit . register ( readline . write_history_file , history ) <EOL> readline . set_completer ( self . complete ) <EOL> readline . parse_and_bind ( '<STR_LIT>' ) <EOL> readline . set_completer_delims ( readline . get_completer_delims ( ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) ) <EOL> def setup_commands ( self ) : <EOL> for item in command . DEFAULT : <EOL> self . command_add ( item ) <EOL> def flush ( self ) : <EOL> sys . stdout . flush ( ) <EOL> def send ( self , text , error = False ) : <EOL> if error : <EOL> sys . stderr . write ( text ) <EOL> else : <EOL> sys . stdout . write ( text ) <EOL> def sendline ( self , text , error = False ) : <EOL> self . send ( text + '<STR_LIT:\n>' , error = error ) <EOL> def error ( self , text ) : <EOL> self . sendline ( self . ui . color ( '<STR_LIT:error>' , text ) , error = True ) <EOL> def warn ( self , text ) : <EOL> self . sendline ( self . ui . color ( '<STR_LIT>' , text ) , error = True ) <EOL> def ask_bool ( self , question , min_length = None , default = None ) : <EOL> if min_length : <EOL> question = question . ljust ( min_length , '<STR_LIT:U+0020>' ) <EOL> answer = None <EOL> prompt = question + '<STR_LIT>' <EOL> if default is not None : <EOL> if default is True or default in '<STR_LIT>' : <EOL> prompt = question + '<STR_LIT>' <EOL> default = True <EOL> elif default is False or default in '<STR_LIT>' : <EOL> prompt = question + '<STR_LIT>' <EOL> default = False <EOL> else : <EOL> default = None <EOL> while answer is None : <EOL> answer = raw_input ( prompt ) <EOL> if answer : <EOL> if answer in '<STR_LIT>' : <EOL> answer = True <EOL> elif answer in '<STR_LIT>' : <EOL> answer = False <EOL> else : <EOL> self . error ( '<STR_LIT>' ) <EOL> answer = None <EOL> else : <EOL> answer = default <EOL> return answer <EOL> def ask_str ( self , question , min_length = None ) : <EOL> if min_length : <EOL> question = question . ljust ( min_length , '<STR_LIT:U+0020>' ) <EOL> answer = None <EOL> prompt = question + '<STR_LIT>' <EOL> while answer is None : <EOL> answer = raw_input ( prompt ) <EOL> if not answer : <EOL> answer = None <EOL> return answer <EOL> def command_add ( self , obj ) : <EOL> self . commands [ obj . name ] = obj <EOL> self . commands [ obj . name ] . cli = self <EOL> def command_run ( self , name , args ) : <EOL> if name not in self . commands : <EOL> matches = [ ] <EOL> for state in xrange ( <NUM_LIT:0> , len ( self . commands ) ) : <EOL> match = self . complete ( name , state ) <EOL> if match is None : <EOL> break <EOL> else : <EOL> matches . append ( match ) <EOL> if len ( matches ) == <NUM_LIT:1> : <EOL> return self . command_run ( matches [ <NUM_LIT:0> ] , args ) <EOL> return False <EOL> if not self . commands [ name ] . valid_in_context ( self . context ) : <EOL> print '<STR_LIT>' <EOL> return False <EOL> try : <EOL> self . commands [ name ] . run ( * args ) <EOL> except TypeError , e : <EOL> msg = e . message <EOL> info = RE_RUN_GIVEN . search ( e . message ) <EOL> if info : <EOL> args = self . commands [ name ] . run . func_code . co_argcount - <NUM_LIT:1> <EOL> try : <EOL> least = args - len ( self . commands [ name ] . run . func_defaults ) <EOL> except TypeError : <EOL> least = <NUM_LIT:0> <EOL> given = int ( info . groupdict ( ) [ '<STR_LIT>' ] ) - <NUM_LIT:1> <EOL> if args == least : <EOL> error = '<STR_LIT>' <EOL> count = args <EOL> elif given < least : <EOL> error = '<STR_LIT>' <EOL> count = least <EOL> elif given > args : <EOL> error = '<STR_LIT>' <EOL> count = args <EOL> else : <EOL> print '<STR_LIT>' , args , least , given <EOL> return <EOL> plural = '<STR_LIT>' <EOL> if count != <NUM_LIT:1> : <EOL> plural = '<STR_LIT:s>' <EOL> self . error ( '<STR_LIT>' % ( name , error , count , plural , given ) ) <EOL> return True <EOL> else : <EOL> print dir ( e ) <EOL> print e . message <EOL> print e . args <EOL> raise <EOL> return True <EOL> def complete ( self , text , state ) : <EOL> if state == <NUM_LIT:0> : <EOL> if readline : <EOL> line = readline . get_line_buffer ( ) <EOL> else : <EOL> line = text <EOL> part = RE_SPACING . split ( line ) <EOL> if part and part [ <NUM_LIT:0> ] : <EOL> cmnd = part [ <NUM_LIT:0> ] <EOL> if len ( part ) == <NUM_LIT:1> : <EOL> self . matches = [ key <EOL> for key in self . commands . keys ( ) <EOL> if key . startswith ( text ) and ( <EOL> self . commands [ key ] . is_global or self . commands [ key ] . valid_in_context ( self . context ) ) ] <EOL> elif cmnd in self . commands : <EOL> text = text . replace ( cmnd , '<STR_LIT>' ) . lstrip ( ) <EOL> self . matches = self . commands [ cmnd ] . complete ( text , state ) <EOL> else : <EOL> self . matches = [ ] <EOL> else : <EOL> self . matches = [ key <EOL> for key in self . commands . keys ( ) <EOL> if self . commands [ key ] . is_global or self . commands [ key ] . valid_in_context ( self . context ) ] <EOL> try : <EOL> return self . matches [ state ] <EOL> except IndexError : <EOL> pass <EOL> def dispatch ( self , line ) : <EOL> if not line : <EOL> self . sendline ( '<STR_LIT>' ) <EOL> return False <EOL> part = RE_SPACING . split ( line ) <EOL> if not part : <EOL> self . sendline ( '<STR_LIT>' ) <EOL> return False <EOL> cmnd , args = part [ <NUM_LIT:0> ] , part [ <NUM_LIT:1> : ] <EOL> if not self . command_run ( cmnd , args ) : <EOL> self . error ( '<STR_LIT>' % ( cmnd , ) ) <EOL> return True <EOL> else : <EOL> return False <EOL> def run ( self , banner = '<STR_LIT>' ) : <EOL> if not self . config . options . quiet : <EOL> self . sendline ( banner ) <EOL> self . running = True <EOL> while self . running : <EOL> try : <EOL> line = raw_input ( self . prompt ) <EOL> except EOFError : <EOL> line = '<STR_LIT>' <EOL> except KeyboardInterrupt : <EOL> line = '<STR_LIT>' <EOL> self . dispatch ( line ) </s>
