[Pkg-bazaar-commits] ./bzr-gtk/unstable r132: Use decorator for catching and showing bzr-gtk errors graphically. Eventually, this should go away and should be handled by the ui factory.

Jelmer Vernooij jelmer at samba.org
Fri Apr 10 07:45:52 UTC 2009


------------------------------------------------------------
revno: 132
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: trunk
timestamp: Tue 2007-01-30 00:27:09 +0100
message:
  Use decorator for catching and showing bzr-gtk errors graphically. Eventually, this should go away and should be handled by the ui factory.
added:
  olive/errors.py
modified:
  olive/__init__.py
  olive/commit.py
  olive/menu.py
  olive/merge.py
  olive/mkdir.py
  olive/move.py
  olive/push.py
  olive/remove.py
  olive/rename.py
-------------- next part --------------
=== modified file 'olive/__init__.py'
--- a/olive/__init__.py	2007-01-29 21:27:40 +0000
+++ b/olive/__init__.py	2007-01-29 23:27:09 +0000
@@ -35,10 +35,8 @@
 import bzrlib.errors as errors
 from bzrlib.workingtree import WorkingTree
 
-# Olive GTK UI version
-__version__ = '0.14.0'
-
 from dialog import error_dialog, info_dialog
+from errors import show_bzr_error
 from guifiles import GLADEFILENAME
 
 # import this classes only once
@@ -278,6 +276,7 @@
             info_dialog(_('Local branch up to date'),
                         _('There are no missing revisions.'))
 
+    @show_bzr_error
     def on_menuitem_branch_pull_activate(self, widget):
         """ Branch/Pull menu handler. """
         branch_to = self.wt.branch
@@ -288,11 +287,7 @@
                                      _('Pulling is not possible until there is a parent location.'))
             return
 
-        try:
-            branch_from = Branch.open(location)
-        except errors.NotBranchError:
-            error_dialog(_('Directory is not a branch'),
-                                     _('You can perform this action only in a branch.'))
+        branch_from = Branch.open(location)
 
         if branch_to.get_parent() is None:
             branch_to.set_parent(branch_from.base)
@@ -318,37 +313,30 @@
         status = OliveStatus(self.wt, self.wtpath)
         status.display()
     
+    @show_bzr_error
     def on_menuitem_branch_initialize_activate(self, widget):
         """ Initialize current directory. """
         import bzrlib.bzrdir as bzrdir
         
+        if not os.path.exists(self.path):
+            os.mkdir(self.path)
+ 
         try:
-            if not os.path.exists(self.path):
-                os.mkdir(self.path)
-     
-            try:
-                existing_bzrdir = bzrdir.BzrDir.open(self.path)
-            except errors.NotBranchError:
-                bzrdir.BzrDir.create_branch_convenience(self.path)
-            else:
-                if existing_bzrdir.has_branch():
-                    if existing_bzrdir.has_workingtree():
-                        raise errors.AlreadyBranchError(self.path)
-                    else:
-                        raise errors.BranchExistsWithoutWorkingTree(self.path)
+            existing_bzrdir = bzrdir.BzrDir.open(self.path)
+        except errors.NotBranchError:
+            bzrdir.BzrDir.create_branch_convenience(self.path)
+        else:
+            if existing_bzrdir.has_branch():
+                if existing_bzrdir.has_workingtree():
+                    raise errors.AlreadyBranchError(self.path)
                 else:
-                    existing_bzrdir.create_branch()
-                    existing_bzrdir.create_workingtree()
-        except errors.AlreadyBranchError, errmsg:
-            error_dialog(_('Directory is already a branch'),
-                         _('The current directory (%s) is already a branch.\nYou can start using it, or initialize another directory.') % errmsg)
-        except errors.BranchExistsWithoutWorkingTree, errmsg:
-            error_dialog(_('Branch without a working tree'),
-                         _('The current directory (%s)\nis a branch without a working tree.') % errmsg)
-        else:
-            info_dialog(_('Initialize successful'),
-                        _('Directory successfully initialized.'))
-            self.refresh_right()
+                    raise errors.BranchExistsWithoutWorkingTree(self.path)
+            else:
+                existing_bzrdir.create_branch()
+                existing_bzrdir.create_workingtree()
+        info_dialog(_('Initialize successful'),
+                    _('Directory successfully initialized.'))
+        self.refresh_right()
         
     def on_menuitem_file_annotate_activate(self, widget):
         """ File/Annotate... menu handler. """
