aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/utility.py
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 /libbe/utility.py
parentf9e8f7bb4360d473073b59e4dcb9e1a38d7e0d56 (diff)
downloadbugseverywhere-43d8c19ae299bab2cba0f29b560396ba6d8bd1a5.tar.gz
Moved FileString and get_file() into utility.py
Diffstat (limited to 'libbe/utility.py')
-rw-r--r--libbe/utility.py51
1 files changed, 51 insertions, 0 deletions
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
+
+