* Readonly document view.
[redakcja.git] / lib / vstorage.py
index 4cfd59a..82151da 100644 (file)
@@ -266,34 +266,25 @@ class VersionedStorage(object):
         self.repo.remove([repo_file])
         self._commit([repo_file], text, user)
 
-#    @with_working_copy_locked
-#    def _open_page(self, title):
-#        if title not in self:
-#            raise DocumentNotFound()
-#
-#        path = self._title_to_file(title)
-#        logger.debug("Opening page %s", path)
-#        try:
-#            return self.repo.wfile(path, 'rb')
-#        except IOError:
-#            logger.exception("Failed to open page %s", title)
-#            raise DocumentNotFound()
-
     def page_text(self, title, revision=None):
         """Read unicode text of a page."""
         ctx = self._find_filectx(title, revision)
+
+        if ctx is None:
+            raise DocumentNotFound(title)
+
         return ctx.data().decode(self.charset, 'replace'), ctx.filerev()
 
     def page_text_by_tag(self, title, tag):
         """Read unicode text of a taged page."""
-        tag = u"{title}#{tag}".format(**locals()).encode('utf-8')
         fname = self._title_to_file(title)
+        tag = u"{fname}#{tag}".format(**locals()).encode('utf-8')
 
         try:
             ctx = self.repo[tag][fname]
             return ctx.data().decode(self.charset, 'replace'), ctx.filerev()
         except IndexError:
-            raise DocumentNotFound()
+            raise DocumentNotFound(fname)
 
     @with_working_copy_locked
     def page_file_meta(self, title):
@@ -306,20 +297,19 @@ class VersionedStorage(object):
         return st_ino, st_size, st_mtime
 
     @with_working_copy_locked
-    def page_meta(self, title):
+    def page_meta(self, title, revision=None):
         """Get page's revision, date, last editor and his edit comment."""
-        if not title in self:
-            raise DocumentNotFound()
+        fctx = self._find_filectx(title, revision)
 
-        filectx_tip = self._find_filectx(title)
-        if filectx_tip is None:
-            raise DocumentNotFound()
-        rev = filectx_tip.filerev()
-        filectx = filectx_tip.filectx(rev)
-        date = datetime.datetime.fromtimestamp(filectx.date()[0])
-        author = filectx.user().decode("utf-8", 'replace')
-        comment = filectx.description().decode("utf-8", 'replace')
-        return rev, date, author, comment
+        if fctx is None:
+            raise DocumentNotFound(title)
+
+        return {
+            "revision": fctx.filerev(),
+            "date": datetime.datetime.fromtimestamp(fctx.date()[0]),
+            "author": fctx.user().decode("utf-8", 'replace'),
+            "comment": fctx.description().decode("utf-8", 'replace'),
+        }
 
     def repo_revision(self):
         return self.repo['tip'].rev()
@@ -336,9 +326,7 @@ class VersionedStorage(object):
 
     def _find_filectx(self, title, rev=None):
         """Find the last revision in which the file existed."""
-
         repo_file = self._title_to_file(title)
-
         changectx = self._changectx()  # start with tip
         stack = [changectx]
 
@@ -360,7 +348,7 @@ class VersionedStorage(object):
 
             return fctx
         except (IndexError, LookupError) as e:
-            raise DocumentNotFound()
+            raise DocumentNotFound(title)
 
     def page_history(self, title):
         """Iterate over the page's history."""
@@ -386,10 +374,12 @@ class VersionedStorage(object):
 
     @with_working_copy_locked
     def add_page_tag(self, title, rev, tag, user, doctag=True):
+        ctitle = self._title_to_file(title)
+
         if doctag:
-            tag = u"{title}#{tag}".format(**locals()).encode('utf-8')
+            tag = u"{ctitle}#{tag}".format(**locals()).encode('utf-8')
 
-        message = u"Assigned tag {tag!r} to version {rev!r} of {title!r}".format(**locals()).encode('utf-8')
+        message = u"Assigned tag {tag!r} to version {rev!r} of {ctitle!r}".format(**locals()).encode('utf-8')
 
         fctx = self._find_filectx(title, rev)
         self.repo.tag(