@@ -796,8 +784,6 @@
             tree1 = WorkingTree.open_containing(path)[0]
         except (errors.NotBranchError, errors.NoWorkingTree):
             notbranch = True
-        except errors.PermissionDenied:
-            print "DEBUG: permission denied."
         
         if not notbranch:
             branch = tree1.branch

=== modified file 'olive/commit.py'
--- a/olive/commit.py	2007-01-29 16:59:02 +0000
+++ b/olive/commit.py	2007-01-29 23:27:09 +0000
@@ -29,6 +29,7 @@
 from bzrlib import osutils
 
 from dialog import error_dialog, question_dialog
+from errors import show_bzr_error
 from guifiles import GLADEFILENAME
 
 
@@ -465,6 +466,7 @@
                 pass
             diff.show()
     
+    @show_bzr_error
     def _on_commit_clicked(self, button):
         """ Commit button clicked handler. """
         textbuffer = self._textview_message.get_buffer()
@@ -491,54 +493,19 @@
         
         try:
             self.wt.commit(message,
-                           allow_pointless=False,
-                           strict=self._check_strict.get_active(),
-                           local=local,
-                           specific_files=specific_files)
-        except errors.NotBranchError:
-            error_dialog(_('Directory is not a branch'),
-                         _('You can perform this action only in a branch.'))
-            return
-        except errors.LocalRequiresBoundBranch:
-            error_dialog(_('Directory is not a checkout'),
-                         _('You can perform local commit only on checkouts.'))
-            return
-        except errors.ConflictsInTree:
-            error_dialog(_('Conflicts in tree'),
-                         _('You need to resolve the conflicts before committing.'))
-            return
-        except errors.StrictCommitFailed:
-            error_dialog(_('Strict commit failed'),
-                         _('There are unknown files in the working tree.\nPlease add or delete them.'))
-            return
-        except errors.BoundBranchOutOfDate, errmsg:
-            error_dialog(_('Bound branch is out of date'),
-                         _('%s') % errmsg)
-            return
+                       allow_pointless=False,
+                       strict=self._check_strict.get_active(),
+                       local=local,
+                       specific_files=specific_files)
         except errors.PointlessCommit:
             response = question_dialog(_('Commit with no changes?'),
                                        _('There are no changes in the working tree.'))
             if response == gtk.RESPONSE_YES:
-                # Try to commit again
-                try:
-                    self.wt.commit(message,
-                                   allow_pointless=True,
-                                   strict=self._check_strict.get_active(),
-                                   local=local,
-                                   specific_files=specific_files)
-                except errors.BzrError, msg:
-                    error_dialog(_('Unknown bzr error'), str(msg))
-                    return
-                except Exception, msg:
-                    error_dialog(_('Unknown error'), str(msg))
-                    return
-        except errors.BzrError, msg:
-            error_dialog(_('Unknown bzr error'), str(msg))
-            return
-        except Exception, msg:
-            error_dialog(_('Unknown error'), str(msg))
-            return
-        
+                self.wt.commit(message,
+                               allow_pointless=True,
+                               strict=self._check_strict.get_active(),
+                               local=local,
+                               specific_files=specific_files)
         self.response(gtk.RESPONSE_OK)
 
     def _pending_merges(self, wt):

