Commits

Wojciech Walczak  committed da572a9

Better filename - paragraph IDs - paragraph's contents database handling

  • Participants
  • Parent commits 1d20fcd

Comments (0)

Files changed (7)

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

          </tr>
          <tr>
             <td colspan=2>
-               <div class="submitFixFields_{{ paragraph_id }}" style="display: none;">
+               <div class="submitFixFields_{{ paragraph_id }}" style="display: none;" value="{{ node }}">
                   <table>
                      <tr>
                         <td>Priority:</td>
                      <tr>
                         <!-- XXX: rawsources should be loaded automatically from jQuery script -->
                         <td>Paragraph:</td>
-                        <td><textarea name="paragraph" rows=10 cols=70>{{ rawsource }}</textarea></td>
-                        <textarea name="paragraph_unchanged" style="display: none">{{ rawsource }}</textarea>
+                        <td><textarea name="paragraph" rows=10 cols=70></textarea></td>
+                        <textarea name="paragraph_unchanged" style="display: none"></textarea>
                      </tr>
                      <tr>
                         <td colspan=2> &nbsp; </td>

File sphinx/builders/webapp/webapp.py

 
 from sphinx.builders.html import StandaloneHTMLBuilder
 from sphinx.errors import SphinxError
-from sphinx.web.reposums import PickleLoadSave
+from sphinx.web.dbutils import PidDb
 from sphinx.web.webconfig import WebConfig
 from sphinx.writers.html import HTMLTranslator
 
         StandaloneHTMLBuilder.finish(self)
         self.outdir = self.html_dir
         self.handle_page('xapiansearch', {}, 'web/xapiansearch.html')
-
-        self.copy_files(os.getcwd(), self.orig_outdir, (piddbfile,))
+        self.copy_files(os.getcwd(), self.orig_outdir, (piddbfile,), move=True)
         self.create_repo()
         self.add_pids_to_paragraphs()
 
         files = self.piddb.get_filenames()
         for f in files:
             add_pids_to_lines(f)
+        self.piddb.dbsave(piddbfile, self.piddb.data)
 
 
     ###
     ### general use functions
     ###
