<s> from __future__ import absolute_import <EOL> """<STR_LIT>""" <EOL> import codecs <EOL> import os <EOL> import silme <EOL> from collections import OrderedDict <EOL> from copy import copy <EOL> from silme . format . dtd import FormatParser as DTDParser <EOL> from silme . format . ini import FormatParser as IniParser <EOL> from silme . format . inc import FormatParser as IncParser <EOL> from silme . format . properties import FormatParser as PropertiesParser <EOL> from pontoon . sync import SyncError <EOL> from pontoon . sync . formats . base import ParsedResource <EOL> from pontoon . sync . vcs . models import VCSTranslation <EOL> class SilmeEntity ( VCSTranslation ) : <EOL> def __init__ ( self , silme_object , comments = None , order = <NUM_LIT:0> , copy_string = True ) : <EOL> """<STR_LIT>""" <EOL> self . silme_object = silme_object <EOL> self . comments = comments or [ ] <EOL> self . order = order <EOL> self . last_translator = None <EOL> self . last_update = None <EOL> if copy_string : <EOL> self . strings = { None : self . silme_object . value } <EOL> else : <EOL> self . strings = { } <EOL> @ property <EOL> def key ( self ) : <EOL> return self . silme_object . id <EOL> @ property <EOL> def source_string ( self ) : <EOL> return self . silme_object . value <EOL> @ property <EOL> def source_string_plural ( self ) : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def fuzzy ( self ) : <EOL> return False <EOL> @ fuzzy . setter <EOL> def fuzzy ( self , fuzzy ) : <EOL> pass <EOL> @ property <EOL> def source ( self ) : <EOL> return [ ] <EOL> def __eq__ ( self , other ) : <EOL> return self . key == other . key and self . strings . get ( None ) == other . strings . get ( None ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . strings ) <EOL> class SilmeResource ( ParsedResource ) : <EOL> def __init__ ( self , parser , path , source_resource = None ) : <EOL> self . parser = parser <EOL> self . path = path <EOL> self . source_resource = source_resource <EOL> self . entities = OrderedDict ( ) <EOL> self . escape_quotes_on = '<STR_LIT>' in path and parser is DTDParser <EOL> if source_resource : <EOL> for key , entity in source_resource . entities . items ( ) : <EOL> self . entities [ key ] = copy_source_entity ( entity ) <EOL> try : <EOL> self . structure = parser . get_structure ( read_file ( <EOL> path , <EOL> uncomment_moz_langpack = parser is IncParser and not source_resource <EOL> ) ) <EOL> except IOError : <EOL> if source_resource : <EOL> return <EOL> else : <EOL> raise <EOL> comments = [ ] <EOL> current_order = <NUM_LIT:0> <EOL> for obj in self . structure : <EOL> if isinstance ( obj , silme . core . entity . Entity ) : <EOL> if self . escape_quotes_on : <EOL> obj . value = self . unescape_quotes ( obj . value ) <EOL> entity = SilmeEntity ( obj , comments , current_order ) <EOL> self . entities [ entity . key ] = entity <EOL> current_order += <NUM_LIT:1> <EOL> comments = [ ] <EOL> elif isinstance ( obj , silme . core . structure . Comment ) : <EOL> for comment in obj : <EOL> lines = unicode ( comment ) . strip ( ) . split ( '<STR_LIT:\n>' ) <EOL> comments += [ line . strip ( ) for line in lines ] <EOL> @ property <EOL> def translations ( self ) : <EOL> return self . entities . values ( ) <EOL> def escape_quotes ( self , value ) : <EOL> """<STR_LIT>""" <EOL> value = value . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> value = value . replace ( "<STR_LIT:'>" , '<STR_LIT>' ) <EOL> return value <EOL> def unescape_quotes ( self , value ) : <EOL> value = value . replace ( '<STR_LIT>' , '<STR_LIT:">' ) <EOL> value = value . replace ( '<STR_LIT>' , '<STR_LIT:">' ) <EOL> value = value . replace ( '<STR_LIT>' , "<STR_LIT:'>" ) <EOL> value = value . replace ( "<STR_LIT>" , "<STR_LIT:'>" ) <EOL> return value <EOL> def save ( self , locale ) : <EOL> """<STR_LIT>""" <EOL> if self . source_resource is None : <EOL> raise SyncError ( '<STR_LIT>' <EOL> . format ( self . path ) ) <EOL> new_structure = self . parser . get_structure ( read_file ( <EOL> self . source_resource . path , <EOL> uncomment_moz_langpack = self . entities . get ( '<STR_LIT>' , False ) <EOL> ) ) <EOL> entities = [ <EOL> SilmeEntity ( obj ) for obj in new_structure if isinstance ( obj , silme . core . entity . Entity ) <EOL> ] <EOL> for silme_entity in entities : <EOL> key = silme_entity . key <EOL> translated_entity = self . entities . get ( key ) <EOL> if translated_entity and None in translated_entity . strings : <EOL> translation = translated_entity . strings [ None ] <EOL> if self . escape_quotes_on : <EOL> translation = self . escape_quotes ( translation ) <EOL> new_structure . modify_entity ( key , translation ) <EOL> else : <EOL> pos = new_structure . entity_pos ( key ) <EOL> new_structure . remove_entity ( key ) <EOL> try : <EOL> line = new_structure [ pos ] <EOL> except IndexError : <EOL> continue <EOL> if type ( line ) == unicode and line . startswith ( '<STR_LIT:\n>' ) : <EOL> line = line [ len ( '<STR_LIT:\n>' ) : ] <EOL> new_structure [ pos ] = line <EOL> if len ( line ) is <NUM_LIT:0> : <EOL> new_structure . remove_element ( pos ) <EOL> if self . path . endswith ( '<STR_LIT>' ) and locale . code == '<STR_LIT>' : <EOL> new_entity = silme . core . entity . Entity ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> new_structure . add_entity ( new_entity ) <EOL> new_structure . add_string ( '<STR_LIT:\n>' ) <EOL> try : <EOL> os . makedirs ( os . path . dirname ( self . path ) ) <EOL> except OSError : <EOL> pass <EOL> with codecs . open ( self . path , '<STR_LIT:w>' , '<STR_LIT:utf-8>' ) as f : <EOL> f . write ( self . parser . dump_structure ( new_structure ) ) <EOL> def read_file ( path , uncomment_moz_langpack = False ) : <EOL> """<STR_LIT>""" <EOL> with codecs . open ( path , '<STR_LIT:r>' , '<STR_LIT:utf-8>' ) as f : <EOL> if uncomment_moz_langpack : <EOL> lines = [ ] <EOL> for line in f : <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> line = line [ <NUM_LIT:2> : ] <EOL> lines . append ( line ) <EOL> content = '<STR_LIT>' . join ( lines ) <EOL> else : <EOL> content = f . read ( ) <EOL> return content <EOL> def copy_source_entity ( entity ) : <EOL> """<STR_LIT>""" <EOL> return SilmeEntity ( <EOL> entity . silme_object , <EOL> copy ( entity . comments ) , <EOL> entity . order , <EOL> copy_string = False <EOL> ) <EOL> def parse ( parser , path , source_path = None , locale = None ) : <EOL> if source_path is not None : <EOL> source_resource = SilmeResource ( parser , source_path ) <EOL> else : <EOL> source_resource = None <EOL> return SilmeResource ( parser , path , source_resource = source_resource ) <EOL> def parse_properties ( path , source_path = None , locale = None ) : <EOL> return parse ( PropertiesParser , path , source_path ) <EOL> def parse_ini ( path , source_path = None , locale = None ) : <EOL> return parse ( IniParser , path , source_path ) <EOL> def parse_inc ( path , source_path = None , locale = None ) : <EOL> return parse ( IncParser , path , source_path ) <EOL> def parse_dtd ( path , source_path = None , locale = None ) : <EOL> return parse ( DTDParser , path , source_path ) </s>