=== added file 'olive/errors.py'
--- a/olive/errors.py	1970-01-01 00:00:00 +0000
+++ b/olive/errors.py	2007-01-29 23:27:09 +0000
@@ -0,0 +1,71 @@
+# Copyright (C) 2007 Jelmer Vernooij <jelmer at samba.org>
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+import bzrlib.errors as errors
+
+from dialog import error_dialog
+
+def show_bzr_error(unbound):
+    """Decorator that shows bazaar exceptions. """
+    def convert(*args, **kwargs):
+        try:
+            unbound(*args, **kwargs)
+        except errors.NotBranchError:
+            error_dialog(_('Directory is not a branch'),
+                         _('You can perform this action only in a branch.'))
+        except errors.LocalRequiresBoundBranch:
+            error_dialog(_('Directory is not a checkout'),
+                         _('You can perform local commit only on checkouts.'))
+        except errors.PointlessCommit:
+            error_dialog(_('No changes to commit'),
+                         _('Try force commit if you want to commit anyway.'))
+        except errors.ConflictsInTree:
+            error_dialog(_('Conflicts in tree'),
+                         _('You need to resolve the conflicts before committing.'))
+        except errors.StrictCommitFailed:
+            error_dialog(_('Strict commit failed'),
+                         _('There are unknown files in the working tree.\nPlease add or delete them.'))
+        except errors.BoundBranchOutOfDate, errmsg:
+            error_dialog(_('Bound branch is out of date'),
+                         _('%s') % errmsg)
+        except errors.NotVersionedError:
+            error_dialog(_('File not versioned'),
+                         _('The selected file is not versioned.'))
+        except errors.DivergedBranches:
+            error_dialog(_('Branches have been diverged'),
+                         _('You cannot push if branches have diverged. Use the\noverwrite option if you want to push anyway.'))
+        except errors.NoSuchFile:
+            error_dialog(_("No diff output"),
+                         _("The selected file hasn't changed."))
+        except errors.AlreadyBranchError, errmsg:
+            error_dialog(_('Directory is already a branch'),
+                         _('The current directory (%s) is already a branch.\nYou can start using it, or initialize another directory.') % errmsg)
+        except errors.BranchExistsWithoutWorkingTree, errmsg:
+            error_dialog(_('Branch without a working tree'),
+                         _('The current directory (%s)\nis a branch without a working tree.') % errmsg)
+        except errors.BzrError, msg:
+            error_dialog(_('Unknown bzr error'), str(msg))
+        except errors.PermissionDenied:
+            error_dialog(_("Permission denied"), _("permission denied."))
+        except Exception, msg:
+            error_dialog(_('Unknown error'), str(msg))
+
+    convert.__doc__ = unbound.__doc__
+    convert.__name__ = unbound.__name__
+    return convert
+
+
+

=== modified file 'olive/menu.py'
--- a/olive/menu.py	2007-01-29 17:55:55 +0000
+++ b/olive/menu.py	2007-01-29 23:27:09 +0000
@@ -29,6 +29,7 @@
 from bzrlib.workingtree import WorkingTree
 
 from dialog import error_dialog, info_dialog, warning_dialog
+from errors import show_bzr_error
 from launch import launch
 from olive import OlivePreferences, DiffWindow
 
@@ -130,6 +131,7 @@
     def left_context_menu(self):
         return self.cmenu_left
     
+    @show_bzr_error
     def add_file(self, action):
         """ Right context menu -> Add """
         import bzrlib.add
@@ -143,13 +145,9 @@
                          _('Please select a file from the list,\nor choose the other option.'))
             return
         
-        try:
-            bzrlib.add.smart_add([os.path.join(directory, filename)])
-        except errors.NotBranchError:
-            error_dialog(_('Directory is not a branch'),
-                         _('You can perform this action only in a branch.'))
-            return
+        bzrlib.add.smart_add([os.path.join(directory, filename)])
     
+    @show_bzr_error
     def remove_file(self, action):
         """ Right context menu -> Remove """
         # Remove only the selected file
@@ -161,19 +159,8 @@
                          _('Please select a file from the list,\nor choose the other option.'))
             return
         
-        try:
-            wt, path = WorkingTree.open_containing(os.path.join(directory, filename))
-            wt.remove(path)
-
-        except errors.NotBranchError:
-            error_dialog(_('Directory is not a branch'),
-                         _('You can perform this action only in a branch.'))
-            return
-        except errors.NotVersionedError:
-            error_dialog(_('File not versioned'),
-                         _('The selected file is not versioned.'))
-            return
-
+        wt, path = WorkingTree.open_containing(os.path.join(directory, filename))
+        wt.remove(path)
         self.app.set_path(self.path)
         self.app.refresh_right()
 
@@ -218,24 +205,14 @@
         commit = CommitDialog(wt, path, not branch)
         commit.display()
     
+    @show_bzr_error
     def diff(self, action):
         """ Right context menu -> Diff """
-        try:
-            wt = WorkingTree.open_containing(self.path)[0]
-        except errors.NotBranchError:
-            error_dialog(_('File is not in a branch'),
-                         _('The selected file is not in a branch.'))
-            return
-        
+        wt = WorkingTree.open_containing(self.path)[0]
         window = DiffWindow()
         parent_tree = wt.branch.repository.revision_tree(wt.branch.last_revision())
         window.set_diff(wt.branch.nick, wt, parent_tree)
-        try:
-            window.set_file(wt.relpath(self.path + os.sep + self.selected))
-        except errors.NoSuchFile:
-            error_dialog(_("No diff output"),
-                         _("The selected file hasn't changed."))
-            return
+        window.set_file(wt.relpath(self.path + os.sep + self.selected))
         window.show()
     
     def bookmark(self, action):

=== modified file 'olive/merge.py'
--- a/olive/merge.py	2006-10-25 16:29:18 +0000
+++ b/olive/merge.py	2007-01-29 23:27:09 +0000
@@ -29,6 +29,7 @@
 import bzrlib.errors as errors
 
 from dialog import error_dialog, info_dialog, warning_dialog
