[Pkg-bazaar-commits] ./bzr-gtk/unstable r154: Add trivial generic class for storing URL history.

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


------------------------------------------------------------
revno: 154
committer: Jelmer Vernooij <jelmer at samba.org>
branch nick: trunk
timestamp: Sat 2007-02-03 16:04:56 +0100
message:
  Add trivial generic class for storing URL history.
added:
  history.py
  tests/test_history.py
modified:
  branch.py
  checkout.py
  olive/__init__.py
  push.py
  tests/__init__.py
-------------- next part --------------
=== modified file 'branch.py'
--- a/branch.py	2007-02-03 14:19:44 +0000
+++ b/branch.py	2007-02-03 15:04:56 +0000
@@ -24,7 +24,6 @@
 
 import gtk
 
-from olive import delimiter
 from errors import show_bzr_error
 
 from bzrlib.branch import Branch
@@ -33,6 +32,7 @@
 
 from dialog import error_dialog, info_dialog
 
+from history import UrlHistory
 
 class BranchDialog(gtk.Dialog):
     """ New implementation of the Branch dialog. """
@@ -101,30 +101,16 @@
         self.vbox.show_all()
         
         # Build branch history
+        self._history = UrlHistory(GlobalConfig(), 'branch_history')
         self._build_history()
     
     def _build_history(self):
         """ Build up the branch history. """
-        config = GlobalConfig()
-        history = config.get_user_option('gbranch_history')
-        if history is not None:
-            self._combo_model = gtk.ListStore(str)
-            for item in history.split(delimiter):
-                self._combo_model.append([ item ])
-            self._combo.set_model(self._combo_model)
-            self._combo.set_text_column(0)
-    
-    def _add_to_history(self, location):
-        """ Add specified location to the history (if not yet added). """
-        config = GlobalConfig()
-        history = config.get_user_option('gbranch_history')
-        if history is None:
-            config.set_user_option('gbranch_history', location)
-        else:
-            h = history.split(delimiter)
-            if location not in h:
-                h.append(location)
-            config.set_user_option('gbranch_history', delimiter.join(h))                
+        self._combo_model = gtk.ListStore(str)
+        for item in self._history.get_entries():
+            self._combo_model.append([ item ])
+        self._combo.set_model(self._combo_model)
+        self._combo.set_text_column(0)
     
     def _get_last_revno(self):
         """ Get the revno of the last revision (if any). """
@@ -205,7 +191,7 @@
         finally:
             br_from.unlock()
                 
-        self._add_to_history(location)
+        self._history.add_entry(location)
         info_dialog(_('Branching successful'),
                     _('%d revision(s) branched.') % revs)
         

=== modified file 'checkout.py'
--- a/checkout.py	2007-02-03 10:45:37 +0000
+++ b/checkout.py	2007-02-03 15:04:56 +0000
@@ -24,14 +24,14 @@
 
 import gtk
 
-from olive import delimiter
 from errors import show_bzr_error
 
 from bzrlib.branch import Branch
 from bzrlib.config import GlobalConfig
 
-from olive.dialog import error_dialog
+from dialog import error_dialog
 
+from history import UrlHistory
 
 class CheckoutDialog(gtk.Dialog):
     """ New implementation of the Checkout dialog. """
@@ -103,30 +103,16 @@
         self.vbox.show_all()
         
         # Build checkout history
+        self._history = UrlHistory(GlobalConfig(), 'branch_history')
         self._build_history()
     
     def _build_history(self):
         """ Build up the checkout history. """
-        config = GlobalConfig()
-        history = config.get_user_option('gcheckout_history')
-        if history is not None:
-            self._combo_model = gtk.ListStore(str)
-            for item in history.split(delimiter):
-                self._combo_model.append([ item ])
-            self._combo.set_model(self._combo_model)
-            self._combo.set_text_column(0)
-    
-    def _add_to_history(self, location):
-        """ Add specified location to the history (if not yet added). """
-        config = GlobalConfig()
-        history = config.get_user_option('gcheckout_history')
-        if history is None:
-            config.set_user_option('gcheckout_history', location)
-        else:
-            h = history.split(delimiter)
-            if location not in h:
-                h.append(location)
-            config.set_user_option('gcheckout_history', delimiter.join(h))                
+        self._combo_model = gtk.ListStore(str)
+        for item in self._history.get_entries():
+            self._combo_model.append([ item ])
+        self._combo.set_model(self._combo_model)
+        self._combo.set_text_column(0)
     
     def _get_last_revno(self):
         """ Get the revno of the last revision (if any). """
@@ -189,7 +175,7 @@
         
         br_from.create_checkout(to_location, revision_id, lightweight)
         
-        self._add_to_history(location)
+        self._history.add_entry(location)
         
         self.response(gtk.RESPONSE_OK)
     

=== added file 'history.py'
--- a/history.py	1970-01-01 00:00:00 +0000
+++ b/history.py	2007-02-03 15:04:56 +0000
@@ -0,0 +1,32 @@
+# 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
+
+delimiter = " "
+
+class UrlHistory:
+    def __init__(self, config, name):
+        self._config = config
+        self._name = name
+
+    def add_entry(self, url):
+        self._config.set_user_option(self._name, delimiter.join(self.get_entries() + [url]))
+
+    def get_entries(self):
+        history = self._config.get_user_option(self._name)
+        if history is None:
+            return []
+        else:
+            return history.split(delimiter)

