<s> import os <EOL> import unittest <EOL> import xml . dom . minidom <EOL> from xml . etree . ElementTree import parse <EOL> from transifex . languages . models import Language <EOL> from transifex . resources . models import * <EOL> from transifex . resources . formats . qt import LinguistHandler , _getElementByTagName , _getText <EOL> from transifex . addons . suggestions . models import Suggestion <EOL> from transifex . resources . tests . lib . base import FormatsBaseTestCase <EOL> class TestQtFile ( FormatsBaseTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestQtFile , self ) . setUp ( ) <EOL> self . resource . i18n_method = '<STR_LIT>' <EOL> self . resource . save ( ) <EOL> def test_problematic_file ( self ) : <EOL> filename = '<STR_LIT>' <EOL> handler = LinguistHandler ( os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> filename <EOL> ) ) <EOL> handler . set_language ( self . resource . source_language ) <EOL> handler . parse_file ( True ) <EOL> def test_qt_parser ( self ) : <EOL> """<STR_LIT>""" <EOL> path = os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> handler = LinguistHandler ( path ) <EOL> root = parse ( path ) . getroot ( ) <EOL> messages = { } <EOL> for context in list ( root ) : <EOL> for message in list ( context ) : <EOL> children = list ( message ) <EOL> if not children : <EOL> continue <EOL> source = message . find ( '<STR_LIT:source>' ) . text <EOL> translation = message . find ( '<STR_LIT>' ) . text <EOL> messages [ source ] = translation or source <EOL> handler . set_language ( self . resource . source_language ) <EOL> handler . parse_file ( True ) <EOL> self . stringset = handler . stringset <EOL> entities = <NUM_LIT:0> <EOL> for s in self . stringset . strings : <EOL> if not s . pluralized : <EOL> self . assertEqual ( messages [ s . source_entity ] , s . translation ) <EOL> if s . source_entity == '<STR_LIT>' : <EOL> self . assertTrue ( s . rule in [ <NUM_LIT:1> , <NUM_LIT:5> ] ) <EOL> if s . rule == <NUM_LIT:5> : <EOL> entities += <NUM_LIT:1> <EOL> self . assertEqual ( entities , <NUM_LIT> ) <EOL> def test_qt_parser_fi ( self ) : <EOL> """<STR_LIT>""" <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . set_language ( self . language ) <EOL> handler . parse_file ( ) <EOL> self . stringset = handler . stringset <EOL> nplurals = <NUM_LIT:0> <EOL> entities = <NUM_LIT:0> <EOL> for s in self . stringset . strings : <EOL> if s . source_entity == '<STR_LIT>' and s . pluralized : <EOL> nplurals += <NUM_LIT:1> <EOL> entities += <NUM_LIT:1> <EOL> self . assertEqual ( nplurals , <NUM_LIT:2> ) <EOL> self . assertEqual ( entities , <NUM_LIT> ) <EOL> def test_qt_save2db ( self ) : <EOL> """<STR_LIT>""" <EOL> handler = LinguistHandler ( <EOL> os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> ) <EOL> handler . set_language ( self . resource . source_language ) <EOL> handler . parse_file ( is_source = True ) <EOL> r = self . resource <EOL> l = self . resource . source_language <EOL> handler . bind_resource ( r ) <EOL> handler . save2db ( is_source = True ) <EOL> self . assertEqual ( SourceEntity . objects . filter ( resource = r ) . count ( ) , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> len ( Translation . objects . filter ( resource = r , language = l ) ) , <NUM_LIT> <EOL> ) <EOL> handler . bind_file ( '<STR_LIT>' % os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> l = Language . objects . by_code_or_alias ( '<STR_LIT>' ) <EOL> handler . set_language ( l ) <EOL> handler . parse_file ( ) <EOL> handler . save2db ( ) <EOL> self . assertEqual ( SourceEntity . objects . filter ( resource = r ) . count ( ) , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> len ( Translation . objects . filter ( resource = r , language = l ) ) , <NUM_LIT> <EOL> ) <EOL> r . delete ( ) <EOL> def test_convert_to_suggestions ( self ) : <EOL> """<STR_LIT>""" <EOL> SourceEntity . objects . filter ( resource = self . resource ) . delete ( ) <EOL> self . assertEqual ( Suggestion . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:0> ) <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . resource . source_language ) <EOL> handler . parse_file ( is_source = True ) <EOL> handler . save2db ( is_source = True ) <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . language ) <EOL> handler . parse_file ( ) <EOL> handler . save2db ( ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:8> ) <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . resource . source_language ) <EOL> handler . parse_file ( is_source = True ) <EOL> handler . save2db ( is_source = True ) <EOL> self . assertEqual ( Suggestion . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:4> ) <EOL> def test_special_characters ( self ) : <EOL> """<STR_LIT>""" <EOL> unescaped_string = "<STR_LIT>" <EOL> escaped_string = "<STR_LIT>" <EOL> SourceEntity . objects . filter ( resource = self . resource ) . delete ( ) <EOL> self . assertEqual ( Suggestion . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:0> ) <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . resource . source_language ) <EOL> handler . parse_file ( is_source = True ) <EOL> handler . save2db ( is_source = True ) <EOL> self . assertEqual ( SourceEntity . objects . filter ( <EOL> resource = self . resource ) . values ( '<STR_LIT:id>' ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:1> ) <EOL> source = SourceEntity . objects . filter ( resource = self . resource ) [ <NUM_LIT:0> ] <EOL> translation = Translation . objects . get ( source_entity = source ) <EOL> self . assertEqual ( source . string , unescaped_string ) <EOL> self . assertEqual ( translation . string , unescaped_string ) <EOL> handler . compile ( ) <EOL> self . assertTrue ( escaped_string in handler . compiled_template ) <EOL> self . assertFalse ( unescaped_string in handler . compiled_template ) <EOL> def test_unfinished_entries ( self ) : <EOL> """<STR_LIT>""" <EOL> SourceEntity . objects . filter ( resource = self . resource ) . delete ( ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:0> ) <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . resource . source_language ) <EOL> handler . parse_file ( is_source = True ) <EOL> handler . save2db ( is_source = True ) <EOL> self . assertEqual ( SourceEntity . objects . filter ( <EOL> resource = self . resource ) . values ( '<STR_LIT:id>' ) . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:2> ) <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . language ) <EOL> handler . parse_file ( ) <EOL> handler . save2db ( ) <EOL> self . assertEqual ( SourceEntity . objects . filter ( <EOL> resource = self . resource ) . values ( '<STR_LIT:id>' ) . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( Suggestion . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_obsolete_entries ( self ) : <EOL> """<STR_LIT>""" <EOL> SourceEntity . objects . filter ( resource = self . resource ) . delete ( ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:0> ) <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . resource . source_language ) <EOL> handler . parse_file ( is_source = True ) <EOL> handler . save2db ( is_source = True ) <EOL> self . assertEqual ( SourceEntity . objects . filter ( <EOL> resource = self . resource ) . values ( '<STR_LIT:id>' ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:1> ) <EOL> handler = LinguistHandler ( '<STR_LIT>' % <EOL> os . path . split ( __file__ ) [ <NUM_LIT:0> ] ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . language ) <EOL> handler . parse_file ( ) <EOL> handler . save2db ( ) <EOL> self . assertEqual ( SourceEntity . objects . filter ( <EOL> resource = self . resource ) . values ( '<STR_LIT:id>' ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Translation . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( Suggestion . objects . filter ( source_entity__in = <EOL> SourceEntity . objects . filter ( resource = self . resource ) . values ( '<STR_LIT:id>' ) ) . count ( ) , <NUM_LIT:0> ) <EOL> def test_unfinished_entries ( self ) : <EOL> testfile = os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> handler = LinguistHandler ( testfile ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . language ) <EOL> handler . parse_file ( is_source = True ) <EOL> handler . save2db ( is_source = True ) <EOL> lang = Language . objects . by_code_or_alias ( '<STR_LIT>' ) <EOL> testfile = os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> handler = LinguistHandler ( testfile ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( lang ) <EOL> handler . parse_file ( ) <EOL> handler . save2db ( ) <EOL> handler . compile ( ) <EOL> self . assertEqual ( handler . compiled_template . count ( '<STR_LIT>' ) , <NUM_LIT:2> ) <EOL> def test_entries_with_comment_tag ( self ) : <EOL> """<STR_LIT>""" <EOL> testfile = os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> handler = LinguistHandler ( testfile ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . language ) <EOL> handler . parse_file ( is_source = True ) <EOL> handler . save2db ( is_source = True ) <EOL> self . assertEqual ( SourceEntity . objects . get ( resource = self . resource , <EOL> string = '<STR_LIT>' ) . context_string , <EOL> u'<STR_LIT>' ) <EOL> self . assertEqual ( SourceEntity . objects . get ( resource = self . resource , <EOL> string = '<STR_LIT>' ) . context_string , <EOL> u'<STR_LIT>' ) <EOL> self . assertEqual ( SourceEntity . objects . get ( resource = self . resource , <EOL> string = '<STR_LIT>' ) . context_string , u'<STR_LIT>' ) <EOL> self . assertEqual ( SourceEntity . objects . get ( resource = self . resource , <EOL> string = '<STR_LIT>' ) . context_string , u'<STR_LIT:None>' ) <EOL> self . assertEqual ( SourceEntity . objects . get ( resource = self . resource , <EOL> string = '<STR_LIT>' ) . context_string , u'<STR_LIT>' ) <EOL> def test_context_generation ( self ) : <EOL> """<STR_LIT>""" <EOL> testfile = os . path . join ( <EOL> os . path . dirname ( __file__ ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> handler = LinguistHandler ( testfile ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . language ) <EOL> handler . parse_file ( is_source = True ) <EOL> handler . save2db ( is_source = True ) <EOL> handler . compile ( ) <EOL> doc = xml . dom . minidom . parseString ( handler . compiled_template ) <EOL> root = doc . documentElement <EOL> for message in doc . getElementsByTagName ( "<STR_LIT:message>" ) : <EOL> source = _getElementByTagName ( message , "<STR_LIT:source>" ) <EOL> sourceString = _getText ( source . childNodes ) <EOL> generated_context = handler . _context_of_message ( message ) <EOL> se = SourceEntity . objects . get ( <EOL> resource = self . resource , string = sourceString , <EOL> context = generated_context or u"<STR_LIT:None>" <EOL> ) <EOL> def test_source_parsing ( self ) : <EOL> """<STR_LIT>""" <EOL> file_ = os . path . join ( <EOL> os . path . dirname ( __file__ ) , "<STR_LIT>" <EOL> ) <EOL> handler = LinguistHandler ( file_ ) <EOL> handler . bind_resource ( self . resource ) <EOL> handler . set_language ( self . language ) <EOL> handler . parse_file ( is_source = True ) <EOL> strings = handler . stringset . strings <EOL> self . assertEquals ( len ( strings ) , <NUM_LIT:15> ) <EOL> for s in strings : <EOL> if s . source_entity == "<STR_LIT>" : <EOL> self . assertEquals ( <EOL> s . translation , "<STR_LIT>" <EOL> ) <EOL> elif s . source_entity == "<STR_LIT>" : <EOL> self . assertEquals ( s . translation , s . source_entity ) <EOL> elif s . source_entity == '<STR_LIT>' : <EOL> self . assertTrue ( <EOL> s . translation == '<STR_LIT>' or s . translation == '<STR_LIT>' <EOL> ) <EOL> elif s . source_entity == '<STR_LIT>' : <EOL> self . assertEquals ( s . translation , '<STR_LIT>' ) <EOL> elif s . source_entity == '<STR_LIT>' : <EOL> self . assertEquals ( <EOL> s . translation , '<STR_LIT>' <EOL> ) <EOL> elif s . source_entity == '<STR_LIT>' : <EOL> self . assertEquals ( s . translation , "<STR_LIT>" ) <EOL> elif s . source_entity == '<STR_LIT>' : <EOL> self . assertEquals ( s . translation , "<STR_LIT>" ) <EOL> elif s . source_entity == '<STR_LIT>' : <EOL> self . assertTrue ( <EOL> s . translation == '<STR_LIT>' or s . translation == '<STR_LIT>' <EOL> ) <EOL> elif s . source_entity == '<STR_LIT>' : <EOL> self . assertEquals ( s . translation , '<STR_LIT>' ) <EOL> elif s . source_entity == '<STR_LIT>' : <EOL> self . assertEquals ( s . translation , '<STR_LIT>' ) <EOL> else : <EOL> self . assertTrue ( False , "<STR_LIT>" ) </s>
