<s> """<STR_LIT>""" <EOL> import logging <EOL> import socket <EOL> import sys <EOL> __license__ = '''<STR_LIT>''' <EOL> __version__ = '<STR_LIT>' <EOL> DEFAULT_HOST = '<STR_LIT:localhost>' <EOL> DEFAULT_PORT = <NUM_LIT> <EOL> DEFAULT_PRIORITY = <NUM_LIT:2> ** <NUM_LIT> <EOL> DEFAULT_TTR = <NUM_LIT> <EOL> class BeanstalkcException ( Exception ) : pass <EOL> class UnexpectedResponse ( BeanstalkcException ) : pass <EOL> class CommandFailed ( BeanstalkcException ) : pass <EOL> class DeadlineSoon ( BeanstalkcException ) : pass <EOL> class SocketError ( BeanstalkcException ) : <EOL> @ staticmethod <EOL> def wrap ( wrapped_function , * args , ** kwargs ) : <EOL> try : <EOL> return wrapped_function ( * args , ** kwargs ) <EOL> except socket . error : <EOL> err = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> raise SocketError ( err ) <EOL> class Connection ( object ) : <EOL> def __init__ ( self , host = DEFAULT_HOST , port = DEFAULT_PORT , parse_yaml = True , <EOL> connect_timeout = socket . getdefaulttimeout ( ) ) : <EOL> if parse_yaml is True : <EOL> try : <EOL> parse_yaml = __import__ ( '<STR_LIT>' ) . load <EOL> except ImportError : <EOL> logging . error ( '<STR_LIT>' ) <EOL> parse_yaml = False <EOL> self . _connect_timeout = connect_timeout <EOL> self . _parse_yaml = parse_yaml or ( lambda x : x ) <EOL> self . host = host <EOL> self . port = port <EOL> self . connect ( ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> self . close ( ) <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . _socket . settimeout ( self . _connect_timeout ) <EOL> SocketError . wrap ( self . _socket . connect , ( self . host , self . port ) ) <EOL> self . _socket . settimeout ( None ) <EOL> self . _socket_file = self . _socket . makefile ( '<STR_LIT:rb>' ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _socket . sendall ( '<STR_LIT>' ) <EOL> except socket . error : <EOL> pass <EOL> try : <EOL> self . _socket . close ( ) <EOL> except socket . error : <EOL> pass <EOL> def reconnect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . close ( ) <EOL> self . connect ( ) <EOL> def _interact ( self , command , expected_ok , expected_err = [ ] ) : <EOL> SocketError . wrap ( self . _socket . sendall , command ) <EOL> status , results = self . _read_response ( ) <EOL> if status in expected_ok : <EOL> return results <EOL> elif status in expected_err : <EOL> raise CommandFailed ( command . split ( ) [ <NUM_LIT:0> ] , status , results ) <EOL> else : <EOL> raise UnexpectedResponse ( command . split ( ) [ <NUM_LIT:0> ] , status , results ) <EOL> def _read_response ( self ) : <EOL> line = SocketError . wrap ( self . _socket_file . readline ) <EOL> if not line : <EOL> raise SocketError ( ) <EOL> response = line . split ( ) <EOL> return response [ <NUM_LIT:0> ] , response [ <NUM_LIT:1> : ] <EOL> def _read_body ( self , size ) : <EOL> body = SocketError . wrap ( self . _socket_file . read , size ) <EOL> SocketError . wrap ( self . _socket_file . read , <NUM_LIT:2> ) <EOL> if size > <NUM_LIT:0> and not body : <EOL> raise SocketError ( ) <EOL> return body <EOL> def _interact_value ( self , command , expected_ok , expected_err = [ ] ) : <EOL> return self . _interact ( command , expected_ok , expected_err ) [ <NUM_LIT:0> ] <EOL> def _interact_job ( self , command , expected_ok , expected_err , reserved = True ) : <EOL> jid , size = self . _interact ( command , expected_ok , expected_err ) <EOL> body = self . _read_body ( int ( size ) ) <EOL> return Job ( self , int ( jid ) , body , reserved ) <EOL> def _interact_yaml ( self , command , expected_ok , expected_err = [ ] ) : <EOL> size , = self . _interact ( command , expected_ok , expected_err ) <EOL> body = self . _read_body ( int ( size ) ) <EOL> return self . _parse_yaml ( body ) <EOL> def _interact_peek ( self , command ) : <EOL> try : <EOL> return self . _interact_job ( command , [ '<STR_LIT>' ] , [ '<STR_LIT>' ] , False ) <EOL> except CommandFailed : <EOL> return None <EOL> def put ( self , body , priority = DEFAULT_PRIORITY , delay = <NUM_LIT:0> , ttr = DEFAULT_TTR ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( body , str ) , '<STR_LIT>' <EOL> jid = self . _interact_value ( '<STR_LIT>' % ( <EOL> priority , delay , ttr , len ( body ) , body ) , <EOL> [ '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> return int ( jid ) <EOL> def reserve ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if timeout is not None : <EOL> command = '<STR_LIT>' % timeout <EOL> else : <EOL> command = '<STR_LIT>' <EOL> try : <EOL> return self . _interact_job ( command , <EOL> [ '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> except CommandFailed : <EOL> exc = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> _ , status , results = exc . args <EOL> if status == '<STR_LIT>' : <EOL> return None <EOL> elif status == '<STR_LIT>' : <EOL> raise DeadlineSoon ( results ) <EOL> def kick ( self , bound = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . _interact_value ( '<STR_LIT>' % bound , [ '<STR_LIT>' ] ) ) <EOL> def kick_job ( self , jid ) : <EOL> """<STR_LIT>""" <EOL> self . _interact ( '<STR_LIT>' % jid , [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> def peek ( self , jid ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_peek ( '<STR_LIT>' % jid ) <EOL> def peek_ready ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_peek ( '<STR_LIT>' ) <EOL> def peek_delayed ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_peek ( '<STR_LIT>' ) <EOL> def peek_buried ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_peek ( '<STR_LIT>' ) <EOL> def tubes ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_yaml ( '<STR_LIT>' , [ '<STR_LIT:OK>' ] ) <EOL> def using ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_value ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> def use ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_value ( '<STR_LIT>' % name , [ '<STR_LIT>' ] ) <EOL> def watching ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_yaml ( '<STR_LIT>' , [ '<STR_LIT:OK>' ] ) <EOL> def watch ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . _interact_value ( '<STR_LIT>' % name , [ '<STR_LIT>' ] ) ) <EOL> def ignore ( self , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return int ( self . _interact_value ( '<STR_LIT>' % name , <EOL> [ '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' ] ) ) <EOL> except CommandFailed : <EOL> return <NUM_LIT:1> <EOL> def stats ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_yaml ( '<STR_LIT>' , [ '<STR_LIT:OK>' ] ) <EOL> def stats_tube ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_yaml ( '<STR_LIT>' % name , <EOL> [ '<STR_LIT:OK>' ] , <EOL> [ '<STR_LIT>' ] ) <EOL> def pause_tube ( self , name , delay ) : <EOL> """<STR_LIT>""" <EOL> self . _interact ( '<STR_LIT>' % ( name , delay ) , <EOL> [ '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' ] ) <EOL> def delete ( self , jid ) : <EOL> """<STR_LIT>""" <EOL> self . _interact ( '<STR_LIT>' % jid , [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> def release ( self , jid , priority = DEFAULT_PRIORITY , delay = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . _interact ( '<STR_LIT>' % ( jid , priority , delay ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' ] ) <EOL> def bury ( self , jid , priority = DEFAULT_PRIORITY ) : <EOL> """<STR_LIT>""" <EOL> self . _interact ( '<STR_LIT>' % ( jid , priority ) , <EOL> [ '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' ] ) <EOL> def touch ( self , jid ) : <EOL> """<STR_LIT>""" <EOL> self . _interact ( '<STR_LIT>' % jid , [ '<STR_LIT>' ] , [ '<STR_LIT>' ] ) <EOL> def stats_job ( self , jid ) : <EOL> """<STR_LIT>""" <EOL> return self . _interact_yaml ( '<STR_LIT>' % jid , <EOL> [ '<STR_LIT:OK>' ] , <EOL> [ '<STR_LIT>' ] ) <EOL> class Job ( object ) : <EOL> def __init__ ( self , conn , jid , body , reserved = True ) : <EOL> self . conn = conn <EOL> self . jid = jid <EOL> self . body = body <EOL> self . reserved = reserved <EOL> def _priority ( self ) : <EOL> stats = self . stats ( ) <EOL> if isinstance ( stats , dict ) : <EOL> return stats [ '<STR_LIT>' ] <EOL> return DEFAULT_PRIORITY <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . conn . delete ( self . jid ) <EOL> self . reserved = False <EOL> def release ( self , priority = None , delay = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if self . reserved : <EOL> self . conn . release ( self . jid , priority or self . _priority ( ) , delay ) <EOL> self . reserved = False <EOL> def bury ( self , priority = None ) : <EOL> """<STR_LIT>""" <EOL> if self . reserved : <EOL> self . conn . bury ( self . jid , priority or self . _priority ( ) ) <EOL> self . reserved = False <EOL> def kick ( self ) : <EOL> """<STR_LIT>""" <EOL> self . conn . kick_job ( self . jid ) <EOL> def touch ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . reserved : <EOL> self . conn . touch ( self . jid ) <EOL> def stats ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . conn . stats_job ( self . jid ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import nose <EOL> nose . main ( argv = [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' ] ) </s>
