<s> """<STR_LIT>""" <EOL> import itertools <EOL> import inspect <EOL> from . core import GroupMixin , Command <EOL> from . errors import CommandError <EOL> class HelpFormatter : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , show_hidden = False , show_check_failure = False , width = <NUM_LIT> ) : <EOL> self . width = width <EOL> self . show_hidden = show_hidden <EOL> self . show_check_failure = show_check_failure <EOL> def has_subcommands ( self ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( self . command , GroupMixin ) <EOL> def is_bot ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . command is self . context . bot <EOL> def is_cog ( self ) : <EOL> """<STR_LIT>""" <EOL> return not self . is_bot ( ) and not isinstance ( self . command , Command ) <EOL> def shorten ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if len ( text ) > self . width : <EOL> return text [ : self . width - <NUM_LIT:3> ] + '<STR_LIT>' <EOL> return text <EOL> @ property <EOL> def max_name_size ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> commands = self . command . commands if not self . is_cog ( ) else self . context . bot . commands <EOL> if commands : <EOL> return max ( map ( lambda c : len ( c . name ) , commands . values ( ) ) ) <EOL> return <NUM_LIT:0> <EOL> except AttributeError : <EOL> return len ( self . command . name ) <EOL> @ property <EOL> def clean_prefix ( self ) : <EOL> """<STR_LIT>""" <EOL> user = self . context . bot . user <EOL> return self . context . prefix . replace ( user . mention , '<STR_LIT:@>' + user . name ) <EOL> def get_qualified_command_name ( self ) : <EOL> """<STR_LIT>""" <EOL> entries = [ ] <EOL> command = self . command <EOL> while command . parent is not None : <EOL> command = command . parent <EOL> entries . append ( command . name ) <EOL> return '<STR_LIT:U+0020>' . join ( reversed ( entries ) ) <EOL> def get_command_signature ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> prefix = self . clean_prefix <EOL> qualified = self . get_qualified_command_name ( ) <EOL> cmd = self . command <EOL> if len ( cmd . aliases ) > <NUM_LIT:0> : <EOL> aliases = '<STR_LIT:|>' . join ( cmd . aliases ) <EOL> fmt = '<STR_LIT>' <EOL> if qualified : <EOL> fmt = '<STR_LIT>' <EOL> result . append ( fmt . format ( prefix , cmd , aliases , qualified ) ) <EOL> else : <EOL> name = prefix + cmd . name if not qualified else prefix + qualified + '<STR_LIT:U+0020>' + cmd . name <EOL> result . append ( name ) <EOL> params = cmd . clean_params <EOL> if len ( params ) > <NUM_LIT:0> : <EOL> for name , param in params . items ( ) : <EOL> if param . default is not param . empty : <EOL> should_print = param . default if isinstance ( param . default , str ) else param . default is not None <EOL> if should_print : <EOL> result . append ( '<STR_LIT>' . format ( name , param . default ) ) <EOL> else : <EOL> result . append ( '<STR_LIT>' . format ( name ) ) <EOL> elif param . kind == param . VAR_POSITIONAL : <EOL> result . append ( '<STR_LIT>' . format ( name ) ) <EOL> else : <EOL> result . append ( '<STR_LIT>' . format ( name ) ) <EOL> return '<STR_LIT:U+0020>' . join ( result ) <EOL> def get_ending_note ( self ) : <EOL> command_name = self . context . invoked_with <EOL> return "<STR_LIT>" "<STR_LIT>" . format ( self . clean_prefix , command_name ) <EOL> def filter_command_list ( self ) : <EOL> """<STR_LIT>""" <EOL> def predicate ( tuple ) : <EOL> cmd = tuple [ <NUM_LIT:1> ] <EOL> if self . is_cog ( ) : <EOL> if cmd . instance is not self . command : <EOL> return False <EOL> if cmd . hidden and not self . show_hidden : <EOL> return False <EOL> if self . show_check_failure : <EOL> return True <EOL> try : <EOL> return cmd . can_run ( self . context ) <EOL> except CommandError : <EOL> return False <EOL> iterator = self . command . commands . items ( ) if not self . is_cog ( ) else self . context . bot . commands . items ( ) <EOL> return filter ( predicate , iterator ) <EOL> def _check_new_page ( self ) : <EOL> if self . _count + len ( self . _current_page ) >= <NUM_LIT> : <EOL> self . _current_page . append ( '<STR_LIT>' ) <EOL> self . _pages . append ( '<STR_LIT:\n>' . join ( self . _current_page ) ) <EOL> self . _current_page = [ '<STR_LIT>' ] <EOL> self . _count = <NUM_LIT:4> <EOL> return True <EOL> return False <EOL> def _add_subcommands_to_page ( self , max_width , commands ) : <EOL> for name , command in commands : <EOL> if name in command . aliases : <EOL> continue <EOL> entry = '<STR_LIT>' . format ( name , command . short_doc , width = max_width ) <EOL> shortened = self . shorten ( entry ) <EOL> self . _count += len ( shortened ) <EOL> if self . _check_new_page ( ) : <EOL> self . _count += len ( shortened ) <EOL> self . _current_page . append ( shortened ) <EOL> def format_help_for ( self , context , command_or_bot ) : <EOL> """<STR_LIT>""" <EOL> self . context = context <EOL> self . command = command_or_bot <EOL> return self . format ( ) <EOL> def format ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _pages = [ ] <EOL> self . _count = <NUM_LIT:4> <EOL> self . _current_page = [ '<STR_LIT>' ] <EOL> description = self . command . description if not self . is_cog ( ) else inspect . getdoc ( self . command ) <EOL> if description : <EOL> self . _current_page . append ( description ) <EOL> self . _current_page . append ( '<STR_LIT>' ) <EOL> self . _count += len ( description ) <EOL> if isinstance ( self . command , Command ) : <EOL> signature = self . get_command_signature ( ) <EOL> self . _count += <NUM_LIT:2> + len ( signature ) <EOL> self . _current_page . append ( signature ) <EOL> self . _current_page . append ( '<STR_LIT>' ) <EOL> if self . command . help : <EOL> self . _count += <NUM_LIT:2> + len ( self . command . help ) <EOL> self . _current_page . append ( self . command . help ) <EOL> self . _current_page . append ( '<STR_LIT>' ) <EOL> self . _check_new_page ( ) <EOL> if not self . has_subcommands ( ) : <EOL> self . _current_page . append ( '<STR_LIT>' ) <EOL> self . _pages . append ( '<STR_LIT:\n>' . join ( self . _current_page ) ) <EOL> return self . _pages <EOL> max_width = self . max_name_size <EOL> def category ( tup ) : <EOL> cog = tup [ <NUM_LIT:1> ] . cog_name <EOL> return cog + '<STR_LIT::>' if cog is not None else '<STR_LIT>' <EOL> if self . is_bot ( ) : <EOL> data = sorted ( self . filter_command_list ( ) , key = category ) <EOL> for category , commands in itertools . groupby ( data , key = category ) : <EOL> commands = list ( commands ) <EOL> if len ( commands ) > <NUM_LIT:0> : <EOL> self . _current_page . append ( category ) <EOL> self . _count += len ( category ) <EOL> self . _check_new_page ( ) <EOL> self . _add_subcommands_to_page ( max_width , commands ) <EOL> else : <EOL> self . _current_page . append ( '<STR_LIT>' ) <EOL> self . _count += <NUM_LIT:1> + len ( self . _current_page [ - <NUM_LIT:1> ] ) <EOL> self . _add_subcommands_to_page ( max_width , self . filter_command_list ( ) ) <EOL> self . _current_page . append ( '<STR_LIT>' ) <EOL> ending_note = self . get_ending_note ( ) <EOL> self . _count += len ( ending_note ) <EOL> self . _check_new_page ( ) <EOL> self . _current_page . append ( ending_note ) <EOL> if len ( self . _current_page ) > <NUM_LIT:1> : <EOL> self . _current_page . append ( '<STR_LIT>' ) <EOL> self . _pages . append ( '<STR_LIT:\n>' . join ( self . _current_page ) ) <EOL> return self . _pages </s>
