Commits

guillermooo committed 1be398b

manage servers in a class

  • Participants
  • Parent commits d2bf171

Comments (0)

Files changed (2)

 import sublime
 import os
 import contextlib
+import client
 
 
 @contextlib.contextmanager
 
 def is_flag_set(flags, which_one):
     return flags & which_one == which_one
+
+
+# TODO(guillermooo): Subclass dict instead?
+class HgServers(object):
+    def __getitem__(self, key):
+        try:
+            return self._select_server(key)
+        except EnvironmentError, e:
+            sublime.status_message("SublimeHg: " + e.message)
+            print "SublimeHg: " + e.message
+            return None
+
+    def _select_server(self, current_path=None):
+        """Finds an existing server for the given path. If none is found, it
+        creates one for the path.
+        """
+        v = sublime.active_window().active_view()
+        repo_root = find_hg_root(current_path or v.file_name())
+        if not repo_root:
+            raise EnvironmentError("No repo found here.")
+        if not repo_root in self.__dict__:
+            server = self._start_server(repo_root)
+            self.__dict__[repo_root] = server
+        return self.__dict__[repo_root]
+
+    def _start_server(self, repo_root):
+        """Starts a new Mercurial command server.
+        """
+        # By default, hglib uses 'hg'. User might need to change that on
+        # Windows, for example.
+        hg_bin = get_hg_exe_name()
+        server = client.CmdServerClient(hg_bin=hg_bin, repo_root=repo_root)
+        return server
 import subprocess
 import os
 
-from shglib import client
 from shglib import commands
 from shglib import utils
 from shglib.commands import AmbiguousCommandError
 from shglib.commands import RUN_IN_OWN_CONSOLE
 
 
-VERSION = '12.6.8'
+VERSION = '12.7.18'
 
 
 CMD_LINE_SYNTAX = 'Packages/SublimeHg/Support/SublimeHg Command Line.hidden-tmLanguage'
 # Globals
 #------------------------------------------------------------------------------
 # Holds the existing server so it doesn't have to be reloaded.
-running_servers = {}
+running_servers = utils.HgServers()
 # Helps find the file where the cmdline should be restored.
 recent_file_name = None
 #==============================================================================
 
 
-def start_server(repo_root):
-    """Starts a new Mercurial command server.
-    """
-    # By default, hglib uses 'hg'. User might need to change that on
-    # Windows, for example.
-    hg_bin = utils.get_hg_exe_name()
-    server = client.CmdServerClient(hg_bin=hg_bin, repo_root=repo_root)
-    global running_servers
-    running_servers[repo_root] = server
-    return server
-
-
-def select_server(current_path=None):
-    """Finds an existing server for the given path. If none is found, it
-    creates one for the path.
-    """
-    v = sublime.active_window().active_view()
-    repo_root = utils.find_hg_root(current_path or v.file_name())
-    if not repo_root:
-        raise EnvironmentError("No repo found here.")
-    if not repo_root in running_servers:
-        return start_server(repo_root)
-    else:
-        return running_servers[repo_root]
-
-
 def run_hg_cmd(server, cmd_string):
     """Runs a Mercurial command through the given command server.
     """
         self.display_name = self.display_name or s.split(' ')[0]
 
         try:
-            hgs = select_server(current_path=self.cwd)
+            hgs = running_servers[self.cwd]
         except EnvironmentError, e:
             sublime.status_message("SublimeHg: " + str(e))
             return