~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/rio.py

 * bzr add now lists how many files were ignored per glob.  add --verbose
   lists the specific files.  (Aaron Bentley)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#
3
3
# Distributed under the GNU General Public Licence v2
4
 
 
 
4
#
5
5
# \subsection{\emph{rio} - simple text metaformat}
6
6
7
7
# \emph{r} stands for `restricted', `reproducible', or `rfc822-like'.
18
18
# stream representation of an object and vice versa, and that this relation
19
19
# will continue to hold for future versions of bzr.
20
20
 
 
21
# In comments, $\min(1,10)$
 
22
 
 
23
min(1,10)
 
24
 
21
25
import re
22
26
 
23
27
# XXX: some redundancy is allowing to write stanzas in isolation as well as
88
92
        """Append a name and value to the stanza."""
89
93
        assert valid_tag(tag), \
90
94
            ("invalid tag %r" % tag)
91
 
        if isinstance(value, str):
92
 
            value = unicode(value)
93
 
        elif isinstance(value, unicode):
 
95
        if isinstance(value, (str, unicode)):
94
96
            pass
95
97
        ## elif isinstance(value, (int, long)):
96
98
        ##    value = str(value)           # XXX: python2.4 without L-suffix
97
99
        else:
98
 
            raise TypeError("invalid type for rio value: %r of type %s"
99
 
                            % (value, type(value)))
 
100
            raise ValueError("invalid value %r" % value)
100
101
        self.items.append((tag, value))
101
102
        
102
103
    def __contains__(self, find_tag):
126
127
        return iter(self.items)
127
128
 
128
129
    def to_lines(self):
129
 
        """Generate sequence of lines for external version of this file.
130
 
        
131
 
        The lines are always utf-8 encoded strings.
132
 
        """
 
130
        """Generate sequence of lines for external version of this file."""
133
131
        if not self.items:
134
132
            # max() complains if sequence is empty
135
133
            return []
136
134
        result = []
137
135
        for tag, value in self.items:
138
 
            assert isinstance(tag, str)
139
 
            assert isinstance(value, unicode)
 
136
            assert isinstance(value, (str, unicode))
140
137
            if value == '':
141
138
                result.append(tag + ': \n')
142
139
            elif '\n' in value:
143
140
                # don't want splitlines behaviour on empty lines
144
141
                val_lines = value.split('\n')
145
 
                result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
 
142
                result.append(tag + ': ' + val_lines[0] + '\n')
146
143
                for line in val_lines[1:]:
147
 
                    result.append('\t' + line.encode('utf-8') + '\n')
 
144
                    result.append('\t' + line + '\n')
148
145
            else:
149
 
                result.append(tag + ': ' + value.encode('utf-8') + '\n')
 
146
                result.append(tag + ': ' + value + '\n')
150
147
        return result
151
148
 
152
149
    def to_string(self):
177
174
            if t == tag:
178
175
                r.append(v)
179
176
        return r
180
 
 
181
 
    def as_dict(self):
182
 
        """Return a dict containing the unique values of the stanza.
183
 
        """
184
 
        d = {}
185
 
        for tag, value in self.items:
186
 
            assert tag not in d
187
 
            d[tag] = value
188
 
        return d
189
177
         
190
178
_tag_re = re.compile(r'^[-a-zA-Z0-9_]+$')
191
179
def valid_tag(tag):
202
190
 
203
191
    Only the stanza lines and the trailing blank (if any) are consumed
204
192
    from the line_iter.
205
 
 
206
 
    The raw lines must be in utf-8 encoding.
207
193
    """
208
194
    items = []
209
195
    stanza = Stanza()
214
200
            break       # end of file
215
201
        if line == '\n':
216
202
            break       # end of stanza
217
 
        line = line.decode('utf-8')
218
203
        assert line[-1] == '\n'
219
204
        real_l = line
220
205
        if line[0] == '\t': # continues previous value