=== modified file 'olive/__init__.py'
--- a/olive/__init__.py	2007-02-03 14:18:57 +0000
+++ b/olive/__init__.py	2007-02-03 15:04:56 +0000
@@ -46,8 +46,6 @@
 from bzrlib.plugins.gtk.commit import CommitDialog
 from bzrlib.plugins.gtk.push import PushDialog
 
-delimiter = ' '
-
 class OliveGtk:
     """ The main Olive GTK frontend class. This is called when launching the
     program. """
@@ -212,7 +210,7 @@
     
     def on_menuitem_branch_get_activate(self, widget):
         """ Branch/Get... menu handler. """
-        from branch import BranchDialog
+        from bzrlib.plugins.gtk.branch import BranchDialog
         branch = BranchDialog(self.get_path(), self.window)
         response = branch.run()
         if response != gtk.RESPONSE_NONE:
@@ -225,7 +223,7 @@
     
     def on_menuitem_branch_checkout_activate(self, widget):
         """ Branch/Checkout... menu handler. """
-        from checkout import CheckoutDialog
+        from bzrlib.plugins.gtk.checkout import CheckoutDialog
         checkout = CheckoutDialog(self.get_path(), self.window)
         response = checkout.run()
         if response != gtk.RESPONSE_NONE:
@@ -251,7 +249,7 @@
     
     def on_menuitem_branch_merge_activate(self, widget):
         """ Branch/Merge... menu handler. """
-        from merge import MergeDialog
+        from bzrlib.plugins.gtk.merge import MergeDialog
         
         if self.check_for_changes():
             error_dialog(_('There are local changes in the branch'),

=== modified file 'push.py'
--- a/push.py	2007-02-03 14:19:44 +0000
+++ b/push.py	2007-02-03 15:04:56 +0000
@@ -29,7 +29,7 @@
 
 from dialog import error_dialog, info_dialog, question_dialog
 
-from olive import delimiter
+from history import UrlHistory
 
 class PushDialog(gtk.Dialog):
     """ New implementation of the Push dialog. """
@@ -88,35 +88,21 @@
         self.vbox.show_all()
         
         # Build location history
+        self._history = UrlHistory(self.branch.get_config(), 'push_history')
         self._build_history()
         
     def _build_history(self):
         """ Build up the location history. """
-        config = LocationConfig(self.branch.base)
-        history = config.get_user_option('gpush_history')
-        if history is not None:
-            self._combo_model = gtk.ListStore(str)
-            for item in history.split(delimiter):
-                self._combo_model.append([ item ])
-            self._combo.set_model(self._combo_model)
-            self._combo.set_text_column(0)
+        self._combo_model = gtk.ListStore(str)
+        for item in self._history.get_entries():
+            self._combo_model.append([ item ])
+        self._combo.set_model(self._combo_model)
+        self._combo.set_text_column(0)
         
         location = self.branch.get_push_location()
         if location:
             self._combo.get_child().set_text(location)
     
-    def _add_to_history(self, location):
-        """ Add specified location to the history (if not yet added). """
-        config = LocationConfig(self.branch.base)
-        history = config.get_user_option('gpush_history')
-        if history is None:
-            config.set_user_option('gpush_history', location)
-        else:
-            h = history.split(delimiter)
-            if location not in h:
-                h.append(location)
-            config.set_user_option('gpush_history', delimiter.join(h))
-    
     def _on_test_clicked(self, widget):
         """ Test button clicked handler. """
         import re
@@ -158,7 +144,7 @@
                 revs = do_push(self.branch, overwrite=True)
             return
         
-        self._add_to_history(location)
+        self._history.add_entry(location)
         info_dialog(_('Push successful'),
                     _("%d revision(s) pushed.") % revs)
         

=== modified file 'tests/__init__.py'
--- a/tests/__init__.py	2007-02-03 10:25:09 +0000
+++ b/tests/__init__.py	2007-02-03 15:04:56 +0000
@@ -23,7 +23,7 @@
 
     loader = TestUtil.TestLoader()
 
-    testmod_names = ['test_preferences']
+    testmod_names = ['test_preferences', 'test_history']
     result.addTest(loader.loadTestsFromModuleNames(["%s.%s" % (__name__, i) for i in testmod_names]))
     return result
 

=== added file 'tests/test_history.py'
--- a/tests/test_history.py	1970-01-01 00:00:00 +0000
+++ b/tests/test_history.py	2007-02-03 15:04:56 +0000
@@ -0,0 +1,42 @@
+# Copyright (C) 2007 Jelmer Venrooij <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
+
+from bzrlib.tests import TestCaseInTempDir
+from bzrlib.plugins.gtk.history import UrlHistory
+
+from bzrlib import config
+
+class TestsUrlHistory(TestCaseInTempDir):
+    def setUp(self):
+        super(TestsUrlHistory, self).setUp()
+        self.config = config.GlobalConfig()
+
+    def test_add_entry(self):
+        """Tests whether a URL can be added to the history list.
+        The history store should only store the url, not try to
+        access it."""
+        self.history = UrlHistory(self.config, 'test_add_entry')
+        self.history.add_entry("http://foobarbla")
+
+    def test_get_entries(self):
+        self.history = UrlHistory(self.config, 'test_get_entries')
+        self.history.add_entry("http://foobar")
+        self.history.add_entry("file://bla")
+        self.assertEqual(["http://foobar", "file://bla"], self.history.get_entries())
+
+    def test_get_empty(self):
+        self.history = UrlHistory(self.config, 'test_get_empty')
+        self.assertEqual([], self.history.get_entries())



More information about the Pkg-bazaar-commits mailing list