<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> from os import path , walk <EOL> from codecs import open <EOL> from time import time <EOL> from datetime import datetime , tzinfo , timedelta <EOL> from collections import defaultdict <EOL> from uuid import uuid4 <EOL> from six import iteritems <EOL> from sphinx . builders import Builder <EOL> from sphinx . util import split_index_msg <EOL> from sphinx . util . nodes import extract_messages , traverse_translatable_index <EOL> from sphinx . util . osutil import safe_relpath , ensuredir , canon_path <EOL> from sphinx . util . i18n import find_catalog <EOL> from sphinx . util . console import darkgreen , purple , bold <EOL> from sphinx . locale import pairindextypes <EOL> POHEADER = r"""<STR_LIT>""" [ <NUM_LIT:1> : ] <EOL> class Catalog ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . messages = [ ] <EOL> self . metadata = { } <EOL> def add ( self , msg , origin ) : <EOL> if not hasattr ( origin , '<STR_LIT>' ) : <EOL> return <EOL> if msg not in self . metadata : <EOL> self . messages . append ( msg ) <EOL> self . metadata [ msg ] = [ ] <EOL> self . metadata [ msg ] . append ( ( origin . source , origin . line , origin . uid ) ) <EOL> class MsgOrigin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , line ) : <EOL> self . source = source <EOL> self . line = line <EOL> self . uid = uuid4 ( ) . hex <EOL> class I18nBuilder ( Builder ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> versioning_method = '<STR_LIT:text>' <EOL> versioning_compare = None <EOL> def __init__ ( self , app ) : <EOL> self . versioning_compare = app . env . config . gettext_uuid <EOL> super ( I18nBuilder , self ) . __init__ ( app ) <EOL> def init ( self ) : <EOL> Builder . init ( self ) <EOL> self . catalogs = defaultdict ( Catalog ) <EOL> def get_target_uri ( self , docname , typ = None ) : <EOL> return '<STR_LIT>' <EOL> def get_outdated_docs ( self ) : <EOL> return self . env . found_docs <EOL> def prepare_writing ( self , docnames ) : <EOL> return <EOL> def compile_catalogs ( self , catalogs , message ) : <EOL> return <EOL> def write_doc ( self , docname , doctree ) : <EOL> catalog = self . catalogs [ find_catalog ( docname , <EOL> self . config . gettext_compact ) ] <EOL> for node , msg in extract_messages ( doctree ) : <EOL> catalog . add ( msg , node ) <EOL> if '<STR_LIT:index>' in self . env . config . gettext_additional_targets : <EOL> for node , entries in traverse_translatable_index ( doctree ) : <EOL> for typ , msg , tid , main , key_ in entries : <EOL> for m in split_index_msg ( typ , msg ) : <EOL> if typ == '<STR_LIT>' and m in pairindextypes . values ( ) : <EOL> continue <EOL> catalog . add ( m , node ) <EOL> timestamp = time ( ) <EOL> tzdelta = datetime . fromtimestamp ( timestamp ) - datetime . utcfromtimestamp ( timestamp ) <EOL> class LocalTimeZone ( tzinfo ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( LocalTimeZone , self ) . __init__ ( * args , ** kw ) <EOL> self . tzdelta = tzdelta <EOL> def utcoffset ( self , dt ) : <EOL> return self . tzdelta <EOL> def dst ( self , dt ) : <EOL> return timedelta ( <NUM_LIT:0> ) <EOL> ltz = LocalTimeZone ( ) <EOL> class MessageCatalogBuilder ( I18nBuilder ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> def init ( self ) : <EOL> I18nBuilder . init ( self ) <EOL> self . create_template_bridge ( ) <EOL> self . templates . init ( self ) <EOL> def _collect_templates ( self ) : <EOL> template_files = set ( ) <EOL> for template_path in self . config . templates_path : <EOL> tmpl_abs_path = path . join ( self . app . srcdir , template_path ) <EOL> for dirpath , dirs , files in walk ( tmpl_abs_path ) : <EOL> for fn in files : <EOL> if fn . endswith ( '<STR_LIT>' ) : <EOL> filename = canon_path ( path . join ( dirpath , fn ) ) <EOL> template_files . add ( filename ) <EOL> return template_files <EOL> def _extract_from_template ( self ) : <EOL> files = self . _collect_templates ( ) <EOL> self . info ( bold ( '<STR_LIT>' % self . name ) , nonl = <NUM_LIT:1> ) <EOL> self . info ( '<STR_LIT>' % len ( files ) ) <EOL> extract_translations = self . templates . environment . extract_translations <EOL> for template in self . app . status_iterator ( <EOL> files , '<STR_LIT>' , purple , len ( files ) ) : <EOL> with open ( template , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) as f : <EOL> context = f . read ( ) <EOL> for line , meth , msg in extract_translations ( context ) : <EOL> origin = MsgOrigin ( template , line ) <EOL> self . catalogs [ '<STR_LIT>' ] . add ( msg , origin ) <EOL> def build ( self , docnames , summary = None , method = '<STR_LIT>' ) : <EOL> self . _extract_from_template ( ) <EOL> I18nBuilder . build ( self , docnames , summary , method ) <EOL> def finish ( self ) : <EOL> I18nBuilder . finish ( self ) <EOL> data = dict ( <EOL> version = self . config . version , <EOL> copyright = self . config . copyright , <EOL> project = self . config . project , <EOL> ctime = datetime . fromtimestamp ( <EOL> timestamp , ltz ) . strftime ( '<STR_LIT>' ) , <EOL> ) <EOL> for textdomain , catalog in self . app . status_iterator ( <EOL> iteritems ( self . catalogs ) , "<STR_LIT>" , <EOL> darkgreen , len ( self . catalogs ) , <EOL> lambda textdomain__ : textdomain__ [ <NUM_LIT:0> ] ) : <EOL> ensuredir ( path . join ( self . outdir , path . dirname ( textdomain ) ) ) <EOL> pofn = path . join ( self . outdir , textdomain + '<STR_LIT>' ) <EOL> pofile = open ( pofn , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) <EOL> try : <EOL> pofile . write ( POHEADER % data ) <EOL> for message in catalog . messages : <EOL> positions = catalog . metadata [ message ] <EOL> if self . config . gettext_location : <EOL> pofile . write ( "<STR_LIT>" % "<STR_LIT>" . join ( <EOL> "<STR_LIT>" % ( canon_path ( <EOL> safe_relpath ( source , self . outdir ) ) , line ) <EOL> for source , line , _ in positions ) ) <EOL> if self . config . gettext_uuid : <EOL> pofile . write ( "<STR_LIT>" % "<STR_LIT>" . join ( <EOL> uid for _ , _ , uid in positions ) ) <EOL> message = message . replace ( '<STR_LIT:\\>' , r'<STR_LIT:\\>' ) . replace ( '<STR_LIT:">' , r'<STR_LIT>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> pofile . write ( '<STR_LIT>' % message ) <EOL> finally : <EOL> pofile . close ( ) </s>
