File: fix_newlines

package info (click to toggle)
python-hpilo 4.4.3-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,784 kB
  • sloc: python: 3,418; ruby: 467; makefile: 122; sh: 31
file content (21 lines) | stat: -rwxr-xr-x 775 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/python
#
# Try to reconstruct a saved chunked response after an editor has mangled all
# the newlines. Not 100% foolproof, but decent enough that the result requires
# very little manual mangling.

import sys
import re

data = sys.stdin.read()
header, body = data.split('\n\n', 1)
header = header.replace('\n', '\r\n')

# All <?xml ... ?> stanzas end with CRLF. Only match on ?> as the stanza may be split over 2 chunks
body = body.replace('?>', '?>\r')
# Chunked response: each chunk ends with CRLF, as does the line with the chunk size
body = re.sub(r'\n([0-9a-f]{1,3})\n', r'\r\n\1\r\n', body)
# First chunk. Don't try to match the last newline of a previous chunk.
body = re.sub('^([0-9a-f]{1,3})\n', r'\1\r\n', body, 1)

print '\r\n\r\n'.join([header, body])