Commits

Wojciech Walczak  committed 4cd7a4f

Improve configuration handling

  • Participants
  • Parent commits 389b5b6

Comments (0)

Files changed (8)

File sphinx/builders/webapp/templates/html/comments.html

                      </tr>
                      <tr>
                         <td colspan=2>
-                           I agree to publish my fix on a license X: <input type="checkbox" name="licence" value="1"> 
+                           I agree to publish my fix on a license {{ licence }}: <input type="checkbox" name="licence" value="1"> 
                         </td>
                      </tr>
                   </table>

File sphinx/builders/webapp/templates/webapp.conf

+[General]
+licence={{ licence }}
+
 [Repository]
 repodir=repo/doc
 reposums=reposums
+piddbfile={{ piddbfile }}

File sphinx/builders/webapp/webapp.py

 import cPickle
 from os import path
 from hashlib import md5
+from types import ListType, TupleType
 
 from anyvc.workdir import get_workdir_manager_for_path
 
 DOC_PATH = 0
 DOC_TITLE = 1
 
-piddbfile = 'piddb.pkl' # XXX: more it to a configuration file!
-
 class WebAppBuilder(StandaloneHTMLBuilder):
     def init(self):
         self.encoding = self.config.html_output_encoding or 'utf8'
         # _build/webapp/html - HTML templates for webapp/middleware
         self.html_dir = path.join(self.outdir, 'html')
 
+        templ_path = path.join(path.dirname(__file__), 'templates/')
+        self.templ_env = j2.Environment(loader=j2.FileSystemLoader(templ_path))
+
         # init repository
         self.ui = ui.ui()
 
         self.handle_page('xapiansearch', {}, 'web/xapiansearch.html')
         self.create_repo()
         self.add_pids_to_paragraphs()
-        self.copy_files(os.getcwd(), self.orig_outdir, (piddbfile,), move=True)
+        self.copy_files(os.getcwd(), self.orig_outdir, self.config.piddbfile,
+                        move=True)
 
     def init_translator_class(self):
+        WebappHTMLTranslator.licence = self.config.licence
+        WebappHTMLTranslator.piddbfile = self.config.piddbfile
         self.translator_class = WebappHTMLTranslator
 
 
 
         conffile = path.join(self.orig_outdir, 'webapp.conf')
         self.webconfig = WebConfig(conffile)
-        self.piddb = PidDb(piddbfile)
+        self.piddb = PidDb(self.webconfig.piddbfile)
         files = self.piddb.get_filenames()
         for f in files:
             add_pids_to_lines(f)
-        self.piddb.dbsave(piddbfile, self.piddb.data)
+        self.piddb.dbsave(self.webconfig.piddbfile, self.piddb.data)
 
 
     ###
     def copy_files(self, srcdir, dstdir, files, move=False):
         """Copy files from a list/tuple 'files' from 'srcdir' source
         directory to destination directory 'dstdir'."""
+        files = type(files) in (ListType, TupleType) and files or (files, )
         for f in files:
             try:
                 afile = path.join(srcdir, f)
     def copy_webapp_templates(self):
         """
         Copy the files (defined in 'files' tuple) from sphinx/webapp/templates/
-        to self.outdir.
+        to self.outdir. Also copy themes and static files to self.outdir.
         """
         dirs = ('html', 'comments', 'fixes')
-        files = ('server.py', 'developers.txt',
-                 'webapp.conf', 'html/openid.html')
+        files = ('server.py', 'developers.txt', 'html/openid.html')
         templates_dir = path.join(path.dirname(__file__), 'templates')
-
         for d in dirs:
             try:
                 os.mkdir(path.join(self.outdir, d))
             except Exception, err:
                 raise SphinxError(err)
-
         srcdir = path.join(path.dirname(__file__), 'templates')
         self.copy_files(srcdir, self.outdir, files)
 
+        # before copying 'webapp.conf', render it
+        try:
+            conf_template = self.templ_env.get_template('webapp.conf')
+            conf_rendered = conf_template.render(
+                                { 'piddbfile': self.config.piddbfile,
+                                  'licence': self.config.licence })
+            conf_dst = path.join(self.outdir, 'webapp.conf')
+            w = codecs.open(conf_dst, 'w', self.encoding)
+            w.write(conf_rendered)
+            w.close()
+        except Exception, err:
+            raise SphinxError(err)
+
+        # copy themes and static files
         sphinx_dirs = (('../../themes/basic/static', 'public/_static'),
                        ('../../themes', 'themes'))
         for sdir in sphinx_dirs:
     return fpath
 
 class WebappHTMLTranslator(HTMLTranslator):
