aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/storage/util
diff options
context:
space:
mode:
authorW. Trevor King <wking@tremily.us>2012-10-26 13:08:54 -0400
committerW. Trevor King <wking@tremily.us>2012-10-26 13:09:46 -0400
commitb4d97db6f3fd6c3f4f19c4a475f064e305a163f6 (patch)
treeb4f7c16cef4c6c7fcc875a1f964b86f5ef7989da /libbe/storage/util
parent0e0f995a5e7c6f93e8f0e0cf70fb559e482ba19f (diff)
downloadbugseverywhere-b4d97db6f3fd6c3f4f19c4a475f064e305a163f6.tar.gz
storage:util:mapfile: add `context` argument to generate()
Diffstat (limited to 'libbe/storage/util')
-rw-r--r--libbe/storage/util/mapfile.py42
1 files changed, 19 insertions, 23 deletions
diff --git a/libbe/storage/util/mapfile.py b/libbe/storage/util/mapfile.py
index 71e5b0d..1c37e3f 100644
--- a/libbe/storage/util/mapfile.py
+++ b/libbe/storage/util/mapfile.py
@@ -39,7 +39,7 @@ class InvalidMapfileContents (Exception):
self.contents = contents
-def generate(map):
+def generate(map, context=6):
"""Generate a JSON mapfile content string.
Examples
@@ -64,33 +64,29 @@ def generate(map):
<BLANKLINE>
<BLANKLINE>
}
- >>> generate({'q':u'Fran\u00e7ais'})
- '{\\n\\n\\n\\n\\n\\n\\n "q": "Fran\\\\u00e7ais"\\n\\n\\n\\n\\n\\n\\n}\\n'
- >>> generate({'q':u'hello'})
- '{\\n\\n\\n\\n\\n\\n\\n "q": "hello"\\n\\n\\n\\n\\n\\n\\n}\\n'
+
+ The blank lines ensure that merging occurs independent of
+ surrounding content. Because the mapfile format is also used by
+ the :py:mod:`~libbe.command.serve_commands` where merging is not
+ important, the amount of context is controllable.
+
+ >>> sys.stdout.write(generate({'q':u'Fran\u00e7ais'}, context=0))
+ {
+ "q": "Fran\\u00e7ais"
+ }
+ >>> sys.stdout.write(generate({'q':u'hello'}, context=0))
+ {
+ "q": "hello"
+ }
>>> sys.stdout.write(generate(
- ... {'p':'really long line\\n'*10, 'q': 'the next entry'}))
+ ... {'p':'really long line\\n'*10, 'q': 'the next entry'},
+ ... context=1))
{
<BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
"p": "really long line\\nreally long line\\nreally long line\\nreally long line\\nreally long line\\nreally long line\\nreally long line\\nreally long line\\nreally long line\\nreally long line\\n",
<BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
"q": "the next entry"
<BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
- <BLANKLINE>
}
See Also
@@ -98,8 +94,8 @@ def generate(map):
parse : inverse
"""
lines = json.dumps(map, sort_keys=True, indent=4).splitlines()
- # add blank lines for context-less merging
- return '\n\n\n\n\n\n\n'.join(lines) + '\n'
+ sep = '\n' * (1 + context)
+ return sep.join(lines) + '\n'
def parse(contents):
"""Parse a JSON mapfile string.