1 # Copyright (C) 2005-2010 Aaron Bentley and Panometrics, Inc.
2 # Gianluca Montecchi <gian@grys.it>
3 # W. Trevor King <wking@drexel.edu>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 Provide a means of saving and loading dictionaries of parameters. The
21 saved "mapfiles" should be clear, flat-text files, and allow easy merging of
22 independent/conflicting changes.
31 if libbe.TESTING == True:
35 class IllegalKey(Exception):
36 def __init__(self, key):
37 Exception.__init__(self, 'Illegal key "%s"' % key)
40 class IllegalValue(Exception):
41 def __init__(self, value):
42 Exception.__init__(self, 'Illegal value "%s"' % value)
45 class InvalidMapfileContents(Exception):
46 def __init__(self, contents):
47 Exception.__init__(self, 'Invalid YAML contents')
48 self.contents = contents
51 """Generate a YAML mapfile content string.
52 >>> generate({'q':'p'})
54 >>> generate({'q':u'Fran\u00e7ais'})
55 'q: Fran\\xc3\\xa7ais\\n\\n'
56 >>> generate({'q':u'hello'})
58 >>> generate({'q=':'p'})
59 Traceback (most recent call last):
60 IllegalKey: Illegal key "q="
61 >>> generate({'q:':'p'})
62 Traceback (most recent call last):
63 IllegalKey: Illegal key "q:"
64 >>> generate({'q\\n':'p'})
65 Traceback (most recent call last):
66 IllegalKey: Illegal key "q\\n"
67 >>> generate({'':'p'})
68 Traceback (most recent call last):
69 IllegalKey: Illegal key ""
70 >>> generate({'>q':'p'})
71 Traceback (most recent call last):
72 IllegalKey: Illegal key ">q"
73 >>> generate({'q':'p\\n'})
74 Traceback (most recent call last):
75 IllegalValue: Illegal value "p\\n"
81 assert not key.startswith('>')
82 assert('\n' not in key)
83 assert('=' not in key)
84 assert(':' not in key)
86 except AssertionError:
87 raise IllegalKey(unicode(key).encode('unicode_escape'))
89 raise IllegalValue(unicode(map[key]).encode('unicode_escape'))
93 lines.append(yaml.safe_dump({key: map[key]},
94 default_flow_style=False,
97 return '\n'.join(lines)
101 Parse a YAML mapfile string.
102 >>> parse('q: p\\n\\n')['q']
104 >>> parse('q: \\'p\\'\\n\\n')['q']
106 >>> contents = generate({'a':'b', 'c':'d', 'e':'f'})
107 >>> dict = parse(contents)
114 >>> contents = generate({'q':u'Fran\u00e7ais'})
115 >>> dict = parse(contents)
118 >>> dict = parse('a!')
119 Traceback (most recent call last):
121 InvalidMapfileContents: Invalid YAML contents
123 c = yaml.load(contents)
124 if type(c) == types.StringType:
125 raise InvalidMapfileContents(
126 'Unable to parse YAML (BE format missmatch?):\n\n%s' % contents)
129 if libbe.TESTING == True:
130 suite = doctest.DocTestSuite()