<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import os <EOL> from binascii import b2a_hex , a2b_hex <EOL> import gadfly <EOL> from snakemq . message import Message , MAX_UUID_LENGTH <EOL> from snakemq . messaging import MAX_IDENT_LENGTH <EOL> from snakemq . storage import QueuesStorageBase <EOL> class GadflyQueuesStorage ( QueuesStorageBase ) : <EOL> def __init__ ( self , directory , filename ) : <EOL> if os . path . isfile ( os . path . join ( directory , filename + "<STR_LIT>" ) ) : <EOL> self . conn = gadfly . gadfly ( filename , directory ) <EOL> self . crs = self . conn . cursor ( ) <EOL> else : <EOL> self . conn = gadfly . gadfly ( ) <EOL> self . conn . startup ( filename , directory ) <EOL> self . crs = self . conn . cursor ( ) <EOL> self . create_structures ( ) <EOL> def close ( self ) : <EOL> if self . crs : <EOL> self . crs . close ( ) <EOL> self . crs = None <EOL> if self . conn : <EOL> self . conn . close ( ) <EOL> self . conn = None <EOL> def create_structures ( self ) : <EOL> self . crs . execute ( """<STR_LIT>""" % <EOL> ( MAX_IDENT_LENGTH , MAX_UUID_LENGTH * <NUM_LIT:2> ) ) <EOL> self . conn . commit ( ) <EOL> def get_queues ( self ) : <EOL> self . crs . execute ( """<STR_LIT>""" ) <EOL> return [ r [ <NUM_LIT:0> ] for r in self . crs . fetchall ( ) ] <EOL> def get_items ( self , queue_name ) : <EOL> self . crs . execute ( """<STR_LIT>""" , <EOL> ( queue_name , ) ) <EOL> items = [ ] <EOL> for res in self . crs . fetchall ( ) : <EOL> uuid = a2b_hex ( res [ <NUM_LIT:0> ] ) <EOL> data = res [ <NUM_LIT:1> ] <EOL> items . append ( Message ( uuid = uuid , <EOL> data = data , <EOL> ttl = res [ <NUM_LIT:2> ] , <EOL> flags = res [ <NUM_LIT:3> ] ) ) <EOL> return items <EOL> def push ( self , queue_name , item ) : <EOL> self . crs . execute ( """<STR_LIT>""" , <EOL> ( queue_name , b2a_hex ( item . uuid ) , item . data , <EOL> item . ttl , item . flags ) ) <EOL> self . conn . commit ( ) <EOL> def delete_items ( self , items ) : <EOL> for item in items : <EOL> self . crs . execute ( """<STR_LIT>""" , <EOL> ( b2a_hex ( item . uuid ) , ) ) <EOL> self . conn . commit ( ) <EOL> def delete_all ( self ) : <EOL> self . crs . execute ( "<STR_LIT>" ) <EOL> self . conn . commit ( ) <EOL> def update_items_ttl ( self , items ) : <EOL> for item in items : <EOL> self . crs . execute ( """<STR_LIT>""" , <EOL> ( item . ttl , b2a_hex ( item . uuid ) ) ) <EOL> self . conn . commit ( ) </s>
