libbe.storage.util.mapfile
¶
Serializing and deserializing dictionaries of parameters.
The serialized “mapfiles” should be clear, flat-text strings, and allow easy merging of independent/conflicting changes.
-
exception
libbe.storage.util.mapfile.
InvalidMapfileContents
(contents)¶
-
libbe.storage.util.mapfile.
generate
(map, context=6)¶ Generate a JSON mapfile content string.
See also
parse
- inverse
Examples
>>> import sys >>> sys.stdout.write(generate({})) {} >>> sys.stdout.write(generate({'q':'p'})) { "q": "p" }
The blank lines ensure that merging occurs independent of surrounding content. Because the mapfile format is also used by the
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'}, ... context=1)) { "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", "q": "the next entry" }
-
libbe.storage.util.mapfile.
parse
(contents)¶ Parse a JSON mapfile string.
See also
generate
- inverse
Examples
>>> parse('{"q": "p"}')['q'] u'p' >>> contents = generate({'a':'b', 'c':'d', 'e':'f'}) >>> dict = parse(contents) >>> dict['a'] u'b' >>> dict['c'] u'd' >>> dict['e'] u'f' >>> contents = generate({'q':u'Fran\u00e7ais'}) >>> dict = parse(contents) >>> dict['q'] u'Fran\xe7ais' >>> dict = parse('a!') Traceback (most recent call last): ... InvalidMapfileContents: Invalid JSON contents