changeset 1265:4744b7bfa476

layouts: change constructor to take a meta object This change is another step forward to centralize subversion configuration options and help refactor. I couldn't find an easier way to split up this change since there are many interdependent function calls. There is no functionality change in this patch, only renaming ui -> meta.
author Sean Farley <sean.michael.farley@gmail.com>
date Mon, 24 Mar 2014 11:20:49 -0500
parents eded1e736a7e
children 5ee3e5030a35
files hgsubversion/layouts/__init__.py hgsubversion/layouts/base.py hgsubversion/layouts/custom.py hgsubversion/layouts/detect.py hgsubversion/layouts/standard.py hgsubversion/svncommands.py hgsubversion/svnmeta.py hgsubversion/wrappers.py
diffstat 8 files changed, 34 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/layouts/__init__.py
+++ b/hgsubversion/layouts/__init__.py
@@ -31,7 +31,7 @@ NAME_TO_CLASS = {
 }
 
 
-def layout_from_name(name, ui):
+def layout_from_name(name, meta):
     """Returns a layout module given the layout name
 
     You should use one of the layout.detect.* functions to get the
@@ -40,5 +40,5 @@ def layout_from_name(name, ui):
     """
 
     if name not in NAME_TO_CLASS:
-        raise hgutil.Abort('Unknown hgsubversion layout: %s' %name)
-    return NAME_TO_CLASS[name](ui)
+        raise hgutil.Abort('Unknown hgsubversion layout: %s' % name)
+    return NAME_TO_CLASS[name](meta)
--- a/hgsubversion/layouts/base.py
+++ b/hgsubversion/layouts/base.py
@@ -9,8 +9,8 @@ from mercurial import util as hgutil
 
 class BaseLayout(object):
 
