Replaced direct filesystem read from bugdir.py with RCS mediated read.
authorW. Trevor King <wking@drexel.edu>
Mon, 24 Nov 2008 12:31:51 +0000 (07:31 -0500)
committerW. Trevor King <wking@drexel.edu>
Mon, 24 Nov 2008 12:31:51 +0000 (07:31 -0500)
Also replaced utility.FileString with StringIO() in cmdutil.py, which
allowed the removal of utility.FileString and utility.get_file.

The only remaining file().read() outside the RCS framework is the read
in utility.editor_string(), but should probably not go through the
RCS.

libbe/bugdir.py
libbe/cmdutil.py
libbe/utility.py

index 175f51836c76e8440d3770e13c5af2875a18ce9f..d3b7e617a8ffcc73cad88ff0c0eb634b91f73cc3 100644 (file)
@@ -140,13 +140,16 @@ class BugDir (list):
             return beroot
         
     def get_version(self, path=None):
+        if self.rcs_name == None:
+            # Use a temporary RCS to check the version for the first time
+            RCS = rcs.rcs_by_name("None")
+            RCS.root(self.root)
+        else:
+            RCS = self.rcs
+
         if path == None:
             path = self.get_path("version")
-        try:
-            tree_version = self.rcs.get_file_contents(path)
-        except AttributeError, e:
-            # haven't initialized rcs yet
-            tree_version = file(path, "rb").read().decode("utf-8")
+        tree_version = RCS.get_file_contents(path)
         return tree_version
 
     def set_version(self):
@@ -234,7 +237,7 @@ that the Arch RCS backend *enforces* ids with this format.""")
                 raise NoBugDir(self.get_path())
             self.settings = self._get_settings(self.get_path("settings"))
             
-            self.rcs = rcs.rcs_by_name(self.rcs_name) # set real RCS
+            self.rcs = rcs.rcs_by_name(self.rcs_name)
             if self._user_id != None: # was a user name in the settings file
                 self.save_user_id()            
             
index 55a7a721908d3a911ee0374c637cec527a615070..6d7ab0122de7ee01aa858af649e1931fdc9e67d0 100644 (file)
@@ -98,10 +98,9 @@ class CmdOptionParser(optparse.OptionParser):
                             self._long_opt.iterkeys()])
 
     def help_str(self):
-        fs = utility.FileString()
-        self.print_help(fs)
-        return fs.str
-
+        f = StringIO()
+        self.print_help(f)
+        return f.getvalue()
 
 def underlined(instring):
     """Produces a version of a string that is underlined with '='
index 7c1d10aca4f31d27fe3283283e873586aee555a9..1168580e8c4a29d53098a5363277a60b4a82dd4e 100644 (file)
@@ -21,55 +21,6 @@ import tempfile
 import shutil
 import doctest
 
-class FileString(object):
-    """Bare-bones pseudo-file class
-    
-    >>> f = FileString("me\\nyou")
-    >>> len(list(f))
-    2
-    >>> len(list(f))
-    0
-    >>> f = FileString()
-    >>> f.write("hello\\nthere")
-    >>> "".join(list(f))
-    'hello\\nthere'
-    """
-    def __init__(self, str=""):
-        object.__init__(self)
-        self.str = str
-        self._iter = None
-
-    def __iter__(self):
-        if self._iter is None:
-            self._iter = self._get_iter()
-        return self._iter
-
-    def _get_iter(self):
-        for line in self.str.splitlines(True):
-            yield line
-
-    def write(self, line):
-        self.str += line
-
-
-def get_file(f):
-    """
-    Return a file-like object from input.  This is a helper for functions that
-    can take either file or string parameters.
-
-    :param f: file or string
-    :return: a FileString if input is a string, otherwise return the imput 
-    object.
-
-    >>> isinstance(get_file(file("/dev/null")), file)
-    True
-    >>> isinstance(get_file("f"), FileString)
-    True
-    """
-    if isinstance(f, basestring):
-        return FileString(f)
-    else:
-        return f
 
 def search_parent_directories(path, filename):
     """
@@ -175,7 +126,7 @@ def editor_string(comment=None):
         os.close(fhandle)
         oldmtime = os.path.getmtime(fname)
         os.system("%s %s" % (editor, fname))
-        output = trimmed_string(file(fname, "rb").read())
+        output = trimmed_string(file(fname, "rb").read().decode("utf-8"))
         if output.rstrip('\n') == "":
             output = None
     finally: