<s> from itertools import ifilter <EOL> from Queue import Empty <EOL> from beanstalkc import Connection <EOL> from ghettoq . backends . base import BaseBackend <EOL> DEFAULT_HOST = '<STR_LIT:localhost>' <EOL> DEFAULT_PORT = <NUM_LIT> <EOL> class BeanstalkBackend ( BaseBackend ) : <EOL> def _parse_job ( self , job ) : <EOL> item , dest = None , None <EOL> if job : <EOL> try : <EOL> item = job . body <EOL> dest = job . stats ( ) [ '<STR_LIT>' ] <EOL> except : <EOL> job . bury ( ) <EOL> else : <EOL> job . delete ( ) <EOL> else : <EOL> raise Empty <EOL> return item , dest <EOL> def establish_connection ( self ) : <EOL> self . host = self . host or DEFAULT_HOST <EOL> self . port = self . port or DEFAULT_PORT <EOL> return Connection ( host = self . host , port = self . port ) <EOL> def put ( self , queue , message , priority = <NUM_LIT:0> , ** kwargs ) : <EOL> self . client . use ( queue ) <EOL> self . client . put ( message , priority = priority ) <EOL> def get ( self , queue ) : <EOL> if not queue : <EOL> raise Empty <EOL> if queue not in self . client . watching ( ) : <EOL> self . client . watch ( queue ) <EOL> ignore = ifilter ( lambda q : q != queue , self . client . watching ( ) ) <EOL> map ( self . client . ignore , ignore ) <EOL> job = self . client . reserve ( timeout = <NUM_LIT:1> ) <EOL> item , dest = self . _parse_job ( job ) <EOL> return item <EOL> def get_many ( self , queues , timeout = None ) : <EOL> if not queues : <EOL> raise Empty <EOL> if timeout is None : <EOL> timeout = <NUM_LIT:1> <EOL> to_watch = ifilter ( lambda q : q not in self . client . watching ( ) , queues ) <EOL> map ( self . client . watch , to_watch ) <EOL> job = self . client . reserve ( timeout = timeout ) <EOL> return self . _parse_job ( job ) <EOL> def purge ( self , queue ) : <EOL> if queue not in self . client . watching ( ) : <EOL> self . client . watch ( queue ) <EOL> ignore = ifilter ( lambda q : q != queue , self . client . watching ( ) ) <EOL> map ( self . client . ignore , ignore ) <EOL> count = <NUM_LIT:0> <EOL> while True : <EOL> job = self . client . reserve ( timeout = <NUM_LIT:1> ) <EOL> if job : <EOL> job . delete ( ) <EOL> count += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> return count </s>
