14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
17
19
# \subsection{\emph{rio} - simple text metaformat}
19
21
# \emph{r} stands for `restricted', `reproducible', or `rfc822-like'.
119
124
"""Append a name and value to the stanza."""
120
125
if not valid_tag(tag):
121
126
raise ValueError("invalid tag %r" % (tag,))
122
if isinstance(value, str):
123
value = unicode(value)
124
elif isinstance(value, unicode):
127
if isinstance(value, bytes):
128
value = value.decode('ascii')
129
elif isinstance(value, text_type):
126
## elif isinstance(value, (int, long)):
127
## value = str(value) # XXX: python2.4 without L-suffix
129
132
raise TypeError("invalid type for rio value: %r of type %s"
130
133
% (value, type(value)))
171
174
# max() complains if sequence is empty
174
for tag, value in self.items:
176
result.append(tag + ': \n')
177
for text_tag, text_value in self.items:
178
tag = text_tag.encode('ascii')
179
value = text_value.encode('utf-8')
181
result.append(tag + b': \n')
178
183
# don't want splitlines behaviour on empty lines
179
val_lines = value.split('\n')
180
result.append(tag + ': ' + val_lines[0].encode('utf-8') + '\n')
184
val_lines = value.split(b'\n')
185
result.append(tag + b': ' + val_lines[0] + b'\n')
181
186
for line in val_lines[1:]:
182
result.append('\t' + line.encode('utf-8') + '\n')
187
result.append(b'\t' + line + b'\n')
184
result.append(tag + ': ' + value.encode('utf-8') + '\n')
189
result.append(tag + b': ' + value + b'\n')
187
192
def to_string(self):
188
193
"""Return stanza as a single string"""
189
return ''.join(self.to_lines())
194
return b''.join(self.to_lines())
191
196
def to_unicode(self):
192
197
"""Return stanza as a single Unicode string.
299
304
max_rio_width = max_width - 4
301
306
for pline in stanza.to_lines():
302
for line in pline.split('\n')[:-1]:
303
line = re.sub('\\\\', '\\\\\\\\', line)
307
for line in pline.split(b'\n')[:-1]:
308
line = re.sub(b'\\\\', b'\\\\\\\\', line)
304
309
while len(line) > 0:
305
310
partline = line[:max_rio_width]
306
311
line = line[max_rio_width:]
307
if len(line) > 0 and line[0] != [' ']:
312
if len(line) > 0 and line[:1] != [b' ']:
309
break_index = partline.rfind(' ', -20)
314
break_index = partline.rfind(b' ', -20)
310
315
if break_index < 3:
311
break_index = partline.rfind('-', -20)
316
break_index = partline.rfind(b'-', -20)
313
318
if break_index < 3:
314
break_index = partline.rfind('/', -20)
319
break_index = partline.rfind(b'/', -20)
315
320
if break_index >= 3:
316
321
line = partline[break_index:] + line
317
322
partline = partline[:break_index]
318
323
if len(line) > 0:
320
partline = re.sub('\r', '\\\\r', partline)
325
partline = re.sub(b'\r', b'\\\\r', partline)
321
326
blank_line = False
322
327
if len(line) > 0:
324
elif re.search(' $', partline):
329
elif re.search(b' $', partline):
326
331
blank_line = True
327
lines.append('# ' + partline + '\n')
332
lines.append(b'# ' + partline + b'\n')
334
lines.append(b'# \n')
333
338
def _patch_stanza_iter(line_iter):
339
map = {b'\\\\': b'\\',
337
342
def mapget(match):
338
343
return map[match.group(0)]
341
346
for line in line_iter:
342
if line.startswith('# '):
347
if line.startswith(b'# '):
344
elif line.startswith('#'):
349
elif line.startswith(b'#'):
347
352
raise ValueError("bad line %r" % (line,))
348
353
if last_line is not None and len(line) > 2:
350
line = re.sub('\r', '', line)
351
line = re.sub('\\\\(.|\n)', mapget, line)
355
line = re.sub(b'\r', b'', line)
356
line = re.sub(b'\\\\(.|\n)', mapget, line)
352
357
if last_line is None:
355
360
last_line += line
356
if last_line[-1] == '\n':
361
if last_line[-1:] == b'\n':
359
364
if last_line is not None: