def process_request(self, request):
     device_families = importlib.import_module(getattr(settings, 'DEVICE_FAMILIES', 'templateswitcher.device_families'))
     device_obj = getattr(request, 'device', None)
     # Use hash as the key since UA's can be quite llong, dont want to hit memcache 250 byte limit
     device_cache_key = hash(request.META['HTTP_USER_AGENT'])
     template_set = cache.get(device_cache_key)
     full_path = request.get_full_path()
     media_request = (full_path.startswith(settings.MEDIA_URL) or
                     full_path.startswith(settings.ADMIN_MEDIA_PREFIX) or full_path.startswith('/favicon'))
     
     if not template_set:
         # set the device switcher library according to the settings - defaults to wurfl
         device_switch_libs = getattr(settings, 'DEVICE_SWITCH_LIB', ['WurlfSniffer'])
         da_api = getattr(settings, 'DEVICE_ATLAS_API_FILE', None)
         device_cache_timeout = getattr(settings, 'DEVICE_CACHE_TIMEOUT', 72000)
             
         # import requied device library classes
         if 'ApexVertexSniffer' in device_switch_libs:
             from mobile.sniffer.apexvertex.sniffer import ApexVertexSniffer
         if 'WAPProfileSniffer' in device_switch_libs:
             from mobile.sniffer.wapprofile.sniffer import WAPProfileSniffer
         if 'DeviceAtlasSniffer' in device_switch_libs:
             if not os.path.exists(da_api):
                 raise Exception('DEVICE_ATLAS_API_FILE must be in your setting and contain a valid path to use Device Atlas.')
             from mobile.sniffer.deviceatlas.sniffer import DeviceAtlasSniffer
         if 'WurlfSniffer' in device_switch_libs:
             from mobile.sniffer.wurlf.sniffer import WurlfSniffer
         
         chained_libs = []
         for device_lib in device_switch_libs:
             chained_libs.append(eval(device_lib)())
         
         # instantiate the sniffer and device object
         sniffer = ChainedSniffer(chained_libs)
         device_object = sniffer.sniff(request)
         template_set = device_families.get_device_family(device_object)
             
         # copy the device object to the request object
         request.device = device_object
         cache.set(device_cache_key, template_set, device_cache_timeout)
         
     if not media_request:
         # switch the template dir for the given device
         settings.TEMPLATE_DIRS = (
             settings.DEVICE_TEMPLATE_DIRS[template_set],
         )
def run():
    
    # Do imports here, because Django init order causes otherwise circular imports
    from mobile.sniffer.apexvertex.sniffer import ApexVertexSniffer
    from mobile.sniffer.wapprofile.sniffer import WAPProfileSniffer 
    from mobile.sniffer.deviceatlas.sniffer import DeviceAtlasSniffer
    
    
    # Create all supported sniffers
    da = DeviceAtlasSniffer(da_api_file)
    apex = ApexVertexSniffer()
    wap = WAPProfileSniffer()
        
    # Preferred order of sniffers    
    sniffer = ChainedSniffer([apex, wap, da])
        
    n95 = "Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95/11.0.026; Profile MIDP-2.0 Configuration/CLDC-1.1) AppleWebKit/413 (KHTML, like Gecko) Safari/413"
    req = create_test_request(n95)
    
    multi_backend_ua = sniffer.sniff(req)    
    provider, val = multi_backend_ua.get_with_provider("stream.3gp.h263")
    
    assert provider == apex        
    assert multi_backend_ua.get("mp4.h264.level11") == True    

    # This should come from deviceatlas - BlackBerry does not announce WAP profiles    
    blackberry = "BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103"
    req = create_test_request(blackberry)
    
    multi_backend_ua = sniffer.sniff(req)    
    provider, val = multi_backend_ua.get_with_provider("usableDisplayWidth")
    assert provider == da
    
    print "Got:" +str(provider) + " " + str(val)
    
    # Then test something which is not user agent based -
    # this very old Sony
    sony = "http://wap.sonyericsson.com/UAprof/T68R502.xml"
    req = create_test_request(profile_url = sony)
    multi_backend_ua = sniffer.sniff(req)        
    provider, val = multi_backend_ua.get_with_provider("usableDisplayWidth")
    assert provider == wap, "Got backend: " + str(provider)
    assert val == 101, "Got " + str(val)