<s> """<STR_LIT>""" <EOL> from ConfigParser import RawConfigParser <EOL> from datetime import datetime <EOL> from distutils import log <EOL> from distutils . cmd import Command <EOL> from distutils . errors import DistutilsOptionError , DistutilsSetupError <EOL> from locale import getpreferredencoding <EOL> import logging <EOL> from optparse import OptionParser <EOL> import os <EOL> import shutil <EOL> from StringIO import StringIO <EOL> import sys <EOL> import tempfile <EOL> from babel import __version__ as VERSION <EOL> from babel import Locale , localedata <EOL> from babel . core import UnknownLocaleError <EOL> from babel . messages . catalog import Catalog <EOL> from babel . messages . extract import extract_from_dir , DEFAULT_KEYWORDS , DEFAULT_MAPPING <EOL> from babel . messages . mofile import write_mo <EOL> from babel . messages . pofile import read_po , write_po <EOL> from babel . util import odict , LOCALTZ <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> __docformat__ = '<STR_LIT>' <EOL> class compile_catalog ( Command ) : <EOL> """<STR_LIT>""" <EOL> description = '<STR_LIT>' <EOL> user_options = [ <EOL> ( '<STR_LIT>' , '<STR_LIT:D>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:d>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:i>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:o>' , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:l>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:f>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) <EOL> ] <EOL> boolean_options = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def initialize_options ( self ) : <EOL> self . domain = '<STR_LIT>' <EOL> self . directory = None <EOL> self . input_file = None <EOL> self . output_file = None <EOL> self . locale = None <EOL> self . use_fuzzy = False <EOL> self . statistics = False <EOL> def finalize_options ( self ) : <EOL> if not self . input_file and not self . directory : <EOL> raise DistutilsOptionError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if not self . output_file and not self . directory : <EOL> raise DistutilsOptionError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def run ( self ) : <EOL> po_files = [ ] <EOL> mo_files = [ ] <EOL> if not self . input_file : <EOL> if self . locale : <EOL> po_files . append ( ( self . locale , <EOL> os . path . join ( self . directory , self . locale , <EOL> '<STR_LIT>' , <EOL> self . domain + '<STR_LIT>' ) ) ) <EOL> mo_files . append ( os . path . join ( self . directory , self . locale , <EOL> '<STR_LIT>' , <EOL> self . domain + '<STR_LIT>' ) ) <EOL> else : <EOL> for locale in os . listdir ( self . directory ) : <EOL> po_file = os . path . join ( self . directory , locale , <EOL> '<STR_LIT>' , self . domain + '<STR_LIT>' ) <EOL> if os . path . exists ( po_file ) : <EOL> po_files . append ( ( locale , po_file ) ) <EOL> mo_files . append ( os . path . join ( self . directory , locale , <EOL> '<STR_LIT>' , <EOL> self . domain + '<STR_LIT>' ) ) <EOL> else : <EOL> po_files . append ( ( self . locale , self . input_file ) ) <EOL> if self . output_file : <EOL> mo_files . append ( self . output_file ) <EOL> else : <EOL> mo_files . append ( os . path . join ( self . directory , self . locale , <EOL> '<STR_LIT>' , <EOL> self . domain + '<STR_LIT>' ) ) <EOL> if not po_files : <EOL> raise DistutilsOptionError ( '<STR_LIT>' ) <EOL> for idx , ( locale , po_file ) in enumerate ( po_files ) : <EOL> mo_file = mo_files [ idx ] <EOL> infile = open ( po_file , '<STR_LIT:r>' ) <EOL> try : <EOL> catalog = read_po ( infile , locale ) <EOL> finally : <EOL> infile . close ( ) <EOL> if self . statistics : <EOL> translated = <NUM_LIT:0> <EOL> for message in list ( catalog ) [ <NUM_LIT:1> : ] : <EOL> if message . string : <EOL> translated += <NUM_LIT:1> <EOL> percentage = <NUM_LIT:0> <EOL> if len ( catalog ) : <EOL> percentage = translated * <NUM_LIT:100> // len ( catalog ) <EOL> log . info ( '<STR_LIT>' , <EOL> translated , len ( catalog ) , percentage , po_file ) <EOL> if catalog . fuzzy and not self . use_fuzzy : <EOL> log . warn ( '<STR_LIT>' , po_file ) <EOL> continue <EOL> for message , errors in catalog . check ( ) : <EOL> for error in errors : <EOL> log . error ( '<STR_LIT>' , po_file , message . lineno , <EOL> error ) <EOL> log . info ( '<STR_LIT>' , po_file , mo_file ) <EOL> outfile = open ( mo_file , '<STR_LIT:wb>' ) <EOL> try : <EOL> write_mo ( outfile , catalog , use_fuzzy = self . use_fuzzy ) <EOL> finally : <EOL> outfile . close ( ) <EOL> class extract_messages ( Command ) : <EOL> """<STR_LIT>""" <EOL> description = '<STR_LIT>' <EOL> user_options = [ <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:k>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:F>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:o>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:w>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:c>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> boolean_options = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> def initialize_options ( self ) : <EOL> self . charset = '<STR_LIT:utf-8>' <EOL> self . keywords = '<STR_LIT>' <EOL> self . _keywords = DEFAULT_KEYWORDS . copy ( ) <EOL> self . no_default_keywords = False <EOL> self . mapping_file = None <EOL> self . no_location = False <EOL> self . omit_header = False <EOL> self . output_file = None <EOL> self . input_dirs = None <EOL> self . width = None <EOL> self . no_wrap = False <EOL> self . sort_output = False <EOL> self . sort_by_file = False <EOL> self . msgid_bugs_address = None <EOL> self . copyright_holder = None <EOL> self . add_comments = None <EOL> self . _add_comments = [ ] <EOL> self . strip_comments = False <EOL> def finalize_options ( self ) : <EOL> if self . no_default_keywords and not self . keywords : <EOL> raise DistutilsOptionError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if self . no_default_keywords : <EOL> self . _keywords = { } <EOL> if self . keywords : <EOL> self . _keywords . update ( parse_keywords ( self . keywords . split ( ) ) ) <EOL> if not self . output_file : <EOL> raise DistutilsOptionError ( '<STR_LIT>' ) <EOL> if self . no_wrap and self . width : <EOL> raise DistutilsOptionError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not self . no_wrap and not self . width : <EOL> self . width = <NUM_LIT> <EOL> elif self . width is not None : <EOL> self . width = int ( self . width ) <EOL> if self . sort_output and self . sort_by_file : <EOL> raise DistutilsOptionError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not self . input_dirs : <EOL> self . input_dirs = dict . fromkeys ( [ k . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> for k in self . distribution . packages <EOL> ] ) . keys ( ) <EOL> if self . add_comments : <EOL> self . _add_comments = self . add_comments . split ( '<STR_LIT:U+002C>' ) <EOL> def run ( self ) : <EOL> mappings = self . _get_mappings ( ) <EOL> outfile = open ( self . output_file , '<STR_LIT:w>' ) <EOL> try : <EOL> catalog = Catalog ( project = self . distribution . get_name ( ) , <EOL> version = self . distribution . get_version ( ) , <EOL> msgid_bugs_address = self . msgid_bugs_address , <EOL> copyright_holder = self . copyright_holder , <EOL> charset = self . charset ) <EOL> for dirname , ( method_map , options_map ) in mappings . items ( ) : <EOL> def callback ( filename , method , options ) : <EOL> if method == '<STR_LIT:ignore>' : <EOL> return <EOL> filepath = os . path . normpath ( os . path . join ( dirname , filename ) ) <EOL> optstr = '<STR_LIT>' <EOL> if options : <EOL> optstr = '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( k , v ) for <EOL> k , v in options . items ( ) ] ) <EOL> log . info ( '<STR_LIT>' , filepath , optstr ) <EOL> extracted = extract_from_dir ( dirname , method_map , options_map , <EOL> keywords = self . _keywords , <EOL> comment_tags = self . _add_comments , <EOL> callback = callback , <EOL> strip_comment_tags = <EOL> self . strip_comments ) <EOL> for filename , lineno , message , comments in extracted : <EOL> filepath = os . path . normpath ( os . path . join ( dirname , filename ) ) <EOL> catalog . add ( message , None , [ ( filepath , lineno ) ] , <EOL> auto_comments = comments ) <EOL> log . info ( '<STR_LIT>' % self . output_file ) <EOL> write_po ( outfile , catalog , width = self . width , <EOL> no_location = self . no_location , <EOL> omit_header = self . omit_header , <EOL> sort_output = self . sort_output , <EOL> sort_by_file = self . sort_by_file ) <EOL> finally : <EOL> outfile . close ( ) <EOL> def _get_mappings ( self ) : <EOL> mappings = { } <EOL> if self . mapping_file : <EOL> fileobj = open ( self . mapping_file , '<STR_LIT>' ) <EOL> try : <EOL> method_map , options_map = parse_mapping ( fileobj ) <EOL> for dirname in self . input_dirs : <EOL> mappings [ dirname ] = method_map , options_map <EOL> finally : <EOL> fileobj . close ( ) <EOL> elif getattr ( self . distribution , '<STR_LIT>' , None ) : <EOL> message_extractors = self . distribution . message_extractors <EOL> for dirname , mapping in message_extractors . items ( ) : <EOL> if isinstance ( mapping , basestring ) : <EOL> method_map , options_map = parse_mapping ( StringIO ( mapping ) ) <EOL> else : <EOL> method_map , options_map = [ ] , { } <EOL> for pattern , method , options in mapping : <EOL> method_map . append ( ( pattern , method ) ) <EOL> options_map [ pattern ] = options or { } <EOL> mappings [ dirname ] = method_map , options_map <EOL> else : <EOL> for dirname in self . input_dirs : <EOL> mappings [ dirname ] = DEFAULT_MAPPING , { } <EOL> return mappings <EOL> def check_message_extractors ( dist , name , value ) : <EOL> """<STR_LIT>""" <EOL> assert name == '<STR_LIT>' <EOL> if not isinstance ( value , dict ) : <EOL> raise DistutilsSetupError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class init_catalog ( Command ) : <EOL> """<STR_LIT>""" <EOL> description = '<STR_LIT>' <EOL> user_options = [ <EOL> ( '<STR_LIT>' , '<STR_LIT:D>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:i>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:d>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:o>' , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:l>' , <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> def initialize_options ( self ) : <EOL> self . output_dir = None <EOL> self . output_file = None <EOL> self . input_file = None <EOL> self . locale = None <EOL> self . domain = '<STR_LIT>' <EOL> def finalize_options ( self ) : <EOL> if not self . input_file : <EOL> raise DistutilsOptionError ( '<STR_LIT>' ) <EOL> if not self . locale : <EOL> raise DistutilsOptionError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> self . _locale = Locale . parse ( self . locale ) <EOL> except UnknownLocaleError , e : <EOL> raise DistutilsOptionError ( e ) <EOL> if not self . output_file and not self . output_dir : <EOL> raise DistutilsOptionError ( '<STR_LIT>' ) <EOL> if not self . output_file : <EOL> self . output_file = os . path . join ( self . output_dir , self . locale , <EOL> '<STR_LIT>' , self . domain + '<STR_LIT>' ) <EOL> if not os . path . exists ( os . path . dirname ( self . output_file ) ) : <EOL> os . makedirs ( os . path . dirname ( self . output_file ) ) <EOL> def run ( self ) : <EOL> log . info ( '<STR_LIT>' , self . output_file , <EOL> self . input_file ) <EOL> infile = open ( self . input_file , '<STR_LIT:r>' ) <EOL> try : <EOL> catalog = read_po ( infile , locale = self . locale ) <EOL> finally : <EOL> infile . close ( ) <EOL> catalog . locale = self . _locale <EOL> catalog . fuzzy = False <EOL> outfile = open ( self . output_file , '<STR_LIT:w>' ) <EOL> try : <EOL> write_po ( outfile , catalog ) <EOL> finally : <EOL> outfile . close ( ) <EOL> class update_catalog ( Command ) : <EOL> """<STR_LIT>""" <EOL> description = '<STR_LIT>' <EOL> user_options = [ <EOL> ( '<STR_LIT>' , '<STR_LIT:D>' , <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:i>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:d>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:o>' , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:l>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:N>' , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None , <EOL> '<STR_LIT>' ) <EOL> ] <EOL> boolean_options = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def initialize_options ( self ) : <EOL> self . domain = '<STR_LIT>' <EOL> self . input_file = None <EOL> self . output_dir = None <EOL> self . output_file = None <EOL> self . locale = None <EOL> self . ignore_obsolete = False <EOL> self . no_fuzzy_matching = False <EOL> self . previous = False <EOL> def finalize_options ( self ) : <EOL> if not self . input_file : <EOL> raise DistutilsOptionError ( '<STR_LIT>' ) <EOL> if not self . output_file and not self . output_dir : <EOL> raise DistutilsOptionError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if self . output_file and not self . locale : <EOL> raise DistutilsOptionError ( '<STR_LIT>' ) <EOL> if self . no_fuzzy_matching and self . previous : <EOL> self . previous = False <EOL> def run ( self ) : <EOL> po_files = [ ] <EOL> if not self . output_file : <EOL> if self . locale : <EOL> po_files . append ( ( self . locale , <EOL> os . path . join ( self . output_dir , self . locale , <EOL> '<STR_LIT>' , <EOL> self . domain + '<STR_LIT>' ) ) ) <EOL> else : <EOL> for locale in os . listdir ( self . output_dir ) : <EOL> po_file = os . path . join ( self . output_dir , locale , <EOL> '<STR_LIT>' , <EOL> self . domain + '<STR_LIT>' ) <EOL> if os . path . exists ( po_file ) : <EOL> po_files . append ( ( locale , po_file ) ) <EOL> else : <EOL> po_files . append ( ( self . locale , self . output_file ) ) <EOL> domain = self . domain <EOL> if not domain : <EOL> domain = os . path . splitext ( os . path . basename ( self . input_file ) ) [ <NUM_LIT:0> ] <EOL> infile = open ( self . input_file , '<STR_LIT>' ) <EOL> try : <EOL> template = read_po ( infile ) <EOL> finally : <EOL> infile . close ( ) <EOL> if not po_files : <EOL> raise DistutilsOptionError ( '<STR_LIT>' ) <EOL> for locale , filename in po_files : <EOL> log . info ( '<STR_LIT>' , filename , <EOL> self . input_file ) <EOL> infile = open ( filename , '<STR_LIT>' ) <EOL> try : <EOL> catalog = read_po ( infile , locale = locale , domain = domain ) <EOL> finally : <EOL> infile . close ( ) <EOL> catalog . update ( template , self . no_fuzzy_matching ) <EOL> tmpname = os . path . join ( os . path . dirname ( filename ) , <EOL> tempfile . gettempprefix ( ) + <EOL> os . path . basename ( filename ) ) <EOL> tmpfile = open ( tmpname , '<STR_LIT:w>' ) <EOL> try : <EOL> try : <EOL> write_po ( tmpfile , catalog , <EOL> ignore_obsolete = self . ignore_obsolete , <EOL> include_previous = self . previous ) <EOL> finally : <EOL> tmpfile . close ( ) <EOL> except : <EOL> os . remove ( tmpname ) <EOL> raise <EOL> try : <EOL> os . rename ( tmpname , filename ) <EOL> except OSError : <EOL> os . remove ( filename ) <EOL> shutil . copy ( tmpname , filename ) <EOL> os . remove ( tmpname ) <EOL> class CommandLineInterface ( object ) : <EOL> """<STR_LIT>""" <EOL> usage = '<STR_LIT>' <EOL> version = '<STR_LIT>' % VERSION <EOL> commands = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def run ( self , argv = sys . argv ) : <EOL> """<STR_LIT>""" <EOL> self . parser = OptionParser ( usage = self . usage % ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> version = self . version ) <EOL> self . parser . disable_interspersed_args ( ) <EOL> self . parser . print_help = self . _help <EOL> self . parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> self . parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , const = logging . DEBUG , <EOL> help = '<STR_LIT>' ) <EOL> self . parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , const = logging . ERROR , <EOL> help = '<STR_LIT>' ) <EOL> self . parser . set_defaults ( list_locales = False , loglevel = logging . INFO ) <EOL> options , args = self . parser . parse_args ( argv [ <NUM_LIT:1> : ] ) <EOL> self . _configure_logging ( options . loglevel ) <EOL> if options . list_locales : <EOL> identifiers = localedata . locale_identifiers ( ) <EOL> longest = max ( [ len ( identifier ) for identifier in identifiers ] ) <EOL> identifiers . sort ( ) <EOL> format = u'<STR_LIT>' % ( longest + <NUM_LIT:1> ) <EOL> for identifier in identifiers : <EOL> locale = Locale . parse ( identifier ) <EOL> output = format % ( identifier , locale . english_name ) <EOL> print output . encode ( sys . stdout . encoding or <EOL> getpreferredencoding ( ) or <EOL> '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> return <NUM_LIT:0> <EOL> if not args : <EOL> self . parser . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> cmdname = args [ <NUM_LIT:0> ] <EOL> if cmdname not in self . commands : <EOL> self . parser . error ( '<STR_LIT>' % cmdname ) <EOL> return getattr ( self , cmdname ) ( args [ <NUM_LIT:1> : ] ) <EOL> def _configure_logging ( self , loglevel ) : <EOL> self . log = logging . getLogger ( '<STR_LIT>' ) <EOL> self . log . setLevel ( loglevel ) <EOL> if self . log . handlers : <EOL> handler = self . log . handlers [ <NUM_LIT:0> ] <EOL> else : <EOL> handler = logging . StreamHandler ( ) <EOL> self . log . addHandler ( handler ) <EOL> handler . setLevel ( loglevel ) <EOL> formatter = logging . Formatter ( '<STR_LIT>' ) <EOL> handler . setFormatter ( formatter ) <EOL> def _help ( self ) : <EOL> print self . parser . format_help ( ) <EOL> print "<STR_LIT>" <EOL> longest = max ( [ len ( command ) for command in self . commands ] ) <EOL> format = "<STR_LIT>" % max ( <NUM_LIT:8> , longest + <NUM_LIT:1> ) <EOL> commands = self . commands . items ( ) <EOL> commands . sort ( ) <EOL> for name , description in commands : <EOL> print format % ( name , description ) <EOL> def compile ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> parser = OptionParser ( usage = self . usage % ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> description = self . commands [ '<STR_LIT>' ] ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . set_defaults ( domain = '<STR_LIT>' , use_fuzzy = False , <EOL> compile_all = False , statistics = False ) <EOL> options , args = parser . parse_args ( argv ) <EOL> po_files = [ ] <EOL> mo_files = [ ] <EOL> if not options . input_file : <EOL> if not options . directory : <EOL> parser . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if options . locale : <EOL> po_files . append ( ( options . locale , <EOL> os . path . join ( options . directory , <EOL> options . locale , '<STR_LIT>' , <EOL> options . domain + '<STR_LIT>' ) ) ) <EOL> mo_files . append ( os . path . join ( options . directory , options . locale , <EOL> '<STR_LIT>' , <EOL> options . domain + '<STR_LIT>' ) ) <EOL> else : <EOL> for locale in os . listdir ( options . directory ) : <EOL> po_file = os . path . join ( options . directory , locale , <EOL> '<STR_LIT>' , options . domain + '<STR_LIT>' ) <EOL> if os . path . exists ( po_file ) : <EOL> po_files . append ( ( locale , po_file ) ) <EOL> mo_files . append ( os . path . join ( options . directory , locale , <EOL> '<STR_LIT>' , <EOL> options . domain + '<STR_LIT>' ) ) <EOL> else : <EOL> po_files . append ( ( options . locale , options . input_file ) ) <EOL> if options . output_file : <EOL> mo_files . append ( options . output_file ) <EOL> else : <EOL> if not options . directory : <EOL> parser . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> mo_files . append ( os . path . join ( options . directory , options . locale , <EOL> '<STR_LIT>' , <EOL> options . domain + '<STR_LIT>' ) ) <EOL> if not po_files : <EOL> parser . error ( '<STR_LIT>' ) <EOL> for idx , ( locale , po_file ) in enumerate ( po_files ) : <EOL> mo_file = mo_files [ idx ] <EOL> infile = open ( po_file , '<STR_LIT:r>' ) <EOL> try : <EOL> catalog = read_po ( infile , locale ) <EOL> finally : <EOL> infile . close ( ) <EOL> if options . statistics : <EOL> translated = <NUM_LIT:0> <EOL> for message in list ( catalog ) [ <NUM_LIT:1> : ] : <EOL> if message . string : <EOL> translated += <NUM_LIT:1> <EOL> percentage = <NUM_LIT:0> <EOL> if len ( catalog ) : <EOL> percentage = translated * <NUM_LIT:100> // len ( catalog ) <EOL> self . log . info ( "<STR_LIT>" , <EOL> translated , len ( catalog ) , percentage , po_file ) <EOL> if catalog . fuzzy and not options . use_fuzzy : <EOL> self . log . warn ( '<STR_LIT>' , <EOL> po_file ) <EOL> continue <EOL> for message , errors in catalog . check ( ) : <EOL> for error in errors : <EOL> self . log . error ( '<STR_LIT>' , po_file , message . lineno , <EOL> error ) <EOL> self . log . info ( '<STR_LIT>' , po_file , mo_file ) <EOL> outfile = open ( mo_file , '<STR_LIT:wb>' ) <EOL> try : <EOL> write_mo ( outfile , catalog , use_fuzzy = options . use_fuzzy ) <EOL> finally : <EOL> outfile . close ( ) <EOL> def extract ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> parser = OptionParser ( usage = self . usage % ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> description = self . commands [ '<STR_LIT>' ] ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:width>' , type = '<STR_LIT:int>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT:version>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT:-c>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . set_defaults ( charset = '<STR_LIT:utf-8>' , keywords = [ ] , <EOL> no_default_keywords = False , no_location = False , <EOL> omit_header = False , width = None , no_wrap = False , <EOL> sort_output = False , sort_by_file = False , <EOL> comment_tags = [ ] , strip_comment_tags = False ) <EOL> options , args = parser . parse_args ( argv ) <EOL> if not args : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if options . output not in ( None , '<STR_LIT:->' ) : <EOL> outfile = open ( options . output , '<STR_LIT:w>' ) <EOL> else : <EOL> outfile = sys . stdout <EOL> keywords = DEFAULT_KEYWORDS . copy ( ) <EOL> if options . no_default_keywords : <EOL> if not options . keywords : <EOL> parser . error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> keywords = { } <EOL> if options . keywords : <EOL> keywords . update ( parse_keywords ( options . keywords ) ) <EOL> if options . mapping_file : <EOL> fileobj = open ( options . mapping_file , '<STR_LIT>' ) <EOL> try : <EOL> method_map , options_map = parse_mapping ( fileobj ) <EOL> finally : <EOL> fileobj . close ( ) <EOL> else : <EOL> method_map = DEFAULT_MAPPING <EOL> options_map = { } <EOL> if options . width and options . no_wrap : <EOL> parser . error ( "<STR_LIT>" ) <EOL> elif not options . width and not options . no_wrap : <EOL> options . width = <NUM_LIT> <EOL> if options . sort_output and options . sort_by_file : <EOL> parser . error ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> catalog = Catalog ( project = options . project , <EOL> version = options . version , <EOL> msgid_bugs_address = options . msgid_bugs_address , <EOL> copyright_holder = options . copyright_holder , <EOL> charset = options . charset ) <EOL> for dirname in args : <EOL> if not os . path . isdir ( dirname ) : <EOL> parser . error ( '<STR_LIT>' % dirname ) <EOL> def callback ( filename , method , options ) : <EOL> if method == '<STR_LIT:ignore>' : <EOL> return <EOL> filepath = os . path . normpath ( os . path . join ( dirname , filename ) ) <EOL> optstr = '<STR_LIT>' <EOL> if options : <EOL> optstr = '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( k , v ) for <EOL> k , v in options . items ( ) ] ) <EOL> self . log . info ( '<STR_LIT>' , filepath , <EOL> optstr ) <EOL> extracted = extract_from_dir ( dirname , method_map , options_map , <EOL> keywords , options . comment_tags , <EOL> callback = callback , <EOL> strip_comment_tags = <EOL> options . strip_comment_tags ) <EOL> for filename , lineno , message , comments in extracted : <EOL> filepath = os . path . normpath ( os . path . join ( dirname , filename ) ) <EOL> catalog . add ( message , None , [ ( filepath , lineno ) ] , <EOL> auto_comments = comments ) <EOL> if options . output not in ( None , '<STR_LIT:->' ) : <EOL> self . log . info ( '<STR_LIT>' % options . output ) <EOL> write_po ( outfile , catalog , width = options . width , <EOL> no_location = options . no_location , <EOL> omit_header = options . omit_header , <EOL> sort_output = options . sort_output , <EOL> sort_by_file = options . sort_by_file ) <EOL> finally : <EOL> if options . output : <EOL> outfile . close ( ) <EOL> def init ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> parser = OptionParser ( usage = self . usage % ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> description = self . commands [ '<STR_LIT>' ] ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . set_defaults ( domain = '<STR_LIT>' ) <EOL> options , args = parser . parse_args ( argv ) <EOL> if not options . locale : <EOL> parser . error ( '<STR_LIT>' ) <EOL> try : <EOL> locale = Locale . parse ( options . locale ) <EOL> except UnknownLocaleError , e : <EOL> parser . error ( e ) <EOL> if not options . input_file : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if not options . output_file and not options . output_dir : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if not options . output_file : <EOL> options . output_file = os . path . join ( options . output_dir , <EOL> options . locale , '<STR_LIT>' , <EOL> options . domain + '<STR_LIT>' ) <EOL> if not os . path . exists ( os . path . dirname ( options . output_file ) ) : <EOL> os . makedirs ( os . path . dirname ( options . output_file ) ) <EOL> infile = open ( options . input_file , '<STR_LIT:r>' ) <EOL> try : <EOL> catalog = read_po ( infile , locale = options . locale ) <EOL> finally : <EOL> infile . close ( ) <EOL> catalog . locale = locale <EOL> catalog . revision_date = datetime . now ( LOCALTZ ) <EOL> self . log . info ( '<STR_LIT>' , options . output_file , <EOL> options . input_file ) <EOL> outfile = open ( options . output_file , '<STR_LIT:w>' ) <EOL> try : <EOL> write_po ( outfile , catalog ) <EOL> finally : <EOL> outfile . close ( ) <EOL> def update ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> parser = OptionParser ( usage = self . usage % ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> description = self . commands [ '<STR_LIT>' ] ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . set_defaults ( domain = '<STR_LIT>' , ignore_obsolete = False , <EOL> no_fuzzy_matching = False , previous = False ) <EOL> options , args = parser . parse_args ( argv ) <EOL> if not options . input_file : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if not options . output_file and not options . output_dir : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if options . output_file and not options . locale : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if options . no_fuzzy_matching and options . previous : <EOL> options . previous = False <EOL> po_files = [ ] <EOL> if not options . output_file : <EOL> if options . locale : <EOL> po_files . append ( ( options . locale , <EOL> os . path . join ( options . output_dir , <EOL> options . locale , '<STR_LIT>' , <EOL> options . domain + '<STR_LIT>' ) ) ) <EOL> else : <EOL> for locale in os . listdir ( options . output_dir ) : <EOL> po_file = os . path . join ( options . output_dir , locale , <EOL> '<STR_LIT>' , <EOL> options . domain + '<STR_LIT>' ) <EOL> if os . path . exists ( po_file ) : <EOL> po_files . append ( ( locale , po_file ) ) <EOL> else : <EOL> po_files . append ( ( options . locale , options . output_file ) ) <EOL> domain = options . domain <EOL> if not domain : <EOL> domain = os . path . splitext ( os . path . basename ( options . input_file ) ) [ <NUM_LIT:0> ] <EOL> infile = open ( options . input_file , '<STR_LIT>' ) <EOL> try : <EOL> template = read_po ( infile ) <EOL> finally : <EOL> infile . close ( ) <EOL> if not po_files : <EOL> parser . error ( '<STR_LIT>' ) <EOL> for locale , filename in po_files : <EOL> self . log . info ( '<STR_LIT>' , filename , <EOL> options . input_file ) <EOL> infile = open ( filename , '<STR_LIT>' ) <EOL> try : <EOL> catalog = read_po ( infile , locale = locale , domain = domain ) <EOL> finally : <EOL> infile . close ( ) <EOL> catalog . update ( template , options . no_fuzzy_matching ) <EOL> tmpname = os . path . join ( os . path . dirname ( filename ) , <EOL> tempfile . gettempprefix ( ) + <EOL> os . path . basename ( filename ) ) <EOL> tmpfile = open ( tmpname , '<STR_LIT:w>' ) <EOL> try : <EOL> try : <EOL> write_po ( tmpfile , catalog , <EOL> ignore_obsolete = options . ignore_obsolete , <EOL> include_previous = options . previous ) <EOL> finally : <EOL> tmpfile . close ( ) <EOL> except : <EOL> os . remove ( tmpname ) <EOL> raise <EOL> try : <EOL> os . rename ( tmpname , filename ) <EOL> except OSError : <EOL> os . remove ( filename ) <EOL> shutil . copy ( tmpname , filename ) <EOL> os . remove ( tmpname ) <EOL> def main ( ) : <EOL> return CommandLineInterface ( ) . run ( sys . argv ) <EOL> def parse_mapping ( fileobj , filename = None ) : <EOL> """<STR_LIT>""" <EOL> extractors = { } <EOL> method_map = [ ] <EOL> options_map = { } <EOL> parser = RawConfigParser ( ) <EOL> parser . _sections = odict ( parser . _sections ) <EOL> parser . readfp ( fileobj , filename ) <EOL> for section in parser . sections ( ) : <EOL> if section == '<STR_LIT>' : <EOL> extractors = dict ( parser . items ( section ) ) <EOL> else : <EOL> method , pattern = [ part . strip ( ) for part in section . split ( '<STR_LIT::>' , <NUM_LIT:1> ) ] <EOL> method_map . append ( ( pattern , method ) ) <EOL> options_map [ pattern ] = dict ( parser . items ( section ) ) <EOL> if extractors : <EOL> for idx , ( pattern , method ) in enumerate ( method_map ) : <EOL> if method in extractors : <EOL> method = extractors [ method ] <EOL> method_map [ idx ] = ( pattern , method ) <EOL> return ( method_map , options_map ) <EOL> def parse_keywords ( strings = [ ] ) : <EOL> """<STR_LIT>""" <EOL> keywords = { } <EOL> for string in strings : <EOL> if '<STR_LIT::>' in string : <EOL> funcname , indices = string . split ( '<STR_LIT::>' ) <EOL> else : <EOL> funcname , indices = string , None <EOL> if funcname not in keywords : <EOL> if indices : <EOL> indices = tuple ( [ ( int ( x ) ) for x in indices . split ( '<STR_LIT:U+002C>' ) ] ) <EOL> keywords [ funcname ] = indices <EOL> return keywords <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