+from errors import show_bzr_error
 from guifiles import GLADEFILENAME
 
 
@@ -58,6 +59,7 @@
         """ Display the Add file(s) dialog. """
         self.window.show_all()
 
+    @show_bzr_error
     def merge(self, widget):
         branch = self.entry.get_text()
         if branch == "":
@@ -65,13 +67,8 @@
                          _('Please specify a branch to merge from.'))
             return
 
-        try:
-            other_branch = Branch.open_containing(branch)[0]
-        except errors.NotBranchError:
-            error_dialog(_('Specified location not a branch'),
-                         _('Please specify a branch you want to merge from.'))
-            return
-        
+        other_branch = Branch.open_containing(branch)[0]
+
         try:
             conflicts = self.wt.merge_from_branch(other_branch)
         except errors.BzrCommandError, errmsg:

=== modified file 'olive/mkdir.py'
--- a/olive/mkdir.py	2006-10-25 16:29:18 +0000
+++ b/olive/mkdir.py	2007-01-29 23:27:09 +0000
@@ -53,6 +53,7 @@
         """ Display the Make directory dialog. """
         self.window.show_all()
 
+    @show_bzr_error
     def mkdir(self, widget):
         # Get the widgets
         entry = self.glade.get_widget('entry_mkdir')
@@ -77,9 +78,6 @@
                                  _('Please specify another name to continue.'))
                 else:
                     raise
-            except errors.NotBranchError:
-                warning_dialog(_('Directory is not in a branch'),
-                               _('You can only create a non-versioned directory.'))
         else:
             # Just a simple directory
             try:

=== modified file 'olive/move.py'
--- a/olive/move.py	2006-10-25 16:29:18 +0000
+++ b/olive/move.py	2007-01-29 23:27:09 +0000
@@ -30,6 +30,7 @@
 from bzrlib.workingtree import WorkingTree
 
 from dialog import error_dialog
+from errors import show_bzr_error
 from guifiles import GLADEFILENAME
 
 
@@ -68,6 +69,7 @@
         """ Display the Move dialog. """
         self.window.show_all()
 
+    @show_bzr_error
     def move(self, widget):
         destination = self.filechooser.get_filename()
 
@@ -81,20 +83,14 @@
         source = os.path.join(self.wtpath, filename)
         
         # Move the file to a directory
-        try:
-            wt1, path1 = WorkingTree.open_containing(self.wt.abspath(source))
-            wt2, path2 = WorkingTree.open_containing(destination)
-            if wt1.basedir != wt2.basedir:
-                error_dialog(_('Not the same branch'),
-                             _('The destination is not in the same branch.'))
-                return
-
-            wt1.move([source], wt1.relpath(destination))
-        except errors.NotBranchError:
-            error_dialog(_('File is not in a branch'),
-                         _('The selected file is not in a branch.'))
+        wt1, path1 = WorkingTree.open_containing(self.wt.abspath(source))
+        wt2, path2 = WorkingTree.open_containing(destination)
+        if wt1.basedir != wt2.basedir:
+            error_dialog(_('Not the same branch'),
+                         _('The destination is not in the same branch.'))
             return
 
+        wt1.move([source], wt1.relpath(destination))
         self.close()
     
     def close(self, widget=None):

=== modified file 'olive/push.py'
--- a/olive/push.py	2006-10-25 16:29:18 +0000
+++ b/olive/push.py	2007-01-29 23:27:09 +0000
@@ -102,14 +102,11 @@
         if self.radio_stored.get_active():
             try:
                 revs = do_push(self.branch,
-                               overwrite=self.check_overwrite.get_active())
-            except errors.NotBranchError:
-                error_dialog(_('Directory is not a branch'),
-                             _('You can perform this action only in a branch.'))
-                return
             except errors.DivergedBranches:
-                error_dialog(_('Branches have been diverged'),
-                             _('You cannot push if branches have diverged. Use the\noverwrite option if you want to push anyway.'))
+                response = question_dialog(_('Branches have been diverged'),
+                             _('You cannot push if branches have diverged. \nOverwrite?'))
+                if response == gtk.RESPONSE_OK:
+                    revs = do_push(self.branch, overwrite=True)
                 return
         elif self.radio_specific.get_active():
             location = self.entry_location.get_text()
@@ -121,16 +118,18 @@
             try:
                 revs = do_push(self.branch, location,
                                self.check_remember.get_active(),
-                               self.check_overwrite.get_active(),
+                               False,
                                self.check_create.get_active())
