<s> import decimal <EOL> import math <EOL> import re <EOL> from oslo_utils import units <EOL> import six <EOL> from django . conf import settings <EOL> from django . contrib . auth import logout <EOL> from django import http <EOL> from django . utils . encoding import force_text <EOL> from django . utils . functional import lazy <EOL> from django . utils import translation <EOL> def _lazy_join ( separator , strings ) : <EOL> return separator . join ( [ force_text ( s ) <EOL> for s in strings ] ) <EOL> lazy_join = lazy ( _lazy_join , six . text_type ) <EOL> def bytes_to_gigabytes ( bytes ) : <EOL> return int ( math . ceil ( float ( bytes ) / units . Gi ) ) <EOL> def add_logout_reason ( request , response , reason ) : <EOL> lang = translation . get_language_from_request ( request ) <EOL> with translation . override ( lang ) : <EOL> reason = six . text_type ( reason ) <EOL> if six . PY2 : <EOL> reason = reason . encode ( '<STR_LIT:utf-8>' ) <EOL> response . set_cookie ( '<STR_LIT>' , reason , max_age = <NUM_LIT:10> ) <EOL> def logout_with_message ( request , msg , redirect = True ) : <EOL> """<STR_LIT>""" <EOL> logout ( request ) <EOL> if redirect : <EOL> response = http . HttpResponseRedirect ( <EOL> '<STR_LIT>' % ( settings . LOGOUT_URL , request . path ) ) <EOL> else : <EOL> response = http . HttpResponseRedirect ( settings . LOGOUT_URL ) <EOL> add_logout_reason ( request , response , msg ) <EOL> return response <EOL> def get_page_size ( request , default = <NUM_LIT:20> ) : <EOL> session = request . session <EOL> cookies = request . COOKIES <EOL> try : <EOL> page_size = int ( session . get ( '<STR_LIT>' , <EOL> cookies . get ( '<STR_LIT>' , <EOL> getattr ( settings , <EOL> '<STR_LIT>' , <EOL> default ) ) ) ) <EOL> except ValueError : <EOL> page_size = session [ '<STR_LIT>' ] = int ( default ) <EOL> return page_size <EOL> def get_log_length ( request , default = <NUM_LIT> ) : <EOL> session = request . session <EOL> cookies = request . COOKIES <EOL> try : <EOL> log_length = int ( session . get ( <EOL> '<STR_LIT>' , <EOL> cookies . get ( '<STR_LIT>' , <EOL> getattr ( settings , <EOL> '<STR_LIT>' , <EOL> default ) ) ) ) <EOL> except ValueError : <EOL> log_length = session [ '<STR_LIT>' ] = int ( default ) <EOL> return log_length <EOL> def natural_sort ( attr ) : <EOL> return lambda x : [ int ( s ) if s . isdigit ( ) else s for s in <EOL> re . split ( r'<STR_LIT>' , getattr ( x , attr , x ) ) ] <EOL> def get_keys ( tuple_of_tuples ) : <EOL> """<STR_LIT>""" <EOL> return tuple ( [ t [ <NUM_LIT:0> ] for t in tuple_of_tuples ] ) <EOL> def value_for_key ( tuple_of_tuples , key ) : <EOL> """<STR_LIT>""" <EOL> for t in tuple_of_tuples : <EOL> if t [ <NUM_LIT:0> ] == key : <EOL> return t [ <NUM_LIT:1> ] <EOL> else : <EOL> return key <EOL> def next_key ( tuple_of_tuples , key ) : <EOL> """<STR_LIT>""" <EOL> for i , t in enumerate ( tuple_of_tuples ) : <EOL> if t [ <NUM_LIT:0> ] == key : <EOL> try : <EOL> return tuple_of_tuples [ i + <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def previous_key ( tuple_of_tuples , key ) : <EOL> """<STR_LIT>""" <EOL> for i , t in enumerate ( tuple_of_tuples ) : <EOL> if t [ <NUM_LIT:0> ] == key : <EOL> try : <EOL> return tuple_of_tuples [ i - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> return None <EOL> def format_value ( value ) : <EOL> """<STR_LIT>""" <EOL> value = decimal . Decimal ( str ( value ) ) <EOL> if int ( value ) == value : <EOL> return int ( value ) <EOL> return float ( round ( value , <NUM_LIT:1> ) ) </s>