-    def copy_files(self, srcdir, dstdir, files):
+    def copy_files(self, srcdir, dstdir, files, move=False):
         """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))
+                action = move and shutil.move or shutil.copyfile
+                action(afile, path.join(dstdir, f))
             except Exception, err:
                 raise SphinxError(err)
 
         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 get_paragraph(self, filename, pid):
-        data = self.dbread(self.dbfile)
-        if filename in data and pid in data[filename]:
-            return data[filename][pid]
-        return None
-
-    def get_pids(self, filename):
-        data = self.dbread(self.dbfile)
-        return data.get(filename)
-
-    def get_filenames(self):
-        data = self.dbread(self.dbfile)
-        return filter(lambda x: x.endswith('.rst'), data.keys())
-
 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
     return fpath
 
 class WebappHTMLTranslator(HTMLTranslator):
+    repodir = None
     def __init__(self, *args, **kwds):
         self.templ_env = None # templates environment
         self.init_templates()
         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,
+        if not self.should_be_compact_paragraph(node) and node.source:
+           nodename = cutcwd(node.source)
+           if self.repodir:
+               nodename = nodename[len(self.repodir)+1:]
+           self.body.append(comm_template.render({ 'node': nodename,
+                                                   'paragraph_id': self.pid,
                                                    'rawsource': node.rawsource }))
-           if node.source:
-               self.piddb.add_record(cutcwd(node.source), self.pid, node.rawsource)
+
+           self.piddb.add_record(nodename, self.pid, node.rawsource, save=True)
+

File sphinx/themes/basic/static/comments.js

          return false;
       }
 
-      if(form.licence.checked) {
+      if(form.submitFix.checked) {
          _db = 'fixes';
       } else {
          _db = 'comments';
 }
 
 function show_submitFixFields(id) {
+   node = $('div[class=submitFixFields_' + id + ']').attr('value');
+   fill_paragraph_from_repo(node, id);
    $('input[name=submitFix]').attr('checked', true);
    $('div.submitFixFields_' + id).css('display', 'block'); 
    _db = 'fixes';
    }
 }
 
+function fill_paragraph_from_repo(node, id) {
+   var p_path = location.protocol + "//" + location.host
+                + "/get_paragraph?node=" + node + "&id=" + id;
+   $.getJSON(p_path, function(data) {
+        alert(data);
+        // get the paragraph
+   });
+   
+}
+
 // *** rating comments ******************************************************
 
 function comments_up_down(id, comment_no, up_down, db) {

File sphinx/web/dbutils.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+    sphinx.web.reposums
+    ~~~~~~~~~~~~~~~~~~~
+
+    Handle MD5 sums database and checking.
+  
+    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+
+import cPickle
+from sphinx.errors import SphinxError
+
+class PickleLoadSave(object):
+    """Basic class for loading and saving pickle files. Contains two methods:
+    dbread() and dbsave()."""
+    def dbread(self, dbfile):
+        try:
+            w = open(dbfile)
+            data = cPickle.load(w)
+            w.close()
+        except:
+            return {}
+        return data
+
+    def dbsave(self, dbfile, data):
+        try:
+            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
+        self.dbsave(self.sumsfile, data)
+
+    def get_record(self, filename):
+        data = self.dbread(self.sumsfile)
+        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)
+            return True
+
+        dbsum = data.get(filename)
+        if dbsum != md5sum:
+            self.add_record(filename, md5sum)
+            return True
+        else:
+            return False
+
+
+class PidDb(PickleLoadSave):
+    def __init__(self, dbfile):
+        self.dbfile = dbfile
+        self.data = self.dbread(self.dbfile)
+
+    def add_record(self, filename, pid, paragraph, save=False):
+        """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
+        """
+        if filename not in self.data:
+            self.data[filename] = {}
+        else:
+            self.data[filename][pid] = paragraph
+        if save:
+            self.dbsave(self.dbfile, self.data)
+
+    def get_paragraph(self, filename, pid):
+        if filename in self.data and pid in self.data[filename]:
+            return self.data[filename][pid]
+        return None
+
+    def get_pids(self, filename):
+        return self.data.get(filename)
+
+    def get_filenames(self):
+        return filter(lambda x: x.endswith('.rst'), self.data.keys())

File sphinx/web/middleware/appserver.py

 import os
 import time
 import locale
-import cPickle
 from os import path
 from difflib import Differ
 from hashlib import md5
 from webob import Request, Response
 
 from sphinx.errors import SphinxError
-from sphinx.web.reposums import RepoSums
+from sphinx.web.dbutils import PidDb, RepoSums
 from sphinx.web.webauth import WebAuth
 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.webauth = WebAuth()
         self.webauth.developers_load('developers.txt')
         self.webconfig = WebConfig()
             resp = self.doReadComments(req, resp)
         elif SW('/fixes/'):
             resp = self.doReadFixes(req, resp)
+        elif SW('/get_paragraph'):
+            resp = self.doGetParagraph(req, resp)
         elif SW('/delete_comment'):
             resp = self.doDeleteComment(req, resp)
         elif SW('/isdeveloper'):
         else:
             resp.status = 404
         return resp
-        
+
+    def doGetParagraph(self, req, resp):
+        node = req.GET['node']
+        pid = req.GET['id']
+        resp.status = 200
+        resp.body = json.dumps(self.piddb.get_paragraph(node, pid))
+        return resp
+
     def doHTMLFile(self, req, resp):
         """Rendering the HTML files happens here: things like themes,
         OpenID info and so on."""

File sphinx/web/reposums.py

-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-"""
-    sphinx.web.reposums
-    ~~~~~~~~~~~~~~~~~~~
-
-    Handle MD5 sums database and checking.
-  
-    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
-    :license: BSD, see LICENSE for details.
-"""
-
-import cPickle
-from sphinx.errors import SphinxError
-
-class PickleLoadSave(object):
-    """Basic class for loading and saving pickle files. Contains two methods:
-    dbread() and dbsave()."""
-    def dbread(self, dbfile):
-        try:
-            w = open(dbfile)
-            data = cPickle.load(w)
-            w.close()
-        except:
-            return {}
-        return data
-
-    def dbsave(self, dbfile, data):
-        try:
-            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
-        self.dbsave(self.sumsfile, data)
-
-    def get_record(self, filename):
-        data = self.dbread(self.sumsfile)
-        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)
-            return True
-
-        dbsum = data.get(filename)
-        if dbsum != md5sum:
-            self.add_record(filename, md5sum)
-            return True
-        else:
-            return False

File sphinx/web/singlebuilder.py

         self.images = {}
 
     def init_translator_class(self):
+        WebappHTMLTranslator.repodir = self.srcdir
         self.translator_class = WebappHTMLTranslator
 
     def render_page(self, docname):