Commits

Georg Brandl  committed 09f635e

Move the initialization of env and i18n into the app.

  • Participants
  • Parent commits f4a3d92

Comments (0)

Files changed (4)

File sphinx/application.py

 from docutils.parsers.rst import directives, roles
 
 import sphinx
+from sphinx import package_dir, locale
 from sphinx.roles import XRefRole
 from sphinx.config import Config
 from sphinx.errors import SphinxError, SphinxWarning, ExtensionError
 from sphinx.domains import all_domains
 from sphinx.builders import BUILTIN_BUILDERS
 from sphinx.directives import GenericDesc, Target, additional_xref_types
-from sphinx.environment import SphinxStandaloneReader
+from sphinx.environment import BuildEnvironment, SphinxStandaloneReader
 from sphinx.util import pycompat
 from sphinx.util.tags import Tags
 from sphinx.util.compat import Directive, directive_dwim
 }
 
 CONFIG_FILENAME = 'conf.py'
+ENV_PICKLE_FILENAME = 'environment.pickle'
 
 
 class Sphinx(object):
         self._listeners = {}
         self.builderclasses = BUILTIN_BUILDERS.copy()
         self.builder = None
+        self.env = None
 
         self.srcdir = srcdir
         self.confdir = confdir
         # intialize domains
         self.domains = {}
         for domain in all_domains.keys():
-            self.domains[domain] = all_domains[domain](self)
+            self.domains[domain] = all_domains[domain]()
 
+        # set up translation infrastructure
+        self._init_i18n()
+        # set up the build environment
+        self._init_env(freshenv)
+        # set up the builder
+        self._init_builder(buildername)
+
+    def _init_i18n(self):
+        """
+        Load translated strings from the configured localedirs if
+        enabled in the configuration.
+        """
+        if self.config.language is not None:
+            self.info(bold('loading translations [%s]... ' %
+                           self.config.language), nonl=True)
+            locale_dirs = [None, path.join(package_dir, 'locale')] + \
+                [path.join(self.srcdir, x) for x in self.config.locale_dirs]
+        else:
+            locale_dirs = []
+        self.translator, has_translation = locale.init(locale_dirs,
+                                                       self.config.language)
+        if self.config.language is not None:
+            if has_translation:
+                self.info('done')
+            else:
+                self.info('locale not available')
+
+    def _init_env(self, freshenv):
+        if freshenv:
+            self.env = BuildEnvironment(self.srcdir, self.doctreedir,
+                                        self.config)
+            self.env.find_files(self.config)
+        else:
+            try:
+                self.info(bold('loading pickled environment... '), nonl=True)
+                self.env = BuildEnvironment.frompickle(self.config,
+                    path.join(self.doctreedir, ENV_PICKLE_FILENAME))
+                self.info('done')
+            except Exception, err:
+                if type(err) is IOError and err.errno == 2:
+                    self.info('not yet created')
+                else:
+                    self.info('failed: %s' % err)
+                self.env = BuildEnvironment(self.srcdir, self.doctreedir,
+                                            self.config)
+                self.env.find_files(self.config)
+        self.env.set_warnfunc(self.warn)
+
+    def _init_builder(self, buildername):
         if buildername is None:
-            print >>status, 'No builder selected, using default: html'
+            print >>self._status, 'No builder selected, using default: html'
             buildername = 'html'
         if buildername not in self.builderclasses:
             raise SphinxError('Builder name %s not registered' % buildername)
             mod, cls = builderclass
             builderclass = getattr(
                 __import__('sphinx.builders.' + mod, None, None, [cls]), cls)
-        self.builder = builderclass(self, freshenv=freshenv)
-        self.builder.tags = self.tags
-        self.builder.tags.add(self.builder.format)
+        self.builder = builderclass(self)
         self.emit('builder-inited')
 
     def build(self, all_files, filenames):

File sphinx/builders/__init__.py

 
 from docutils import nodes
 
-from sphinx import package_dir, locale
 from sphinx.util import SEP, relative_uri
-from sphinx.environment import BuildEnvironment
 from sphinx.util.console import bold, purple, darkgreen, term_width_line
 
 # side effect: registers roles and directives
 from sphinx import directives
 
 
