/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/splatfile.py

  • Committer: Aaron Bentley
  • Date: 2006-02-24 19:00:42 UTC
  • mto: (2027.1.2 revert-subpath-56549)
  • mto: This revision was merged to the branch mainline in revision 1595.
  • Revision ID: abentley@panoramicfeedback.com-20060224190042-7131c3fb7b75cc24
Record hashes produced by merges

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical
 
2
 
 
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.
 
7
 
 
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.
 
12
 
 
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
 
16
 
 
17
"""A simple format for saving lists of lists of unicode strings"""
 
18
 
 
19
from urllib import unquote
 
20
 
 
21
from bzrlib.errors import UnknownSplatFormat, MalformedSplatDict
 
22
 
 
23
SPLATFILE_1_HEADER = "BZR Splatfile Format 1"
 
24
FORBIDDEN = ' \t\r\n%'
 
25
 
 
26
def write_splat(fileobj, pairs):
 
27
    fileobj.write(SPLATFILE_1_HEADER+'\n')
 
28
    for values in pairs:
 
29
        fileobj.write(" ".join([escape(v) for v in values])+"\n")
 
30
 
 
31
 
 
32
def escape(value):
 
33
    result = []
 
34
    for c in value.encode('UTF-8'):
 
35
        if c in FORBIDDEN:
 
36
            result.append('%%%.2x' % ord(c))
 
37
        else:
 
38
            result.append(c)
 
39
    return ''.join(result)
 
40
 
 
41
 
 
42
def read_splat(fileobj):
 
43
    header = fileobj.next().rstrip('\n')
 
44
    if header != SPLATFILE_1_HEADER:
 
45
        raise UnknownSplatFormat(header)
 
46
    for line in fileobj:
 
47
        yield [unescape(v) for v in line.rstrip('\n').split(' ')]
 
48
 
 
49
 
 
50
def unescape(input):
 
51
    return unquote(input).decode('UTF-8')
 
52
 
 
53
 
 
54
def dump_dict(my_file, dict):
 
55
    write_splat(my_file, dict.iteritems())
 
56
 
 
57
 
 
58
def read_dict(my_file):
 
59
    result = {}
 
60
    for values in read_splat(my_file):
 
61
        if len(values) != 2:
 
62
            raise MalformedSplatDict(values)
 
63
        result[values[0]] = values[1]
 
64
    return result