1
# Copyright (C) 2006 Canonical
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""A simple format for saving lists of lists of unicode strings"""
19
from urllib import unquote
21
from bzrlib.errors import UnknownSplatFormat, MalformedSplatDict
23
SPLATFILE_1_HEADER = "BZR Splatfile Format 1"
24
FORBIDDEN = ' \t\r\n%'
26
def write_splat(fileobj, pairs):
27
fileobj.write(SPLATFILE_1_HEADER+'\n')
29
fileobj.write(" ".join([escape(v) for v in values])+"\n")
34
for c in value.encode('UTF-8'):
36
result.append('%%%.2x' % ord(c))
39
return ''.join(result)
42
def read_splat(fileobj):
43
header = fileobj.next().rstrip('\n')
44
if header != SPLATFILE_1_HEADER:
45
raise UnknownSplatFormat(header)
47
yield [unescape(v) for v in line.rstrip('\n').split(' ')]
51
return unquote(input).decode('UTF-8')
54
def dump_dict(my_file, dict):
55
write_splat(my_file, dict.iteritems())
58
def read_dict(my_file):
60
for values in read_splat(my_file):
62
raise MalformedSplatDict(values)
63
result[values[0]] = values[1]