<s> """<STR_LIT>""" <EOL> import math <EOL> from django . db . models . query import QuerySet <EOL> from django . utils import six <EOL> from django . utils . encoding import force_text <EOL> from django . utils . translation import ugettext as _ <EOL> LOGARITHMIC , LINEAR = <NUM_LIT:1> , <NUM_LIT:2> <EOL> def parse_tag_input ( input ) : <EOL> """<STR_LIT>""" <EOL> if not input : <EOL> return [ ] <EOL> input = force_text ( input ) <EOL> if '<STR_LIT:U+002C>' not in input and '<STR_LIT:">' not in input : <EOL> words = list ( set ( split_strip ( input , '<STR_LIT:U+0020>' ) ) ) <EOL> words . sort ( ) <EOL> return words <EOL> words = [ ] <EOL> buffer = [ ] <EOL> to_be_split = [ ] <EOL> saw_loose_comma = False <EOL> open_quote = False <EOL> i = iter ( input ) <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> c = next ( i ) <EOL> if c == '<STR_LIT:">' : <EOL> if buffer : <EOL> to_be_split . append ( '<STR_LIT>' . join ( buffer ) ) <EOL> buffer = [ ] <EOL> open_quote = True <EOL> c = next ( i ) <EOL> while c != '<STR_LIT:">' : <EOL> buffer . append ( c ) <EOL> c = next ( i ) <EOL> if buffer : <EOL> word = '<STR_LIT>' . join ( buffer ) . strip ( ) <EOL> if word : <EOL> words . append ( word ) <EOL> buffer = [ ] <EOL> open_quote = False <EOL> else : <EOL> if not saw_loose_comma and c == '<STR_LIT:U+002C>' : <EOL> saw_loose_comma = True <EOL> buffer . append ( c ) <EOL> except StopIteration : <EOL> if buffer : <EOL> if open_quote and '<STR_LIT:U+002C>' in buffer : <EOL> saw_loose_comma = True <EOL> to_be_split . append ( '<STR_LIT>' . join ( buffer ) ) <EOL> if to_be_split : <EOL> if saw_loose_comma : <EOL> delimiter = '<STR_LIT:U+002C>' <EOL> else : <EOL> delimiter = '<STR_LIT:U+0020>' <EOL> for chunk in to_be_split : <EOL> words . extend ( split_strip ( chunk , delimiter ) ) <EOL> words = list ( set ( words ) ) <EOL> words . sort ( ) <EOL> return words <EOL> def split_strip ( input , delimiter = '<STR_LIT:U+002C>' ) : <EOL> """<STR_LIT>""" <EOL> if not input : <EOL> return [ ] <EOL> words = [ w . strip ( ) for w in input . split ( delimiter ) ] <EOL> return [ w for w in words if w ] <EOL> def edit_string_for_tags ( tags ) : <EOL> """<STR_LIT>""" <EOL> names = [ ] <EOL> use_commas = False <EOL> for tag in tags : <EOL> name = tag . name <EOL> if '<STR_LIT:U+002C>' in name : <EOL> names . append ( '<STR_LIT>' % name ) <EOL> continue <EOL> elif '<STR_LIT:U+0020>' in name : <EOL> if not use_commas : <EOL> use_commas = True <EOL> names . append ( name ) <EOL> if use_commas : <EOL> glue = '<STR_LIT:U+002CU+0020>' <EOL> else : <EOL> glue = '<STR_LIT:U+0020>' <EOL> return glue . join ( names ) <EOL> def get_queryset_and_model ( queryset_or_model ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return queryset_or_model , queryset_or_model . model <EOL> except AttributeError : <EOL> return queryset_or_model . _default_manager . all ( ) , queryset_or_model <EOL> def get_tag_list ( tags ) : <EOL> """<STR_LIT>""" <EOL> from tagging . models import Tag <EOL> if isinstance ( tags , Tag ) : <EOL> return [ tags ] <EOL> elif isinstance ( tags , QuerySet ) and tags . model is Tag : <EOL> return tags <EOL> elif isinstance ( tags , six . string_types ) : <EOL> return Tag . objects . filter ( name__in = parse_tag_input ( tags ) ) <EOL> elif isinstance ( tags , ( list , tuple ) ) : <EOL> if len ( tags ) == <NUM_LIT:0> : <EOL> return tags <EOL> contents = set ( ) <EOL> for item in tags : <EOL> if isinstance ( item , six . string_types ) : <EOL> contents . add ( '<STR_LIT:string>' ) <EOL> elif isinstance ( item , Tag ) : <EOL> contents . add ( '<STR_LIT>' ) <EOL> elif isinstance ( item , six . integer_types ) : <EOL> contents . add ( '<STR_LIT:int>' ) <EOL> if len ( contents ) == <NUM_LIT:1> : <EOL> if '<STR_LIT:string>' in contents : <EOL> return Tag . objects . filter ( name__in = [ force_text ( tag ) for tag in tags ] ) <EOL> elif '<STR_LIT>' in contents : <EOL> return tags <EOL> elif '<STR_LIT:int>' in contents : <EOL> return Tag . objects . filter ( id__in = tags ) <EOL> else : <EOL> raise ValueError ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> raise ValueError ( _ ( '<STR_LIT>' ) ) <EOL> def get_tag ( tag ) : <EOL> """<STR_LIT>""" <EOL> from tagging . models import Tag <EOL> if isinstance ( tag , Tag ) : <EOL> return tag <EOL> try : <EOL> if isinstance ( tag , six . string_types ) : <EOL> return Tag . objects . get ( name = tag ) <EOL> elif isinstance ( tag , six . integer_types ) : <EOL> return Tag . objects . get ( id = tag ) <EOL> except Tag . DoesNotExist : <EOL> pass <EOL> return None <EOL> def _calculate_thresholds ( min_weight , max_weight , steps ) : <EOL> delta = ( max_weight - min_weight ) / float ( steps ) <EOL> return [ min_weight + i * delta for i in range ( <NUM_LIT:1> , steps + <NUM_LIT:1> ) ] <EOL> def _calculate_tag_weight ( weight , max_weight , distribution ) : <EOL> """<STR_LIT>""" <EOL> if distribution == LINEAR or max_weight == <NUM_LIT:1> : <EOL> return weight <EOL> elif distribution == LOGARITHMIC : <EOL> return math . log ( weight ) * max_weight / math . log ( max_weight ) <EOL> raise ValueError ( _ ( '<STR_LIT>' ) % distribution ) <EOL> def calculate_cloud ( tags , steps = <NUM_LIT:4> , distribution = LOGARITHMIC ) : <EOL> """<STR_LIT>""" <EOL> if len ( tags ) > <NUM_LIT:0> : <EOL> counts = [ tag . count for tag in tags ] <EOL> min_weight = float ( min ( counts ) ) <EOL> max_weight = float ( max ( counts ) ) <EOL> thresholds = _calculate_thresholds ( min_weight , max_weight , steps ) <EOL> for tag in tags : <EOL> font_set = False <EOL> tag_weight = _calculate_tag_weight ( tag . count , max_weight , distribution ) <EOL> for i in range ( steps ) : <EOL> if not font_set and tag_weight <= thresholds [ i ] : <EOL> tag . font_size = i + <NUM_LIT:1> <EOL> font_set = True <EOL> return tags </s>
