aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Bentley <abentley@panoramicfeedback.com>2005-03-23 14:34:51 +0000
committerAaron Bentley <abentley@panoramicfeedback.com>2005-03-23 14:34:51 +0000
commit43d8c19ae299bab2cba0f29b560396ba6d8bd1a5 (patch)
tree1b8edf5f70e875f300b9157ef1c6b503eceae9b9
parentf9e8f7bb4360d473073b59e4dcb9e1a38d7e0d56 (diff)
downloadbugseverywhere-43d8c19ae299bab2cba0f29b560396ba6d8bd1a5.tar.gz
Moved FileString and get_file() into utility.py
-rw-r--r--becommands/list.py5
-rw-r--r--libbe/cmdutil.py6
-rw-r--r--libbe/mapfile.py44
-rw-r--r--libbe/utility.py51
4 files changed, 71 insertions, 35 deletions
diff --git a/becommands/list.py b/becommands/list.py
index 7e91bb3..4b0fa1d 100644
--- a/becommands/list.py
+++ b/becommands/list.py
@@ -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
diff --git a/libbe/cmdutil.py b/libbe/cmdutil.py
index e6d5a8f..31a542d 100644
--- a/libbe/cmdutil.py
+++ b/libbe/cmdutil.py
@@ -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 '='
diff --git a/libbe/mapfile.py b/libbe/mapfile.py
index 82eadae..3c3fcdc 100644
--- a/libbe/mapfile.py
+++ b/libbe/mapfile.py
@@ -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
index 0000000..a67037d
--- /dev/null
+++ b/libbe/utility.py
@@ -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
+
+