Updated copyright information
[be.git] / libbe / storage / util / mapfile.py
1 # Copyright (C) 2005-2010 Aaron Bentley and Panometrics, Inc.
2 #                         Gianluca Montecchi <gian@grys.it>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
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.
9 #
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.
14 #
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.
18
19 """
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.
23 """
24
25 import errno
26 import os.path
27 import types
28 import yaml
29
30 import libbe
31 if libbe.TESTING == True:
32     import doctest
33
34
35 class IllegalKey(Exception):
36     def __init__(self, key):
37         Exception.__init__(self, 'Illegal key "%s"' % key)
38         self.key = key
39
40 class IllegalValue(Exception):
41     def __init__(self, value):
42         Exception.__init__(self, 'Illegal value "%s"' % value)
43         self.value = value
44
45 class InvalidMapfileContents(Exception):
46     def __init__(self, contents):
47         Exception.__init__(self, 'Invalid YAML contents')
48         self.contents = contents
49
50 def generate(map):
51     """Generate a YAML mapfile content string.
52     >>> generate({'q':'p'})
53     'q: p\\n\\n'
54     >>> generate({'q':u'Fran\u00e7ais'})
55     'q: Fran\\xc3\\xa7ais\\n\\n'
56     >>> generate({'q':u'hello'})
57     'q: hello\\n\\n'
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"
76     """
77     keys = map.keys()
78     keys.sort()
79     for key in keys:
80         try:
81             assert not key.startswith('>')
82             assert('\n' not in key)
83             assert('=' not in key)
84             assert(':' not in key)
85             assert(len(key) > 0)
86         except AssertionError:
87             raise IllegalKey(unicode(key).encode('unicode_escape'))
88         if '\n' in map[key]:
89             raise IllegalValue(unicode(map[key]).encode('unicode_escape'))
90
91     lines = []
92     for key in keys:
93         lines.append(yaml.safe_dump({key: map[key]},
94                                     default_flow_style=False,
95                                     allow_unicode=True))
96         lines.append('')
97     return '\n'.join(lines)
98
99 def parse(contents):
100     """
101     Parse a YAML mapfile string.
102     >>> parse('q: p\\n\\n')['q']
103     'p'
104     >>> parse('q: \\'p\\'\\n\\n')['q']
105     'p'
106     >>> contents = generate({'a':'b', 'c':'d', 'e':'f'})
107     >>> dict = parse(contents)
108     >>> dict['a']
109     'b'
110     >>> dict['c']
111     'd'
112     >>> dict['e']
113     'f'
114     >>> contents = generate({'q':u'Fran\u00e7ais'})
115     >>> dict = parse(contents)
116     >>> dict['q']
117     u'Fran\\xe7ais'
118     >>> dict = parse('a!')
119     Traceback (most recent call last):
120       ...
121     InvalidMapfileContents: Invalid YAML contents
122     """
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)
127     return c or {}
128
129 if libbe.TESTING == True:
130     suite = doctest.DocTestSuite()