~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/splatfile.py

  • Committer: Aaron Bentley
  • Date: 2006-02-24 21:57:55 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-20060224215755-0147b9bf4e26322a
Cleanups

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
from bzrlib.errors import UnknownSplatFormat, MalformedSplatDict
22
22
 
 
23
 
23
24
SPLATFILE_1_HEADER = "BZR Splatfile Format 1"
24
25
FORBIDDEN = ' \t\r\n%'
25
26
 
26
 
def write_splat(fileobj, pairs):
 
27
 
 
28
def write_splat(fileobj, stanzas):
 
29
    """Write an iterable of iterables of unicode strings as a splatfile"""
27
30
    fileobj.write(SPLATFILE_1_HEADER+'\n')
28
 
    for values in pairs:
 
31
    for values in stanzas:
29
32
        fileobj.write(" ".join([escape(v) for v in values])+"\n")
30
33
 
31
34
 
32
35
def escape(value):
 
36
    """Convert a unicode values to safe bytes"""
33
37
    result = []
34
38
    for c in value.encode('UTF-8'):
35
39
        if c in FORBIDDEN:
40
44
 
41
45
 
42
46
def read_splat(fileobj):
 
47
    """Generate an iterator of lists of unicode strings"""
43
48
    header = fileobj.next().rstrip('\n')
44
49
    if header != SPLATFILE_1_HEADER:
45
50
        raise UnknownSplatFormat(header)
48
53
 
49
54
 
50
55
def unescape(input):
 
56
    """Convert a splatfile value to a unicode string"""
51
57
    return unquote(input).decode('UTF-8')
52
58
 
53
59
 
54
60
def dump_dict(my_file, dict):
 
61
    """Write a dictionary to a splatfile"""
55
62
    write_splat(my_file, dict.iteritems())
56
63
 
57
64
 
58
65
def read_dict(my_file):
 
66
    """Produce a dictionary from a splatfile"""
59
67
    result = {}
60
68
    for values in read_splat(my_file):
61
69
        if len(values) != 2: