Commits

David Schleimer committed 7a3b938

layouts: refactor layout loading and persisting out of svnmeta.py

Comments (0)

Files changed (4)

hgsubversion/layouts/__init__.py

 """
 
 import detect
+import persist
 
 __all__ = [
     "detect",
+    "persist",
     ]

hgsubversion/layouts/detect.py

 
 """
 
+import os.path
+
 from mercurial import util as hgutil
 
 import hgsubversion.svnwrap
     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

hgsubversion/layouts/persist.py

+"""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()

hgsubversion/svnmeta.py

             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()
         # 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
         # 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]