<s> from __future__ import print_function <EOL> import codecs <EOL> from optparse import make_option <EOL> import os <EOL> import re <EOL> import django <EOL> import vinaigrette <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from django . core . management . commands import makemessages as django_makemessages <EOL> from django . utils . translation import ugettext <EOL> def _get_po_paths ( locales = [ ] ) : <EOL> """<STR_LIT>""" <EOL> basedirs = [ os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) , '<STR_LIT>' ] <EOL> if os . environ . get ( '<STR_LIT>' ) : <EOL> from django . conf import settings <EOL> basedirs . extend ( settings . LOCALE_PATHS ) <EOL> basedirs = set ( map ( os . path . abspath , filter ( os . path . isdir , basedirs ) ) ) <EOL> if not basedirs : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> po_paths = [ ] <EOL> for basedir in basedirs : <EOL> for locale in locales : <EOL> basedir_locale = os . path . join ( basedir , locale , '<STR_LIT>' ) <EOL> for dirpath , dirnames , filenames in os . walk ( basedir_locale ) : <EOL> for f in filenames : <EOL> if f . endswith ( '<STR_LIT>' ) : <EOL> po_paths . append ( os . path . join ( dirpath , f ) ) <EOL> return po_paths <EOL> class Command ( django_makemessages . Command ) : <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def add_arguments ( self , parser ) : <EOL> super ( Command , self ) . add_arguments ( parser ) <EOL> parser . add_argument ( '<STR_LIT>' , default = True , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) , <EOL> parser . add_argument ( '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> requires_system_checks = True <EOL> def handle ( self , * args , ** options ) : <EOL> if not options . get ( '<STR_LIT>' ) : <EOL> return super ( Command , self ) . handle ( * args , ** options ) <EOL> verbosity = int ( options [ '<STR_LIT>' ] ) <EOL> vinfilepath = '<STR_LIT>' <EOL> sources = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> vinfile = codecs . open ( vinfilepath , '<STR_LIT:w>' , encoding = '<STR_LIT:utf8>' ) <EOL> try : <EOL> vinfile . write ( '<STR_LIT>' ) <EOL> if verbosity > <NUM_LIT:0> : <EOL> self . stdout . write ( '<STR_LIT>' ) <EOL> for model in sorted ( vinaigrette . _registry . keys ( ) , key = lambda m : m . _meta . object_name ) : <EOL> strings_seen = set ( ) <EOL> modelname = "<STR_LIT>" % ( model . _meta . app_label , model . _meta . object_name ) <EOL> reg = vinaigrette . _registry [ model ] <EOL> fields = reg [ '<STR_LIT>' ] <EOL> properties = reg [ '<STR_LIT>' ] <EOL> query_fields = set ( fields ) <EOL> if properties : <EOL> fields += properties . keys ( ) <EOL> for prop in properties . itervalues ( ) : <EOL> query_fields . update ( prop ) <EOL> manager = reg [ '<STR_LIT>' ] if reg [ '<STR_LIT>' ] else model . _default_manager <EOL> qs = manager . filter ( reg [ '<STR_LIT>' ] ) if reg [ '<STR_LIT>' ] else manager . all ( ) <EOL> for instance in qs . order_by ( '<STR_LIT>' ) . only ( '<STR_LIT>' , * query_fields ) : <EOL> try : <EOL> idnum = int ( instance . pk ) <EOL> except ( ValueError , TypeError ) : <EOL> idnum = <NUM_LIT:0> <EOL> for field in fields : <EOL> val = getattr ( instance , field ) <EOL> if val and val not in strings_seen : <EOL> strings_seen . add ( val ) <EOL> sources . append ( '<STR_LIT>' % ( modelname , field , idnum ) ) <EOL> vinfile . write ( <EOL> '<STR_LIT>' <EOL> % val . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) . replace ( '<STR_LIT:%>' , '<STR_LIT>' ) <EOL> ) <EOL> finally : <EOL> vinfile . close ( ) <EOL> try : <EOL> super ( Command , self ) . handle ( * args , ** options ) <EOL> finally : <EOL> if not options . get ( '<STR_LIT>' ) : <EOL> os . unlink ( vinfilepath ) <EOL> r_lineref = re . compile ( r'<STR_LIT>' % re . escape ( vinfilepath ) ) <EOL> def lineref_replace ( match ) : <EOL> try : <EOL> return sources [ int ( match . group ( <NUM_LIT:1> ) ) ] <EOL> except ( IndexError , ValueError ) : <EOL> return match . group ( <NUM_LIT:0> ) <EOL> if options . get ( '<STR_LIT:all>' ) : <EOL> po_paths = _get_po_paths ( ) <EOL> else : <EOL> locales = options . get ( '<STR_LIT>' ) <EOL> if hasattr ( locales , '<STR_LIT>' ) : <EOL> locales = [ locales ] <EOL> po_paths = _get_po_paths ( locales ) <EOL> if options . get ( '<STR_LIT>' ) : <EOL> obsolete_warning = [ '<STR_LIT>' % <EOL> ugettext ( '<STR_LIT>' ) . encode ( '<STR_LIT:utf8>' ) , <EOL> '<STR_LIT>' ] <EOL> for po_path in po_paths : <EOL> po_file = open ( po_path ) <EOL> new_contents = [ ] <EOL> lastline = '<STR_LIT>' <EOL> for line in po_file : <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> new_contents . append ( r_lineref . sub ( lineref_replace , line ) ) <EOL> else : <EOL> if ( line . startswith ( '<STR_LIT>' ) <EOL> and lastline . startswith ( '<STR_LIT>' ) <EOL> and vinfilepath in lastline ) : <EOL> continue <EOL> if options . get ( '<STR_LIT>' ) : <EOL> if line in obsolete_warning : <EOL> continue <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> new_contents . extend ( obsolete_warning ) <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> line = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , line ) <EOL> new_contents . append ( line ) <EOL> lastline = line <EOL> po_file . close ( ) <EOL> po_file = open ( po_path , '<STR_LIT:w>' ) <EOL> for line in new_contents : <EOL> po_file . write ( line ) <EOL> po_file . close ( ) </s>
