<s> import re <EOL> import uuid <EOL> import collections <EOL> try : <EOL> from urllib . parse import unquote <EOL> except ImportError : <EOL> from urllib import unquote <EOL> try : <EOL> import asyncio <EOL> from asyncio . queues import Queue <EOL> except ImportError : <EOL> import trollius as asyncio <EOL> from trollius . queues import Queue <EOL> try : <EOL> from configparser import ConfigParser <EOL> except ImportError : <EOL> from ConfigParser import ConfigParser <EOL> EOL = '<STR_LIT:\r\n>' <EOL> re_code = re . compile ( r'<STR_LIT>' ) <EOL> re_kv = re . compile ( r'<STR_LIT>' ) <EOL> def parse_agi_result ( line ) : <EOL> """<STR_LIT>""" <EOL> if line == '<STR_LIT>' : <EOL> return { '<STR_LIT:error>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> code = <NUM_LIT:0> <EOL> m = re_code . search ( line ) <EOL> if m : <EOL> code , response = m . groups ( ) <EOL> code = int ( code ) <EOL> return agi_code_check ( code , response , line ) <EOL> def agi_code_check ( code , response , line ) : <EOL> """<STR_LIT>""" <EOL> result = { '<STR_LIT>' : code , '<STR_LIT:result>' : ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' : '<STR_LIT>' } <EOL> if code == <NUM_LIT:200> : <EOL> for key , value , data in re_kv . findall ( response ) : <EOL> result [ key ] = ( value , data ) <EOL> if data == '<STR_LIT>' : <EOL> return { '<STR_LIT:error>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> if key == '<STR_LIT:result>' and value == '<STR_LIT>' : <EOL> return { '<STR_LIT:error>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> return result <EOL> elif code == <NUM_LIT> : <EOL> result [ '<STR_LIT:error>' ] = '<STR_LIT>' <EOL> return result <EOL> elif code == <NUM_LIT> : <EOL> usage = [ line ] <EOL> usage = '<STR_LIT>' % '<STR_LIT:\n>' . join ( usage ) <EOL> result [ '<STR_LIT:error>' ] = '<STR_LIT>' <EOL> result [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return result <EOL> else : <EOL> result [ '<STR_LIT:error>' ] = '<STR_LIT>' <EOL> return result <EOL> class IdGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> instances = [ ] <EOL> def __init__ ( self , prefix ) : <EOL> self . instances . append ( self ) <EOL> self . prefix = prefix <EOL> self . uid = str ( uuid . uuid4 ( ) ) <EOL> self . generator = self . get_generator ( ) <EOL> def get_generator ( self ) : <EOL> i = <NUM_LIT:0> <EOL> max_val = <NUM_LIT> <EOL> while True : <EOL> yield "<STR_LIT>" % ( self . prefix , self . uid , ( i // max_val ) + <NUM_LIT:1> , ( i % max_val ) + <NUM_LIT:1> ) <EOL> i += <NUM_LIT:1> <EOL> @ classmethod <EOL> def reset ( cls , uid = None ) : <EOL> """<STR_LIT>""" <EOL> for instance in cls . instances : <EOL> if uid : <EOL> instance . uid = uid <EOL> instance . generator = instance . get_generator ( ) <EOL> def get_instances ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ "<STR_LIT>" % ( self . __class__ . __name__ , i . prefix , self . uid ) <EOL> for i in self . instances ] <EOL> def __call__ ( self ) : <EOL> return next ( self . generator ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . prefix , self . uid ) <EOL> class CaseInsensitiveDict ( collections . MutableMapping ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , data = None , ** kwargs ) : <EOL> self . _store = dict ( ) <EOL> self . update ( data or { } , ** kwargs ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _store [ key . lower ( ) ] = ( key , value ) <EOL> def __contains__ ( self , key ) : <EOL> return key . lower ( ) in self . _store <EOL> def __getattr__ ( self , attr ) : <EOL> return self . get ( attr , '<STR_LIT>' ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . _store [ key . lower ( ) ] [ <NUM_LIT:1> ] <EOL> def __delitem__ ( self , key ) : <EOL> raise NotImplementedError ( ) <EOL> def __iter__ ( self ) : <EOL> return ( key for key , value in self . _store . values ( ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _store ) <EOL> def __repr__ ( self ) : <EOL> return str ( dict ( self . items ( ) ) ) <EOL> def config ( filename_or_fd , section = '<STR_LIT>' ) : <EOL> config = ConfigParser ( ) <EOL> if hasattr ( filename_or_fd , '<STR_LIT>' ) : <EOL> config . readfp ( filename_or_fd ) <EOL> else : <EOL> config . read ( filename_or_fd ) <EOL> return dict ( config . items ( section ) ) </s>
