changeset 1006:7a3b938825cd

layouts: refactor layout loading and persisting out of svnmeta.py
author David Schleimer <dschleimer@fb.com>
date Wed, 17 Apr 2013 16:14:15 -0700
parents 5bba4d1becde
children e95f4de8709a
files hgsubversion/layouts/__init__.py hgsubversion/layouts/detect.py hgsubversion/layouts/persist.py hgsubversion/svnmeta.py
diffstat 4 files changed, 41 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/layouts/__init__.py
+++ b/hgsubversion/layouts/__init__.py
@@ -10,7 +10,9 @@ NB: this has a long way to go before it 
 """
 
 import detect
+import persist
 
 __all__ = [
     "detect",
+    "persist",
     ]
--- a/hgsubversion/layouts/detect.py
+++ b/hgsubversion/layouts/detect.py
@@ -6,6 +6,8 @@ it on.
 
 """
 
+import os.path
+
 from mercurial import util as hgutil
 
 import hgsubversion.svnwrap
@@ -50,3 +52,21 @@ def layout_from_config(ui, allow_auto=Fa
     elif layout not in ('auto', 'single', 'standard'):
         raise hgutil.Abort("unknown layout '%s'" % layout)
     return layout
+
+def layout_from_file(meta_data_dir, ui=None):
+    """ 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.
+
+    """
+
+    layout = None
+    layoutfile = os.path.join(meta_data_dir, 'layout')
+    if os.path.exists(layoutfile):
+        f = open(layoutfile)
+        layout = f.read().strip()
+        f.close()
+        if ui:
+            ui.setconfig('hgsubversion', 'layout', layout)
+    return layout
new file mode 100644
--- /dev/null
+++ b/hgsubversion/layouts/persist.py
@@ -0,0 +1,16 @@
+"""Code for persisting the layout config in various locations.
+
+Basically, if you want to save the layout, this is where you should go
+to do it.
+
+"""
+
+import os.path
+
+def layout_to_file(meta_data_dir, layout):
+    """Save the given layout to a file under the given meta_data_dir"""
+
+    layoutfile = os.path.join(meta_data_dir, 'layout')
+    f = open(layoutfile, 'w')
+    f.write(layout)
+    f.close()
--- a/hgsubversion/svnmeta.py
+++ b/hgsubversion/svnmeta.py
@@ -70,13 +70,8 @@ class SVNMeta(object):
             f.close()
         else:
             self.tag_locations = tag_locations
-        if os.path.exists(self.layoutfile):
-            f = open(self.layoutfile)
-            self._layout = f.read().strip()
-            f.close()
-            self.repo.ui.setconfig('hgsubversion', 'layout', self._layout)
-        else:
-            self._layout = None
+        self._layout = layouts.detect.layout_from_file(self.meta_data_dir,
+                                                       ui=self.repo.ui)
         pickle_atomic(self.tag_locations, self.tag_locations_file)
         # ensure nested paths are handled properly
         self.tag_locations.sort()
@@ -109,9 +104,7 @@ class SVNMeta(object):
         # gets called
         if not self._layout or self._layout == 'auto':
             self._layout = layouts.detect.layout_from_config(self.repo.ui)
-            f = open(self.layoutfile, 'w')
-            f.write(self._layout)
-            f.close()
+            layouts.persist.layout_to_file(self.meta_data_dir, self._layout)
         return self._layout
 
     @property
@@ -204,10 +197,6 @@ class SVNMeta(object):
         # called tag-renames for backwards compatibility
         return os.path.join(self.meta_data_dir, 'tag-renames')
 
-    @property
-    def layoutfile(self):
-        return os.path.join(self.meta_data_dir, 'layout')
-
     def fixdate(self, date):
         if date is not None:
             date = date.replace('T', ' ').replace('Z', '').split('.')[0]