<s> from __future__ import ( absolute_import , division , print_function , <EOL> unicode_literals ) <EOL> import re <EOL> from . events import Events <EOL> from . components import component_class , observes , keyword <EOL> @ component_class <EOL> class Triggers ( Events ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , irc_c , config ) : <EOL> Events . __init__ ( self , irc_c ) <EOL> self . prefix = config . prefix or '<STR_LIT:!>' <EOL> irc_c [ '<STR_LIT>' ] = self <EOL> self . _keywordRE = re . compile ( r'<STR_LIT>' , <EOL> re . I ) <EOL> self . _argRE = re . compile ( r"""<STR_LIT>""" <EOL> r"""<STR_LIT>""" ) <EOL> print ( "<STR_LIT>" ) <EOL> def _generate_command_words ( self , commands , msg ) : <EOL> """<STR_LIT>""" <EOL> def _size ( alist ) : <EOL> size = <NUM_LIT:0> <EOL> for words in alist : <EOL> for word in words : <EOL> size += len ( word ) + <NUM_LIT:2> <EOL> return size <EOL> messages = [ [ '<STR_LIT>' ] ] <EOL> prefix_len = len ( '<STR_LIT>' % msg . nick ) <EOL> for word in sorted ( commands ) : <EOL> show = False <EOL> event_handler = self . get ( word ) <EOL> if event_handler : <EOL> for observer in event_handler . observers ( ) : <EOL> if observer . __doc__ : <EOL> show = True <EOL> break <EOL> if show : <EOL> if _size ( messages [ - <NUM_LIT:1> ] ) + len ( word ) + prefix_len <= <NUM_LIT> : <EOL> messages [ - <NUM_LIT:1> ] . append ( word ) <EOL> else : <EOL> messages . append ( [ word ] ) <EOL> return messages <EOL> def _clean_doc ( self , doc ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:U+0020>' . join ( [ s . strip ( ) for s in doc . strip ( ) . split ( '<STR_LIT:\n>' ) ] ) <EOL> def _generate_long_help ( self , commands , msg ) : <EOL> for k in sorted ( commands ) : <EOL> event_handler = self . get ( k ) <EOL> if event_handler : <EOL> for observer in event_handler . observers ( ) : <EOL> if observer . __doc__ : <EOL> doc = self . _clean_doc ( observer . __doc__ ) <EOL> if hasattr ( observer , '<STR_LIT>' ) : <EOL> for sub in observer . _subs : <EOL> msg . reply ( "<STR_LIT>" <EOL> % ( k , sub , doc ) ) <EOL> else : <EOL> msg . reply ( "<STR_LIT>" % ( k , doc ) ) <EOL> @ keyword ( '<STR_LIT>' ) <EOL> @ keyword . autohelp <EOL> def autohelp ( self , irc_c , msg , trigger , args , kargs ) : <EOL> """<STR_LIT>""" <EOL> if args : <EOL> commands = args <EOL> else : <EOL> commands = self . list ( ) <EOL> if msg . channel and not args : <EOL> if '<STR_LIT>' in kargs : <EOL> msg . reply_target = msg . nick <EOL> else : <EOL> kargs [ '<STR_LIT:list>' ] = True <EOL> if '<STR_LIT:list>' in kargs and '<STR_LIT>' not in kargs : <EOL> messages = self . _generate_command_words ( commands , msg ) <EOL> for words in messages : <EOL> msg . reply ( '<STR_LIT:%s>' % '<STR_LIT:U+0020>' . join ( words ) ) <EOL> else : <EOL> self . _generate_long_help ( commands , msg ) <EOL> def parse ( self , next ) : <EOL> """<STR_LIT>""" <EOL> args = [ ] <EOL> kwargs = { } <EOL> while next : <EOL> getnext = None <EOL> keymatch = self . _keywordRE . search ( next ) <EOL> if keymatch : <EOL> name , getnext , next = keymatch . groups ( ) <EOL> kwargs [ name ] = True <EOL> if not getnext : <EOL> continue <EOL> argmatch = self . _argRE . search ( next ) <EOL> if argmatch : <EOL> quotetype , quoted , naked , next = argmatch . groups ( ) <EOL> arg = quoted if quoted is not None else naked <EOL> arg = re . sub ( r"""<STR_LIT>""" , r'<STR_LIT>' , arg ) <EOL> if getnext : <EOL> kwargs [ name ] = arg <EOL> else : <EOL> args . append ( arg ) <EOL> return [ args , kwargs ] <EOL> @ observes ( '<STR_LIT>' ) <EOL> def _handler ( self , irc_c , msg ) : <EOL> address = '<STR_LIT>' % irc_c . botnick <EOL> message = msg . message . strip ( ) <EOL> if ( message . startswith ( self . prefix ) <EOL> or message . lower ( ) . startswith ( address ) <EOL> or msg . channel is None ) : <EOL> if message . lower ( ) . startswith ( address ) : <EOL> message = message [ len ( address ) : ] . strip ( ) <EOL> parts = message . split ( None , <NUM_LIT:1> ) <EOL> if parts : <EOL> word = parts . pop ( <NUM_LIT:0> ) . lstrip ( self . prefix ) <EOL> else : <EOL> return <EOL> if parts : <EOL> allargs = parts . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> allargs = '<STR_LIT>' <EOL> trigger = self . get ( word ) <EOL> if trigger : <EOL> args , keywords = self . parse ( allargs ) <EOL> msg . unparsed = allargs <EOL> trigger ( irc_c , msg , word , args , keywords ) </s>
