<s> """<STR_LIT>""" <EOL> import inspect <EOL> from django . utils . safestring import SafeUnicode , SafeData , EscapeData <EOL> from jinja2 import Markup , environmentfilter , Undefined <EOL> __all__ = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) <EOL> DJANGO = '<STR_LIT>' <EOL> JINJA2 = '<STR_LIT>' <EOL> def django_filter_to_jinja2 ( filter_func ) : <EOL> """<STR_LIT>""" <EOL> def _convert_out ( v ) : <EOL> if isinstance ( v , SafeData ) : <EOL> return Markup ( v ) <EOL> if isinstance ( v , EscapeData ) : <EOL> return Markup . escape ( v ) <EOL> return v <EOL> def _convert_in ( v ) : <EOL> if isinstance ( v , Undefined ) : <EOL> return '<STR_LIT>' <EOL> return v <EOL> def conversion_wrapper ( value , * args , ** kwargs ) : <EOL> result = filter_func ( _convert_in ( value ) , * args , ** kwargs ) <EOL> return _convert_out ( result ) <EOL> if hasattr ( filter_func , '<STR_LIT>' ) : <EOL> @ environmentfilter <EOL> def autoescape_wrapper ( environment , * args , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = environment . autoescape <EOL> return conversion_wrapper ( * args , ** kwargs ) <EOL> return autoescape_wrapper <EOL> else : <EOL> return conversion_wrapper <EOL> def jinja2_filter_to_django ( filter_func ) : <EOL> """<STR_LIT>""" <EOL> if guess_filter_type ( filter_func ) [ <NUM_LIT:0> ] == DJANGO : <EOL> return filter_func <EOL> def _convert ( v ) : <EOL> return v <EOL> def wrapped ( value , * args , ** kwargs ) : <EOL> result = filter_func ( value , * args , ** kwargs ) <EOL> return _convert ( result ) <EOL> return wrapped <EOL> def guess_filter_type ( filter_func ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( filter_func , '<STR_LIT>' ) or hasattr ( filter_func , '<STR_LIT>' ) : <EOL> return JINJA2 , False <EOL> args = inspect . getargspec ( filter_func ) <EOL> if len ( args [ <NUM_LIT:0> ] ) - ( len ( args [ <NUM_LIT:3> ] ) if args [ <NUM_LIT:3> ] else <NUM_LIT:0> ) > <NUM_LIT:2> : <EOL> return JINJA2 , False <EOL> if hasattr ( filter_func , '<STR_LIT>' ) : <EOL> return DJANGO , True <EOL> return False , True </s>
