progress: move the singleton logic to the ui module
authorPierre-Yves David <pierre-yves.david@fb.com>
Sun, 07 Jun 2015 17:26:34 -0700
changeset 25498 7a5335ed7e1a
parent 25497 93b8b0049932
child 25499 0fa964d6fd48
progress: move the singleton logic to the ui module The use of a singleton for all of progress handling is debatable (because config may vary). However this is how the extension has been doing it so far. We move that code into the ui module because this is where is should belong when progress is moved into core.
hgext/progress.py
mercurial/ui.py
tests/test-module-imports.t
--- a/hgext/progress.py	Sun Jun 07 17:19:20 2015 -0700
+++ b/hgext/progress.py	Sun Jun 07 17:26:34 2015 -0700
@@ -36,11 +36,9 @@
 """
 
 from mercurial import progress
-
-_singleton = None
+from mercurial import ui as uimod
 
 def uisetup(ui):
-    global _singleton
     class progressui(ui.__class__):
         _progbar = None
 
@@ -73,9 +71,7 @@
             # we instantiate one globally-shared progress bar to avoid
             # competing progress bars when multiple UI objects get created
             if not progressui._progbar:
-                if _singleton is None:
-                    _singleton = progress.progbar(ui)
-                progressui._progbar = _singleton
+                progressui._progbar = uimod.getprogbar(ui)
 
 def reposetup(ui, repo):
     uisetup(repo.ui)
--- a/mercurial/ui.py	Sun Jun 07 17:19:20 2015 -0700
+++ b/mercurial/ui.py	Sun Jun 07 17:26:34 2015 -0700
@@ -7,7 +7,7 @@
 
 from i18n import _
 import errno, getpass, os, socket, sys, tempfile, traceback
-import config, scmutil, util, error, formatter
+import config, scmutil, util, error, formatter, progress
 from node import hex
 
 samplehgrcs = {
@@ -983,3 +983,15 @@
         self.name = name
         # We'll do more intelligent things with rawloc in the future.
         self.loc = rawloc
+
+# we instantiate one globally shared progress bar to avoid
+# competing progress bars when multiple UI objects get created
+_progresssingleton = None
+
+def getprogbar(ui):
+    global _progresssingleton
+    if _progresssingleton is None:
+        # passing 'ui' object to the singleton is fishy,
+        # this is how the extension used to work but feel free to rework it.
+        _progresssingleton = progress.progbar(ui)
+    return _progresssingleton
--- a/tests/test-module-imports.t	Sun Jun 07 17:19:20 2015 -0700
+++ b/tests/test-module-imports.t	Sun Jun 07 17:26:34 2015 -0700
@@ -31,7 +31,7 @@
      relative:  config, error, templatefilters, templatekw, util
   mercurial/ui.py mixed imports
      stdlib:    formatter
-     relative:  config, error, scmutil, util
+     relative:  config, error, progress, scmutil, util
   Import cycle: mercurial.cmdutil -> mercurial.context -> mercurial.subrepo -> mercurial.cmdutil
   Import cycle: hgext.largefiles.basestore -> hgext.largefiles.localstore -> hgext.largefiles.basestore
   Import cycle: mercurial.commands -> mercurial.commandserver -> mercurial.dispatch -> mercurial.commands