<s> from transifex . txcommon . tests . base import USER_ROLES <EOL> from transifex . txcommon . log import logger <EOL> def getitem ( list , index , default = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return list [ index ] <EOL> except IndexError : <EOL> return default <EOL> def assert_status_code ( unittest , response , expected_code , url , user_role ) : <EOL> unittest . assertEqual ( response . status_code , expected_code , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( url , response . status_code , expected_code , <EOL> user_role , response ) ) <EOL> def check_page_status ( unittest , user_role , url_roles ) : <EOL> """<STR_LIT>""" <EOL> if not user_role in USER_ROLES : <EOL> unittest . fail ( "<STR_LIT>" % user_role ) <EOL> for url , date_dict in url_roles . items ( ) : <EOL> try : <EOL> expected_code = date_dict [ user_role ] [ <NUM_LIT:0> ] <EOL> args = getitem ( date_dict [ user_role ] , <NUM_LIT:1> , { } ) <EOL> expected_contents = getitem ( date_dict [ user_role ] , <NUM_LIT:2> , None ) <EOL> unexpected_contents = getitem ( date_dict [ user_role ] , <NUM_LIT:3> , None ) <EOL> follow_redirect = getitem ( date_dict [ user_role ] , <NUM_LIT:4> , False ) <EOL> client = unittest . client [ user_role ] <EOL> method , url = tuple ( url . split ( '<STR_LIT::>' ) ) <EOL> if method == '<STR_LIT:GET>' : <EOL> response = client . get ( url , args , follow = follow_redirect ) <EOL> elif method == '<STR_LIT:POST>' : <EOL> response = client . post ( url , args , follow = follow_redirect ) <EOL> else : <EOL> unittest . fail ( "<STR_LIT>" % role ) <EOL> assert_status_code ( unittest , response , expected_code , url , <EOL> user_role ) <EOL> if expected_contents : <EOL> unittest . assertContains ( response , expected_contents , <EOL> status_code = expected_code ) <EOL> if unexpected_contents : <EOL> unittest . assertNotContains ( response , unexpected_contents , <EOL> status_code = expected_code ) <EOL> except KeyError : <EOL> logger . info ( "<STR_LIT>" % ( <EOL> user_role , url ) ) <EOL> def convert_url_roles ( url_with_roles_as_key ) : <EOL> """<STR_LIT>""" <EOL> url_keys = { } <EOL> for url_role , urls in url_with_roles_as_key . items ( ) : <EOL> user_roles_dict = { } <EOL> url_role = eval ( url_role ) <EOL> for user_role in USER_ROLES : <EOL> user_roles_dict . update ( { user_role : url_role } ) <EOL> for url in urls : <EOL> url_keys . update ( { url : user_roles_dict } ) <EOL> return url_keys <EOL> COLORS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def color_text ( text , color_name , bold = False ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( <EOL> int ( bold ) , COLORS . index ( color_name ) + <NUM_LIT:30> , text ) <EOL> def grep ( haystack , needle , ln = <NUM_LIT:0> , color = None ) : <EOL> """<STR_LIT>""" <EOL> ret = [ ] <EOL> lines = haystack . split ( ) <EOL> for i , line in enumerate ( lines ) : <EOL> if needle in line : <EOL> first_possible = i - ln <EOL> start = first_possible if first_possible >= <NUM_LIT:0> else <NUM_LIT:0> <EOL> before = '<STR_LIT:\n>' . join ( lines [ start : i ] ) <EOL> last_possible = len ( lines ) <EOL> end = i + <NUM_LIT:1> + ln if i + <NUM_LIT:1> + ln <= last_possible else last_possible <EOL> after = '<STR_LIT:\n>' . join ( lines [ i + <NUM_LIT:1> : end ] ) <EOL> txt = '<STR_LIT>' % { <EOL> '<STR_LIT>' : before + '<STR_LIT:\n>' if before else '<STR_LIT>' , <EOL> '<STR_LIT>' : color_text ( lines [ i ] , color ) if color else lines [ i ] , <EOL> '<STR_LIT>' : '<STR_LIT:\n>' + after if after else '<STR_LIT>' } <EOL> ret . append ( txt ) <EOL> return ret <EOL> def highlight_grep ( resp , text , context = <NUM_LIT:2> ) : <EOL> """<STR_LIT>""" <EOL> res = grep ( resp . content , text , context , '<STR_LIT>' ) <EOL> print ( "<STR_LIT>" + <EOL> "<STR_LIT>" . join ( res ) + <EOL> "<STR_LIT>" ) <EOL> def response_in_browser ( resp , halt = True ) : <EOL> """<STR_LIT>""" <EOL> import tempfile , webbrowser , time <EOL> with tempfile . NamedTemporaryFile ( ) as f : <EOL> f . write ( resp . content ) <EOL> f . flush ( ) <EOL> webbrowser . open ( f . name ) <EOL> if halt : <EOL> raw_input ( "<STR_LIT>" ) <EOL> else : <EOL> time . sleep ( <NUM_LIT:1> ) </s>