-    def __init__(self, ui):
-        self.ui = ui
+    def __init__(self, meta):
+        self.meta = meta
 
     def __unimplemented(self, method_name):
         raise NotImplementedError(
--- a/hgsubversion/layouts/custom.py
+++ b/hgsubversion/layouts/custom.py
@@ -12,13 +12,13 @@ import base
 
 class CustomLayout(base.BaseLayout):
 
-    def __init__(self, ui):
-        base.BaseLayout.__init__(self, ui)
+    def __init__(self, meta):
+        base.BaseLayout.__init__(self, meta)
 
         self.svn_to_hg = {}
         self.hg_to_svn = {}
 
-        for hg_branch, svn_path in ui.configitems('hgsubversionbranch'):
+        for hg_branch, svn_path in meta.ui.configitems('hgsubversionbranch'):
 
             hg_branch = hg_branch.strip()
             if hg_branch == 'default' or not hg_branch:
--- a/hgsubversion/layouts/detect.py
+++ b/hgsubversion/layouts/detect.py
@@ -12,7 +12,7 @@ from mercurial import util as hgutil
 
 import __init__ as layouts
 
-def layout_from_subversion(svn, revision=None, ui=None):
+def layout_from_subversion(svn, revision=None, meta=None):
     """ Guess what layout to use based on directories under the svn root.
 
     This is intended for use during bootstrapping.  It guesses which
@@ -39,10 +39,10 @@ def layout_from_subversion(svn, revision
         layout = 'standard'
     else:
         layout = 'single'
-    ui.setconfig('hgsubversion', 'layout', layout)
+    meta.ui.setconfig('hgsubversion', 'layout', layout)
     return layout
 
-def layout_from_config(ui, allow_auto=False):
+def layout_from_config(meta, allow_auto=False):
     """ Load the layout we are using based on config
 
     We will read the config from the ui object.  Pass allow_auto=True
@@ -51,19 +51,15 @@ def layout_from_config(ui, allow_auto=Fa
     detect the layout as auto.
     """
 
-    layout = ui.config('hgsubversion', 'layout', default='auto')
+    layout = meta.ui.config('hgsubversion', 'layout', default='auto')
     if layout == 'auto' and not allow_auto:
         raise hgutil.Abort('layout not yet determined')
     elif layout not in layouts.NAME_TO_CLASS and layout != 'auto':
         raise hgutil.Abort("unknown layout '%s'" % layout)
     return layout
 
-def layout_from_file(metapath, ui=None):
+def layout_from_file(meta):
     """ Load the layout in use from the metadata file.
-
-    If you pass the ui arg, we will also write the layout to the
-    config for that ui.
-
     """
 
     # import late to avoid trouble when running the test suite
@@ -72,13 +68,12 @@ def layout_from_file(metapath, ui=None):
     except ImportError:
         from hgsubversion import util
 
-    layoutfile = os.path.join(metapath, 'layout')
-    layout = util.load(layoutfile)
-    if layout is not None and ui:
-        ui.setconfig('hgsubversion', 'layout', layout)
+    layout = util.load(meta.layout_file)
+    if layout:
+        meta.ui.setconfig('hgsubversion', 'layout', layout)
     return layout
 
-def layout_from_commit(subdir, revpath, branch, ui):
+def layout_from_commit(subdir, revpath, branch, meta):
     """ Guess what the layout is based existing commit info
 
     Specifically, this compares the subdir for the repository and the
@@ -95,7 +90,7 @@ def layout_from_commit(subdir, revpath, 
 
     candidates = set()
     for layout in layouts.NAME_TO_CLASS:
-        layoutobj = layouts.layout_from_name(layout, ui)
+        layoutobj = layouts.layout_from_name(layout, meta)
         try:
             remotepath = layoutobj.remotepath(branch, subdir)
         except KeyError:
@@ -106,7 +101,7 @@ def layout_from_commit(subdir, revpath, 
     if len(candidates) == 1:
         return candidates.pop()
     elif candidates:
-        config_layout = layout_from_config(ui, allow_auto=True)
+        config_layout = layout_from_config(meta, allow_auto=True)
         if config_layout in candidates:
             return config_layout
 
--- a/hgsubversion/layouts/standard.py
+++ b/hgsubversion/layouts/standard.py
@@ -5,18 +5,18 @@ import base
 class StandardLayout(base.BaseLayout):
     """The standard trunk, branches, tags layout"""
 
-    def __init__(self, ui):
-        base.BaseLayout.__init__(self, ui)
+    def __init__(self, meta):
+        base.BaseLayout.__init__(self, meta)
 
         self._tag_locations = None
 
-        self._branch_dir = ui.config('hgsubversion', 'branchdir', 'branches')
+        self._branch_dir = self.meta.ui.config('hgsubversion', 'branchdir', 'branches')
         if self._branch_dir[0] == '/':
             self._branch_dir = self._branch_dir[1:]
         if self._branch_dir[-1] != '/':
             self._branch_dir += '/'
 
-        self._infix = ui.config('hgsubversion', 'infix', '').strip('/')
+        self._infix = self.meta.ui.config('hgsubversion', 'infix', '').strip('/')
         if self._infix:
             self._infix = '/' + self._infix
 
@@ -69,9 +69,9 @@ class StandardLayout(base.BaseLayout):
             self._tag_locations = util.load(tag_locations_file)
 
             if not self._tag_locations:
-                self._tag_locations = self.ui.configlist('hgsubversion',
-                                                        'tagpaths',
-                                                        ['tags'])
+                self._tag_locations = self.meta.ui.configlist('hgsubversion',
+                                                              'tagpaths',
+                                                              ['tags'])
             util.dump(self._tag_locations, tag_locations_file)
 
             # ensure nested paths are handled properly
--- a/hgsubversion/svncommands.py
+++ b/hgsubversion/svncommands.py
@@ -189,11 +189,11 @@ def _buildmeta(ui, repo, args, partial=F
 
         if layout is None:
             layout = layouts.detect.layout_from_commit(subdir, revpath,
-                                                       ctx.branch(), ui)
-            existing_layout = layouts.detect.layout_from_file(meta.metapath)
+                                                       ctx.branch(), meta)
+            existing_layout = layouts.detect.layout_from_file(meta)
             if layout != existing_layout:
                 util.dump(layout, meta.layout_file)
-            layoutobj = layouts.layout_from_name(layout, ui)
+            layoutobj = layouts.layout_from_name(layout, meta)
         elif layout == 'single':
             assert (subdir or '/') == revpath, ('Possible layout detection'
                                                 ' defect in replay')
--- a/hgsubversion/svnmeta.py
+++ b/hgsubversion/svnmeta.py
@@ -59,8 +59,7 @@ class SVNMeta(object):
         # misc
         self.branches = util.load(self.branch_info_file) or {}
         self.prevbranches = dict(self.branches)
-        self._layout = layouts.detect.layout_from_file(self.metapath,
-                                                       ui=self.repo.ui)
+        self._layout = layouts.detect.layout_from_file(self)
 
     def _get_cachedconfig(self, name, filename, configname, default):
         """Return a cached value for a config option. If the cache is uninitialized
@@ -147,14 +146,14 @@ class SVNMeta(object):
         # resolved into something other than auto before this ever
         # gets called
         if not self._layout or self._layout == 'auto':
-            self._layout = layouts.detect.layout_from_config(self.repo.ui)
+            self._layout = layouts.detect.layout_from_config(self)
             util.dump(self._layout, self.layout_file)
         return self._layout
 
     @property
     def layoutobj(self):
         if not self._layoutobj:
-            self._layoutobj = layouts.layout_from_name(self.layout, self.ui)
+            self._layoutobj = layouts.layout_from_name(self.layout, self)
         return self._layoutobj
 
     @property
--- a/hgsubversion/wrappers.py
+++ b/hgsubversion/wrappers.py
@@ -398,11 +398,11 @@ def pull(repo, source, heads=[], force=F
 
         stopat_rev = util.parse_revnum(svn, checkout)
 
-        layout = layouts.detect.layout_from_config(repo.ui, allow_auto=True)
+        layout = layouts.detect.layout_from_config(meta, allow_auto=True)
         if layout == 'auto':
             layout = layouts.detect.layout_from_subversion(svn,
                                                            (stopat_rev or None),
-                                                           repo.ui)
+                                                           meta)
             repo.ui.note('using %s layout\n' % layout)
 
         branch = repo.ui.config('hgsubversion', 'branch')