gump-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosim...@apache.org
Subject svn commit: r124041 - /gump/branches/Dec04MajorCleanup/pygump/python/gump/engine/modeller.py
Date Mon, 03 Jan 2005 23:50:15 GMT
Author: leosimons
Date: Mon Jan  3 15:50:14 2005
New Revision: 124041

URL: http://svn.apache.org/viewcvs?view=rev&rev=124041
Log:
All of pygump should use a stream abstraction instead of working on files directly if possible.
Easy enough, and it allows, among other things, much better unit testing. Simply scan for
'open(' or 'file(' :-D
Modified:
   gump/branches/Dec04MajorCleanup/pygump/python/gump/engine/modeller.py

Modified: gump/branches/Dec04MajorCleanup/pygump/python/gump/engine/modeller.py
Url: http://svn.apache.org/viewcvs/gump/branches/Dec04MajorCleanup/pygump/python/gump/engine/modeller.py?view=diff&rev=124041&p1=gump/branches/Dec04MajorCleanup/pygump/python/gump/engine/modeller.py&r1=124040&p2=gump/branches/Dec04MajorCleanup/pygump/python/gump/engine/modeller.py&r2=124041
==============================================================================
--- gump/branches/Dec04MajorCleanup/pygump/python/gump/engine/modeller.py	(original)
+++ gump/branches/Dec04MajorCleanup/pygump/python/gump/engine/modeller.py	Mon Jan  3 15:50:14
2005
@@ -27,6 +27,13 @@
 
 from gump.model import *
 
+import types # TODO: move into utility module
+try:
+    _StringTypes = [types.StringType, types.UnicodeType]
+except AttributeError:
+    _StringTypes = [types.StringType]
+
+
 class ModellerError(Exception):
     """Generic error thrown for all internal Modeller module exceptions."""
     pass
@@ -70,7 +77,7 @@
     
     Of course, other parts of the modeller package are not so tolerant!
     """
-    def __init__(self, log, vfs=None, mergefile=None, dropfile=None):
+    def __init__(self, log, vfs=None, merge_file_or_stream=None, drop_file_or_stream=None):
         """
         Create a new Loader.
 
@@ -82,15 +89,27 @@
                      any hrefs.
             - mergefile -- the full path to the file to write the merged
                            workspace xml to. If None, no such file will be
-                           written.
+                           written. Alternatively can be a stream.
             - dropfile -- the full path to the file to write the xml
                            describing dropped projects to. If None, no such
-                           file will be written.
+                           file will be written. Alternatively can be a
+                           stream.
         """
         self.log = log
         self.vfs = vfs
-        self.mergefile = mergefile
-        self.dropfile = dropfile
+        self.mergestream = self._open_file_or_stream(merge_file_or_stream)
+        self.dropstream = self._open_file_or_stream(drop_file_or_stream)
+    
+    def _open_file_or_stream(self, file_or_stream): # TODO: move into utility module
+        """Utility for accepting both filenames and streams.
+        
+        If the argument is a string, attempts to open the specified file and
+        return a reference to the open file. Otherwise, returns the argument.
+        """
+        if type(file_or_stream) in _StringTypes:
+            return open(file_or_stream, 'w')
+        else:
+            return file_or_stream
 
     def get_workspace_tree(self, workspace):
         """Parse the provided workspace, then resolve all hrefs.
@@ -290,20 +309,18 @@
         Also writes an XML file detailing any projects and modules that were
         dropped because of a HREF resolution issue.
         """
-        if self.mergefile:
-            merged = open(self.mergefile, 'w')
-            merged.write( wsdom.toprettyxml() )
-            merged.close()
+        if self.mergestream:
+            self.mergestream.write( wsdom.toprettyxml() )
+            self.mergestream.close()
         
-        if self.dropfile and len(dropped_nodes) > 0:
+        if self.dropstream and len(dropped_nodes) > 0:
             impl = dom.getDOMImplementation()
             dropdoc = impl.createDocument(None, "dropped-projects-and-modules", None)
             dropdocroot = dropdoc.documentElement
             for node in dropped_nodes:
                 dropdocroot.appendChild(node)
-            dropped = open(self.dropfile, 'w')
-            dropped.write( dropdoc.toprettyxml() )
-            dropped.close()        
+            self.dropstream.write( dropdoc.toprettyxml() )
+            self.dropstream.close()        
 
 class Objectifier:
     """Turns DOM workspace into Pythonified workspace."""

Mime
View raw message