<s> import time <EOL> import traceback <EOL> import logging <EOL> from apns import APNs , Payload <EOL> _ignored_content_keys = ( "<STR_LIT:message>" , ) <EOL> class APNsWrapper ( APNs ) : <EOL> def __init__ ( self , use_sandbox = False , cert_file = None , key_file = None ) : <EOL> super ( APNsWrapper , self ) . __init__ ( use_sandbox , cert_file , key_file ) <EOL> self . _payloads = [ ] <EOL> def append ( self , token , notification , alert = None , badge = None , sound = None ) : <EOL> if not alert : <EOL> alert = notification . get ( "<STR_LIT:message>" , None ) <EOL> if alert and isinstance ( alert , dict ) : <EOL> alert = alert . get ( "<STR_LIT:message>" , None ) <EOL> for key in _ignored_content_keys : <EOL> try : <EOL> del notification [ key ] <EOL> except KeyError : <EOL> pass <EOL> payload = Payload ( alert , badge , sound , custom = notification ) <EOL> self . _payloads . append ( ( token , payload ) ) <EOL> def flush ( self ) : <EOL> if self . _payloads : <EOL> for token , payload in self . _payloads : <EOL> try : <EOL> self . gateway_server . write ( self . gateway_server . _get_notification ( token , payload ) ) <EOL> except : <EOL> logging . error ( traceback . format_exc ( ) ) <EOL> self . _gateway_connection = None <EOL> self . _payloads = [ ] </s>
