<s> import argparse2 as argparse <EOL> from contextlib import contextmanager <EOL> import logging <EOL> import os <EOL> import sys <EOL> from textwrap import dedent <EOL> from traceback import format_exc <EOL> import colorlog <EOL> from openrcv . scripts . argparse import ( parse_log_level , HelpRequested , <EOL> UsageException ) <EOL> EXIT_STATUS_SUCCESS = <NUM_LIT:0> <EOL> EXIT_STATUS_FAIL = <NUM_LIT:1> <EOL> EXIT_STATUS_USAGE_ERROR = <NUM_LIT:2> <EOL> PROG_NAME = os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> log = logging . getLogger ( PROG_NAME ) <EOL> class DisplayNameFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> def filter ( self , record ) : <EOL> record . display_name = record . name <EOL> return True <EOL> class TruncatedDisplayNameFilter ( object ) : <EOL> """<STR_LIT>""" <EOL> def filter ( self , record ) : <EOL> parts = record . name . split ( "<STR_LIT:.>" ) <EOL> if len ( parts ) <= <NUM_LIT:3> : <EOL> display_name = record . name <EOL> else : <EOL> display_name = '<STR_LIT:.>' . join ( parts [ : <NUM_LIT:2> ] + [ '<STR_LIT:.>' , parts [ - <NUM_LIT:1> ] ] ) <EOL> record . display_name = display_name <EOL> return True <EOL> def get_filter ( level ) : <EOL> if level <= logging . DEBUG : <EOL> cls = DisplayNameFilter <EOL> else : <EOL> cls = TruncatedDisplayNameFilter <EOL> return cls ( ) <EOL> def make_formatter ( ) : <EOL> format_string = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> colors = colorlog . default_log_colors <EOL> colors [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> formatter = colorlog . ColoredFormatter ( format_string , log_colors = colors ) <EOL> return formatter <EOL> def make_log_handler ( level , file_ = None ) : <EOL> if file_ is None : <EOL> file_ = sys . stderr <EOL> handler = logging . StreamHandler ( file_ ) <EOL> filter_ = get_filter ( level ) <EOL> handler . addFilter ( filter_ ) <EOL> formatter = make_formatter ( ) <EOL> handler . setFormatter ( formatter ) <EOL> return handler <EOL> @ contextmanager <EOL> def log_config ( level , file_ = None ) : <EOL> """<STR_LIT>""" <EOL> if level is None : <EOL> level = LOG_LEVEL_DEFAULT <EOL> root = logging . getLogger ( ) <EOL> already_configured = root . hasHandlers ( ) <EOL> handler = make_log_handler ( level , file_ = file_ ) <EOL> root . addHandler ( handler ) <EOL> if not already_configured : <EOL> root . setLevel ( level ) <EOL> log . debug ( "<STR_LIT>" % logging . getLevelName ( level ) ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> yield <EOL> root . removeHandler ( handler ) <EOL> def make_usage_error ( msg , help_options ) : <EOL> text = dedent ( """<STR_LIT>""" ) . format ( msg , help_options ) <EOL> return text <EOL> def print_usage_error ( parser , msg , file_ = None ) : <EOL> if file_ is None : <EOL> file_ = sys . stderr <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> parser . print_help ( ) <EOL> parser . print_usage ( file_ ) <EOL> text = make_usage_error ( msg , parser . option_help . display ( '<STR_LIT>' ) ) <EOL> log . error ( text ) <EOL> def non_exiting_main ( parser , argv , stdout = None , log_file = None ) : <EOL> """<STR_LIT>""" <EOL> args = argv [ <NUM_LIT:1> : ] <EOL> log_level = parser . safe_get_log_level ( args ) <EOL> if stdout is None : <EOL> stdout = sys . stdout <EOL> with log_config ( level = log_level , file_ = log_file ) : <EOL> log . debug ( "<STR_LIT>" % argv ) <EOL> try : <EOL> ns = parser . parse_args ( args = args ) <EOL> log . debug ( "<STR_LIT>" % ns ) <EOL> try : <EOL> command = ns . run_command <EOL> except AttributeError : <EOL> raise HelpRequested ( parser = parser ) <EOL> output = command ( ns , stdout = stdout ) <EOL> if output is not None : <EOL> stdout . write ( output ) <EOL> status = EXIT_STATUS_SUCCESS <EOL> except HelpRequested as exc : <EOL> parser = exc . parser <EOL> parser . print_help ( file = stdout ) <EOL> status = EXIT_STATUS_SUCCESS <EOL> except UsageException as exc : <EOL> err_args , parser = exc . args , exc . parser <EOL> assert len ( err_args ) == <NUM_LIT:1> <EOL> print_usage_error ( parser , err_args [ <NUM_LIT:0> ] , file_ = log_file ) <EOL> status = EXIT_STATUS_USAGE_ERROR <EOL> return status <EOL> def main ( parser , argv = None ) : <EOL> if argv is None : <EOL> argv = sys . argv <EOL> status = non_exiting_main ( parser , argv ) <EOL> sys . exit ( status ) </s>
