<s> from __future__ import unicode_literals <EOL> from __future__ import print_function <EOL> import click <EOL> import os <EOL> import platform <EOL> import subprocess <EOL> import traceback <EOL> import webbrowser <EOL> from prompt_toolkit import AbortAction , Application , CommandLineInterface <EOL> from prompt_toolkit . enums import DEFAULT_BUFFER <EOL> from prompt_toolkit . filters import Always , HasFocus , IsDone <EOL> from prompt_toolkit . interface import AcceptAction <EOL> from prompt_toolkit . layout . processors import HighlightMatchingBracketProcessor , ConditionalProcessor <EOL> from prompt_toolkit . buffer import Buffer <EOL> from prompt_toolkit . shortcuts import create_default_layout , create_eventloop <EOL> from prompt_toolkit . history import FileHistory <EOL> from prompt_toolkit . key_binding . input_processor import KeyPress <EOL> from prompt_toolkit . keys import Keys <EOL> from prompt_toolkit . auto_suggest import AutoSuggestFromHistory <EOL> from awscli import completer as awscli_completer <EOL> from . completer import AwsCompleter <EOL> from . lexer import CommandLexer <EOL> from . config import Config <EOL> from . style import StyleFactory <EOL> from . keys import KeyManager <EOL> from . toolbar import Toolbar <EOL> from . commands import AwsCommands <EOL> from . logger import SawsLogger <EOL> from . __init__ import __version__ <EOL> class Saws ( object ) : <EOL> """<STR_LIT>""" <EOL> PYGMENTS_CMD = '<STR_LIT>' <EOL> def __init__ ( self , refresh_resources = True ) : <EOL> """<STR_LIT>""" <EOL> self . aws_cli = None <EOL> self . key_manager = None <EOL> self . config = Config ( ) <EOL> self . config_obj = self . config . read_configuration ( ) <EOL> self . theme = self . config_obj [ self . config . MAIN ] [ self . config . THEME ] <EOL> self . logger = SawsLogger ( <EOL> __name__ , <EOL> self . config_obj [ self . config . MAIN ] [ self . config . LOG_FILE ] , <EOL> self . config_obj [ self . config . MAIN ] [ self . config . LOG_LEVEL ] ) . logger <EOL> self . all_commands = AwsCommands ( ) . all_commands <EOL> self . commands = self . all_commands [ AwsCommands . CommandType . COMMANDS . value ] <EOL> self . sub_commands = self . all_commands [ AwsCommands . CommandType . SUB_COMMANDS . value ] <EOL> self . completer = AwsCompleter ( <EOL> awscli_completer , <EOL> self . all_commands , <EOL> self . config , <EOL> self . config_obj , <EOL> self . log_exception , <EOL> fuzzy_match = self . get_fuzzy_match ( ) , <EOL> shortcut_match = self . get_shortcut_match ( ) ) <EOL> if refresh_resources : <EOL> self . completer . refresh_resources_and_options ( ) <EOL> self . _create_cli ( ) <EOL> def log_exception ( self , e , traceback , echo = False ) : <EOL> """<STR_LIT>""" <EOL> self . logger . debug ( '<STR_LIT>' , str ( e ) ) <EOL> self . logger . error ( "<STR_LIT>" , traceback . format_exc ( ) ) <EOL> if echo : <EOL> click . secho ( str ( e ) , fg = '<STR_LIT>' ) <EOL> def set_color ( self , color ) : <EOL> """<STR_LIT>""" <EOL> self . config_obj [ self . config . MAIN ] [ self . config . COLOR ] = color <EOL> def get_color ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . config_obj [ self . config . MAIN ] . as_bool ( self . config . COLOR ) <EOL> def set_fuzzy_match ( self , fuzzy ) : <EOL> """<STR_LIT>""" <EOL> self . config_obj [ self . config . MAIN ] [ self . config . FUZZY ] = fuzzy <EOL> self . completer . fuzzy_match = fuzzy <EOL> def get_fuzzy_match ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . config_obj [ self . config . MAIN ] . as_bool ( self . config . FUZZY ) <EOL> def set_shortcut_match ( self , shortcut ) : <EOL> """<STR_LIT>""" <EOL> self . config_obj [ self . config . MAIN ] [ self . config . SHORTCUT ] = shortcut <EOL> self . completer . shortcut_match = shortcut <EOL> def get_shortcut_match ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . config_obj [ self . config . MAIN ] . as_bool ( self . config . SHORTCUT ) <EOL> def refresh_resources_and_options ( self ) : <EOL> """<STR_LIT>""" <EOL> self . completer . refresh_resources_and_options ( force_refresh = True ) <EOL> def handle_docs ( self , text = None , from_fkey = False ) : <EOL> """<STR_LIT>""" <EOL> base_url = '<STR_LIT>' <EOL> index_html = '<STR_LIT>' <EOL> if text is None : <EOL> text = self . aws_cli . current_buffer . document . text <EOL> if from_fkey : <EOL> text = text . strip ( ) + '<STR_LIT:U+0020>' + AwsCommands . AWS_DOCS <EOL> tokens = text . split ( ) <EOL> if len ( tokens ) > <NUM_LIT:2> and tokens [ - <NUM_LIT:1> ] == AwsCommands . AWS_DOCS : <EOL> prev_word = tokens [ - <NUM_LIT:2> ] <EOL> if prev_word in self . commands : <EOL> prev_word = prev_word + '<STR_LIT:/>' <EOL> url = base_url + prev_word + index_html <EOL> webbrowser . open ( url ) <EOL> return True <EOL> elif prev_word in self . sub_commands : <EOL> command_url = tokens [ - <NUM_LIT:3> ] + '<STR_LIT:/>' <EOL> sub_command_url = tokens [ - <NUM_LIT:2> ] + '<STR_LIT>' <EOL> url = base_url + command_url + sub_command_url <EOL> webbrowser . open ( url ) <EOL> return True <EOL> webbrowser . open ( base_url + index_html ) <EOL> if from_fkey or AwsCommands . AWS_DOCS in tokens : <EOL> webbrowser . open ( base_url + index_html ) <EOL> return True <EOL> return False <EOL> def _handle_cd ( self , text ) : <EOL> """<STR_LIT>""" <EOL> CD_CMD = '<STR_LIT>' <EOL> stripped_text = text . strip ( ) <EOL> if stripped_text . startswith ( CD_CMD ) : <EOL> directory = '<STR_LIT>' <EOL> if stripped_text == CD_CMD : <EOL> directory = os . path . expanduser ( '<STR_LIT>' ) <EOL> else : <EOL> tokens = text . split ( CD_CMD + '<STR_LIT:U+0020>' ) <EOL> directory = tokens [ - <NUM_LIT:1> ] <EOL> try : <EOL> os . chdir ( directory ) <EOL> except OSError as e : <EOL> self . log_exception ( e , traceback , echo = True ) <EOL> return True <EOL> return False <EOL> def _colorize_output ( self , text ) : <EOL> """<STR_LIT>""" <EOL> stripped_text = text . strip ( ) <EOL> if not self . get_color ( ) or stripped_text == '<STR_LIT>' : <EOL> return text <EOL> if AwsCommands . AWS_COMMAND not in stripped_text . split ( ) : <EOL> return text <EOL> excludes = [ AwsCommands . AWS_CONFIGURE , <EOL> AwsCommands . AWS_HELP , <EOL> '<STR_LIT:|>' ] <EOL> if not any ( substring in stripped_text for substring in excludes ) : <EOL> return text . strip ( ) + self . PYGMENTS_CMD <EOL> else : <EOL> return text <EOL> def _handle_keyboard_interrupt ( self , e , platform ) : <EOL> """<STR_LIT>""" <EOL> if platform == '<STR_LIT>' : <EOL> raise e <EOL> else : <EOL> self . aws_cli . renderer . clear ( ) <EOL> self . aws_cli . input_processor . feed_key ( KeyPress ( Keys . ControlM , '<STR_LIT>' ) ) <EOL> def _process_command ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if AwsCommands . AWS_COMMAND in text : <EOL> text = self . completer . replace_shortcut ( text ) <EOL> if self . handle_docs ( text ) : <EOL> return <EOL> try : <EOL> if not self . _handle_cd ( text ) : <EOL> text = self . _colorize_output ( text ) <EOL> subprocess . call ( text , shell = True ) <EOL> print ( '<STR_LIT>' ) <EOL> except KeyboardInterrupt as e : <EOL> self . _handle_keyboard_interrupt ( e , platform . system ( ) ) <EOL> except Exception as e : <EOL> self . log_exception ( e , traceback , echo = True ) <EOL> def _create_cli ( self ) : <EOL> """<STR_LIT>""" <EOL> history = FileHistory ( os . path . expanduser ( '<STR_LIT>' ) ) <EOL> toolbar = Toolbar ( self . get_color , <EOL> self . get_fuzzy_match , <EOL> self . get_shortcut_match ) <EOL> layout = create_default_layout ( <EOL> message = '<STR_LIT>' , <EOL> reserve_space_for_menu = True , <EOL> lexer = CommandLexer , <EOL> get_bottom_toolbar_tokens = toolbar . handler , <EOL> extra_input_processors = [ <EOL> ConditionalProcessor ( <EOL> processor = HighlightMatchingBracketProcessor ( <EOL> chars = '<STR_LIT>' ) , <EOL> filter = HasFocus ( DEFAULT_BUFFER ) & ~ IsDone ( ) ) <EOL> ] <EOL> ) <EOL> cli_buffer = Buffer ( <EOL> history = history , <EOL> auto_suggest = AutoSuggestFromHistory ( ) , <EOL> enable_history_search = True , <EOL> completer = self . completer , <EOL> complete_while_typing = Always ( ) , <EOL> accept_action = AcceptAction . RETURN_DOCUMENT ) <EOL> self . key_manager = KeyManager ( <EOL> self . set_color , <EOL> self . get_color , <EOL> self . set_fuzzy_match , <EOL> self . get_fuzzy_match , <EOL> self . set_shortcut_match , <EOL> self . get_shortcut_match , <EOL> self . refresh_resources_and_options , <EOL> self . handle_docs ) <EOL> style_factory = StyleFactory ( self . theme ) <EOL> application = Application ( <EOL> mouse_support = False , <EOL> style = style_factory . style , <EOL> layout = layout , <EOL> buffer = cli_buffer , <EOL> key_bindings_registry = self . key_manager . manager . registry , <EOL> on_exit = AbortAction . RAISE_EXCEPTION , <EOL> on_abort = AbortAction . RETRY , <EOL> ignore_case = True ) <EOL> eventloop = create_eventloop ( ) <EOL> self . aws_cli = CommandLineInterface ( <EOL> application = application , <EOL> eventloop = eventloop ) <EOL> def run_cli ( self ) : <EOL> """<STR_LIT>""" <EOL> print ( '<STR_LIT>' , __version__ ) <EOL> print ( '<STR_LIT>' , self . theme ) <EOL> while True : <EOL> document = self . aws_cli . run ( ) <EOL> self . _process_command ( document . text ) </s>