-            except errors.NotBranchError:
-                error_dialog(_('Directory is not a branch'),
-                             _('You can perform this action only in a branch.'))
-                return
             except errors.DivergedBranches:
-                error_dialog(_('Branches have been diverged'),
-                             _('You cannot push if branches have diverged. Use the\noverwrite option if you want to push anyway.'))
-                return
+                response = question_dialog(_('Branches have been diverged'),
+                             _('You cannot push if branches have diverged. \nOverwrite?'))
+                if response == gtk.RESPONSE_OK:
+                    revs = do_push(self.branch, location,
+                        self.check_remember.get_active(),
+                            True,
+                            self.check_create.get_active())
+                else:
+                    return
         
         self.close()
         info_dialog(_('Push successful'),

=== modified file 'olive/remove.py'
--- a/olive/remove.py	2006-12-17 23:22:10 +0000
+++ b/olive/remove.py	2007-01-29 23:27:09 +0000
@@ -54,6 +54,7 @@
         """ Display the Remove file(s) dialog. """
         self.window.show_all()
         
+    @show_bzr_error
     def remove(self, widget):
         radio_selected = self.glade.get_widget('radiobutton_remove_selected')
         radio_new = self.glade.get_widget('radiobutton_remove_new')
@@ -69,16 +70,7 @@
             
             fullpath = self.wt.abspath(os.path.join(self.wtpath, filename))
             
-            try:
-                self.wt.remove(fullpath)
-            except errors.NotBranchError:
-                error_dialog(_('Directory is not a branch'),
-                             _('You can perform this action only in a branch.'))
-                return
-            except errors.NotVersionedError:
-                error_dialog(_('File not versioned'),
-                             _('The selected file is not versioned.'))
-                return
+            self.wt.remove(fullpath)
         elif radio_new.get_active():
             # Remove added files recursively
             added = self.wt.changes_from(self.wt.basis_tree()).added
@@ -126,6 +118,7 @@
         self.vbox.set_spacing(3)
         self.vbox.show_all()
         
+    @show_bzr_error
     def _on_remove_clicked(self, button):
         """ Remove button clicked handler. """
         if self._radio_selected.get_active():
@@ -137,16 +130,7 @@
                              _('Please select a file from the list,\nor choose the other option.'))
                 return
             
-            try:
-                self.wt.remove(os.path.join(self.wtpath, filename))
-            except errors.NotBranchError:
-                error_dialog(_('Directory is not a branch'),
-                             _('You can perform this action only in a branch.'))
-                return
-            except errors.NotVersionedError:
-                error_dialog(_('File not versioned'),
-                             _('The selected file is not versioned.'))
-                return
+            self.wt.remove(os.path.join(self.wtpath, filename))
         elif self._radio_added.get_active():
             # Remove added files recursively
             added = self.wt.changes_from(self.wt.basis_tree()).added

=== modified file 'olive/rename.py'
--- a/olive/rename.py	2006-12-07 16:35:25 +0000
+++ b/olive/rename.py	2007-01-29 23:27:09 +0000
@@ -59,6 +59,7 @@
         
         self.window.show_all()
 
+    @show_bzr_error
     def rename(self, widget):
         # Get entry
         old_filename = self.selected
@@ -78,22 +79,14 @@
         destination = os.path.join(self.wtpath, new_filename)
         
         # Rename the file
-        try:
-            wt1, path1 = WorkingTree.open_containing(self.wt.abspath(source))
-            wt2, path2 = WorkingTree.open_containing(self.wt.abspath(source))
+        wt1, path1 = WorkingTree.open_containing(self.wt.abspath(source))
+        wt2, path2 = WorkingTree.open_containing(self.wt.abspath(source))
 
-            if wt1.basedir != wt2.basedir:
-                error_dialog(_('Not the same branch'),
-                             _('The destination is not in the same branch.'))
-                return
-            wt1.rename_one(source, destination)
-        except errors.NotBranchError:
-            error_dialog(_('File is not in a branch'),
-                         _('The selected file is not in a branch.'))
+        if wt1.basedir != wt2.basedir:
+            error_dialog(_('Not the same branch'),
+                         _('The destination is not in the same branch.'))
             return
-        except errors.BzrError, msg:
-            error_dialog(_('Unknown bzr error'), str(msg))
-
+        wt1.rename_one(source, destination)
         self.close()
     
     def close(self, widget=None):



More information about the Pkg-bazaar-commits mailing list