<s> import os <EOL> import sys <EOL> import bdb <EOL> import pdb <EOL> import importlib <EOL> import shlex <EOL> import subprocess <EOL> import click <EOL> from click . utils import make_str <EOL> try : <EOL> import raven <EOL> except ImportError : <EOL> raven = None <EOL> from . import __version__ <EOL> from . settings import load_config , diff_config , as_dict <EOL> from . settings import PROJECT_CONFIG , DEFAULT_CONFIG <EOL> from . base import Lancet , WarnIntegrationHelper , ShellIntegrationHelper <EOL> from . utils import hr <EOL> CONTEXT_SETTINGS = dict ( help_option_names = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> IGNORED_EXCEPTIONS = set ( [ <EOL> bdb . BdbQuit , <EOL> ] ) <EOL> def get_sentry_client ( sentry_dsn ) : <EOL> return raven . Client ( <EOL> sentry_dsn , <EOL> release = __version__ , <EOL> processors = ( <EOL> '<STR_LIT>' , <EOL> ) <EOL> ) <EOL> class SubprocessExecuter ( click . BaseCommand ) : <EOL> def parse_args ( self , ctx , args ) : <EOL> ctx . args = args <EOL> return args <EOL> def invoke ( self , ctx ) : <EOL> ctx . exit ( subprocess . call ( ctx . args [ <NUM_LIT:0> ] , shell = True ) ) <EOL> class ConfigurableLoader ( click . Group ) : <EOL> _config = None <EOL> _path_setup_complete = False <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . __class__ . setup_path ( ) <EOL> super ( ) . __init__ ( * args , ** kwargs ) <EOL> @ classmethod <EOL> def setup_path ( cls ) : <EOL> if not cls . _path_setup_complete : <EOL> paths = cls . get_config ( ) . getlist ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for p in reversed ( paths ) : <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . expanduser ( p ) ) <EOL> @ classmethod <EOL> def get_config ( cls ) : <EOL> if not cls . _config : <EOL> if os . path . exists ( PROJECT_CONFIG ) : <EOL> cls . _config = load_config ( PROJECT_CONFIG ) <EOL> else : <EOL> cls . _config = load_config ( ) <EOL> return cls . _config <EOL> @ classmethod <EOL> def get_configured_commands ( cls , config = None ) : <EOL> if config is None : <EOL> config = cls . get_config ( ) <EOL> return config . options ( '<STR_LIT>' ) <EOL> @ classmethod <EOL> def get_configured_aliases ( cls , config = None ) : <EOL> if config is None : <EOL> config = cls . get_config ( ) <EOL> return config . options ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def show_help_all ( ctx , param , value ) : <EOL> if value and not ctx . resilient_parsing : <EOL> ctx . show_hidden_subcommands = True <EOL> click . echo ( ctx . get_help ( ) ) <EOL> ctx . exit ( ) <EOL> def list_commands ( self , ctx ) : <EOL> commands = set ( super ( ) . list_commands ( ctx ) ) <EOL> commands = commands . union ( self . get_configured_commands ( ) ) <EOL> if not getattr ( ctx , '<STR_LIT>' , True ) : <EOL> commands = [ c for c in commands if not c . startswith ( '<STR_LIT:_>' ) ] <EOL> return sorted ( commands ) <EOL> def list_aliases ( self , ctx ) : <EOL> return sorted ( self . get_configured_aliases ( ) ) <EOL> def get_help ( self , ctx ) : <EOL> ctx . show_hidden_subcommands = getattr ( <EOL> ctx , '<STR_LIT>' , False ) <EOL> return super ( ) . get_help ( ctx ) <EOL> def format_options ( self , ctx , formatter ) : <EOL> super ( ) . format_options ( ctx , formatter ) <EOL> self . format_aliases ( ctx , formatter ) <EOL> def format_aliases ( self , ctx , formatter ) : <EOL> rows = [ ] <EOL> for alias in self . list_aliases ( ctx ) : <EOL> rows . append ( ( alias , self . get_config ( ) . get ( '<STR_LIT>' , alias ) ) ) <EOL> if rows : <EOL> with formatter . section ( '<STR_LIT>' ) : <EOL> formatter . write_dl ( rows ) <EOL> def resolve_command ( self , ctx , args ) : <EOL> cmd_name = make_str ( args [ <NUM_LIT:0> ] ) <EOL> if cmd_name in self . get_configured_aliases ( ) : <EOL> if cmd_name in self . list_commands ( ctx ) : <EOL> click . secho ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( cmd_name ) , <EOL> fg = '<STR_LIT>' ) <EOL> else : <EOL> alias = self . get_config ( ) . get ( '<STR_LIT>' , cmd_name ) <EOL> args = args [ <NUM_LIT:1> : ] <EOL> if alias . startswith ( '<STR_LIT:!>' ) : <EOL> cmd = SubprocessExecuter ( '<STR_LIT>' ) <EOL> additional_args = '<STR_LIT:U+0020>' . join ( shlex . quote ( a ) for a in args ) <EOL> return '<STR_LIT>' , cmd , [ alias [ <NUM_LIT:1> : ] + '<STR_LIT:U+0020>' + additional_args ] <EOL> else : <EOL> args = shlex . split ( alias ) + args <EOL> return super ( ) . resolve_command ( ctx , args ) <EOL> def get_command ( self , ctx , name ) : <EOL> if name in self . get_configured_commands ( ) : <EOL> path = self . get_config ( ) . get ( '<STR_LIT>' , name ) <EOL> module_path , attr_name = path . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> module = importlib . import_module ( module_path ) <EOL> return getattr ( module , attr_name ) <EOL> else : <EOL> return super ( ) . get_command ( ctx , name ) <EOL> @ click . command ( context_settings = CONTEXT_SETTINGS , cls = ConfigurableLoader ) <EOL> @ click . version_option ( version = __version__ , message = '<STR_LIT>' ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , default = False , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , is_eager = True , expose_value = False , <EOL> callback = ConfigurableLoader . show_help_all , <EOL> help = '<STR_LIT>' ) <EOL> @ click . pass_context <EOL> def main ( ctx , debug ) : <EOL> if debug : <EOL> def exception_handler ( type , value , traceback ) : <EOL> click . secho ( '<STR_LIT>' <EOL> '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> hr ( fg = '<STR_LIT>' ) <EOL> sys . __excepthook__ ( type , value , traceback ) <EOL> click . secho ( '<STR_LIT>' <EOL> '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> hr ( fg = '<STR_LIT>' ) <EOL> pdb . post_mortem ( traceback ) <EOL> sys . excepthook = exception_handler <EOL> try : <EOL> integration_helper = ShellIntegrationHelper ( <EOL> os . environ [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> integration_helper = WarnIntegrationHelper ( ) <EOL> if os . path . exists ( PROJECT_CONFIG ) : <EOL> config = load_config ( PROJECT_CONFIG ) <EOL> else : <EOL> config = load_config ( ) <EOL> ctx . obj = Lancet ( config , integration_helper ) <EOL> ctx . obj . call_on_close = ctx . call_on_close <EOL> ctx . call_on_close ( integration_helper . close ) <EOL> sentry_dsn = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if sentry_dsn and not debug : <EOL> if not raven : <EOL> click . secho ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> fg = '<STR_LIT>' ) <EOL> else : <EOL> sentry_client = get_sentry_client ( sentry_dsn ) <EOL> def exception_handler ( type , value , traceback ) : <EOL> settings_diff = diff_config ( <EOL> load_config ( DEFAULT_CONFIG , defaults = False ) , <EOL> config , <EOL> exclude = set ( [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] ) <EOL> ) <EOL> sys . __excepthook__ ( type , value , traceback ) <EOL> if type in IGNORED_EXCEPTIONS : <EOL> return <EOL> click . echo ( ) <EOL> hr ( fg = '<STR_LIT>' ) <EOL> click . secho ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> error_id = sentry_client . captureException ( <EOL> ( type , value , traceback ) , <EOL> extra = { <EOL> '<STR_LIT>' : as_dict ( settings_diff ) , <EOL> '<STR_LIT>' : os . getcwd ( ) , <EOL> } , <EOL> ) [ <NUM_LIT:0> ] <EOL> click . secho ( '<STR_LIT>' . format ( error_id ) , fg = '<STR_LIT>' ) <EOL> sys . excepthook = exception_handler <EOL> @ main . command ( ) <EOL> def _setup_helper ( ) : <EOL> """<STR_LIT>""" <EOL> base = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> helper = os . path . join ( base , '<STR_LIT>' ) <EOL> with open ( helper ) as fh : <EOL> click . echo ( fh . read ( ) ) <EOL> @ main . command ( ) <EOL> @ click . pass_context <EOL> def _commands ( ctx ) : <EOL> """<STR_LIT>""" <EOL> ctx = ctx . parent <EOL> ctx . show_hidden_subcommands = False <EOL> main = ctx . command <EOL> for subcommand in main . list_commands ( ctx ) : <EOL> cmd = main . get_command ( ctx , subcommand ) <EOL> if cmd is None : <EOL> continue <EOL> help = cmd . short_help or '<STR_LIT>' <EOL> click . echo ( '<STR_LIT>' . format ( subcommand , help ) ) <EOL> @ main . command ( ) <EOL> @ click . argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , required = False ) <EOL> @ click . pass_context <EOL> def _arguments ( ctx , command_name = None ) : <EOL> """<STR_LIT>""" <EOL> ctx = ctx . parent <EOL> main = ctx . command <EOL> if command_name : <EOL> command = main . get_command ( ctx , command_name ) <EOL> if not command : <EOL> return <EOL> else : <EOL> command = main <EOL> types = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> all_params = sorted ( command . get_params ( ctx ) , <EOL> key = lambda p : types . index ( p . param_type_name ) ) <EOL> def get_name ( param ) : <EOL> return max ( param . opts , key = len ) <EOL> for param in all_params : <EOL> if param . param_type_name == '<STR_LIT>' : <EOL> option = get_name ( param ) <EOL> same_dest = [ get_name ( p ) for p in all_params <EOL> if p . name == param . name ] <EOL> if same_dest : <EOL> option = '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( same_dest ) ) + option <EOL> if param . help : <EOL> option += '<STR_LIT>' . format ( param . help or '<STR_LIT>' ) <EOL> if not param . is_flag : <EOL> option += '<STR_LIT>' <EOL> click . echo ( option ) <EOL> elif param . param_type_name == '<STR_LIT>' : <EOL> option = get_name ( param ) <EOL> click . echo ( '<STR_LIT>' . format ( option ) ) <EOL> @ main . command ( ) <EOL> @ click . argument ( '<STR_LIT>' , required = False ) <EOL> @ click . pass_context <EOL> def _autocomplete ( ctx , shell ) : <EOL> """<STR_LIT>""" <EOL> if not shell : <EOL> shell = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> shell = os . path . basename ( shell ) . lower ( ) <EOL> if not shell : <EOL> click . secho ( '<STR_LIT>' <EOL> '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> ctx . exit ( - <NUM_LIT:1> ) <EOL> base = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> autocomplete = os . path . join ( base , '<STR_LIT>' , '<STR_LIT>' . format ( shell ) ) <EOL> if not os . path . exists ( autocomplete ) : <EOL> click . secho ( '<STR_LIT>' <EOL> '<STR_LIT>' , fg = '<STR_LIT>' ) <EOL> ctx . exit ( - <NUM_LIT:1> ) <EOL> with open ( autocomplete ) as fh : <EOL> click . echo ( fh . read ( ) ) </s>
