Commits

Anonymous committed 18c5762

Add rstfile - paragraph_id - paragraph_content database

Comments (0)

Files changed (4)

sphinx/builders/webapp/webapp.py

 
 from sphinx.builders.html import StandaloneHTMLBuilder
 from sphinx.errors import SphinxError
+from sphinx.web.reposums import PickleLoadSave
 from sphinx.writers.html import HTMLTranslator
 
 try:
 DOC_PATH = 0
 DOC_TITLE = 1
 
+piddbfile = 'piddb.pkl' # XXX: more it to a configuration file!
+
 class WebAppBuilder(StandaloneHTMLBuilder):
     def init(self):
+        self.orig_outdir = self.outdir
+
         # _build/webapp/public - ordinary HTML files produced by Sphinx
         self.public_dir = path.join(self.outdir, 'public')
         
         self.outdir = self.html_dir
         self.handle_page('xapiansearch', {}, 'web/xapiansearch.html')
 
+        self.copy_files(os.getcwd(), self.orig_outdir, (piddbfile,))
         self.create_repo()
 
     def init_translator_class(self):
                     commands.add(self.ui, repo, dstfile)
         path.walk(self.srcdir, callback, '')
 
+    def copy_files(self, srcdir, dstdir, files):
+        """Copy files from a list/tuple 'files' from 'srcdir' source
+        directory to destination directory 'dstdir'."""
+        for f in files:
+            try:
+                afile = path.join(srcdir, f)
+                shutil.copyfile(afile, path.join(dstdir, f))
+            except Exception, err:
+                raise SphinxError(err)
+
+
     ###
     ### webapp templates
     ###
             except Exception, err:
                 raise SphinxError(err)
 
-        for f in files:
-            try:
-                afile = path.join(templates_dir, f)
-                shutil.copyfile(afile, path.join(self.outdir, f))
-            except Exception, err:
-                raise SphinxError(err)
+        srcdir = path.join(path.dirname(__file__), 'templates')
+        self.copy_files(srcdir, self.outdir, files)
 
         sphinx_dirs = (('../../themes/basic/static', 'public/_static'),
                        ('../../themes', 'themes'))
         except Exception, err:
             raise SphinxError(err)
 
+class PidDb(PickleLoadSave):
+    def __init__(self, dbfile):
+        self.dbfile = dbfile
+
+    def add_record(self, filename, pid, paragraph):
+        """Add a record to the database. 'filename' is key
+        for the first-level dictionary, and 'pid' is a key for the
+        second-level dictionary. 'paragraph' is a value assigned
+        to the second-level dictionary:
+
+        database[filename][pid] = paragraph
+        """
+        data = self.dbread(self.dbfile)
+        if filename not in data:
+            data[filename] = {}
+        else:
+            data[filename][pid] = paragraph
+        self.dbsave(self.dbfile, data)
+        
+
+def cutcwd(fpath):
+    """For path which starts with the value returned by os.getcwd() 
+    return what follows. If a value to be returned starts with a slash
+    ("/"), cut the slash out.
+    
+    For example, if the current working directory is '/etc/' and 'fpath'
+    is equal to '/etc/passwd', return 'passwd'.
+    
+    If 'fpath' starts with something else than `os.getcwd()`, return 'fpath'."""
+    cwd = os.getcwd()
+    cwdlen = len(cwd)
+    if fpath.startswith(cwd):
+        fpath = fpath[cwdlen:]
+        if fpath and fpath[0] == '/':
+            return fpath[1:]
+    return fpath
 
 class WebappHTMLTranslator(HTMLTranslator):
-    # XXX: there is no differentiation between paragraphs in show/hide switch
-    # yet, so they all will appear/disappear as you press one of the buttons :)
-
     def __init__(self, *args, **kwds):
         self.templ_env = None # templates environment
         self.init_templates()
-
         self.pid = None # paragraph ID
-
+        self.piddb = PidDb(piddbfile)
         HTMLTranslator.__init__(self, *args, **kwds)
 
     def init_templates(self):
             self.context.append('')
 
     def depart_paragraph(self, node):
+        """Extend HTMLTranslator.depart_paragraph() by appending
+        the body of the document with rendered template for comments/fixes
+        post form. Also, add paragraph's rawsource (reST source) to the
+        PidDb database."""
         comm_template = self.templ_env.get_template('comments.html')
         HTMLTranslator.depart_paragraph(self, node)
         if not self.should_be_compact_paragraph(node):
            self.body.append(comm_template.render({ 'paragraph_id': self.pid,
                                                    'rawsource': node.rawsource }))
+           if node.source:
+               self.piddb.add_record(cutcwd(node.source), self.pid, node.rawsource)

sphinx/web/middleware/appserver.py

 from sphinx.web.webconfig import WebConfig
 from sphinx.web.singlebuilder import SinglefileHTMLBuilder
 
-tmp_template = """
-{%% extends "basic/layout.html" %%}
-{%% block body %%} %(body)s {%% endblock %%}
-"""
-
 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,

sphinx/web/reposums.py

 import cPickle
 from sphinx.errors import SphinxError
 
-class RepoSums(object):
-    def __init__(self, sumsfile):
-        self.sumsfile = sumsfile
-
-    def dbread(self, filename):
+class PickleLoadSave(object):
+    """Basic class for loading and saving pickle files. Contains two methods:
+    dbread() and dbsave()."""
+    def dbread(self, dbfile):
         try:
-            w = open(self.sumsfile)
+            w = open(dbfile)
             data = cPickle.load(w)
             w.close()
         except:
             return {}
         return data
 
-    def dbsave(self, filename, data):
+    def dbsave(self, dbfile, data):
         try:
-            w = open(self.sumsfile, 'w')
+            w = open(dbfile, 'w')
             cPickle.dump(data, w)
             w.close()
         except Exception, err:
             raise SphinxError(err)
 
+
+class RepoSums(PickleLoadSave):
+    def __init__(self, sumsfile):
+        self.sumsfile = sumsfile
+
     def add_record(self, filename, md5sum):
         data = self.dbread(self.sumsfile)
         data[filename] = md5sum
         return data.get(filename)
 
     def sum_changed(self, filename, md5sum):
+        """Compare the pair filename:md5sum with the pair from the database.
+
+        Return True if the pair with which the method was invoked is not the
+        same as the pair saved in the database (also, return True, if the pair
+        did not exist in the database before the method was invoked).
+        In any of above cases - add a new pair to the database.
+
+        Return False otherwise (e.g. the pairs are the same)."""
         data = self.dbread(self.sumsfile)
         if not data:
             self.add_record(filename, md5sum)

sphinx/web/singlebuilder.py

         self.config.html_theme_path = 'themes/' + self.config.html_theme
 
         self.env = BuildEnvironment(self.srcdir, self.doctreedir, self.config)
-        self.env.find_files(self.config)
+        #self.env.find_files(self.config)
         self.env.set_warnfunc(self.warn)
 
         # XXX got to figure out what's this