<s> import six <EOL> from django . core . cache import cache <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from . conf import settings <EOL> class Device ( object ) : <EOL> def __init__ ( self , <EOL> width = settings . RESPONSIVE_DEFAULT_WIDTH , <EOL> height = settings . RESPONSIVE_DEFAULT_HEIGHT , <EOL> pixel_ratio = settings . RESPONSIVE_DEFAULT_PIXEL_RATIO ) : <EOL> self . width = int ( width ) <EOL> self . height = int ( height ) <EOL> self . pixel_ratio = float ( pixel_ratio ) <EOL> self . matched = [ ] <EOL> self . cache_key = '<STR_LIT>' % { <EOL> '<STR_LIT>' : settings . RESPONSIVE_CACHE_PREFIX , <EOL> '<STR_LIT:width>' : self . width , <EOL> '<STR_LIT>' : self . height , <EOL> '<STR_LIT>' : self . pixel_ratio <EOL> } <EOL> matches = cache . get ( self . cache_key , None ) <EOL> if not matches : <EOL> matches = self . match_media_queries ( ) <EOL> cache . set ( self . cache_key , matches , settings . RESPONSIVE_CACHE_DURATION ) <EOL> for name , value in six . iteritems ( matches ) : <EOL> setattr ( self , '<STR_LIT>' % name , value ) <EOL> if value is True : <EOL> self . matched . append ( name ) <EOL> def match_media_queries ( self ) : <EOL> matches = { } <EOL> media_queries = self . get_media_queries ( ) <EOL> for alias , config in six . iteritems ( media_queries ) : <EOL> yesno = [ ] <EOL> if config [ '<STR_LIT>' ] : <EOL> yesno . append ( self . width >= config [ '<STR_LIT>' ] ) <EOL> if config [ '<STR_LIT>' ] : <EOL> yesno . append ( self . width <= config [ '<STR_LIT>' ] ) <EOL> if config [ '<STR_LIT>' ] : <EOL> yesno . append ( self . height >= config [ '<STR_LIT>' ] ) <EOL> if config [ '<STR_LIT>' ] : <EOL> yesno . append ( self . height <= config [ '<STR_LIT>' ] ) <EOL> if config [ '<STR_LIT>' ] : <EOL> yesno . append ( self . pixel_ratio == config [ '<STR_LIT>' ] ) <EOL> matches [ alias ] = all ( yesno ) <EOL> return matches <EOL> @ staticmethod <EOL> def get_media_queries ( ) : <EOL> media_queries = settings . RESPONSIVE_MEDIA_QUERIES <EOL> for name , config in six . iteritems ( media_queries ) : <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if attr not in config : <EOL> config [ attr ] = None <EOL> elif config [ attr ] is not None : <EOL> try : <EOL> config [ attr ] = int ( config [ attr ] ) if attr != '<STR_LIT>' else float ( config [ attr ] ) <EOL> except ValueError : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( attr , name ) ) <EOL> else : <EOL> media_queries [ name ] [ attr ] = config [ attr ] <EOL> return media_queries <EOL> def clear ( self ) : <EOL> cache . delete ( self . cache_key ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % { <EOL> '<STR_LIT:width>' : self . width , <EOL> '<STR_LIT>' : self . height , <EOL> '<STR_LIT>' : self . pixel_ratio <EOL> } </s>
