-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)
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'
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.
'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"]
>>> dict["e"]
'f'
"""
- f = get_file(f)
+ f = utility.get_file(f)
result = {}
for line in f:
line = line.rstrip('\n')
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
>>> other.str
'status=closedd\\n'
"""
- f = get_file(f)
+ f = utility.get_file(f)
this_active = True
other_active = True
conflicts = False
>>> 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:
--- /dev/null
+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
+
+