<s> import operator <EOL> from django import template <EOL> from django . utils . timesince import timesince <EOL> from transifex . languages . models import Language <EOL> register = template . Library ( ) <EOL> class StatBarsPositions ( dict ) : <EOL> """<STR_LIT>""" <EOL> class BarPos : <EOL> def __init__ ( self , width , left = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . w = width <EOL> self . l = left <EOL> def __init__ ( self , bar_data , width = <NUM_LIT:100> , border = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> innerwidth = width <EOL> if innerwidth < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( len ( bar_data ) , width , border ) ) <EOL> totsegwidth = reduce ( operator . add , ( x [ <NUM_LIT:1> ] for x in bar_data ) , <NUM_LIT:0> ) <EOL> if totsegwidth == <NUM_LIT:0> : <EOL> self [ '<STR_LIT>' ] = self . BarPos ( width , <NUM_LIT:0> ) <EOL> self [ '<STR_LIT>' ] = self . BarPos ( <NUM_LIT:0> , width ) <EOL> self [ '<STR_LIT>' ] = self . BarPos ( <NUM_LIT:0> , width ) <EOL> return <EOL> oldend = <NUM_LIT:0> <EOL> for segnum , segment in enumerate ( bar_data ) : <EOL> if segment [ <NUM_LIT:1> ] < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( segment [ <NUM_LIT:1> ] , segnum + <NUM_LIT:1> ) ) <EOL> fl = oldend <EOL> fr = fl + segment [ <NUM_LIT:1> ] * innerwidth <EOL> oldend = fr <EOL> l = int ( round ( float ( fl ) / totsegwidth ) ) <EOL> r = int ( round ( float ( fr ) / totsegwidth ) ) <EOL> self [ segment [ <NUM_LIT:0> ] ] = self . BarPos ( r - l , l ) <EOL> return <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def stats_bar_simple ( stat , width = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> total = stat . total <EOL> trans = stat . translated <EOL> try : <EOL> trans_percent = ( trans * <NUM_LIT:100> / total ) <EOL> except ZeroDivisionError : <EOL> trans_percent = <NUM_LIT:100> <EOL> untrans_percent = <NUM_LIT:100> - trans_percent <EOL> untrans = total - trans <EOL> return { '<STR_LIT>' : untrans_percent , <EOL> '<STR_LIT>' : trans_percent , <EOL> '<STR_LIT>' : untrans , <EOL> '<STR_LIT>' : trans , <EOL> '<STR_LIT>' : StatBarsPositions ( [ ( '<STR_LIT>' , trans_percent ) , <EOL> ( '<STR_LIT>' , untrans_percent ) ] , width ) , <EOL> '<STR_LIT:width>' : width } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def stats_bar_actions ( stat , width = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> trans_percent = ( stat . translated * <NUM_LIT:100> / stat . total ) <EOL> except ZeroDivisionError : <EOL> trans_percent = <NUM_LIT:100> <EOL> untrans_percent = <NUM_LIT:100> - trans_percent <EOL> return { '<STR_LIT>' : untrans_percent , <EOL> '<STR_LIT>' : trans_percent , <EOL> '<STR_LIT>' : StatBarsPositions ( [ ( '<STR_LIT>' , trans_percent ) , <EOL> ( '<STR_LIT>' , untrans_percent ) ] , width ) , <EOL> '<STR_LIT:width>' : width } <EOL> @ register . filter ( name = '<STR_LIT>' ) <EOL> def percentage ( fraction , population ) : <EOL> try : <EOL> return "<STR_LIT>" % int ( ( ( fraction ) * <NUM_LIT:100> / ( population ) ) ) <EOL> except ZeroDivisionError : <EOL> if population == fraction : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return '<STR_LIT>' <EOL> except ValueError : <EOL> return '<STR_LIT>' </s>