-ENV_PICKLE_FILENAME = 'environment.pickle'
-
-
 class Builder(object):
     """
     Builds target formats from the reST sources.
     # builder's output format, or '' if no document output is produced
     format = ''
 
-    def __init__(self, app, env=None, freshenv=False):
+    def __init__(self, app):
+        self.env = app.env
         self.srcdir = app.srcdir
         self.confdir = app.confdir
         self.outdir = app.outdir
         self.warn = app.warn
         self.info = app.info
         self.config = app.config
-
-        self.load_i18n()
+        self.tags = app.tags
+        self.tags.add(self.format)
 
         # images that need to be copied over (source -> dest)
         self.images = {}
 
-        # if None, this is set in load_env()
-        self.env = env
-        self.freshenv = freshenv
-
         self.init()
-        self.load_env()
 
     # helper methods
 
 
     # build methods
 
-    def load_i18n(self):
-        """
-        Load translated strings from the configured localedirs if
-        enabled in the configuration.
-        """
-        if self.config.language is not None:
-            self.info(bold('loading translations [%s]... ' %
-                           self.config.language), nonl=True)
-            locale_dirs = [None, path.join(package_dir, 'locale')] + \
-                [path.join(self.srcdir, x) for x in self.config.locale_dirs]
-        else:
-            locale_dirs = []
-        self.translator, has_translation = locale.init(locale_dirs,
-                                                       self.config.language)
-        if self.config.language is not None:
-            if has_translation:
-                self.info('done')
-            else:
-                self.info('locale not available')
-
-    def load_env(self):
-        """Set up the build environment."""
-        if self.env:
-            return
-        if not self.freshenv:
-            try:
-                self.info(bold('loading pickled environment... '), nonl=True)
-                self.env = BuildEnvironment.frompickle(self.config,
-                    path.join(self.doctreedir, ENV_PICKLE_FILENAME))
-                self.info('done')
-            except Exception, err:
-                if type(err) is IOError and err.errno == 2:
-                    self.info('not found')
-                else:
-                    self.info('failed: %s' % err)
-                self.env = BuildEnvironment(self.srcdir, self.doctreedir,
-                                            self.config)
-                self.env.find_files(self.config)
-        else:
-            self.env = BuildEnvironment(self.srcdir, self.doctreedir,
-                                        self.config)
-            self.env.find_files(self.config)
-        self.env.set_warnfunc(self.warn)
-
     def build_all(self):
         """Build all source files."""
         self.build(None, summary='all source files', method='all')
 
         if updated_docnames:
             # save the environment
+            from sphinx.application import ENV_PICKLE_FILENAME
             self.info(bold('pickling environment... '), nonl=True)
             self.env.topickle(path.join(self.doctreedir, ENV_PICKLE_FILENAME))
             self.info('done')

File sphinx/builders/html.py

 from sphinx.errors import SphinxError
 from sphinx.search import js_index
 from sphinx.theming import Theme
-from sphinx.builders import Builder, ENV_PICKLE_FILENAME
+from sphinx.builders import Builder
+from sphinx.application import ENV_PICKLE_FILENAME
 from sphinx.highlighting import PygmentsBridge
 from sphinx.util.console import bold
 from sphinx.writers.html import HTMLWriter, HTMLTranslator, \

File sphinx/jinja2glue.py

         # make the paths into loaders
         self.loaders = map(SphinxFileSystemLoader, chain)
 
-        use_i18n = builder.translator is not None
+        use_i18n = builder.app.translator is not None
         extensions = use_i18n and ['jinja2.ext.i18n'] or []
         self.environment = SandboxedEnvironment(loader=self,
                                                 extensions=extensions)
         self.environment.globals['debug'] = contextfunction(pformat)
         self.environment.globals['accesskey'] = contextfunction(accesskey)
         if use_i18n:
-            self.environment.install_gettext_translations(builder.translator)
+            self.environment.install_gettext_translations(builder.app.translator)
 
     def render(self, template, context):
         return self.environment.get_template(template).render(context)