<s> """<STR_LIT>""" <EOL> import re <EOL> import math <EOL> from random import choice <EOL> from itertools import groupby <EOL> from collections import namedtuple <EOL> from jinja2 . utils import Markup , escape , pformat , urlize , soft_unicode , unicode_urlencode <EOL> from jinja2 . runtime import Undefined <EOL> from jinja2 . exceptions import FilterArgumentError <EOL> from jinja2 . _compat import imap , string_types , text_type , iteritems <EOL> _word_re = re . compile ( r'<STR_LIT>' ) <EOL> _word_beginning_split_re = re . compile ( r'<STR_LIT>' ) <EOL> def contextfilter ( f ) : <EOL> """<STR_LIT>""" <EOL> f . contextfilter = True <EOL> return f <EOL> def evalcontextfilter ( f ) : <EOL> """<STR_LIT>""" <EOL> f . evalcontextfilter = True <EOL> return f <EOL> def environmentfilter ( f ) : <EOL> """<STR_LIT>""" <EOL> f . environmentfilter = True <EOL> return f <EOL> def make_attrgetter ( environment , attribute ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( attribute , string_types ) or ( '<STR_LIT:.>' not in attribute and not attribute . isdigit ( ) ) : <EOL> return lambda x : environment . getitem ( x , attribute ) <EOL> attribute = attribute . split ( '<STR_LIT:.>' ) <EOL> def attrgetter ( item ) : <EOL> for part in attribute : <EOL> if part . isdigit ( ) : <EOL> part = int ( part ) <EOL> item = environment . getitem ( item , part ) <EOL> return item <EOL> return attrgetter <EOL> def do_forceescape ( value ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . __html__ ( ) <EOL> return escape ( text_type ( value ) ) <EOL> def do_urlencode ( value ) : <EOL> """<STR_LIT>""" <EOL> itemiter = None <EOL> if isinstance ( value , dict ) : <EOL> itemiter = iteritems ( value ) <EOL> elif not isinstance ( value , string_types ) : <EOL> try : <EOL> itemiter = iter ( value ) <EOL> except TypeError : <EOL> pass <EOL> if itemiter is None : <EOL> return unicode_urlencode ( value ) <EOL> return u'<STR_LIT:&>' . join ( unicode_urlencode ( k ) + '<STR_LIT:=>' + <EOL> unicode_urlencode ( v , for_qs = True ) <EOL> for k , v in itemiter ) <EOL> @ evalcontextfilter <EOL> def do_replace ( eval_ctx , s , old , new , count = None ) : <EOL> """<STR_LIT>""" <EOL> if count is None : <EOL> count = - <NUM_LIT:1> <EOL> if not eval_ctx . autoescape : <EOL> return text_type ( s ) . replace ( text_type ( old ) , text_type ( new ) , count ) <EOL> if hasattr ( old , '<STR_LIT>' ) or hasattr ( new , '<STR_LIT>' ) and not hasattr ( s , '<STR_LIT>' ) : <EOL> s = escape ( s ) <EOL> else : <EOL> s = soft_unicode ( s ) <EOL> return s . replace ( soft_unicode ( old ) , soft_unicode ( new ) , count ) <EOL> def do_upper ( s ) : <EOL> """<STR_LIT>""" <EOL> return soft_unicode ( s ) . upper ( ) <EOL> def do_lower ( s ) : <EOL> """<STR_LIT>""" <EOL> return soft_unicode ( s ) . lower ( ) <EOL> @ evalcontextfilter <EOL> def do_xmlattr ( _eval_ctx , d , autospace = True ) : <EOL> """<STR_LIT>""" <EOL> rv = u'<STR_LIT:U+0020>' . join ( <EOL> u'<STR_LIT>' % ( escape ( key ) , escape ( value ) ) <EOL> for key , value in iteritems ( d ) <EOL> if value is not None and not isinstance ( value , Undefined ) <EOL> ) <EOL> if autospace and rv : <EOL> rv = u'<STR_LIT:U+0020>' + rv <EOL> if _eval_ctx . autoescape : <EOL> rv = Markup ( rv ) <EOL> return rv <EOL> def do_capitalize ( s ) : <EOL> """<STR_LIT>""" <EOL> return soft_unicode ( s ) . capitalize ( ) <EOL> def do_title ( s ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . join ( <EOL> [ item [ <NUM_LIT:0> ] . upper ( ) + item [ <NUM_LIT:1> : ] . lower ( ) <EOL> for item in _word_beginning_split_re . split ( soft_unicode ( s ) ) <EOL> if item ] ) <EOL> def do_dictsort ( value , case_sensitive = False , by = '<STR_LIT:key>' ) : <EOL> """<STR_LIT>""" <EOL> if by == '<STR_LIT:key>' : <EOL> pos = <NUM_LIT:0> <EOL> elif by == '<STR_LIT:value>' : <EOL> pos = <NUM_LIT:1> <EOL> else : <EOL> raise FilterArgumentError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def sort_func ( item ) : <EOL> value = item [ pos ] <EOL> if isinstance ( value , string_types ) and not case_sensitive : <EOL> value = value . lower ( ) <EOL> return value <EOL> return sorted ( value . items ( ) , key = sort_func ) <EOL> @ environmentfilter <EOL> def do_sort ( environment , value , reverse = False , case_sensitive = False , <EOL> attribute = None ) : <EOL> """<STR_LIT>""" <EOL> if not case_sensitive : <EOL> def sort_func ( item ) : <EOL> if isinstance ( item , string_types ) : <EOL> item = item . lower ( ) <EOL> return item <EOL> else : <EOL> sort_func = None <EOL> if attribute is not None : <EOL> getter = make_attrgetter ( environment , attribute ) <EOL> def sort_func ( item , processor = sort_func or ( lambda x : x ) ) : <EOL> return processor ( getter ( item ) ) <EOL> return sorted ( value , key = sort_func , reverse = reverse ) <EOL> def do_default ( value , default_value = u'<STR_LIT>' , boolean = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , Undefined ) or ( boolean and not value ) : <EOL> return default_value <EOL> return value <EOL> @ evalcontextfilter <EOL> def do_join ( eval_ctx , value , d = u'<STR_LIT>' , attribute = None ) : <EOL> """<STR_LIT>""" <EOL> if attribute is not None : <EOL> value = imap ( make_attrgetter ( eval_ctx . environment , attribute ) , value ) <EOL> if not eval_ctx . autoescape : <EOL> return text_type ( d ) . join ( imap ( text_type , value ) ) <EOL> if not hasattr ( d , '<STR_LIT>' ) : <EOL> value = list ( value ) <EOL> do_escape = False <EOL> for idx , item in enumerate ( value ) : <EOL> if hasattr ( item , '<STR_LIT>' ) : <EOL> do_escape = True <EOL> else : <EOL> value [ idx ] = text_type ( item ) <EOL> if do_escape : <EOL> d = escape ( d ) <EOL> else : <EOL> d = text_type ( d ) <EOL> return d . join ( value ) <EOL> return soft_unicode ( d ) . join ( imap ( soft_unicode , value ) ) <EOL> def do_center ( value , width = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return text_type ( value ) . center ( width ) <EOL> @ environmentfilter <EOL> def do_first ( environment , seq ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return next ( iter ( seq ) ) <EOL> except StopIteration : <EOL> return environment . undefined ( '<STR_LIT>' ) <EOL> @ environmentfilter <EOL> def do_last ( environment , seq ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return next ( iter ( reversed ( seq ) ) ) <EOL> except StopIteration : <EOL> return environment . undefined ( '<STR_LIT>' ) <EOL> @ environmentfilter <EOL> def do_random ( environment , seq ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return choice ( seq ) <EOL> except IndexError : <EOL> return environment . undefined ( '<STR_LIT>' ) <EOL> def do_filesizeformat ( value , binary = False ) : <EOL> """<STR_LIT>""" <EOL> bytes = float ( value ) <EOL> base = binary and <NUM_LIT> or <NUM_LIT:1000> <EOL> prefixes = [ <EOL> ( binary and '<STR_LIT>' or '<STR_LIT>' ) , <EOL> ( binary and '<STR_LIT>' or '<STR_LIT>' ) , <EOL> ( binary and '<STR_LIT>' or '<STR_LIT>' ) , <EOL> ( binary and '<STR_LIT>' or '<STR_LIT>' ) , <EOL> ( binary and '<STR_LIT>' or '<STR_LIT>' ) , <EOL> ( binary and '<STR_LIT>' or '<STR_LIT>' ) , <EOL> ( binary and '<STR_LIT>' or '<STR_LIT>' ) , <EOL> ( binary and '<STR_LIT>' or '<STR_LIT>' ) <EOL> ] <EOL> if bytes == <NUM_LIT:1> : <EOL> return '<STR_LIT>' <EOL> elif bytes < base : <EOL> return '<STR_LIT>' % bytes <EOL> else : <EOL> for i , prefix in enumerate ( prefixes ) : <EOL> unit = base ** ( i + <NUM_LIT:2> ) <EOL> if bytes < unit : <EOL> return '<STR_LIT>' % ( ( base * bytes / unit ) , prefix ) <EOL> return '<STR_LIT>' % ( ( base * bytes / unit ) , prefix ) <EOL> def do_pprint ( value , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> return pformat ( value , verbose = verbose ) <EOL> @ evalcontextfilter <EOL> def do_urlize ( eval_ctx , value , trim_url_limit = None , nofollow = False , <EOL> target = None ) : <EOL> """<STR_LIT>""" <EOL> rv = urlize ( value , trim_url_limit , nofollow , target ) <EOL> if eval_ctx . autoescape : <EOL> rv = Markup ( rv ) <EOL> return rv <EOL> def do_indent ( s , width = <NUM_LIT:4> , indentfirst = False ) : <EOL> """<STR_LIT>""" <EOL> indention = u'<STR_LIT:U+0020>' * width <EOL> rv = ( u'<STR_LIT:\n>' + indention ) . join ( s . splitlines ( ) ) <EOL> if indentfirst : <EOL> rv = indention + rv <EOL> return rv <EOL> def do_truncate ( s , length = <NUM_LIT:255> , killwords = False , end = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if len ( s ) <= length : <EOL> return s <EOL> elif killwords : <EOL> return s [ : length - len ( end ) ] + end <EOL> result = s [ : length - len ( end ) ] . rsplit ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if len ( result ) < length : <EOL> result += '<STR_LIT:U+0020>' <EOL> return result + end <EOL> @ environmentfilter <EOL> def do_wordwrap ( environment , s , width = <NUM_LIT> , break_long_words = True , <EOL> wrapstring = None ) : <EOL> """<STR_LIT>""" <EOL> if not wrapstring : <EOL> wrapstring = environment . newline_sequence <EOL> import textwrap <EOL> return wrapstring . join ( textwrap . wrap ( s , width = width , expand_tabs = False , <EOL> replace_whitespace = False , <EOL> break_long_words = break_long_words ) ) <EOL> def do_wordcount ( s ) : <EOL> """<STR_LIT>""" <EOL> return len ( _word_re . findall ( s ) ) <EOL> def do_int ( value , default = <NUM_LIT:0> , base = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( value , string_types ) : <EOL> return int ( value , base ) <EOL> return int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> try : <EOL> return int ( float ( value ) ) <EOL> except ( TypeError , ValueError ) : <EOL> return default <EOL> def do_float ( value , default = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return float ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> return default <EOL> def do_format ( value , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if args and kwargs : <EOL> raise FilterArgumentError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return soft_unicode ( value ) % ( kwargs or args ) <EOL> def do_trim ( value ) : <EOL> """<STR_LIT>""" <EOL> return soft_unicode ( value ) . strip ( ) <EOL> def do_striptags ( value ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = value . __html__ ( ) <EOL> return Markup ( text_type ( value ) ) . striptags ( ) <EOL> def do_slice ( value , slices , fill_with = None ) : <EOL> """<STR_LIT>""" <EOL> seq = list ( value ) <EOL> length = len ( seq ) <EOL> items_per_slice = length // slices <EOL> slices_with_extra = length % slices <EOL> offset = <NUM_LIT:0> <EOL> for slice_number in range ( slices ) : <EOL> start = offset + slice_number * items_per_slice <EOL> if slice_number < slices_with_extra : <EOL> offset += <NUM_LIT:1> <EOL> end = offset + ( slice_number + <NUM_LIT:1> ) * items_per_slice <EOL> tmp = seq [ start : end ] <EOL> if fill_with is not None and slice_number >= slices_with_extra : <EOL> tmp . append ( fill_with ) <EOL> yield tmp <EOL> def do_batch ( value , linecount , fill_with = None ) : <EOL> """<STR_LIT>""" <EOL> tmp = [ ] <EOL> for item in value : <EOL> if len ( tmp ) == linecount : <EOL> yield tmp <EOL> tmp = [ ] <EOL> tmp . append ( item ) <EOL> if tmp : <EOL> if fill_with is not None and len ( tmp ) < linecount : <EOL> tmp += [ fill_with ] * ( linecount - len ( tmp ) ) <EOL> yield tmp <EOL> def do_round ( value , precision = <NUM_LIT:0> , method = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not method in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise FilterArgumentError ( '<STR_LIT>' ) <EOL> if method == '<STR_LIT>' : <EOL> return round ( value , precision ) <EOL> func = getattr ( math , method ) <EOL> return func ( value * ( <NUM_LIT:10> ** precision ) ) / ( <NUM_LIT:10> ** precision ) <EOL> _GroupTuple = namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT:list>' ] ) <EOL> @ environmentfilter <EOL> def do_groupby ( environment , value , attribute ) : <EOL> """<STR_LIT>""" <EOL> expr = make_attrgetter ( environment , attribute ) <EOL> return [ _GroupTuple ( key , list ( values ) ) for key , values in groupby ( sorted ( value , key = expr ) , expr ) ] <EOL> @ environmentfilter <EOL> def do_sum ( environment , iterable , attribute = None , start = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if attribute is not None : <EOL> iterable = imap ( make_attrgetter ( environment , attribute ) , iterable ) <EOL> return sum ( iterable , start ) <EOL> def do_list ( value ) : <EOL> """<STR_LIT>""" <EOL> return list ( value ) <EOL> def do_mark_safe ( value ) : <EOL> """<STR_LIT>""" <EOL> return Markup ( value ) <EOL> def do_mark_unsafe ( value ) : <EOL> """<STR_LIT>""" <EOL> return text_type ( value ) <EOL> def do_reverse ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , string_types ) : <EOL> return value [ : : - <NUM_LIT:1> ] <EOL> try : <EOL> return reversed ( value ) <EOL> except TypeError : <EOL> try : <EOL> rv = list ( value ) <EOL> rv . reverse ( ) <EOL> return rv <EOL> except TypeError : <EOL> raise FilterArgumentError ( '<STR_LIT>' ) <EOL> @ environmentfilter <EOL> def do_attr ( environment , obj , name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> name = str ( name ) <EOL> except UnicodeError : <EOL> pass <EOL> else : <EOL> try : <EOL> value = getattr ( obj , name ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if environment . sandboxed and not environment . is_safe_attribute ( obj , name , value ) : <EOL> return environment . unsafe_undefined ( obj , name ) <EOL> return value <EOL> return environment . undefined ( obj = obj , name = name ) <EOL> @ contextfilter <EOL> def do_map ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = args [ <NUM_LIT:0> ] <EOL> seq = args [ <NUM_LIT:1> ] <EOL> if len ( args ) == <NUM_LIT:2> and '<STR_LIT>' in kwargs : <EOL> attribute = kwargs . pop ( '<STR_LIT>' ) <EOL> if kwargs : <EOL> raise FilterArgumentError ( '<STR_LIT>' % <EOL> next ( iter ( kwargs ) ) ) <EOL> func = make_attrgetter ( context . environment , attribute ) <EOL> else : <EOL> try : <EOL> name = args [ <NUM_LIT:2> ] <EOL> args = args [ <NUM_LIT:3> : ] <EOL> except LookupError : <EOL> raise FilterArgumentError ( '<STR_LIT>' ) <EOL> func = lambda item : context . environment . call_filter ( <EOL> name , item , args , kwargs , context = context ) <EOL> if seq : <EOL> for item in seq : <EOL> yield func ( item ) <EOL> @ contextfilter <EOL> def do_select ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return _select_or_reject ( args , kwargs , lambda x : x , False ) <EOL> @ contextfilter <EOL> def do_reject ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return _select_or_reject ( args , kwargs , lambda x : not x , False ) <EOL> @ contextfilter <EOL> def do_selectattr ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return _select_or_reject ( args , kwargs , lambda x : x , True ) <EOL> @ contextfilter <EOL> def do_rejectattr ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return _select_or_reject ( args , kwargs , lambda x : not x , True ) <EOL> def _select_or_reject ( args , kwargs , modfunc , lookup_attr ) : <EOL> context = args [ <NUM_LIT:0> ] <EOL> seq = args [ <NUM_LIT:1> ] <EOL> if lookup_attr : <EOL> try : <EOL> attr = args [ <NUM_LIT:2> ] <EOL> except LookupError : <EOL> raise FilterArgumentError ( '<STR_LIT>' ) <EOL> transfunc = make_attrgetter ( context . environment , attr ) <EOL> off = <NUM_LIT:1> <EOL> else : <EOL> off = <NUM_LIT:0> <EOL> transfunc = lambda x : x <EOL> try : <EOL> name = args [ <NUM_LIT:2> + off ] <EOL> args = args [ <NUM_LIT:3> + off : ] <EOL> func = lambda item : context . environment . call_test ( <EOL> name , item , args , kwargs ) <EOL> except LookupError : <EOL> func = bool <EOL> if seq : <EOL> for item in seq : <EOL> if modfunc ( func ( transfunc ( item ) ) ) : <EOL> yield item <EOL> FILTERS = { <EOL> '<STR_LIT>' : abs , <EOL> '<STR_LIT>' : do_attr , <EOL> '<STR_LIT>' : do_batch , <EOL> '<STR_LIT>' : do_capitalize , <EOL> '<STR_LIT>' : do_center , <EOL> '<STR_LIT:count>' : len , <EOL> '<STR_LIT:d>' : do_default , <EOL> '<STR_LIT:default>' : do_default , <EOL> '<STR_LIT>' : do_dictsort , <EOL> '<STR_LIT:e>' : escape , <EOL> '<STR_LIT>' : escape , <EOL> '<STR_LIT>' : do_filesizeformat , <EOL> '<STR_LIT>' : do_first , <EOL> '<STR_LIT:float>' : do_float , <EOL> '<STR_LIT>' : do_forceescape , <EOL> '<STR_LIT>' : do_format , <EOL> '<STR_LIT>' : do_groupby , <EOL> '<STR_LIT>' : do_indent , <EOL> '<STR_LIT:int>' : do_int , <EOL> '<STR_LIT>' : do_join , <EOL> '<STR_LIT>' : do_last , <EOL> '<STR_LIT>' : len , <EOL> '<STR_LIT:list>' : do_list , <EOL> '<STR_LIT>' : do_lower , <EOL> '<STR_LIT>' : do_map , <EOL> '<STR_LIT>' : do_pprint , <EOL> '<STR_LIT>' : do_random , <EOL> '<STR_LIT>' : do_reject , <EOL> '<STR_LIT>' : do_rejectattr , <EOL> '<STR_LIT:replace>' : do_replace , <EOL> '<STR_LIT>' : do_reverse , <EOL> '<STR_LIT>' : do_round , <EOL> '<STR_LIT>' : do_mark_safe , <EOL> '<STR_LIT>' : do_select , <EOL> '<STR_LIT>' : do_selectattr , <EOL> '<STR_LIT>' : do_slice , <EOL> '<STR_LIT>' : do_sort , <EOL> '<STR_LIT:string>' : soft_unicode , <EOL> '<STR_LIT>' : do_striptags , <EOL> '<STR_LIT>' : do_sum , <EOL> '<STR_LIT:title>' : do_title , <EOL> '<STR_LIT>' : do_trim , <EOL> '<STR_LIT>' : do_truncate , <EOL> '<STR_LIT>' : do_upper , <EOL> '<STR_LIT>' : do_urlencode , <EOL> '<STR_LIT>' : do_urlize , <EOL> '<STR_LIT>' : do_wordcount , <EOL> '<STR_LIT>' : do_wordwrap , <EOL> '<STR_LIT>' : do_xmlattr , <EOL> } </s>
