Moved FileString and get_file() into utility.py
authorAaron Bentley <abentley@panoramicfeedback.com>
Wed, 23 Mar 2005 14:34:51 +0000 (14:34 +0000)
committerAaron Bentley <abentley@panoramicfeedback.com>
Wed, 23 Mar 2005 14:34:51 +0000 (14:34 +0000)
becommands/list.py
libbe/cmdutil.py
libbe/mapfile.py
libbe/utility.py [new file with mode: 0644]

index 7e91bb369b52080b31169c4e7853d0576dfde113..4b0fa1dd235fd59bf5d7eb2df8af304b9357fc43 100644 (file)
@@ -1,6 +1,5 @@
 """List bugs"""
 from libbe import bugdir, cmdutil, names
-from libbe.mapfile import FileString
 import os
 def execute(args):
     options, args = get_parser().parse_args(args)
@@ -98,6 +97,4 @@ closed bugs assigned to you.
 """
 
 def help():
-    fs = FileString()
-    get_parser().print_help(fs)
-    return fs.str + longhelp
+    return get_parser().help_str() + longhelp
index e6d5a8f4f2f4fc6f1f7dec3320457f948a550e1e..31a542dcbbffd75e58ba3cf9f12b9ba3f849dd3e 100644 (file)
@@ -2,6 +2,7 @@ import bugdir
 import plugin
 import os
 import optparse
+import utility
 
 def unique_name(bug, bugs):
     chars = 1
@@ -111,6 +112,11 @@ class CmdOptionParser(optparse.OptionParser):
         return iter_combine([self._short_opt.iterkeys(), 
                             self._long_opt.iterkeys()])
 
+    def help_str(self):
+        fs = utility.FileString()
+        self.print_help(fs)
+        return fs.str
+
 
 def underlined(instring):
     """Produces a version of a string that is underlined with '='
index 82eadae642162760321e95380dd589f867e77f70..3c3fcdc82dda7b64ffdfc5d989920183639cd896 100644 (file)
@@ -1,17 +1,4 @@
-class FileString(object):
-    """Bare-bones pseudo-file class"""
-    def __init__(self, str=""):
-        object.__init__(self)
-        self.str = str
-
-    def __iter__(self):
-        for line in self.str.splitlines(True):
-            yield line
-
-    def write(self, line):
-        self.str += line
-
-
+import utility
 class IllegalKey(Exception):
     def __init__(self, key):
         Exception.__init__(self, 'Illegal key "%s"' % key)
@@ -27,7 +14,7 @@ def generate(f, map, context=3):
     better, because there's no chance of confusion for appends, and lines
     are unique for both key and value.
 
-    >>> f = FileString()
+    >>> f = utility.FileString()
     >>> generate(f, {"q":"p"})
     >>> f.str
     '\\n\\n\\nq=p\\n\\n\\n\\n'
@@ -68,12 +55,6 @@ def generate(f, map, context=3):
         for i in range(context):
             f.write("\n")
 
-def get_file(f):
-    if isinstance(f, basestring):
-        return FileString(f)
-    else:
-        return f
-
 def parse(f):
     """
     Parse a format-2 mapfile.
@@ -81,7 +62,7 @@ def parse(f):
     'p'
     >>> parse('\\n\\nq=\\'p\\'\\n\\n\\n\\n')['q']
     "\'p\'"
-    >>> f = FileString()
+    >>> f = utility.FileString()
     >>> generate(f, {"a":"b", "c":"d", "e":"f"})
     >>> dict = parse(f)
     >>> dict["a"]
@@ -91,7 +72,7 @@ def parse(f):
     >>> dict["e"]
     'f'
     """
-    f = get_file(f)
+    f = utility.get_file(f)
     result = {}
     for line in f:
         line = line.rstrip('\n')
@@ -106,15 +87,16 @@ def parse(f):
 def split_diff3(this, other, f):
     """Split a file or string with diff3 conflicts into two files.
 
-    :param this: The THIS file to write.  May be a FileString
-    :param other: The OTHER file to write.  May be a FileString
+    :param this: The THIS file to write.  May be a utility.FileString
+    :param other: The OTHER file to write.  May be a utility.FileString
     :param f: The file or string to split.
     :return: True if there were conflicts
 
-    >>> split_diff3(FileString(), FileString(), "a\\nb\\nc\\nd\\n")
+    >>> split_diff3(utility.FileString(), utility.FileString(), 
+    ...             "a\\nb\\nc\\nd\\n")
     False
-    >>> this = FileString()
-    >>> other = FileString()
+    >>> this = utility.FileString()
+    >>> other = utility.FileString()
     >>> split_diff3(this, other, "<<<<<<< values1\\nstatus=closed\\n=======\\nstatus=closedd\\n>>>>>>> values2\\n")
     True
     >>> this.str
@@ -122,7 +104,7 @@ def split_diff3(this, other, f):
     >>> other.str
     'status=closedd\\n'
     """
-    f = get_file(f)
+    f = utility.get_file(f)
     this_active = True
     other_active = True
     conflicts = False
@@ -164,8 +146,8 @@ def split_diff3_str(f):
     >>> result[0]
     'a\\nb\\nc\\nd\\n'
     """
-    this = FileString()
-    other = FileString()
+    this = utility.FileString()
+    other = utility.FileString()
     if split_diff3(this, other, f):
         return (this.str, other.str)
     else:
diff --git a/libbe/utility.py b/libbe/utility.py
new file mode 100644 (file)
index 0000000..a67037d
--- /dev/null
@@ -0,0 +1,51 @@
+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
+
+