aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/mapfile.py
diff options
context:
space:
mode:
authorW. Trevor King <wking@drexel.edu>2008-11-24 07:09:03 -0500
committerW. Trevor King <wking@drexel.edu>2008-11-24 07:09:03 -0500
commit63a7726eba738fe2ed340027039ba655ff91898a (patch)
tree5fab6648ee4bcbe14c6413f46bfcc683cb9f34f2 /libbe/mapfile.py
parentd80720fcad22215cdbd1f2b39434945364ba11d5 (diff)
downloadbugseverywhere-63a7726eba738fe2ed340027039ba655ff91898a.tar.gz
Added 'allow_no_rcs' flag to RCS file system access methods.
Now mapfile access has fewer special cases, and there is less redundant rcs.add/update code.
Diffstat (limited to 'libbe/mapfile.py')
-rw-r--r--libbe/mapfile.py84
1 files changed, 34 insertions, 50 deletions
diff --git a/libbe/mapfile.py b/libbe/mapfile.py
index 9a7fa8b..559d713 100644
--- a/libbe/mapfile.py
+++ b/libbe/mapfile.py
@@ -29,28 +29,27 @@ class IllegalValue(Exception):
Exception.__init__(self, 'Illegal value "%s"' % value)
self.value = value
-def generate(f, map, context=3):
- """Generate a format-2 mapfile. This is a simpler format, but should merge
- better, because there's no chance of confusion for appends, and lines
- are unique for both key and value.
+def generate(map, context=3):
+ """Generate a format-2 mapfile content string. This is a simpler
+ format, but should merge better, because there's no chance of
+ confusion for appends, and lines are unique for both key and
+ value.
- >>> f = utility.FileString()
- >>> generate(f, {"q":"p"})
- >>> f.str
+ >>> generate({"q":"p"})
'\\n\\n\\nq=p\\n\\n\\n\\n'
- >>> generate(f, {"q=":"p"})
+ >>> generate({"q=":"p"})
Traceback (most recent call last):
IllegalKey: Illegal key "q="
- >>> generate(f, {"q\\n":"p"})
+ >>> generate({"q\\n":"p"})
Traceback (most recent call last):
IllegalKey: Illegal key "q\\n"
- >>> generate(f, {"":"p"})
+ >>> generate({"":"p"})
Traceback (most recent call last):
IllegalKey: Illegal key ""
- >>> generate(f, {">q":"p"})
+ >>> generate({">q":"p"})
Traceback (most recent call last):
IllegalKey: Illegal key ">q"
- >>> generate(f, {"q":"p\\n"})
+ >>> generate({"q":"p\\n"})
Traceback (most recent call last):
IllegalValue: Illegal value "p\\n"
"""
@@ -68,63 +67,48 @@ def generate(f, map, context=3):
if "\n" in map[key]:
raise IllegalValue(map[key].encode('string_escape'))
+ lines = []
for key in keys:
for i in range(context):
- f.write("\n")
- f.write("%s=%s\n" % (key.encode("utf-8"), map[key].encode("utf-8")))
+ lines.append("")
+ lines.append("%s=%s" % (key, map[key]))
for i in range(context):
- f.write("\n")
+ lines.append("")
+ return '\n'.join(lines) + '\n'
-def parse(f):
+def parse(contents):
"""
- Parse a format-2 mapfile.
+ Parse a format-2 mapfile string.
>>> parse('\\n\\n\\nq=p\\n\\n\\n\\n')['q']
- u'p'
+ 'p'
>>> parse('\\n\\nq=\\'p\\'\\n\\n\\n\\n')['q']
- u"\'p\'"
- >>> f = utility.FileString()
- >>> generate(f, {"a":"b", "c":"d", "e":"f"})
- >>> dict = parse(f)
+ "\'p\'"
+ >>> contents = generate({"a":"b", "c":"d", "e":"f"})
+ >>> dict = parse(contents)
>>> dict["a"]
- u'b'
+ 'b'
>>> dict["c"]
- u'd'
+ 'd'
>>> dict["e"]
- u'f'
+ 'f'
"""
- f = utility.get_file(f)
result = {}
- for line in f:
- line = line.decode("utf-8").rstrip('\n')
+ for line in contents.splitlines():
+ line = line.rstrip('\n')
if len(line) == 0:
continue
- name,value = [f for f in line.split('=', 1)]
+ name,value = [field for field in line.split('=', 1)]
assert not result.has_key(name)
result[name] = value
return result
-def map_save(rcs, path, map):
+def map_save(rcs, path, map, allow_no_rcs=False):
"""Save the map as a mapfile to the specified path"""
- add = not os.path.exists(path)
- output = file(path, "wb")
- generate(output, map)
- output.close()
- if add:
- rcs.add(path)
- else:
- rcs.update(path)
+ contents = generate(map)
+ rcs.set_file_contents(path, contents, allow_no_rcs)
-class NoSuchFile(Exception):
- def __init__(self, pathname):
- Exception.__init__(self, "No such file: %s" % pathname)
-
-
-def map_load(path):
- try:
- return parse(file(path, "rb"))
- except IOError, e:
- if e.errno != errno.ENOENT:
- raise e
- raise NoSuchFile(path)
+def map_load(rcs, path, allow_no_rcs=False):
+ contents = rcs.get_file_contents(path, allow_no_rcs=allow_no_rcs)
+ return parse(contents)
suite = doctest.DocTestSuite()