conversion stub wl1->wl2
[redakcja.git] / lib / vstorage / __init__.py
index 9f7084b..bc66d50 100644 (file)
@@ -18,10 +18,11 @@ os.environ['HGENCODING'] = 'utf-8'
 os.environ['HGMERGE'] = "internal:merge"
 
 import mercurial.hg
-import mercurial.ui
 import mercurial.revlog
 import mercurial.util
 
+from vstorage.hgui import SilentUI
+
 
 def urlquote(url, safe='/'):
     """Quotes URL
@@ -129,10 +130,7 @@ class VersionedStorage(object):
             os.makedirs(self.path)
         self.repo_path = find_repo_path(self.path)
 
-        self.ui = mercurial.ui.ui()
-        self.ui.quiet = True
-        self.ui._report_untrusted = False
-        self.ui.setconfig('ui', 'interactive', False)
+        self.ui = SilentUI()
 
         if self.repo_path is None:
             self.repo_path = self.path
@@ -148,19 +146,27 @@ class VersionedStorage(object):
         """Close and reopen the repo, to make sure we are up to date."""
         self.repo = mercurial.hg.repository(self.ui, self.repo_path)
 
-    def _file_path(self, title):
-        return os.path.join(self.path, urlquote(title, safe=''))
+    def _file_path(self, title, type='.xml'):
+        """ Return plain version if exists in repo, add extension otherwise. """
+        path = os.path.join(self.path, urlquote(title, safe=''))
+        if type and self._title_to_file(title, '') not in self.repo['tip']:
+            path += type
+        return path
 
     def _title_to_file(self, title, type=".xml"):
-        return os.path.join(self.repo_prefix, urlquote(title, safe='')) + type
+        """ Return plain version if exists in repo, add extension otherwise. """
+        path = os.path.join(self.repo_prefix, urlquote(title, safe=''))
+        if type and path not in self.repo['tip']:
+            path += type
+        return path
 
     def _file_to_title(self, filename):
         assert filename.startswith(self.repo_prefix)
-        name = filename[len(self.repo_prefix):].strip('/').split('.', 1)[0]
+        name = filename[len(self.repo_prefix):].strip('/').rsplit('.', 1)[0]
         return urlunquote(name)
 
     def __contains__(self, title):
-        return urlquote(title) in self.repo['tip']
+        return self._title_to_file(title) in self.repo['tip']
 
     def __iter__(self):
         return self.all_pages()
@@ -174,7 +180,8 @@ class VersionedStorage(object):
         self.repo.dirstate.setparents(parent_node)
         node = self._commit([repo_file], text, user)
 
-        partial = lambda filename: repo_file == filename
+        def partial(filename):
+            return repo_file == filename
 
         # If p1 is equal to p2, there is no work to do. Even the dirstate is correct.
         p1, p2 = self.repo[None].parents()[0], self.repo[tip_node]
@@ -213,7 +220,7 @@ class VersionedStorage(object):
             filectx_tip = changectx[repo_file]
             current_page_rev = filectx_tip.filerev()
         except mercurial.revlog.LookupError:
-            self.repo.add([repo_file])
+            self.repo[None].add([repo_file])
             current_page_rev = -1
 
         if parent is not None and current_page_rev != parent:
@@ -221,13 +228,15 @@ class VersionedStorage(object):
             author = '<wiki>'
             comment = msg.encode('utf-8')
 
+        logger.debug("Commiting %r", repo_file)
+
         self._commit([repo_file], comment, author)
 
     def save_data(self, title, data, **kwargs):
         """Save data as specified page."""
+        temp_path = tempfile.mkdtemp(dir=self.path)
+        file_path = os.path.join(temp_path, 'saved')
         try:
-            temp_path = tempfile.mkdtemp(dir=self.path)
-            file_path = os.path.join(temp_path, 'saved')
             f = open(file_path, "wb")
             f.write(data)
             f.close()
@@ -263,7 +272,7 @@ class VersionedStorage(object):
             os.unlink(file_path)
         except OSError:
             pass
-        self.repo.remove([repo_file])
+        self.repo[None].remove([repo_file])
         self._commit([repo_file], text, user)
 
     def page_text(self, title, revision=None):
@@ -325,42 +334,28 @@ class VersionedStorage(object):
         return guess_mime(self._file_path(title))
 
     def _find_filectx(self, title, rev=None):
-        """Find the last revision in which the file existed."""
-        tip = self._changectx()  # start with tip
-
-        def tree_search(tip, repo_file):
-            logging.info("Searching for %r", repo_file)
-            current = tip
-            visited = set()
-
-            stack = [current]
-            visited.add(current)
-
-            while repo_file not in current:
-                if not stack:
-                    raise LookupError
-
-                current = stack.pop()
-                for parent in current.parents():
-                    if parent not in visited:
-                        stack.append(parent)
-                        visited.add(parent)
-
-            fctx = current[repo_file]
-            if rev is not None:
-                fctx = fctx.filectx(rev)
-                fctx.filerev()
-            return fctx
-
-        try:
-            return tree_search(tip, self._title_to_file(title))
-        except (IndexError, LookupError) as e:
-            logging.info("XML file not found, trying plain")
-            try:
-                return tree_search(tip, self._title_to_file(title, type=''))
-            except (IndexError, LookupError) as e:
+        """
+        Find the revision of the file in repo.
+        Only look for files still existing in repo's tip.
+        """
+        tip = self._changectx()
+        file = self._title_to_file(title)
+        logging.info('Looking for %s', file)
+        if file in tip:
+            fctx = tip[file]
+        else:
+            file = self._title_to_file(title, type='')
+            logging.info('.xml not found, trying plain')
+            if file in tip:
+                fctx = tip[file]
+            else:
                 raise DocumentNotFound(title)
 
+        if rev is not None:
+            fctx = fctx.filectx(rev)
+            fctx.filerev()
+        return fctx
+
     def page_history(self, title):
         """Iterate over the page's history."""
 
@@ -422,8 +417,7 @@ class VersionedStorage(object):
         tip = self.repo['tip']
         """Iterate over the titles of all pages in the wiki."""
         return [self._file_to_title(filename) for filename in tip
-                  if not filename.startswith('.')
-                    and filename.endswith(type) ]
+                if not filename.startswith('.') and filename.endswith(type)]
 
     def changed_since(self, rev):
         """Return all pages that changed since specified repository revision."""
@@ -434,9 +428,19 @@ class VersionedStorage(object):
             for page in self.all_pages():
                 yield page
                 return
+            return
         current = self.repo.lookup('tip')
         status = self.repo.status(current, last)
         modified, added, removed, deleted, unknown, ignored, clean = status
         for filename in modified + added + removed + deleted:
             if filename.startswith(self.repo_prefix):
                 yield self._file_to_title(filename)
+
+    def revert(self, pageid, rev, **commit_args):
+        """ Make the given version of page the current version (reverting changes). """
+
+        # Find the old version
+        fctx = self._find_filectx(pageid, rev)
+
+        # Restore the contents
+        self.save_data(pageid, fctx.data(), **commit_args)