+    licence = None
     repodir = None
+    piddbfile = None
     def __init__(self, *args, **kwds):
         self.templ_env = None # templates environment
         self.init_templates()
         self.pid = None # paragraph ID
-        self.piddb = PidDb(piddbfile)
+        self.piddb = PidDb(self.piddbfile)
         HTMLTranslator.__init__(self, *args, **kwds)
 
+    def init_attributes(self, repodir, piddbfile, licence):
+        self.repodir = repodir
+        self.piddbfile = piddbfile
+        self.licence = licence
+
     def init_templates(self):
         templ_path = path.join(path.dirname(__file__), 'templates/html')
         self.templ_env = j2.Environment(loader=j2.FileSystemLoader(templ_path),
            nodename = cutcwd(node.source)
            if self.repodir:
                nodename = nodename[len(self.repodir)+1:]
-           self.body.append(comm_template.render({ 'node': nodename,
+           self.body.append(comm_template.render({ 'licence': self.licence,
+                                                   'node': nodename,
                                                    'paragraph_id': self.pid,
                                                    'rawsource': node.rawsource }))
 

File sphinx/config.py

 
         # Web application options
         xapian_db = ('xapian_db', 'webapp'),
+        piddbfile = ('piddb.pkl', 'webapp'),
+        licence = ('BSD', 'webapp'),
     )
 
     def __init__(self, dirname, filename, overrides, tags):

File sphinx/quickstart.py

 
 # -- Options for WebApp --------------------------------------------------------
 # configuration options for Xapian:
-#xapian_db = 'xapian_db'
+xapian_db = 'xapian_db'
+piddbfile = 'piddb.pkl'
+licence = 'BSD'
 '''
 
 INTERSPHINX_CONFIG = '''

File sphinx/web/middleware/appserver.py

 from sphinx.web.webconfig import WebConfig
 from sphinx.web.singlebuilder import SinglefileHTMLBuilder
 
-piddbfile = 'piddb.pkl' # XXX: more it to a configuration file!
-
 def index_by_key_val(data, key, val):
     """For a list of dictionaries return an index of a dictionary
     with key:val pair. If there's no such a dictionary in the list,
         self.env = None
         self.www_dir = www_dir
         self.template_vals = {}
-        self.piddb = PidDb(piddbfile)
+        self.webconfig = WebConfig()
+        self.piddb = PidDb(self.webconfig.piddbfile)
         self.webauth = WebAuth()
         self.webauth.developers_load('developers.txt')
-        self.webconfig = WebConfig()
         self.reposums = RepoSums(self.webconfig.reposums)
         self.htmlbuilder = SinglefileHTMLBuilder(self.www_dir,
-                                                 self.webconfig.repodir)
+                                                 self.webconfig.repodir,
+                                                 self.webconfig.piddbfile,
+                                                 self.webconfig.licence)
         self.init_templates()
 
         # an argument for 'reverse' option of sorted function:

File sphinx/web/singlebuilder.py

 class SinglefileHTMLBuilder(StandaloneHTMLBuilder, Sphinx):
     """Render a file when requested using data from a pickled archive.
     Handle theming."""
-    def __init__(self, www_dir, repodir):
+    def __init__(self, www_dir, repodir, piddbfile, licence):
         self.outdir = www_dir
         self.srcdir = repodir
+        self.piddbfile = piddbfile
+        self.licence = licence
         self.cwd = os.getcwd()
         self.confdir = self.cwd
         self.doctreedir = path.join(self.cwd, '../doctrees')
         self.images = {}
 
     def init_translator_class(self):
+        WebappHTMLTranslator.licence = self.licence
+        WebappHTMLTranslator.piddbfile = self.piddbfile
         WebappHTMLTranslator.repodir = self.srcdir
         self.translator_class = WebappHTMLTranslator
 

File sphinx/web/webconfig.py

         self.conf = ConfigParser()
         self.conf.read(self.configfile)
 
+        self.licence = self.conf.get('General', 'licence')
+
+        self.piddbfile = self.conf.get('Repository', 'piddbfile')
         self.repodir = self.conf.get('Repository', 'repodir')
         self.reposums = self.conf.get('Repository', 'reposums')