<s> import re , traceback <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet import defer <EOL> from droned . errors import DroneCommandFailed <EOL> from droned . responders import responder , responders , jabber_config <EOL> import config <EOL> @ responder ( pattern = "<STR_LIT>" , form = "<STR_LIT>" , auth_required = False , <EOL> help = "<STR_LIT>" ) <EOL> def help ( conversation , regex ) : <EOL> help_text = "<STR_LIT>" <EOL> groups = { } <EOL> for func in responders . values ( ) : <EOL> if func . module not in groups : <EOL> groups [ func . module ] = [ ] <EOL> groups [ func . module ] . append ( func ) <EOL> for group , funcs in sorted ( groups . items ( ) ) : <EOL> help_text += "<STR_LIT>" % group <EOL> for func in funcs : <EOL> help_text += "<STR_LIT>" % ( func . form , func . help ) <EOL> if regex : <EOL> regex = re . compile ( regex ) <EOL> output = "<STR_LIT>" <EOL> for line in help_text . split ( '<STR_LIT:\n>' ) : <EOL> if regex . search ( line ) : <EOL> output += line + '<STR_LIT:\n>' <EOL> else : <EOL> output = help_text <EOL> conversation . say ( '<STR_LIT:\n>' + output , useHTML = False ) <EOL> if not regex : <EOL> doc_url = "<STR_LIT>" % ( config . HOSTNAME , config . DRONED_PORT ) <EOL> conversation . say ( "<STR_LIT>" % doc_url ) <EOL> @ responder ( pattern = "<STR_LIT>" , form = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> def python ( conversation , source ) : <EOL> from droned . entity import namespace <EOL> source = source . strip ( ) <EOL> try : <EOL> code = compile ( source , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> try : <EOL> code = compile ( source , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except : <EOL> msg = "<STR_LIT>" % traceback . format_exc ( ) <EOL> conversation . say ( msg , useHTML = False ) <EOL> return <EOL> try : <EOL> result = eval ( code , namespace ) <EOL> except : <EOL> result = traceback . format_exc ( ) <EOL> conversation . say ( str ( result ) , useHTML = False ) <EOL> @ responder ( pattern = "<STR_LIT>" , context_key = '<STR_LIT>' , form = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> @ defer . deferredGenerator <EOL> def droneblast ( conversation , command ) : <EOL> server = conversation . context . get ( '<STR_LIT>' ) <EOL> response = None <EOL> d = None <EOL> try : <EOL> if not isinstance ( server , Server ) : <EOL> conversation . say ( '<STR_LIT>' ) <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> options = { } <EOL> if '<STR_LIT>' in conversation . context : <EOL> options [ '<STR_LIT>' ] = conversation . context [ '<STR_LIT>' ] <EOL> try : <EOL> conversation . say ( "<STR_LIT>" ) <EOL> d = server . manager . run ( command , ** options ) <EOL> deferreds = conversation . context . get ( '<STR_LIT>' , [ ] ) <EOL> deferreds . append ( d ) <EOL> conversation . context . update ( { '<STR_LIT>' : deferreds } ) <EOL> wfd = defer . waitForDeferred ( d ) <EOL> yield wfd <EOL> result = wfd . getResult ( ) <EOL> except : <EOL> failure = Failure ( ) <EOL> if failure . check ( DroneCommandFailed ) : <EOL> rc = failure . value . resultContext <EOL> conversation . say ( rc . get ( '<STR_LIT:description>' ) or str ( rc ) , useHTML = False ) <EOL> else : <EOL> conversation . say ( failure . getTraceback ( ) , useHTML = False ) <EOL> else : <EOL> if isinstance ( result , dict ) : <EOL> output = result . values ( ) [ <NUM_LIT:0> ] . get ( '<STR_LIT:description>' , str ( result ) ) <EOL> else : <EOL> output = str ( result ) <EOL> deferreds = conversation . context . get ( '<STR_LIT>' , [ ] ) <EOL> try : deferreds . remove ( d ) <EOL> except : pass <EOL> conversation . context . update ( { '<STR_LIT>' : deferreds } ) <EOL> conversation . say ( "<STR_LIT>" % output , useHTML = False ) <EOL> except AssertionError : pass <EOL> except : <EOL> result = Failure ( ) <EOL> yield result <EOL> @ responder ( pattern = "<STR_LIT>" , form = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> def _set ( conversation , var , value ) : <EOL> try : <EOL> value = float ( value ) <EOL> except : <EOL> try : <EOL> value = int ( value ) <EOL> except : pass <EOL> conversation . context [ var ] = value <EOL> conversation . say ( "<STR_LIT>" ) <EOL> @ responder ( pattern = "<STR_LIT>" , form = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> def unset ( conversation , var ) : <EOL> if var in conversation . context : <EOL> del conversation . context [ var ] <EOL> conversation . say ( "<STR_LIT>" ) <EOL> @ responder ( pattern = "<STR_LIT>" , form = "<STR_LIT>" , auth_required = False , help = "<STR_LIT>" ) <EOL> def wtf ( conversation ) : <EOL> msg = "<STR_LIT>" <EOL> for key , value in sorted ( conversation . context . items ( ) ) : <EOL> if key in ( '<STR_LIT>' , ) : continue <EOL> msg += "<STR_LIT>" % ( key , value ) <EOL> conversation . say ( msg , useHTML = False ) <EOL> @ responder ( pattern = "<STR_LIT>" , form = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> def clear ( conversation ) : <EOL> conversation . say ( '<STR_LIT:\n>' * <NUM_LIT:30> , useHTML = False ) <EOL> @ responder ( pattern = "<STR_LIT>" , form = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> def tasks ( conversation ) : <EOL> deferreds = conversation . context . get ( '<STR_LIT>' , [ ] ) <EOL> if deferreds : <EOL> listing = '<STR_LIT:\n>' + '<STR_LIT:\n>' . join ( "<STR_LIT>" % i for i in enumerate ( deferreds ) ) <EOL> conversation . say ( listing , useHTML = False ) <EOL> else : <EOL> conversation . say ( "<STR_LIT>" ) <EOL> @ responder ( pattern = "<STR_LIT>" , form = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> def failed ( conversation , num ) : <EOL> failures = conversation . context . get ( '<STR_LIT>' , [ ] ) <EOL> droned_results = [ f . value . resultContext for f in failures if f . check ( DroneCommandFailed ) ] <EOL> other_failures = [ f for f in failures if not f . check ( DroneCommandFailed ) ] <EOL> if num : <EOL> droned_errors = [ str ( rc ) for rc in droned_results ] <EOL> other_errors = [ f . getTraceback ( ) for f in other_failures ] <EOL> errors = droned_errors + other_errors <EOL> num = int ( num ) <EOL> try : <EOL> conversation . say ( '<STR_LIT:\n>' + errors [ num ] , useHTML = False ) <EOL> except : <EOL> conversation . say ( '<STR_LIT>' % num ) <EOL> elif failures : <EOL> droned_errors = [ '<STR_LIT>' % rc for rc in droned_results ] <EOL> other_errors = [ str ( f ) for f in other_failures ] <EOL> errors = droned_errors + other_errors <EOL> listing = '<STR_LIT:\n>' + '<STR_LIT:\n>' . join ( "<STR_LIT>" % i for i in enumerate ( errors ) ) <EOL> conversation . say ( listing , useHTML = False ) <EOL> else : <EOL> conversation . say ( "<STR_LIT>" ) <EOL> @ responder ( pattern = '<STR_LIT>' , form = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> def pollall ( conversation ) : <EOL> count = <NUM_LIT:0> <EOL> for server in Server . objects : <EOL> server . droned . poll ( ) <EOL> count += <NUM_LIT:1> <EOL> conversation . say ( "<STR_LIT>" % count ) <EOL> @ responder ( pattern = '<STR_LIT>' , form = '<STR_LIT>' , auth_required = False , <EOL> help = '<STR_LIT>' ) <EOL> @ defer . deferredGenerator <EOL> def auth_request ( conversation ) : <EOL> result = None <EOL> try : <EOL> if conversation . authorized : <EOL> conversation . say ( "<STR_LIT>" ) <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> conversation . say ( "<STR_LIT>" ) <EOL> deputy = Conversation ( jabber_config . DEPUTY ) <EOL> try : <EOL> question = "<STR_LIT>" % conversation . buddy <EOL> answers = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> d = deputy . ask ( question , answers ) <EOL> wfd = defer . waitForDeferred ( d ) <EOL> yield wfd <EOL> answer = wfd . getResult ( ) <EOL> except AlreadyAskingQuestion : <EOL> err = "<STR_LIT>" <EOL> err += "<STR_LIT>" <EOL> err += "<STR_LIT>" % jabber_config . DEPUTY <EOL> conversation . say ( err ) <EOL> raise AssertionError ( '<STR_LIT>' ) <EOL> if answer == '<STR_LIT>' : <EOL> conversation . grantAuthorization ( ) <EOL> deputy . say ( "<STR_LIT>" % conversation . buddy ) <EOL> else : <EOL> err = "<STR_LIT>" <EOL> err += "<STR_LIT>" <EOL> err += "<STR_LIT>" % jabber_config . DEPUTY <EOL> conversation . say ( err ) <EOL> deputy . say ( "<STR_LIT>" % conversation . buddy ) <EOL> except AssertionError : pass <EOL> except : <EOL> result = Failure ( ) <EOL> yield result <EOL> @ responder ( pattern = '<STR_LIT>' , form = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> def revoke ( conversation , user ) : <EOL> if conversation . buddy != jabber_config . DEPUTY : <EOL> conversation . say ( "<STR_LIT>" % jabber_config . DEPUTY ) <EOL> return <EOL> naughtyUser = Conversation . byName ( user ) <EOL> if naughtyUser : <EOL> conversation . say ( "<STR_LIT>" % naughtyUser . buddy ) <EOL> naughtyUser . revokeAuthorization ( ) <EOL> else : <EOL> conversation . say ( "<STR_LIT>" ) <EOL> @ responder ( pattern = '<STR_LIT>' , form = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> def who ( conversation ) : <EOL> listing = [ ] <EOL> for user in Conversation . objects : <EOL> minutes = int ( user . idleTime ) / <NUM_LIT> <EOL> if minutes > <NUM_LIT> : <EOL> continue <EOL> elif minutes > <NUM_LIT:0> : <EOL> plural = ( minutes > <NUM_LIT:1> and '<STR_LIT:s>' ) or '<STR_LIT>' <EOL> listing . append ( "<STR_LIT>" % ( user . buddyName , minutes , plural ) ) <EOL> else : <EOL> listing . append ( "<STR_LIT>" % user . buddyName ) <EOL> heading = "<STR_LIT>" <EOL> heading += "<STR_LIT>" % '<STR_LIT:username>' <EOL> conversation . say ( heading + '<STR_LIT:\n>' . join ( listing ) , useHTML = False ) <EOL> @ responder ( pattern = '<STR_LIT>' , form = '<STR_LIT>' , auth_required = False , help = '<STR_LIT>' ) <EOL> def repeat ( conversation ) : <EOL> if conversation . askingQuestion : <EOL> conversation . say ( conversation . context [ '<STR_LIT>' ] ) <EOL> conversation . say ( "<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( conversation . expectedAnswers ) ) <EOL> else : <EOL> conversation . say ( "<STR_LIT>" ) <EOL> from droned . models . server import Server <EOL> from droned . models . conversation import Conversation , AlreadyAskingQuestion </s>
