ui: represent paths as classes
authorGregory Szorc <gregory.szorc@gmail.com>
Sat, 07 Feb 2015 12:42:10 -0800
changeset 24250 9c32eea2ca04
parent 24249 5058e6962fcc
child 24251 a330660b3d71
ui: represent paths as classes Many have long wanted for paths to have expanded functionality and flexibility. In order to make that transition possible, we need to start representing paths as something more than simple strings. This patch introduces two classes: 1) "path" for representing a single path instance 2) "paths" for representing a collection of "paths" Since we don't like patches that introduce new code without any consumers, we convert ui.expandpath() to use the new APIs internally. Upcoming patches will start exposing "path" instances to consumers that currently interface with string paths. The new "paths" attribute of ui is populated from config data the first time it is accessed. Since it isn't updated when the configs are modified, this could lead to some inaccurate caching behavior. It shouldn't be an issue, as paths information is typically not accessed until command dispatch, which occurs after the repository config and extensions have been loaded. Time will tell if we need to refresh paths information when the underlying config changes.
mercurial/ui.py
--- a/mercurial/ui.py	Tue Mar 10 13:40:14 2015 -0400
+++ b/mercurial/ui.py	Sat Feb 07 12:42:10 2015 -0800
@@ -531,10 +531,14 @@
         if util.hasscheme(loc) or os.path.isdir(os.path.join(loc, '.hg')):
             return loc
 
-        path = self.config('paths', loc)
-        if not path and default is not None:
-            path = self.config('paths', default)
-        return path or loc
+        p = self.paths.getpath(loc, default=default)
+        if p:
+            return p.loc
+        return loc
+
+    @util.propertycache
+    def paths(self):
+        return paths(self)
 
     def pushbuffer(self, error=False):
         """install a buffer to capture standard output of the ui object
@@ -923,3 +927,48 @@
         ui.write(ui.label(s, 'label')).
         '''
         return msg
+
+class paths(dict):
+    """Represents a collection of paths and their configs.
+
+    Data is initially derived from ui instances and the config files they have
+    loaded.
+    """
+    def __init__(self, ui):
+        dict.__init__(self)
+
+        for name, loc in ui.configitems('paths'):
+            # No location is the same as not existing.
+            if not loc:
+                continue
+            self[name] = path(name, rawloc=loc)
+
+    def getpath(self, name, default=None):
+        """Return a ``path`` for the specified name, falling back to a default.
+
+        Returns the first of ``name`` or ``default`` that is present, or None
+        if neither is present.
+        """
+        try:
+            return self[name]
+        except KeyError:
+            if default is not None:
+                try:
+                    return self[default]
+                except KeyError:
+                    pass
+
+        return None
+
+class path(object):
+    """Represents an individual path and its configuration."""
+
+    def __init__(self, name, rawloc=None):
+        """Construct a path from its config options.
+
+        ``name`` is the symbolic name of the path.
+        ``rawloc`` is the raw location, as defined in the config.
+        """
+        self.name = name
+        # We'll do more intelligent things with rawloc in the future.
+        self.loc = rawloc